Overview

Namespaces

  • Cron
  • None

Classes

  • _HtmlGrid_TableOut
  • _TransactionModel_RawSaveOperation
  • AccessControlComponent
  • AccessControlHelper
  • ActionListHelper
  • ActiveDirectoryUtils
  • AddCurrentPidToSchedulingShellCallLogs
  • AnonymousFunctionOperation
  • ArrayUtil
  • AssociationIntegrityBehavior
  • AtomicOperation
  • AuthenticationComponent
  • AuthenticationController
  • AuthenticationUser
  • AutocompleteDatasourceComponent
  • BaseModelComponent
  • Basics
  • CakeLayersHelper
  • CheckAndRunShell
  • ClassSearcher
  • CommandLineUtil
  • CommonValidationBehavior
  • ConfigurableShellCallsSchedulingTask
  • ConfigurationKey
  • ConfigurationKeys
  • ConfigurationKeysController
  • Context
  • ContextComponent
  • ContextHelper
  • Contexts
  • ControllerInspector
  • ControllerMenuHelper
  • CreateJournalingTables
  • CreateTableConfigurationKeys
  • CreateTableSchedulingConfigurableShellCalls
  • CreateTableSchedulingShellCallLogs
  • CreateTableSettedConfigurationKeys
  • CreateTableUserResetPasswordRequests
  • CreateTableUsers
  • CronSchedulingInstaller
  • CronValidationBehavior
  • CssBox
  • CssController
  • CssProperties
  • CssShell
  • CsvUtil
  • CustomDataModel
  • DatasourceDumperManager
  • DateTimeInput
  • DependencyShell
  • DetailHelper
  • DumperShell
  • ExtendedFieldsAccessControl
  • ExtendedFieldSet
  • ExtendedFieldSetHelper
  • ExtendedFieldsParser
  • ExtendedFormHelper
  • ExtendedHasManyAppModel
  • ExtendedOperationsBehavior
  • FieldDefinition
  • FieldRowDefinition
  • FieldSetDefinition
  • FieldSetLayoutHelper
  • FileOperations
  • FileOperations_Rename
  • FileOperations_SymLink
  • FileOperations_Touch
  • FileOperations_Unlink
  • FileSystem
  • FixConfigurationKeysPrimaryKey
  • FixSettedConfigurationKeysPrimaryKey
  • HasManyUtilsBehavior
  • HtmlDocument
  • HtmlGrid
  • HtmlGrid_Cell
  • HttpClient
  • HttpResponse
  • ImapClient
  • ImapMailBox
  • ImapParserShell
  • IncludePath
  • InputMasked
  • InputSearchable
  • InputsOnSubmit
  • InstallShell
  • JenkinsBuildShell
  • Journal
  • JournalDetail
  • JournalizedBehavior
  • JsonResponseComponent
  • LayoutsHelper
  • Ldap
  • LdapUtils
  • ListFieldSet
  • ListFieldSetHelper
  • ListsHelper
  • MailParser
  • Make
  • MenuHelper
  • MigrationAllPluginsShell
  • ModelOperations
  • ModelOperations_Delete
  • ModelOperations_Save
  • ModelTraverser
  • MysqlDumper
  • OpenLdapUtils
  • PaginatorUtilComponent
  • PaginatorUtilComponentFilter
  • PaginatorUtilHelper
  • Plugin
  • PluginManager
  • Reflections
  • RenameEnabledToActiveFromUsersTable
  • RunShellCallShell
  • ScaffoldUtilComponent
  • ScaffoldUtilHelper
  • Scheduling
  • SchedulingConfigurableShellCall
  • SchedulingConfigurableShellCallsController
  • SchedulingShellCallLog
  • SchedulingShellCallLogsController
  • SettedConfigurationKey
  • StuffreposPluginsRename
  • TimeZoneBehavior
  • TransactionModel
  • TransactionOperation
  • Translator
  • TranslatorShell
  • User
  • UserAuthenticationComponent
  • UserChangePassword
  • UserResetPassword
  • UserResetPasswordRequest
  • UserResetPasswordRequestSubmission
  • UsersController
  • ViewUtilHelper

Interfaces

  • AccessControlFilter
  • CommitableOperation
  • DatasourceDumper
  • MakeListener
  • SchedulingInstaller
  • TasksObject
  • UndoableOperation

