-
-
Save robksawyer/dea603bede9d6cb74a29 to your computer and use it in GitHub Desktop.
I'm sure this can be simplified. Need some advice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* index method | |
* @param int $limit The total to return | |
* @param int $page The page to pull | |
* @return void | |
*/ | |
public function index($limit = 25, $page = 1) { | |
$this->Cheese->recursive = -1; | |
//Fix the pager. It won't pull page if it's not last in the url stack | |
$page = 1; | |
if(!empty($this->params->named['page'])){ | |
$page = $this->params->named['page']; | |
}else{ | |
$page = $this->params->paging['page']; | |
} | |
//Parse the search query | |
$this->request->params['named']['query'] = $this->parseSearchQuery(); | |
$paginationResult = $this->processSearchInformation( | |
$limit, | |
array(), | |
array( | |
'page' => $page | |
) | |
); | |
$results = $this->Paginator->paginate(); | |
$this->set('_serialize', 'results'); | |
$this->set(compact('results')); | |
//Fill in the search form | |
if(!empty($this->params->query['data'])){ | |
$this->request->data = $this->params->query['data']; | |
} | |
//Set the filter options | |
$this->setCheeseFilterData(); | |
$totalCheeseCount = Cache::read('total_cheese_count', 'minutes'); | |
if(empty($totalCheeseCount)){ | |
$totalCheeseCount = $this->Cheese->find('count', array('conditions'=>array('Cheese.active'=>1))); | |
Cache::write('total_cheese_count', $totalCheeseCount, 'minutes'); | |
} | |
$trendingCheeses = Cache::read('trending_cheeses', 'hour'); | |
if(empty($trendingCheeses)){ | |
$trendingCheeses = $this->Cheese->getTrending(5); | |
Cache::write('trending_cheeses', $trendingCheeses, 'hour'); | |
} | |
$trendingProducers = Cache::read('trending_producers', 'hour'); | |
if(empty($trendingProducers)){ | |
$trendingProducers = $this->Cheese->CheeseProducer->getTrending(5); | |
Cache::write('trending_producers', $trendingProducers, 'hour'); | |
} | |
$this->set(compact('totalCheeseCount','trendingProducers','trendingCheeses')); | |
} | |
/** | |
* processSearchInformation method | |
* Handles setting the conditions for the paginate. The conditions are tricky because of the filters (hence the large amount of code). | |
* @param int $limit Total results to return | |
* @param int $removeIds Items to ignore | |
* @param array $options The optioms of model/model_id | |
* @param array $condtions Extra conditions that should be concatenated onto the main search conditions | |
* @return array The pagination results for the paginate method | |
*/ | |
public function processSearchInformation($limit = 25, $removeIds = array(), $options = array(), $conditions = array()){ | |
$default_options = array( | |
'model' => '', | |
'model_id' => '', | |
'active' => 1, | |
'page' => 1 | |
); | |
$options = Set::merge($default_options,$options); | |
//IMPORTANT: Set the filter variables | |
$this->initializeFilters(); | |
//$userIp = $this->request->clientIp(); | |
//Parse the filter string information and get the conditions | |
$filterConditions = $this->Filter->getConditions(); | |
//Certain records require joins in order to process, this method takes care of those. | |
$filterConditions = $this->applyFilterJoinConditions($options, $filterConditions); | |
//Parse the filter data | |
if( !empty($filterConditions) ){ | |
//Make sure that the query is passed along | |
if(!empty($this->passedArgs['query'])){ | |
$newFilterData = array('AND' => $filterConditions); | |
$filterConditions = array_merge($newFilterData, $this->Cheese->parseCriteria($this->passedArgs)); | |
}else{ | |
$filterConditions = array_merge($filterConditions, $this->Cheese->parseCriteria($this->passedArgs)); | |
} | |
$this->Paginator->settings['conditions'] = $filterConditions; | |
}else{ | |
//Do search | |
$this->Prg->commonProcess(); | |
$this->Paginator->settings['conditions'] = $this->Cheese->parseCriteria($this->Prg->parsedParams()); | |
} | |
//Make sure that the cheese producer id is passed to the conditions array | |
if($options['model'] == 'CheeseProducer' && !empty($options['model_id'])){ | |
$this->Paginator->settings['conditions']['AND'][] = array('Cheese.cheese_producer_id' => intval($options['model_id'])); | |
} | |
$this->Paginator->settings['conditions']['AND'][] = array('Cheese.active' => $options['active']); | |
//Merge any extra conditions that were passed | |
if( !empty($conditions) ){ | |
//$this->Paginator->settings['conditions']['AND'] = array_merge($this->Paginator->settings['conditions']['AND'], $conditions); | |
$this->Paginator->settings['conditions']['AND'][] = $conditions; | |
} | |
//Make sure the conditions array doesn't contain certain records | |
if($removeIds){ | |
$this->Paginator->settings['conditions']['NOT'][] = array('Cheese.id' => $removeIds); | |
} | |
//Apply the processed conditions to the actual pagination condition array | |
$this->Filter->setPaginate('conditions', $this->Paginator->settings['conditions']); | |
$this->Paginator->settings['order'] = array( | |
'Cheese.created' => 'DESC', | |
'Cheese.staff_score' => 'DESC', | |
'Cheese.score' => 'DESC' | |
); | |
$this->Filter->setPaginate('order', $this->Paginator->settings['order']); // optional | |
$this->Filter->setPaginate('limit', $limit); // optional | |
$this->Paginator->settings['contain'] = array( | |
'Attachment' => array( | |
'fields' => array('id','path_small','path_med','path_med_crop','path_small_crop') | |
), | |
'CheeseProducer' => array( | |
'fields' => array('id','name') | |
), | |
'User' => array( | |
'fields' => array('id','name','username','email','profile_image_url','attachment_id'), | |
'Attachment' | |
), | |
'CheeseRegion' => array('fields' => array('id','name')), | |
'CheeseLocation' => array( | |
'Country' => array('fields' => array('name','code')), | |
'StateRegion' => array('fields' => array('name','code')) | |
), | |
'MilkSource' => array( | |
'fields' => array('id','name'), | |
'Attachment' => array('fields' => array('id','path_small')) | |
), | |
'MilkTreatment' => array('fields'=>array('id','name')), | |
'RennetType' => array( | |
'fields'=>array('id','name') | |
), | |
'Rind' => array('fields'=>array('id','name')), | |
'AnimalBreed' => array('fields'=>array('id','name')), | |
'Texture' => array('fields'=>array('id','name')) | |
); | |
$this->Filter->setPaginate('contain', $this->Paginator->settings['contain']); | |
$this->Paginator->settings['limit'] = $limit; | |
$this->Paginator->settings['page'] = $options['page']; | |
debug($this->Paginator->settings); | |
return $this->Paginator->settings; | |
} | |
/** | |
* applyFilterJoinConditions method | |
* Apply any join conditions that need to be added to the main conditions array. | |
* @param array options Some basic record options | |
* @param array $filterConditions The data that was return by the filter plugin | |
* @return array The joins used by the search conditions (pass along to the main conditions array) | |
*/ | |
protected function applyFilterJoinConditions($options = array(), $filterConditions = array()){ | |
$searchJoins = array(); | |
$this->Paginator->settings['joins'] = array(); | |
if($options['model'] == 'Place'){ | |
//Check to see if a place was passed | |
if(!empty($options['model_id'])){ | |
$searchJoins[] = array( | |
'table' => 'cheeses_places', | |
'alias' => 'CheesesPlaces', | |
'type' => 'INNER', | |
'conditions' => array( | |
'Cheese.id = CheesesPlaces.cheese_id', | |
) | |
); | |
$searchJoins[] = array( | |
'table' => 'places', | |
'alias' => 'Place', | |
'type' => 'INNER', | |
'conditions' => array( | |
'CheesesPlaces.place_id = Place.id', | |
'CheesesPlaces.place_id' => intval($options['model_id']) | |
) | |
); | |
} | |
} | |
if(!empty($filterConditions)){ | |
//Search milk sources | |
if(!empty($filterConditions['MilkSource.id ='])){ | |
$milkSourceId = $filterConditions['MilkSource.id =']; | |
unset($filterConditions['MilkSource.id =']); | |
//Add a join to search for the milk_source_id | |
$searchJoins[] = array( | |
'table' => 'cheeses_milk_sources', | |
'alias' => 'CheesesMilkSource', | |
'type' => 'INNER', | |
'conditions' => array( | |
'Cheese.id = CheesesMilkSource.cheese_id', | |
) | |
); | |
$searchJoins[] = array( | |
'table' => 'milk_sources', | |
'alias' => 'MilkSource', | |
'type' => 'INNER', | |
'conditions' => array( | |
'CheesesMilkSource.milk_source_id = MilkSource.id', | |
'CheesesMilkSource.milk_source_id' => $milkSourceId | |
) | |
); | |
} | |
//Do a search to see what cheese locations have the state_region_id and country_id if found | |
if(!empty($filterConditions['CheeseLocation.state_region_id ='])){ | |
$stateRegionId = $filterConditions['CheeseLocation.state_region_id =']; | |
unset($filterConditions['CheeseLocation.state_region_id =']); | |
//Add a join to search | |
$searchJoins[] = array( | |
'table' => 'cheese_locations', | |
'alias' => 'CheeseLocation', | |
'type' => 'INNER', | |
'conditions' => array( | |
'CheeseLocation.cheese_id = Cheese.id', | |
'CheeseLocation.state_region_id' => $stateRegionId | |
) | |
); | |
} | |
// | |
if(!empty($filterConditions['CheeseLocation.country_id ='])){ | |
$countryId = $filterConditions['CheeseLocation.country_id =']; | |
unset($filterConditions['CheeseLocation.country_id =']); | |
//Add a join to search | |
$searchJoins[] = array( | |
'table' => 'cheese_locations', | |
'alias' => 'CheeseLocation', | |
'type' => 'INNER', | |
'conditions' => array( | |
'CheeseLocation.cheese_id = Cheese.id', | |
'CheeseLocation.country_id' => $countryId | |
) | |
); | |
} | |
} | |
//Set the joins | |
if(!empty($searchJoins)){ | |
$this->Filter->setPaginate('joins', $searchJoins); | |
$this->Paginator->settings['joins'] = $searchJoins; | |
} | |
return $filterConditions; | |
} | |
/** | |
* initializeFilters method | |
* This sets up the filter variables used by the plugin | |
* @param void | |
* @return void | |
*/ | |
public function initializeFilters(){ | |
$stateList = Cache::read('active_cheese_state_list','month'); | |
if(empty($stateList)){ | |
$stateList = $this->Cheese->CheeseLocation->getActiveStates(); | |
Cache::write('active_cheese_state_list', $stateList, 'month'); | |
} | |
$countryList = Cache::read('active_cheese_country_list','month'); | |
if(empty($countryList)){ | |
$countryList = $this->Cheese->CheeseLocation->getActiveCountries(); | |
Cache::write('active_cheese_country_list', $countryList, 'month'); | |
} | |
// Add filter | |
$this->Filter->addFilters( | |
array( | |
'milkSource' => array( | |
'MilkSource.id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'milkTreatment' => array( | |
'Cheese.milk_treatment_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'year' => array( | |
'Cheese.year_produced' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'texture' => array( | |
'Cheese.texture_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'rennetType' => array( | |
'Cheese.rennet_type_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'rind' => array( | |
'Cheese.rind_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
), | |
'stateRegion' => array( | |
'CheeseLocation.state_region_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
), | |
'select' => $this->Filter->select('StateRegion',$stateList) | |
) | |
), | |
'country' => array( | |
'CheeseLocation.country_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
), | |
'select' => $this->Filter->select('Country',$countryList) | |
) | |
), | |
/*'cheeseProducer' => array( | |
'Cheese.cheese_producer_id' => array( | |
'operator' => '=', | |
'value' => array( | |
'before' => '', // optional | |
'after' => '' // optional | |
) | |
) | |
)*/ | |
) | |
); | |
unset($countryList,$stateList); | |
} | |
/** | |
* parseSearchQuery method | |
* Handles cleaning up the search query | |
* @param void | |
* @return array | |
*/ | |
public function parseSearchQuery(){ | |
//Make sure the query isn't spaces | |
if( !empty($this->params->named['query']) ){ | |
$this->request->params['named']['query'] = addslashes(trim($this->params->named['query'])); | |
}else{ | |
$this->request->params['named']['query'] = ''; | |
} | |
if($this->request->is('post')){ | |
if(isset($this->request->data['Cheese']['query'])){ | |
$this->request->params['named']['query'] = $this->request->data['Cheese']['query']; | |
} | |
} | |
return $this->request->params['named']['query']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment