/** * beforeFilter * * @return void */ public function beforeFilter() { parent::beforeFilter(); } /** * index */ public function index($in_trash = false) { $this->->recursive = 0; $this->FilterResults->addFilters( array( 'filter_' => array( '.' => array( 'operator' => 'like', ) ), {$assoc} as $associationName => $relation): if (!empty($associationName)): $otherModelName = $this->_modelName($associationName); $otherModelKey = $this->_modelKey($associationName); $filterName = $this->_singularHumanName($otherModelName); ?> 'filter__slug($otherModelName) ?>' => array( '.' => array( 'select' => $this->FilterResults->select(__(''), $this->->->find('list')), ) ), ) ); /* * Regras Fixas */ $conditions = array( '.in_trash' => $in_trash ); /* * Mix com filter_results */ if ($this->FilterResults->getConditions()) { $conditions = array_merge($this->FilterResults->getConditions(), $conditions); } /** * Itens por pagina */ $data = $this->request->params; $itemsPerPage = $this->items_per_page("{$data['controller']}/{$data['action']}"); $params = $itemsPerPage['ItemsPerPage']; $this->FilterResults->setPaginate('limit', $params['quantity']); if ($params['orderby_field'] != '') { $this->FilterResults->setPaginate('order', "{$params['orderby_field']} {$params['orderby_type']}"); } $this->set('items_per_page', $params['quantity']); $this->set('orderby_type', $params['orderby_type']); $this->set('orderby', array( $params['orderby_field'] => $params['orderby_field'], '.name' => __('Name'), {$assoc} as $associationName => $relation): if (!empty($associationName)): $otherModelName = $this->_modelName($associationName); $otherModelKey = $this->_modelKey($associationName); echo "'{$otherModelName}.name' => __('{$associationName}'), \n"; endif; endforeach; endforeach; ?> )); // Define conditions $this->FilterResults->setPaginate('conditions', $conditions); $this->set('box_visible', $this->box_visibility($data['controller'], $data['action'])); $this->set('', $this->paginate()); } /** * view */ public function view($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } $this->set('', $this->->read(null, $id)); {$assoc} as $associationName => $relation): if (!empty($associationName)): $otherModelName = $this->_modelName($associationName); $otherPluralName = $this->_pluralName($associationName); $otherModelKey = $this->_modelKey($associationName); echo " \${$otherPluralName} = array(\"\"=>\"\"); \n"; $compact[] = "'{$otherPluralName}'"; endif; endforeach; endforeach; if (!empty($compact)): echo "\n \$this->set(compact(".join(', ', $compact)."));\n"; endif; ?> } /** * add */ public function add($object_type = '', $object_id = null) { if ($this->request->is('post')) { $this->->create(); if ($this->->save($this->request->data)) { $this->Session->setFlash(__('The has been saved'), 'default', array(), 'good'); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The could not be saved. Please, try again.'), 'default', array(), 'bad'); } } {$assoc} as $associationName => $relation): if (!empty($associationName)): $otherModelName = $this->_modelName($associationName); $otherControllerName = Inflector::pluralize($this->_controllerPath($associationName)); echo " \${$otherControllerName} = \$this->{$currentModelName}->{$otherModelName}->list_all(); \n"; $compact[] = "'{$otherControllerName}'"; endif; endforeach; endforeach; if (!empty($compact)): echo "\n \$this->set(compact(".join(', ', $compact)."));\n"; endif; ?> } /** * edit */ public function edit($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } if ($this->request->is('post') || $this->request->is('put')) { if ($this->->save($this->request->data)) { $this->Session->setFlash(__('The has been saved'), 'default', array(), 'good'); $this->redirect(array('action' => 'view', $id)); } else { $this->Session->setFlash(__('The could not be saved. Please, try again.'), 'default', array(), 'bad'); } } {$currentModelName}->recursive = 2; \n"; echo " \${$currentModelName} = \$this->{$currentModelName}->read(null, \$id); \n"; echo " \$this->request->data = \${$currentModelName}; \n\n"; foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc): foreach ($modelObj->{$assoc} as $associationName => $relation): if (!empty($associationName)): $otherModelName = $this->_modelName($associationName); $otherModelKey = $this->_modelKey($associationName); $otherControllerName = Inflector::pluralize($this->_controllerPath($associationName)); echo " \${$otherControllerName} = \$this->{$currentModelName}->{$otherModelName}->list_all(\${$currentModelName}['{$currentModelName}']['{$otherModelKey}']); \n"; $compact[] = "'{$otherControllerName}'"; endif; endforeach; endforeach; if (!empty($compact)): echo "\n \$this->set(compact(".join(', ', $compact)."));\n"; endif; ?> } /** * remove */ public function remove($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } $this->->set('in_trash', true); if ($this->->save()) { $this->Session->setFlash(__('') .' '. __('deleted'), 'default', array(), 'good'); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('') .' '. __('was not deleted'), 'default', array(), 'bad'); $this->redirect(array('action' => 'index')); } /** * recover */ public function recover($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } $this->->set('in_trash', false); if ($this->->save()) { $this->Session->setFlash(__('') .' '. __('Recovered'), 'default', array(), 'good'); $this->redirect(array('action' => 'view', $id)); } $this->Session->setFlash(__('') .' '. __('was not recovered'), 'default', array(), 'bad'); $this->redirect(array('action' => 'index')); } /** * active_inactive */ public function active_inactive($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } $obj = $this->->read(array('active'), $id); $this->->saveField('active', !$obj['']['active']); $this->redirect(array( 'action' => 'index' )); } /** * Componente de busca Lookup */ public function admin_lookup() { $this->autoRender = false; if ($this->request->is('ajax')) { $query = $this->request->query; $this->autoRender = false; $this->->recursive = -1; $outputs = $this->->find('all', array( 'fields' => array( 'id', 'name', ), 'conditions' => array( 'AND' => array( 'LOWER(.name) LIKE' => '%' . strtolower($query['filter']) . '%', 'in_trash' => false ), ) )); $jsontext = ""; $sep = ''; foreach ($outputs as $obj) { $jsontext .= $sep . '{"id":"' . $obj['']['id'] . '", "text":"' . $obj['']['name'] . '"}'; $sep = ','; } return '[' . $jsontext . ']'; } } /** * Componente de busca Lookup list */ public function admin_lookup_init() { $this->autoRender = false; if ($this->request->is('ajax')) { $this->autoRender = false; $this->->recursive = -1; $outputs = $this->->find('list', array('limit' => 50)); $jsontext = ""; $sep = ''; foreach ($outputs as $obj) { $jsontext .= $sep . '{"id":"' . $obj['']['id'] . '", "text":"' . $obj['']['name'] . '"}'; $sep = ','; } return '[' . $jsontext . ']'; } } /** * set default_selected */ public function admin_default_selected($id = null) { $this->->id = $id; if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } $this->->updateAll(array('default_selected' => false)); $obj = $this->->read(array('default_selected'), $id); $this->->saveField('default_selected', !$obj['']['default_selected']); $this->redirect(array( 'action' => 'index' )); } /** * admin_jquery_Add */ public function admin_jquery_add() { $this->autoRender = false; if ($this->request->is('ajax')) { $query = $this->request->query; $this->->create(); $this->request->data['']['name'] = $query['p_name']; $lastid = ''; if ($this->->save($this->request->data)) { $lastid = $this->->id; } return json_encode(array( "id" => $lastid, "desc" => $query['p_name'] )); } } /** * Retorna um json de itens para popular um combo */ public function list_all() { $this->recursive = -1; $items = $this->find('all', array( 'fields' => array( 'id', 'name' ), 'order' => array( 'default_selected', 'name' ) )); $jsonItems = ''; foreach ($items as $item) { $jsonItems .= "{id: '{$item['']['id']}', 'text': '{$item['']['name']}'},"; } return "[{$jsonItems}]"; } /** * admin_reordenation */ public function admin_reordenation() { if ($this->request->is('post') || $this->request->is('put')) { foreach ($this->request->data[''] as $item) { $this->->id = $item['id']; $this->->saveField('ordenation', $item['ordenation']); } } $this->redirect(array('action' => 'index')); }