Exceptions

  • LdapObjectNotWritableException
  • ModelTraverserException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Operações comuns de model que geram exceção caso não sejam executadas
  5:  * com sucesso (As operações nativas correspondentes retornam com valor
  6:  * falso):
  7:  * <ul>
  8:  *  <li>saveOrThrowException</li>
  9:  *  <li>saveAllOrThrowException</li>
 10:  *  <li>findByIdOrThrowException</li>
 11:  * </ul>
 12:  * 
 13:  * Como em controllers, utilize o atributo \$uses para autocarregamento
 14:  * de modelos:
 15:  * <pre>
 16:  * class MyModel {
 17:  * 
 18:  *  public $actsAs = array(
 19:  *      'Base.ExtendedOperations',
 20:  *  );  
 21:  * 
 22:  *  public $uses = array(
 23:  *      'OtherModel',
 24:  *  );
 25:  * 
 26:  * }
 27:  * 
 28:  * $myModel = ClassRegistry::init('MyModel');
 29:  * $myModel->OtherModel->find(...);
 30:  * </pre>
 31:  */
 32: class ExtendedOperationsBehavior extends ModelBehavior {
 33:     
 34:     public function setup(\Model $model, $config = array()) {
 35:         parent::setup($model, $config);
 36:         $this->_setupUses($model);
 37:     }
 38: 
 39:     public function beforeFind(\Model $model, $query) {
 40:         parent::beforeFind($model, $query);
 41:         $this->applyVirtualFields($model);
 42:     }
 43:     
 44:     public function getVirtualFieldQuery(\Model $model, $virtualFieldName) {
 45:         $this->applyVirtualFields($model);
 46:         return $model->virtualFields[$virtualFieldName];
 47:     }
 48: 
 49:     public function applyVirtualFields(\Model $model) {
 50:         if (empty($model->virtualFieldsSetted)) {
 51:             if (method_exists($model, 'getVirtualFields')) {
 52:                 foreach($this->_parseVirtualFields($model) as $name => $schema) {
 53:                     $model->virtualFields[$name] = $schema['query'];
 54:                     unset($schema['query']);
 55:                     if (!isset($model->virtualFieldsSchema)) {
 56:                         $model->virtualFieldsSchema = array();
 57:                     }
 58:                     $model->virtualFieldsSchema[$name] = $schema;
 59:                 }
 60:             }
 61:             $model->virtualFieldsSetted = true;
 62:         }
 63:     }
 64: 
 65:     public function saveOrThrowException(\Model $model, $data = null, $validate = true, $fieldList = array()) {
 66:         if (!$model->save($data, $validate, $fieldList)) {
 67:             throw new Exception("{$model->name} não foi salvo. " . print_r(array(
 68:                 'parameters' => compact('data', 'validade', 'fieldList'),
 69:                 '$model->data' => $model->data,
 70:                 '$model->validationErrors' => $model->validationErrors
 71:                     ), true));
 72:         }
 73:     }
 74: 
 75:     public function saveAllOrThrowException(\Model $model, $data = array(), $options = array()) {
 76:         if (!$model->saveAll($data, $options)) {
 77:             throw new Exception("{$model->name} não foi salvo. " . print_r(array(
 78:                 'parameters' => compact('data', 'validade', 'fieldList'),
 79:                 '$model->data' => $model->data,
 80:                 '$model->validationErrors' => $model->validationErrors
 81:                     ), true));
 82:         }
 83:     }
 84: 
 85:     public function findByIdOrThrowException(\Model $model, $id) {
 86:         $row = $model->find(
 87:                 'first', array(
 88:             'conditions' => array(
 89:                 "{$model->alias}.{$model->primaryKey}" => $id
 90:             )
 91:         ));
 92:         if (empty($row)) {
 93:             throw new Exception("{$model->name} não foi recuperado com o ID=$id.");
 94:         } else {
 95:             return $row;
 96:         }
 97:     }
 98:     
 99:     public function  valuesFromValidationInList(\Model $model, $field) {
100:         if (($validate = $this->_fieldInListValidation($model, $field))) {
101:             if (!empty($validate['rule'][1]) && is_array($validate['rule'][1])) {
102:                 return $validate['rule'][1];
103:             }
104:             else {
105:                 throw new Exception("\"{$model->name}.$field\" inList validation has no list: ".print_r($validate, true));
106:             }
107:             
108:         } else {
109:             throw new Exception("List validation not found for field \"{$model->name}.$field\"");
110:         }
111:     }
112: 
113:     public function listFromValidationInList(\Model $model, $field) {
114:         $values = $this->valuesFromValidationInList($model, $field);
115:         sort($values);
116:         return array_merge(
117:                 array(
118:             '' => '-- SELECIONE --',
119:                 ), ArrayUtil::keysAsValues($values)
120:         );
121:     }
122: 
123:     public function listsFromValidationsInList(\Model $model) {
124:         $ret = array();
125:         foreach(array_keys($model->schema()) as $field) {
126:             if ( $this->_fieldInListValidation($model,$field) ) {
127:                 $ret[Inflector::variable(Inflector::pluralize($field))] = $this->listFromValidationInList($model, $field);
128:             }
129:         }
130:         return $ret;
131:     }
132: 
133:     /**
134:      * Retorna a validação inList de um campo se existir.
135:      * @param mixed $fieldName O array da validação ou FALSE caso não exista
136:      * uma validação inList para o campo.
137:      */
138:     private function _fieldInListValidation(\Model $model, $field) {
139:         if (!empty($model->validate[$field])) {
140:             foreach ($model->validate[$field] as $validate) {
141:                 if (!empty($validate['rule'][0]) && $validate['rule'][0] == 'inList' && !empty($validate['rule'][1])) {
142:                     return $validate;
143:                 }
144:             }
145:         }
146:         return false;
147:     }
148: 
149:     private function _parseVirtualFields(\Model $model) {
150:         $ret = array();
151:         foreach($model->getVirtualFields() as $name => $data) {
152:             if (!is_array($data)) {
153:                 $data = array(
154:                     'query' => $data
155:                 );
156:             }
157:             $ret[$name] = $data;
158:         }
159:         return $ret;
160:     }
161:     
162:     private function _setupUses(\Model $model) {
163:         if (isset($model->uses)) {
164:             foreach($model->uses as $modelName) {
165:                 $model->{$modelName} = ClassRegistry::init($modelName);
166:             }
167:         }
168:     }
169: 
170: }
171: 
API documentation generated by ApiGen 2.8.0