Skip to content

Instantly share code, notes, and snippets.

@possamai
Created July 23, 2015 01:36
Show Gist options
  • Save possamai/61c8a5935cc0857f2351 to your computer and use it in GitHub Desktop.
Save possamai/61c8a5935cc0857f2351 to your computer and use it in GitHub Desktop.
// NO CAKE 2
// controller\component\searchComponent.php
class SearchComponent extends Component {
/*
* @usage Verify exist parameters to filter
* @return array
*/
public function getCondition() {
$aux_filter = array();
$this->arr_condition = array();
$model = $this->controller->modelClass;
$named = $this->controller->request->params['named'];
$str_controller_action = $this->controller->request->params['controller'].'.'.$this->controller->request->params['action'];
$filtro = (($this->Session->check('Filter.List')) ? unserialize($this->Session->read('Filter.List')) : array()) ;
$filter_url = array('controller' => $this->controller->request->params['controller'], 'action' => $this->controller->request->params['action'], 'page' => 1);
// Direciona Form
if ( isset($this->controller->data['Filter']) ) {
foreach ($this->controller->data['Filter'] as $name => $value) {
if ($value) { $filter_url[$name] = ($value); }
}
return $this->controller->redirect($filter_url); die;
// Filtra pela URL
} else if (count($named)>0) {
foreach ($named as $name => $value) {
if (!in_array($name, array('page','sort', 'direction', 'limit', 'controller', 'action', 'time'))) {
switch( $name ) {
case 'limpar_filtro': $aux_filter[] = array(); break(2);
default: $this->arr_condition[$name] = $value; break;
}
$this->controller->request->data['Filter'][$name] = $value;
}
$aux_filter[$name] = ($value);
}
$aux_filter['time'] = strtotime('now');
$filtro[$this->controller->request->params['controller'].'.'.$this->controller->request->params['action']] = $aux_filter;
// Direciona Session
} else if ( isset($filtro[$str_controller_action]) ) {
if ($filtro[$str_controller_action]['time']>strtotime($this->controller->time_filter)) {
foreach ($filtro[$str_controller_action] as $name => $value) {
if (($value)&&($name<>'time')) { $filter_url[$name] = urldecode($value); }
}
return $this->controller->redirect( $filter_url); die;
} else {
unset($filtro[$str_controller_action]);
}
}
$this->Session->write('Filter.List', serialize($filtro));
return $this->arr_condition;
}
}
// Nos meus controllers
class UsuariosController extends AppController {
public $components = array( 'Search' );
public function admin_index() {
$arr_conditions = $this->Search->getCondition(); // Aqui tenho o array que eu preciso
// lógica
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment