/** * Model */ class extends AppModel { /** * Use database config */ public $useDbConfig = ''; /** * Campos usado nos combos como campo label */ public $displayField = 'name'; $validations): echo " '$field' => array(\n"; foreach ($validations as $key => $validator): echo " '$key' => array(\n"; echo " 'rule' => array('$validator'),\n"; echo " 'message' => 'Your custom message here',\n"; echo " 'allowEmpty' => false,\n"; echo " 'required' => false,\n"; echo " 'last' => false, // Stop validation after this rule\n"; echo " 'on' => 'create', // Limit validation to 'create' or 'update' operations\n"; echo " ),\n"; endforeach; echo " ),\n"; endforeach; echo " );\n"; endif; foreach ($associations as $assoc): if (!empty($assoc)): break; endif; endforeach; foreach (array('hasOne', 'belongsTo') as $assocType): if (!empty($associations[$assocType])): $typeCount = count($associations[$assocType]); echo "\n /**\n * $assocType associations\n *\n * @var array\n */"; echo "\n public \$$assocType = array("; foreach ($associations[$assocType] as $i => $relation): $currentClassName = $relation['className']; if (($relation['className'] === 'Owner') || ($relation['className'] === 'CreatedBy') || ($relation['className'] === 'AssignedTo')){ $currentClassName = 'User'; } $out = "\n '{$relation['alias']}' => array(\n"; $out .= " 'className' => '{$currentClassName}',\n"; $out .= " 'foreignKey' => '{$relation['foreignKey']}',\n"; $out .= " 'conditions' => '',\n"; $out .= " 'fields' => '',\n"; $out .= " 'order' => ''\n"; $out .= " )"; if ($i + 1 < $typeCount) { $out .= ","; } echo $out; endforeach; echo "\n );\n"; endif; endforeach; if (!empty($associations['hasMany'])): $belongsToCount = count($associations['hasMany']); echo "\n /**\n * hasMany associations\n *\n * @var array\n */"; echo "\n public \$hasMany = array("; foreach ($associations['hasMany'] as $i => $relation): $currentClassName = $relation['className']; if (($relation['className'] === 'Owner') || ($relation['className'] === 'CreatedBy') || ($relation['className'] === 'AssignedTo')){ $currentClassName = 'User'; } $out = "\n '{$relation['alias']}' => array(\n"; $out .= " 'className' => '{$currentClassName}',\n"; $out .= " 'foreignKey' => '{$relation['foreignKey']}',\n"; $out .= " 'dependent' => false,\n"; $out .= " 'conditions' => '',\n"; $out .= " 'fields' => '',\n"; $out .= " 'order' => '',\n"; $out .= " 'limit' => '',\n"; $out .= " 'offset' => '',\n"; $out .= " 'exclusive' => '',\n"; $out .= " 'finderQuery' => '',\n"; $out .= " 'counterQuery' => ''\n"; $out .= " )"; if ($i + 1 < $belongsToCount) { $out .= ","; } echo $out; endforeach; echo "\n );\n\n"; endif; if (!empty($associations['hasAndBelongsToMany'])): $habtmCount = count($associations['hasAndBelongsToMany']); echo "\n /**\n * hasAndBelongsToMany associations\n *\n * @var array\n */"; echo "\n public \$hasAndBelongsToMany = array("; foreach ($associations['hasAndBelongsToMany'] as $i => $relation): $currentClassName = $relation['className']; if (($relation['className'] === 'Owner') || ($relation['className'] === 'CreatedBy') || ($relation['className'] === 'AssignedTo')){ $currentClassName = 'User'; } $out = "\n '{$relation['alias']}' => array(\n"; $out .= " 'className' => '{$currentClassName}',\n"; $out .= " 'joinTable' => '{$relation['joinTable']}',\n"; $out .= " 'foreignKey' => '{$relation['foreignKey']}',\n"; $out .= " 'associationForeignKey' => '{$relation['associationForeignKey']}',\n"; $out .= " 'unique' => 'keepExisting',\n"; $out .= " 'conditions' => '',\n"; $out .= " 'fields' => '',\n"; $out .= " 'order' => '',\n"; $out .= " 'limit' => '',\n"; $out .= " 'offset' => '',\n"; $out .= " 'finderQuery' => '',\n"; $out .= " 'deleteQuery' => '',\n"; $out .= " 'insertQuery' => ''\n"; $out .= " )"; if ($i + 1 < $habtmCount) { $out .= ","; } echo $out; endforeach; echo "\n );\n\n"; endif; ?> /** * Retorna um json de itens para popular um combo */ public function list_all($current_item_id = null) { $jsonItems = ''; $where = array(); if ($current_item_id != null) { $current_item = $this->read(array( 'id', 'name' ), $current_item_id); $jsonItems .= "{id: '{$current_item['']['id']}', 'text': '{$current_item['']['name']}'},"; $where = array( 'id !=' => $current_item['']['id'] ); } $this->recursive = -1; $items = $this->find('all', array( 'fields' => array( 'id', 'name' ), 'order' => array( 'default_selected DESC', 'name' ), 'conditions' => $where )); foreach ($items as $item) { $jsonItems .= "{id: '{$item['']['id']}', 'text': '{$item['']['name']}'},"; } return "[{$jsonItems}]"; } $relation): $joinTable = $relation['joinTable']; $foreignKey = $relation['foreignKey']; $associationForeignKey = $relation['associationForeignKey']; $currentClassName = $relation['className']; if (($relation['className'] === 'Owner') || ($relation['className'] === 'CreatedBy') || ($relation['className'] === 'AssignedTo')){ $currentClassName = 'User'; } ?> /** * Retorna a lista de chatqueues formatada * $resource = 'list', 'all', 'keys' * * ==> all * * array( * (int) 0 => array( * '' => array( * 'id' => (int) 1, * 'name' => 'Comercial', * ) * ), * (int) 1 => array( * '' => array( * 'id' => (int) 2, * 'name' => 'Suporte', * ) * )); * * ==> list * * array( * (int) 1 => array( * (int) 1 => 'Comercial' * ), * (int) 2 => array( * (int) 2 => 'Suporte' * )); * * ==> keys * * array( * (int) 0 => (int) 1, * (int) 1 => (int) 2, * ); * * No Controller * * $selecteds = $this->->getById(1, 'keys'); * $this->set(compact('selecteds')); * * Na View * * <\?php * echo $this->Form->input('.', array( * 'label' => false, * 'type' => 'select', * 'multiple' => 'checkbox', * 'class' => 'no-border', * 'selected' => $selecteds * )); * ?> */ public function getById($Id = null, $resource = 'list') { if (empty($Id)) { return false; } $action = $resource; if ($resource == 'keys') { $action = 'list'; } $items = $this->find($action, array( 'fields' => array( '.id', '.name' ), 'joins' => array( array('table' => '', 'alias' => '', 'type' => 'INNER', 'conditions' => array( '.' => $Id, '. = .id' ) ) ), 'group' => '.id', 'recursive' => -1, )); if ($resource == 'keys') { return array_keys($items); } return $items; } /** * EXEMPLO HABTM * * Retorna a lista de chatqueues formatada * $resource = 'list', 'all', 'keys' * * ==> all * * array( * (int) 0 => array( * 'ChatQueue' => array( * 'id' => (int) 1, * 'name' => 'Comercial', * 'code' => '1' * ) * ), * (int) 1 => array( * 'ChatQueue' => array( * 'id' => (int) 2, * 'name' => 'Suporte', * 'code' => '2' * ) * )); * * ==> list * * array( * (int) 1 => array( * (int) 1 => 'Comercial' * ), * (int) 2 => array( * (int) 2 => 'Suporte' * )); * * ==> keys * * array( * (int) 0 => (int) 1, * (int) 1 => (int) 2, * ); * * No Controller * * $selecteds = $this->ChatQueue->getChatQueuesByUserId($this->Auth->user('id'), 'keys'); * $this->set(compact('selecteds')); * * Na View * * <\?php * echo $this->Form->input('ChatQueue.ChatQueue', array( * 'label' => false, * 'type' => 'select', * 'multiple' => 'checkbox', * 'class' => 'no-border', * 'selected' => $selecteds * )); * ?> */ public function getChatQueuesByUserId($userId = null, $resource = 'list') { if (empty($userId)) { return false; } $action = $resource; if ($resource == 'keys') { $action = 'list'; } $chatQueues = $this->find($action, array( 'fields' => array( 'ChatQueue.id', 'ChatQueue.name', 'ChatQueue.code', ), 'joins' => array( array('table' => 'chat_queue_users', 'alias' => 'ChatQueueUser', 'type' => 'INNER', 'conditions' => array( 'ChatQueueUser.user_id' => $userId, 'ChatQueueUser.chat_queue_id = ChatQueue.id' ) ) ), 'group' => 'ChatQueue.id', 'recursive' => -1, )); if ($resource == 'keys') { return array_keys($chatQueues); } return $chatQueues; } }