mzm-dev

controller

May 22nd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. public function index($category = null)
  2.     {
  3.  
  4.         $conditions = $conditionJoin = array();
  5.         //Transform POST into GET
  6.         if (($this->request->is('post') || $this->request->is('put')) && isset($this->data['Filter'])) {
  7.  
  8.             $filter_url[] = $category;
  9.             $filter_url['controller'] = $this->request->params['controller'];
  10.             $filter_url['action'] = $this->request->params['action'];
  11.             $filter_url['page'] = 1; // We need to overwrite the page every time we change the parameters
  12.  
  13.             foreach ($this->data['Filter'] as $name => $value) {// for each filter we will add a GET parameter for the generated url
  14.                 if ($value)
  15.                     $filter_url[$name] = urlencode((is_array($value) ? implode(",", $value) : $value));
  16.             }
  17.             return $this->redirect($filter_url); //now that we have generated an url with GET parameters, we'll redirect to that page
  18.         } else {
  19.             //debug($this->params['named']);
  20.             foreach ($this->params['named'] as $param_name => $value) {// Inspect all the named parameters to apply the filters
  21.                 if (!in_array($param_name, array('page', 'sort', 'direction', 'limit'))) {// Don't apply the default named parameters used for pagination
  22.                     if ($param_name == "search") {// You may use a switch here to make special filters like "between dates", "greater than", etc
  23.                         $conditions['OR'] = array(
  24.                             array('Record.name LIKE' => '%' . urldecode($value) . '%'),
  25.                             array('Record.minit_surat LIKE' => '%' . urldecode($value) . '%'),
  26.                             array('Record.penerima_surat LIKE' => '%' . urldecode($value) . '%'),
  27.                             array('Record.rujukan LIKE' => '%' . urldecode($value) . '%'),
  28.                         );
  29.                     } elseif ($param_name == "year") {
  30.                         $conditions['YEAR(Record.date)'] = $value;
  31.                     } else {
  32.                         if (!in_array($param_name, array('department', 'tag')))
  33.                             $conditions['Record.' . $param_name] = $value;
  34.                     }
  35.  
  36.  
  37.                     if ($param_name == "department") {
  38.                         $value = (is_array($value) ? explode(",", urldecode($value)) : $value);
  39.                         $conditions['Assign.department_id IN'] = array($value);
  40.                         $conditionJoin[] = array(
  41.                             'type' => 'LEFT',
  42.                             'table' => 'assigns',
  43.                             'alias' => 'Assign',
  44.                             'conditions' => array('Assign.record_id = Record.id')
  45.                         );
  46.                     }
  47.                     if ($param_name == "tag") {
  48.                         $value = (is_array($value) ? explode(",", urldecode($value)) : $value);
  49.                         $conditions['RecordsTag.tag_id IN'] = array($value);
  50.                         $conditionJoin[] = array(
  51.                             'type' => 'LEFT',
  52.                             'table' => 'records_tags',
  53.                             'alias' => 'RecordsTag',
  54.                             'conditions' => array('RecordsTag.record_id = Record.id')
  55.                         );
  56.                     }
  57.                     //convet urldecode from urlencode of value
  58.                     //explode if value is encode  from array
  59.                     $value = explode(",", urldecode($value));
  60.                     //array_combine for array selected
  61.                     $this->request->data['Filter'][$param_name] = array_combine($value, $value);
  62.                 }
  63.             }
  64.         }
  65.         $conditions['Record.category_id'] = $category;
  66.         //Set Group Executive open other deparments data
  67.         //$groupCode = AuthComponent::user('department_id');
  68.         $groupCode = AuthComponent::user('group_id');
  69.         if ($groupCode == 3) {//3 = Executive
  70.             $conditions['Record.department_by'] = $this->get_department_permission($groupCode);
  71.         } else {
  72.             $conditions['Record.department_by'] = AuthComponent::user('department_id');
  73.         }
  74.  
  75.         $this->Paginator->settings = array(
  76.             'conditions' => $conditions,
  77.             'order' => array('Record.id' => 'DESC'),
  78.             'contain' => array(
  79.                 'Status',
  80.                 'Tag'
  81.             ),
  82.             'joins' => $conditionJoin
  83.         );
  84.         $records = $this->Paginator->paginate('Record');
  85. //        debug($records);
  86. //        debug($conditions);
  87.         $this->set('records', $records);
  88.  
  89.         // get the possible values for the filters and
  90.         // pass them to the view
  91.         $departments = $this->Department->find('list');
  92.         $statuses = $this->Record->Status->find('list');
  93.         $tags = $this->Record->Tag->find('list');
  94.         $this->set(compact('departments', 'statuses', 'tags'));
  95.  
  96.         // Pass the search parameter to highlight the text
  97.         $this->set('search', isset($this->params['named']['search']) ? $this->params['named']['search'] : "");
  98.         $this->set('category', $category);
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment