<?php
  /**
     * functions/extra_functions/reloaded_custom_search
     * 
     * Add/Check configuration values for a weighted search query
     * 
     * @package ZenCart
     * @author Joe McFrederick
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     */
if( !class_exists('messageStack') )
{
	include('../../classes/message_stack.php');
}
if( !is_object($reloadedStack) )
{
	$reloadedStack = new messageStack();
}   
     
      
     
     //Auto install check
     $reloadedSearchCheck = $db->Execute("SELECT configuration_group_title FROM ".TABLE_CONFIGURATION_GROUP." WHERE configuration_group_title='Reloaded Search'");
     if($reloadedSearchCheck->RecordCount() < 1 AND basename($_SERVER['PHP_SELF']) == 'index.php')
     {
        $reloadedStack->add('Reloaded Custom Search <b>not installed</b>. Click [<a href="index.php?install_reloaded_search=yes">Install</a>]');
     }
     
    
  /**
      * Install configuration into database
      * @param none
      */
      function reloaded_search_install() {
        global $db, $reloadedStack;
        
        
        //Check indexes
        $check_index = array(
                            array(
                                'check_query'  => "SHOW INDEX FROM " . TABLE_PRODUCTS_DESCRIPTION,
                                'Key_name'     => 'idx_reloaded_products_name_description',
                                'create_query' => "CREATE FULLTEXT INDEX idx_reloaded_products_name_description ON " . TABLE_PRODUCTS_DESCRIPTION . " (products_name, products_description)"
                            ),
                            array(
                                'check_query'  => "SHOW INDEX FROM " . TABLE_MANUFACTURERS,
                                'Key_name'     => 'idx_reloaded_manufacturer_name',
                                'create_query' => "CREATE FULLTEXT INDEX idx_reloaded_manufacturer_name ON " . TABLE_MANUFACTURERS . " (manufacturers_name)"
                            ),
                            array(
                                'check_query'  => "SHOW INDEX FROM " . TABLE_PRODUCTS,
                                'Key_name'     => 'idx_reloaded_products_model',
                                'create_query' => "CREATE FULLTEXT INDEX idx_reloaded_products_model ON " . TABLE_PRODUCTS . " (products_model)"
                            ),
                            array(
                                'check_query'  => "SHOW INDEX FROM " . TABLE_META_TAGS_PRODUCTS_DESCRIPTION,
                                'Key_name'     => 'idx_reloaded_metatags_keywords_description',
                                'create_query' => "CREATE FULLTEXT INDEX idx_reloaded_metatags_keywords_description ON " . TABLE_META_TAGS_PRODUCTS_DESCRIPTION . "(metatags_keywords, metatags_description)"
                            )
                       );
                       
        foreach($check_index as $index){
            $pd_index = $db->Execute($index['check_query']);
        
            if($pd_index->RecordCount() > 0){
                
                $column_index = FALSE;
                
                while(!$pd_index->EOF){
                    
                    if($pd_index->fields['Key_name'] == $index['Key_name'])
                        $column_index = TRUE;
                    
                    $pd_index->MoveNext();
                }
                
                if($column_index == FALSE){
                    echo $column_index;
                    $db->Execute($index['create_query']);
                }
                unset($column_index);
            }
        }
        
        //Maintenance
    
        $keys = reloaded_search_keys();
        $db->Execute("DELETE FROM ".TABLE_CONFIGURATION. " WHERE configuration_key IN ('".implode("', '",$keys)."')");
        $db->Execute("DELETE FROM ".TABLE_CONFIGURATION_GROUP." WHERE configuration_group_title='Reloaded Search'");
        
        //Install new configuration group
        $db->Execute("INSERT INTO ". TABLE_CONFIGURATION_GROUP ." VALUES (NULL, 'Reloaded Search', 'Reloaded Search - Config', '1', '1')");
        $insert_id = mysql_insert_id();
        //Set sort order
        $db->Execute("UPDATE ". TABLE_CONFIGURATION_GROUP ." SET sort_order = $insert_id WHERE configuration_group_id = $insert_id");

        //Install new configuration settings
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Model Search Weight','RELOADED_SEARCH_PRODUCT_MODEL_WEIGHT','50','Enter a number. Default=50', '" . $insert_id . "', '1',NULL,now(),NULL,NULL)");
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Description Search Weight','RELOADED_SEARCH_PRODUCT_DESCRIPTION_WEIGHT','40','Enter a number. Default=40', '" . $insert_id . "', '2',NULL,now(),NULL,NULL)");
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Category Search Weight','RELOADED_SEARCH_PRODUCT_CATEGORY_WEIGHT','20','Enter a number. Default=20', '" . $insert_id . "', '3',NULL,now(),NULL,NULL)");
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Meta Tags Search Weight','RELOADED_SEARCH_PRODUCT_META_KEYWORD_WEIGHT','15','Enter a number. Default=10', '" . $insert_id . "', '4',NULL,now(),NULL,NULL)");
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Manufacturer Search Weight','RELOADED_SEARCH_PRODUCT_MANUFACTURER_WEIGHT','20','Enter a number. Default=20', '" . $insert_id . "', '5',NULL,now(),NULL,NULL)");
        $db->Execute("INSERT INTO ".TABLE_CONFIGURATION." VALUES ('','Product Viewed Search Weight','RELOADED_SEARCH_PRODUCT_VIEWED_WEIGHT','10','Enter a number. Default=10', '" . $insert_id . "', '6',NULL,now(),NULL,NULL)");
		
		//Check admin menu registration
		if(PROJECT_VERSION_MINOR == '5.0')
		{
			$reloadedSearchCheck = $db->Execute("SELECT page_key FROM " . TABLE_ADMIN_PAGES . " WHERE page_key = 'configReloadedSearch'");
			if ($reloadedSearchCheck->RecordCount() < 1)
			{
				//Get last sort order id
				$last_id = $db->Execute("SELECT sort_order FROM " . TABLE_ADMIN_PAGES . " WHERE menu_key='configuration' ORDER BY sort_order DESC", 1);
				
				$db->Execute("INSERT INTO " . TABLE_ADMIN_PAGES . " (page_key, language_key, main_page, menu_key, display_on_menu, sort_order, page_params) VALUES ('configReloadedSearch', 'BOX_TOOLS_RELOADED_SEARCH', 'FILENAME_CONFIGURATION', 'configuration', 'Y', '" . $last_id->fields['sort_order'] ."','gID=" . $group_id . "')");
			}
		}
		$reloadedStack->add_session('ReloadedSearch succesfully installed.','success');
		
        zen_redirect(zen_href_link(FILENAME_CONFIGURATION,'gID='.$insert_id));
     
      }
      
   /**
       * Uninstall configuration into database
       * @param none
       */
       function reloaded_search_remove() {
        global $db, $reloadedStack;
        
        $keys = reloaded_search_keys();
        $db->Execute("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN ('" . implode("', '", $keys) . "')");
        $db->Execute("DELETE FROM ".TABLE_CONFIGURATION_GROUP." WHERE configuration_group_title='Reloaded Search'");
		
		if(PROJECT_VERSION_MINOR == '5.0')
		{
			$db->Execute("DELETE FROM " . TABLE_ADMIN_PAGES . " WHERE page_key = 'configReloadedSearch'");
		}
		$reloadedStack->add('ReloadedSearch successfully removed', 'success');
       }
     
  /**
      * Return configuration keys for mod
      * @param none
      * @return mixed array of configuration keys
      */
     function reloaded_search_keys()
     {
        
        return array('RELOADED_SEARCH_PRODUCT_MODEL_WEIGHT', 'RELOADED_SEARCH_PRODUCT_DESCRIPTION_WEIGHT', 
            'RELOADED_SEARCH_PRODUCT_CATEGORY_WEIGHT', 'RELOADED_SEARCH_PRODUCT_META_KEYWORD_WEIGHT',
            'RELOADED_SEARCH_PRODUCT_MANUFACTURER_WEIGHT', 'RELOADED_SEARCH_PRODUCT_VIEWED_WEIGHT');
     }
     
  /**
      * Manual install/uninstall
      */
      if($_GET['install_reloaded_search']=="yes" AND  basename($_SERVER['PHP_SELF']) == 'index.php') reloaded_search_install();
      if($_GET['remove_reloaded_search']=="yes" AND basename($_SERVER['PHP_SELF']) == 'index.php') reloaded_search_remove();
?>