Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function index($category = null)
- {
- $conditions = $conditionJoin = array();
- //Transform POST into GET
- if (($this->request->is('post') || $this->request->is('put')) && isset($this->data['Filter'])) {
- $filter_url[] = $category;
- $filter_url['controller'] = $this->request->params['controller'];
- $filter_url['action'] = $this->request->params['action'];
- $filter_url['page'] = 1; // We need to overwrite the page every time we change the parameters
- foreach ($this->data['Filter'] as $name => $value) {// for each filter we will add a GET parameter for the generated url
- if ($value)
- $filter_url[$name] = urlencode((is_array($value) ? implode(",", $value) : $value));
- }
- return $this->redirect($filter_url); //now that we have generated an url with GET parameters, we'll redirect to that page
- } else {
- //debug($this->params['named']);
- foreach ($this->params['named'] as $param_name => $value) {// Inspect all the named parameters to apply the filters
- if (!in_array($param_name, array('page', 'sort', 'direction', 'limit'))) {// Don't apply the default named parameters used for pagination
- if ($param_name == "search") {// You may use a switch here to make special filters like "between dates", "greater than", etc
- $conditions['OR'] = array(
- array('Record.name LIKE' => '%' . urldecode($value) . '%'),
- array('Record.minit_surat LIKE' => '%' . urldecode($value) . '%'),
- array('Record.penerima_surat LIKE' => '%' . urldecode($value) . '%'),
- array('Record.rujukan LIKE' => '%' . urldecode($value) . '%'),
- );
- } elseif ($param_name == "year") {
- $conditions['YEAR(Record.date)'] = $value;
- } else {
- if (!in_array($param_name, array('department', 'tag')))
- $conditions['Record.' . $param_name] = $value;
- }
- if ($param_name == "department") {
- $value = (is_array($value) ? explode(",", urldecode($value)) : $value);
- $conditions['Assign.department_id IN'] = array($value);
- $conditionJoin[] = array(
- 'type' => 'LEFT',
- 'table' => 'assigns',
- 'alias' => 'Assign',
- 'conditions' => array('Assign.record_id = Record.id')
- );
- }
- if ($param_name == "tag") {
- $value = (is_array($value) ? explode(",", urldecode($value)) : $value);
- $conditions['RecordsTag.tag_id IN'] = array($value);
- $conditionJoin[] = array(
- 'type' => 'LEFT',
- 'table' => 'records_tags',
- 'alias' => 'RecordsTag',
- 'conditions' => array('RecordsTag.record_id = Record.id')
- );
- }
- //convet urldecode from urlencode of value
- //explode if value is encode from array
- $value = explode(",", urldecode($value));
- //array_combine for array selected
- $this->request->data['Filter'][$param_name] = array_combine($value, $value);
- }
- }
- }
- $conditions['Record.category_id'] = $category;
- //Set Group Executive open other deparments data
- //$groupCode = AuthComponent::user('department_id');
- $groupCode = AuthComponent::user('group_id');
- if ($groupCode == 3) {//3 = Executive
- $conditions['Record.department_by'] = $this->get_department_permission($groupCode);
- } else {
- $conditions['Record.department_by'] = AuthComponent::user('department_id');
- }
- $this->Paginator->settings = array(
- 'conditions' => $conditions,
- 'order' => array('Record.id' => 'DESC'),
- 'contain' => array(
- 'Status',
- 'Tag'
- ),
- 'joins' => $conditionJoin
- );
- $records = $this->Paginator->paginate('Record');
- // debug($records);
- // debug($conditions);
- $this->set('records', $records);
- // get the possible values for the filters and
- // pass them to the view
- $departments = $this->Department->find('list');
- $statuses = $this->Record->Status->find('list');
- $tags = $this->Record->Tag->find('list');
- $this->set(compact('departments', 'statuses', 'tags'));
- // Pass the search parameter to highlight the text
- $this->set('search', isset($this->params['named']['search']) ? $this->params['named']['search'] : "");
- $this->set('category', $category);
- }
Advertisement
Add Comment
Please, Sign In to add comment