<commit_before><?php
/**
 * Flagbit_FactFinder
 *
 * @category  Mage
 * @package   Flagbit_FactFinder
 * @copyright Copyright (c) 2010 Flagbit GmbH & Co. KG (http://www.flagbit.de/)
 */

require_once BP.DS.'lib'.DS.'FACTFinder'.DS.'Loader.php';

/**
 * Model class
 * 
 * Adapter between FACT-Finder API Framework and Magento
 * 
 * @category  Mage
 * @package   Flagbit_FactFinder
 * @copyright Copyright (c) 2010 Flagbit GmbH & Co. KG (http://www.flagbit.de/)
 * @author    <NAME> <<EMAIL>>
 * @version   $Id$
 */
class Flagbit_FactFinder_Model_Adapter
{

	/**
	 * FACT-Finder Searchadapter
	 * @var FACTFinder_Abstract_SearchAdapter
	 */
    protected $_searchAdapter = null;
    
	/**
	 * FACT-Finder Suggestadapter
	 * @var FACTFinder_Abstract_SuggestAdapter
	 */
    protected $_suggestAdapter = null;    
    
	/**
	 * FACT-Finder Config
	 * @var FACTFinder_Abstract_Configuration
	 */    
    protected $_config = null;
    
	/**
	 * FACT-Finder Parameter Parser
	 * @var FACTFinder_ParametersParser
	 */    
    protected $_paramsParser = null;
    
	/**
	 * FACT-Finder Data Provider
	 * @var FACTFinder_Abstract_DataProvider
	 */    
    protected $_dataProvider = null;
    
	/**
	 * FACT-Finder After Search Navigation
	 * @var array
	 */    
    protected $_afterSearchNavigation = null;
    
	/**
	 * FACT-Finder Searchadapter
	 * @var array
	 */    
    protected $_searchResultProductIds = null;

    /**
     * get FactFinder SearchAdapter
     * 
     * @return FACTFinder_Abstract_SearchAdapter
     */
    protected function _getSearchAdapter($collectParams = true)
    {
        if ($this->_searchAdapter == null) {
            $config              = $this->_getConfiguration();
            $encodingHandler     = FF::getSingleton('encodingHandler', $config);
            $dataProvider        = $this->_getDataProvider();
            $this->_searchAdapter = FF::getSingleton(
            	'xml65/searchAdapter', 
            	$dataProvider, 
            	$this->_getParamsParser(), 
            	$encodingHandler
            );
			
            if($collectParams == true){
            	$this->_collectParams();
            }                    
        }
        
        return $this->_searchAdapter;
    }
    
    protected function _collectParams()
    {
        // search Helper
        $helper = Mage::helper('factfinder/search');    	
    	$_request = Mage::app()->getRequest();
    	
    	switch($_request->getModuleName()){
    		
    		case "xmlconnect":
		    	$this->_setParam('idsOnly', 'true')
		    		->_setParam('productsPerPage', $_request->getParam('count'))
		    		->_setParam('query', $helper->getQuery()->getQueryText())
		    		->_setParam('page', ($_request->getParam('offset') / $_request->getParam('count')) + 1);   
		    		
				// add Sorting Param
				$params = Mage::app()->getRequest()->getParams();		    		
				foreach($params as $key => $value){
					if(substr($key, 0, 6) == 'order_'){
						$key = substr($key, 6);
						if(!in_array($key, array('position', 'relevance'))){
							$this->_setParam('sort'.$key, $value);
						}
					}
				}	

    			 // add Filter Params
		    	$params = Mage::app()->getRequest()->getParams();
		    	foreach($params as $key => $value){
		    		$value = base64_decode($value);  		
		    		if(strpos($value, '|')){
		    			$param = explode('|', $value);
		    			$this->_setParam('filter'.$param[0], $param[1]);
		    		}
		    	}		    		
		    		
    			break;
    			
    		case "catalogsearch":
    		default:	    	
		            // add Default Params
		    	$this->_setParam('idsOnly', 'true')
		    		->_setParam('productsPerPage', $helper->getPageLimit())
		    		->_setParam('query', $helper->getQuery()->getQueryText())
		    		->_setParam('page', $helper->getCurrentPage());
		    	
		    	// add Sorting Param
		    	if($helper->getCurrentOrder() 
		    		&& $helper->getCurrentDirection()
		    		&& $helper->getCurrentOrder() != 'position'
		    		&& $helper->getCurrentOrder() != 'relevance'){		    		
		    			$this->_setParam('sort'.$helper->getCurrentOrder(), $helper->getCurrentDirection());
		    	}
		
		    	// add Filter Params
		    	$params = Mage::app()->getRequest()->getParams();
		    	foreach($params as $key => $value){  		
		    		if(strpos($value, '|')){
		    			$param = explode('|', $value);
		    			switch($param[1]){
		    				
		    				case 'slider':
		    					$subparam = explode(':', $param[2]);
		    					$this->_setParam($subparam[0], $subparam[1]);
		    					$subparam = explode(':', $param[3]);
		    					$this->_setParam($subparam[0], $subparam[1]);	    					
		    					break;
		    					
		    				default:
		    					$this->_setParam('filter'.$param[0], $param[1]);		
		    					break;
		    			}
		    		}
		    	}      			
    			break;
    		
    	}
    }
    
    /**
     * execute search
     */
    public function checkStatus($configarray = null)
    {
        $status = false;
        try {
        	$this->_getConfiguration($configarray);
            $this->_setParam('query', 'FACT-Finder Version');
            $this->_setParam('productsPerPage', '1');

            $status = $this->_getSearchAdapter(false)->getStatus() == 'resultsFound';
        } catch (Exception $e) {
            $status = false;
        }
        return $status;
    }    
    
    /**
     * get Redirect URL if there is set one
     * 
     * @return string<commit_msg>[+BUGFIX] added Factfinder related Category Params<commit_after><?php
/**
 * Flagbit_FactFinder
 *
 * @category  Mage
 * @package   Flagbit_FactFinder
 * @copyright Copyright (c) 2010 Flagbit GmbH & Co. KG (http://www.flagbit.de/)
 */

require_once BP.DS.'lib'.DS.'FACTFinder'.DS.'Loader.php';

/**
 * Model class
 * 
 * Adapter between FACT-Finder API Framework and Magento
 * 
 * @category  Mage
 * @package   Flagbit_FactFinder
 * @copyright Copyright (c) 2010 Flagbit GmbH & Co. KG (http://www.flagbit.de/)
 * @author    <NAME> <<EMAIL>>
 * @version   $Id$
 */
class Flagbit_FactFinder_Model_Adapter
{

	/**
	 * FACT-Finder Searchadapter
	 * @var FACTFinder_Abstract_SearchAdapter
	 */
    protected $_searchAdapter = null;
    
	/**
	 * FACT-Finder Suggestadapter
	 * @var FACTFinder_Abstract_SuggestAdapter
	 */
    protected $_suggestAdapter = null;    
    
	/**
	 * FACT-Finder Config
	 * @var FACTFinder_Abstract_Configuration
	 */    
    protected $_config = null;
    
	/**
	 * FACT-Finder Parameter Parser
	 * @var FACTFinder_ParametersParser
	 */    
    protected $_paramsParser = null;
    
	/**
	 * FACT-Finder Data Provider
	 * @var FACTFinder_Abstract_DataProvider
	 */    
    protected $_dataProvider = null;
    
	/**
	 * FACT-Finder After Search Navigation
	 * @var array
	 */    
    protected $_afterSearchNavigation = null;
    
	/**
	 * FACT-Finder Searchadapter
	 * @var array
	 */    
    protected $_searchResultProductIds = null;

    /**
     * get FactFinder SearchAdapter
     * 
     * @return FACTFinder_Abstract_SearchAdapter
     */
    protected function _getSearchAdapter($collectParams = true)
    {
        if ($this->_searchAdapter == null) {
            $config              = $this->_getConfiguration();
            $encodingHandler     = FF::getSingleton('encodingHandler', $config);
            $dataProvider        = $this->_getDataProvider();
            $this->_searchAdapter = FF::getSingleton(
            	'xml65/searchAdapter', 
            	$dataProvider, 
            	$this->_getParamsParser(), 
            	$encodingHandler
            );
			
            if($collectParams == true){
            	$this->_collectParams();
            }                    
        }
        
        return $this->_searchAdapter;
    }
    
    protected function _collectParams()
    {
        // search Helper
        $helper = Mage::helper('factfinder/search');    	
    	$_request = Mage::app()->getRequest();

    	switch($_request->getModuleName()){
    		
    		case "xmlconnect":
		    	$this->_setParam('idsOnly', 'true')
		    		->_setParam('productsPerPage', $_request->getParam('count'))
		    		->_setParam('query', $helper->getQuery()->getQueryText())
		    		->_setParam('page', ($_request->getParam('offset') / $_request->getParam('count')) + 1);   
		    		
				// add Sorting Param
				$params = Mage::app()->getRequest()->getParams();		    		
				foreach($params as $key => $value){
					if(substr($key, 0, 6) == 'order_'){
						$key = substr($key, 6);
						if(!in_array($key, array('position', 'relevance'))){
							$this->_setParam('sort'.$key, $value);
						}
					}
				}	

    			 // add Filter Params
		    	$params = Mage::app()->getRequest()->getParams();
		    	foreach($params as $key => $value){
		    		$value = base64_decode($value);  		
		    		if(strpos($value, '|')){
		    			$param = explode('|', $value);
    					if($key == 'category'){
    						$categories = array_merge(array_slice(explode('/', $param[0]), 1), array($param[1]));
    						$filterkey = '';
    						foreach($categories as $category){
    							$this->_setParam('filtercategoryROOT'.$filterkey, $category);
    							$filterkey .= '/'.$category;
    						}
    					}else{
    						$this->_setParam('filter'.$param[0], $param[1]);
    					}	
		    		}
		    	}		    		
		    		
    			break;
    			
    		case "catalogsearch": 
    		default:	    	
		            // add Default Params
		    	$this->_setParam('idsOnly', 'true')
		    		->_setParam('productsPerPage', $helper->getPageLimit())
		    		->_setParam('query', $helper->getQuery()->getQueryText())
		    		->_setParam('page', $helper->getCurrentPage());
		    	
		    	// add Sorting Param
		    	if($helper->getCurrentOrder() 
		    		&& $helper->getCurrentDirection()
		    		&& $helper->getCurrentOrder() != 'position'
		    		&& $helper->getCurrentOrder() != 'relevance'){		    		
		    			$this->_setParam('sort'.$helper->getCurrentOrder(), $helper->getCurrentDirection());
		    	}
		
		    	// add Filter Params
		    	$params = Mage::app()->getRequest()->getParams();
		    	foreach($params as $key => $value){  		
		    		if(strpos($value, '|')){
		    			$param = explode('|', $value);
		    			switch($param[1]){
		    				
		    				case 'slider':
		    					$subparam = explode(':', $param[2]);
		    					$this->_setParam($subparam[0], $subparam[1]);
		    					$subparam = explode(':', $param[3]);
		    					$this->_setParam($subparam[0], $subparam[1]);	    					
		    					break;
		    					
		    				default:
		    					if($key == 'category'){
		    						$categories = array_merge(array_slice(explode('/', $param[0]), 1), array($param[1]));
		    						$filterkey = '';
		    						foreach($categories as $category){
		    							$this->_setParam('filtercategoryROOT'.$filterkey, $category);
		    							$filterkey .= '/'.$category;
		    						}
		    					}else{
		    						$this->_setParam('filter'.$param[0], $param[1]);
		    					}		
		    					break;
		    			}
		    		}
		    	}      			
    			break;
    		
    	}
    }
    
    /**
     * execute search
     */
    public function checkStatus($configarray = null)
    {
        $status = false;
        try {
        	$this->_getConfiguration($configarray);
            $this->_setParam('query', 'FACT-Finder Version');
            $this->_setParam('productsPerPage', '1');

            $status = $this->_getSearchAdapter(false)->getStatus() == 'resultsFound';
        } catch (Exception $e) {
            $status = false;
        }
        return $status;
    }    
    
    /**
     * get Redirect URL if there is set one
     * 
     * @return string
{'fraction_non_alphanumeric': 0.1556228373702422, 'fraction_numerical': 0.005363321799307958, 'mean_word_length': 2.033586985043296, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2367370', 'n_tokens_mistral': 4125, 'n_tokens_neox': 3565, 'n_words': 600}