/**
  * Initiate WhoDidIt Behavior
  *
  * @param object $Model
  * @param array $config  behavior settings you would like to override
  * @return void
  * @access public
  */
 public function setup(Model $Model, $config = array())
 {
     //assign default settings
     $this->settings[$Model->alias] = $this->_defaults;
     //merge custom config with default settings
     $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], (array) $config);
     $hasFieldCreatedBy = $Model->hasField($this->settings[$Model->alias]['created_by_field']);
     $hasFieldModifiedBy = $Model->hasField($this->settings[$Model->alias]['modified_by_field']);
     $hasFieldConfirmedBy = $Model->hasField($this->settings[$Model->alias]['confirmed_by_field']);
     $this->settings[$Model->alias]['has_created_by'] = $hasFieldCreatedBy;
     $this->settings[$Model->alias]['has_modified_by'] = $hasFieldModifiedBy;
     $this->settings[$Model->alias]['has_confirmed_by'] = $hasFieldConfirmedBy;
     //handles model binding to the User model
     //according to the auto_bind settings (default true)
     if ($this->settings[$Model->alias]['auto_bind']) {
         if ($hasFieldCreatedBy) {
             $commonBelongsTo = array('CreatedBy' => array('className' => $this->settings[$Model->alias]['user_model'], 'foreignKey' => $this->settings[$Model->alias]['created_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
         if ($hasFieldModifiedBy) {
             $commonBelongsTo = array('ModifiedBy' => array('className' => $this->settings[$Model->alias]['user_model'], 'foreignKey' => $this->settings[$Model->alias]['modified_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
         if ($hasFieldConfirmedBy) {
             $commonBelongsTo = array('ConfirmedBy' => array('className' => $this->settings[$Model->alias]['user_model'], 'foreignKey' => $this->settings[$Model->alias]['confirmed_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
     }
 }
 /**
  * Initiate WhoMadeIt Behavior
  *
  * @param object $model
  * @param array $config  behavior settings you would like to override
  * @return void
  * @access public
  */
 function setup(Model $model, $config = array())
 {
     if ($model->alias != 'Dashboard' && $model->alias != 'NotificaionType' && $model->alias != 'Page') {
         //assigne default settings
         $this->settings[$model->alias] = $this->_defaults;
         //merge custom config with default settings
         $this->settings[$model->alias] = array_merge($this->settings[$model->alias], (array) $config);
         $hasFieldCreatedBy = $model->hasField($this->settings[$model->alias]['created_by_field']);
         $hasFieldModifiedBy = $model->hasField($this->settings[$model->alias]['modified_by_field']);
         $this->settings[$model->alias]['has_created_by'] = $hasFieldCreatedBy;
         $this->settings[$model->alias]['has_modified_by'] = $hasFieldModifiedBy;
         //handles model binding to the User model
         //according to the auto_bind settings (default true)
         if ($this->settings[$model->alias]['auto_bind']) {
             if ($hasFieldCreatedBy) {
                 $commonBelongsTo = array('CreatedBy' => array('className' => $this->settings[$model->alias]['user_model'], 'foreignKey' => $this->settings[$model->alias]['created_by_field'], 'fields' => array('id', 'name')));
                 $model->bindModel(array('belongsTo' => $commonBelongsTo), false);
             }
             if ($hasFieldModifiedBy) {
                 $commonBelongsTo = array('ModifiedBy' => array('className' => $this->settings[$model->alias]['user_model'], 'foreignKey' => $this->settings[$model->alias]['modified_by_field'], 'fields' => array('id', 'name')));
                 $model->bindModel(array('belongsTo' => $commonBelongsTo), false);
             }
         }
     }
 }
Example #3
0
 /**
  * Binds the commend and user model and the current model to the comments model
  *
  * @param Model $model
  * @return void
  */
 public function bindCommentModels(Model $model)
 {
     $config = $this->settings[$model->alias];
     if (!empty($config['commentModel']) && is_array($config['commentModel'])) {
         $model->bindModel(array('hasMany' => array('Comment' => $config['commentModel'])), false);
     } else {
         $model->bindModel(array('hasMany' => array('Comment' => array('className' => $config['commentModel'], 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'dependent' => true, 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))), false);
     }
     $model->Comment->bindModel(array('belongsTo' => array($model->alias => array('className' => $model->name, 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'counterCache' => true, 'dependent' => false))), false);
     if (!empty($config['userModel']) && is_array($config['userModel'])) {
         $model->bindModel(array('belongsTo' => array($config['userModelAlias'] => $config['userModel'])), false);
     } else {
         $model->Comment->bindModel(array('belongsTo' => array($config['userModelAlias'] => array('className' => $config['userModelClass'], 'foreignKey' => 'user_id', 'conditions' => '', 'fields' => '', 'counterCache' => true, 'order' => ''))), false);
     }
 }
Example #4
0
 /**
  * Enable Multiple Role, dynamically bind User Habtm Role
  */
 protected function _setupMultirole(Model $model)
 {
     if (!Configure::read('Access Control.multiRole')) {
         return;
     }
     $model->bindModel(array('hasAndBelongsToMany' => array('Role' => array('className' => 'Users.Role', 'unique' => 'keepExisting'))), false);
 }
Example #5
0
 /**
  * Bind tag assocations
  *
  * @param Model $model Model instance that behavior is attached to
  * @return void
  */
 public function bindTagAssociations(Model $model)
 {
     extract($this->settings[$model->alias]);
     list($plugin, $withClass) = pluginSplit($withModel, true);
     $model->bindModel(array('hasAndBelongsToMany' => array($tagAlias => array('className' => $tagClass, 'foreignKey' => $foreignKey, 'associationForeignKey' => $associationForeignKey, 'unique' => true, 'conditions' => array($withClass . '.model' => $model->name), 'fields' => '', 'dependent' => true, 'with' => $withModel))), $resetBinding);
     $model->{$tagAlias}->bindModel(array('hasMany' => array($taggedAlias => array('className' => $taggedClass))), $resetBinding);
 }
Example #6
0
 /**
  * beforeFind callback
  * 
  * @param object $Model
  * @param array $query
  */
 public function beforeFind(Model $Model, $query)
 {
     $Model->bindModel(array('hasOne' => array('Alias' => array('className' => 'Alias', 'foreignKey' => 'value', 'dependent' => true, 'conditions' => array('Alias.controller' => Inflector::tableize($Model->name)), 'fields' => '', 'order' => 'Alias.modified DESC'))), false);
     $query['contain'][] = 'Alias';
     $query['group'] = !empty($query['group']) ? $query['group'] : array($Model->alias => 'id');
     return parent::beforeFind($Model, $query);
 }
 /**
  * Binds the cart association if no HABTM assoc named 'Cart' already exists.
  *
  * @param Model $Model
  * @return void
  */
 public function bindCartModel(Model $Model)
 {
     extract($this->settings[$Model->alias]);
     if (!isset($Model->hasAndBelongsToMany['Cart'])) {
         $Model->bindModel(array('hasAndBelongsToMany' => array('Cart' => array('className' => 'Cart.Cart', 'foreignKey' => 'foreign_key', 'associationForeignKey' => 'cart_id', 'joinTable' => 'carts_items', 'with' => 'Cart.CartsItem'))), false);
     }
 }
Example #8
0
 /**
  * Setup
  *
  * @param AppModel $Model
  * @param array $settings
  */
 public function setup(Model $Model, $settings = array())
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->_defaults;
     }
     $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
     $this->settings[$Model->alias]['withModel'] = $this->settings[$Model->alias]['taggedClass'];
     $Model->bindModel(array('hasAndBelongsToMany' => array('Tag' => array('className' => $this->settings[$Model->alias]['tagClass'], 'foreignKey' => $this->settings[$Model->alias]['foreignKey'], 'associationForeignKey' => $this->settings[$Model->alias]['associationForeignKey'], 'unique' => true, 'conditions' => array('Tagged.model' => $Model->name), 'fields' => '', 'dependent' => true, 'with' => $this->settings[$Model->alias]['withModel']))), $this->settings[$Model->alias]['resetBinding']);
 }
Example #9
0
 /**
  *
  * @param Model $Model
  * @param array $settings
  */
 public function setup(Model $Model, $settings = array())
 {
     // settings go here
     $this->settings[$Model->alias] = $settings;
     if (isset($this->settings[$Model->alias]['hasMany'])) {
         // our main $Model hasOne UserGroup
         $Model->bindModel(array('hasOne' => array('UserGroup' => array('className' => 'Users.UserGroup', 'foreignKey' => 'foreign_key', 'conditions' => array('UserGroup.model' => $Model->alias), 'dependent' => true))), false);
     }
 }
 /**
  * Set bindModel BbsArticlesUser
  *
  * @param object $model instance of model
  * @param bool $reset Set to false to make the binding permanent
  * @return void
  */
 public function bindModelBbsArticlesUser(Model $model, $reset)
 {
     if ($model->hasField('bbs_article_key')) {
         $field = 'bbs_article_key';
     } else {
         $field = 'key';
     }
     $model->bindModel(array('belongsTo' => array('BbsArticlesUser' => array('className' => 'Bbses.BbsArticlesUser', 'foreignKey' => false, 'conditions' => array('BbsArticlesUser.bbs_article_key = ' . $model->alias . '.' . $field, 'BbsArticlesUser.user_id' => Current::read('User.id'))))), $reset);
 }
Example #11
0
 /**
  * Bind relationship on the fly
  */
 protected function _setupBelongsTo(Model $model)
 {
     if (!empty($model->belongsTo['TrackableCreator'])) {
         return;
     }
     $config = $this->settings[$model->alias];
     list($plugin, $modelName) = pluginSplit($config['userModel']);
     $className = isset($plugin) ? $plugin . '.' . $modelName : $modelName;
     $model->bindModel(array('belongsTo' => array('TrackableCreator' => array('className' => $className, 'foreignKey' => $config['fields']['created_by']), 'TrackableUpdater' => array('className' => $className, 'foreignKey' => $config['fields']['updated_by']))), false);
 }
 /**
  * Behaviors Setup
  * Called before all actions
  *
  * @param  Model $model
  * @param  $config
  * @author Oleg
  */
 function setup(Model $model, $config = array())
 {
     $this->settings = $config;
     if (!isset($this->settings['required'])) {
         $this->settings['required'] = false;
     }
     $this->settings['baseDir'] = WWW_ROOT . 'img' . DS . $model->name . DS;
     $this->settings['thumbsDir'] = WWW_ROOT . 'img' . DS . $model->name . DS . 'thumbs' . DS;
     $model->bindModel(array('hasMany' => array('Image' => array('className' => 'Image', 'className' => 'Image', 'conditions' => 'model="' . $model->name . '"', 'order' => '', 'limit' => '', 'foreignKey' => 'model_id', 'dependent' => false, 'exclusive' => false, 'finderQuery' => ''))));
 }
Example #13
0
 /**
  * Setup
  *
  * @param AppModel $model
  * @param array $settings
  */
 public function setup(Model $model, $settings = array())
 {
     if (!isset($this->settings[$model->alias])) {
         $this->settings[$model->alias] = $this->defaults;
     }
     $this->settings[$model->alias] = am($this->settings[$model->alias], !empty(is_array($settings)) ? $settings : array());
     $cfg = $this->settings[$model->alias];
     $model->bindModel(array('hasMany' => array($cfg['commentAlias'] => array('className' => $cfg['commentModel'], 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'dependent' => true, 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))), false);
     $model->{$cfg['commentAlias']}->bindModel(array('belongsTo' => array($model->alias => array('className' => $model->name, 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'counterCache' => true, 'dependent' => false))), false);
     $model->{$cfg['commentAlias']}->bindModel(array('belongsTo' => array($cfg['userModelAlias'] => array('className' => $cfg['userModelClass'], 'foreignKey' => 'user_id', 'conditions' => '', 'fields' => '', 'counterCache' => true, 'order' => ''))), false);
 }
Example #14
0
 /**
  * Setup
  *
  * @param Model $model
  * @param array $config
  * @return void
  */
 public function setup(Model $model, $config = array())
 {
     if (is_string($config)) {
         $config = array($config);
     }
     $this->settings[$model->alias] = $config;
     $model->bindModel(array('hasMany' => array('Meta' => array('className' => 'Meta.Meta', 'foreignKey' => 'foreign_key', 'dependent' => true, 'conditions' => array('Meta.model' => $model->alias), 'order' => 'Meta.key ASC'))), false);
     $callback = array($this, 'onBeforeSaveNode');
     $eventManager = $model->getEventManager();
     $eventManager->attach($callback, 'Model.Node.beforeSaveNode');
 }
 /**
  * Setup HABTM relations
  * 
  * @author	Anthony Putignano <*****@*****.**>
  * @since	1.0
  * @param	Model		$Model
  * @return	void
  */
 public function setupHabtmRelations(Model $Model)
 {
     $associated = $Model->getAssociated();
     foreach ($this->__settings[$Model->alias]['fields'] as $field => $settings) {
         extract($settings);
         if (!array_key_exists($joinModel, $associated)) {
             // This relation takes care of deletions for us
             $Model->bindModel(array('hasMany' => array($joinModel => array('className' => $joinModel, 'foreignKey' => $foreignKey, 'dependent' => true, 'request' => true))));
         }
     }
 }
Example #16
0
 /**
  * Initiate behaviour
  *
  * @param object $Model
  * @param array $settings
  */
 public function setup(Model $Model, $settings = array())
 {
     // Setup settings
     $this->settings[$Model->alias] = array_merge($this->_defaults, $settings);
     // Attach captcha behavior
     if (!$Model->Behaviors->loaded('Captcha.Captcha')) {
         $Model->Behaviors->load('Captcha.Captcha');
     }
     // Attach events on model
     $Model->getEventManager()->attach(array($Model, 'beforeRegister'), "Model.{$Model->alias}.beforeRegister", array('passParams' => true));
     $Model->getEventManager()->attach(array($Model, 'afterRegister'), "Model.{$Model->alias}.afterRegister", array('passParams' => true));
     $Model->getEventManager()->attach(array($Model, 'afterTokenize'), "Model.{$Model->alias}.afterTokenize", array('passParams' => true));
     // Bind social models
     if (!empty($this->settings[$Model->alias]['socNetworks'])) {
         foreach ($this->settings[$Model->alias]['socNetworks'] as $network) {
             $Model->bindModel(array('hasOne' => array($network => array('className' => 'Account.Authentication', 'foreignKey' => 'user_id', 'conditions' => array($network . '.provider' => $network), 'fields' => '', 'order' => ''))), false);
         }
         $Model->bindModel(array('hasMany' => array('Authentication' => array('className' => 'Account.Authentication', 'foreignKey' => 'user_id', 'dependent' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))), false);
     }
     $Model->bindModel(array('hasMany' => array('Tokenization' => array('className' => 'Account.Tokenization', 'foreignKey' => 'user_id', 'dependent' => true, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))), false);
 }
Example #17
0
 /**
  * Setup
  *
  * @param Model $Model
  * @param array $settings
  * @return void
  */
 public function setup(Model $Model, $settings = array())
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->_defaults;
     }
     $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
     if (empty($this->settings[$Model->alias]['modelClass'])) {
         $this->settings[$Model->alias]['modelClass'] = $Model->name;
     }
     $Model->bindModel(array('hasMany' => array('Rating' => array('className' => $this->settings[$Model->alias]['rateClass'], 'foreignKey' => $this->settings[$Model->alias]['foreignKey'], 'unique' => true, 'conditions' => '', 'fields' => '', 'dependent' => true))), false);
     $Model->Rating->bindModel(array('belongsTo' => array($Model->alias => array('className' => $this->settings[$Model->alias]['modelClass'], 'foreignKey' => 'foreign_key', 'counterCache' => $this->settings[$Model->alias]['countRates']))), false);
 }
 public function setup(Model $Model, $settings = array())
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->defaultSettings;
     }
     $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], (array) $settings);
     $Model->findMethods['liked'] = true;
     $Model->findMethods['mostLiked'] = true;
     // bind the Like model to the current model
     $Model->bindModel(array('hasMany' => array('Like' => array('className' => 'Like.Like', 'foreignKey' => 'foreign_id', 'conditions' => array('Like.model' => $Model->alias)))), false);
     // bind the current model with Like model
     $Model->Like->bindModel(array('belongsTo' => array($Model->alias => array('className' => $Model->alias, 'foreignKey' => 'foreign_id', 'counterCache' => $this->settings[$Model->alias]['counterCache'], 'conditions' => array('Like.model' => $Model->alias), 'counterScope' => array('Like.model' => $Model->alias)))), false);
 }
 /**
  * afterFind callback
  *
  * @param Model $model
  * @param array $results
  * @param boolean $primary
  * @return array
  */
 public function afterFind(Model $model, $results = array(), $primary = false)
 {
     if ($primary && isset($results[0][$model->alias])) {
         foreach ($results as $i => $result) {
             $model->bindModel(array('hasMany' => array('Multiattach' => array('className' => 'Multiattach.Multiattach'))));
             $model->Multiattach->recursive = -1;
             if (isset($results[$i][$model->alias]['id'])) {
                 $results[$i]['Multiattach'] = $model->Multiattach->find('all', array('order' => array('Multiattach.order ASC, Multiattach.id DESC'), 'conditions' => array('Multiattach.node_id' => $results[$i][$model->alias]['id'])));
             }
         }
     }
     return $results;
 }
Example #20
0
 /**
  * Initiate WhoDidIt Behavior.
  *
  * Checks if the configured fields are available in the model.
  * Also binds the User model as association for each available field.
  *
  * @param Model $Model The model.
  * @param array $config Behavior settings you would like to override.
  * @return void
  */
 public function setup(Model $Model, $config = array())
 {
     $config += $this->_defaultConfig;
     $config['has_created_by'] = $Model->hasField($config['created_by_field']);
     $config['has_modified_by'] = $Model->hasField($config['modified_by_field']);
     $config['has_confirmed_by'] = $Model->hasField($config['confirmed_by_field']);
     // Handles model binding to the User model according to the auto_bind settings (default true).
     if ($config['auto_bind']) {
         if ($config['has_created_by']) {
             $commonBelongsTo = array('CreatedBy' => array('className' => $config['user_model'], 'foreignKey' => $config['created_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
         if ($config['has_modified_by']) {
             $commonBelongsTo = array('ModifiedBy' => array('className' => $config['user_model'], 'foreignKey' => $config['modified_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
         if ($config['has_confirmed_by']) {
             $commonBelongsTo = array('ConfirmedBy' => array('className' => $config['user_model'], 'foreignKey' => $config['confirmed_by_field']));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
     }
     $this->settings[$Model->alias] = $config;
 }
Example #21
0
 public function likesForUser(Model $Model, $fields, $status_field)
 {
     // Binding only when needed :)
     $Model->bindModel(array('hasMany' => array('Like' => array('className' => 'LikeableContent.Like', 'foreignKey' => 'foreign_key', 'conditions' => array('model' => $Model->alias), 'fields' => array('id', 'user_id')))), false);
     $this->recursive = 1;
     // We need to only show the Likes that are currently published
     $paginate = array();
     $paginate['joins'] = array(array('table' => 'lcm_likes AS Likes', 'conditions' => array($Model->alias . '.id = Likes.foreign_key', 'Likes.model' => $Model->alias)));
     $paginate['conditions'] = array('BackOffice' => true, $status_field => 1, 'Likes.user_id' => CakeSession::read("Auth.User.id"));
     $paginate['fields'] = array_merge(array('Likes.id', 'Likes.user_id', 'Likes.created'), $fields);
     $paginate['order'] = 'Likes.created DESC';
     //$paginate['group'] = $Model->alias . '.id';
     return $paginate;
 }
 public function setup(Model $Model, $settings = array())
 {
     if (!isset($this->__settings[$Model->alias])) {
         $this->__settings[$Model->alias] = $this->default;
     }
     $this->__settings[$Model->alias] = array_merge($this->__settings[$Model->alias], is_array($settings) ? $settings : array());
     // bind the Like model to the current model
     $Model->bindModel(array('hasMany' => array('Notification' => array('className' => 'Notification.Notification', 'foreignKey' => 'user_id'))), false);
     $belongsTo = array();
     foreach ($this->__settings[$Model->alias]['subjects'] as $subject) {
         $belongsTo[$subject] = array('className' => $subject, 'foreignKey' => 'model_id', 'conditions' => array('Subject.model' => $subject));
     }
     $Model->Notification->Subject->bindModel(compact('belongsTo'), false);
 }
Example #23
0
 /**
  * Setup
  *
  * @param AppModel $Model
  * @param array $settings
  * @return void
  */
 public function setup(Model $Model, array $settings = array())
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->_defaults;
     }
     $this->settings[$Model->alias] = Set::merge($this->settings[$Model->alias], $settings);
     if (empty($this->settings[$Model->alias]['modelClass'])) {
         $this->settings[$Model->alias]['modelClass'] = $Model->name;
     }
     $Model->bindModel(array('hasMany' => array('Problem' => array('className' => $this->settings[$Model->alias]['problemClass'], 'foreignKey' => $this->settings[$Model->alias]['foreignKey'], 'conditions' => array('Problem.model' => $Model->alias), 'unique' => true, 'fields' => '', 'dependent' => true))), false);
     $Model->Problem->bindModel(array('belongsTo' => array($Model->alias => array('className' => $this->settings[$Model->alias]['modelClass'], 'foreignKey' => 'foreign_key'), 'User' => array('className' => $this->settings[$Model->alias]['userClass'], 'foreignKey' => 'user_id'))), false);
     if (!empty($this->settings[$Model->alias]['problemTypes'])) {
         $Model->Problem->types = $this->settings[$Model->alias]['problemTypes'];
     }
     $Model->Problem->modelTypes[] = $Model->alias;
 }
Example #24
0
 /**
  * afterFind method
  *
  * @param mixed $Model
  * @param mixed $results
  * @param bool $primary
  * @access public
  * @return void
  */
 public function afterFind(Model $Model, $results, $primary = false)
 {
     extract($this->settings[$Model->alias]);
     if (App::import('Vendor', 'MiCache')) {
         $models = MiCache::mi('models');
     } else {
         $models = App::objects('Model');
     }
     if ($primary && isset($results[0][$Model->alias][$modelField]) && isset($results[0][$Model->alias][$foreignKey]) && $Model->recursive > 0) {
         foreach ($results as $key => $result) {
             $associated = array();
             $model = Inflector::classify($result[$Model->alias][$modelField]);
             $foreignId = $result[$Model->alias][$foreignKey];
             if ($model && $foreignId && in_array($model, $models)) {
                 $result = $result[$Model->alias];
                 if (!isset($Model->{$model})) {
                     $Model->bindModel(array('belongsTo' => array($model => array('conditions' => array($Model->alias . '.' . $modelField => $model), 'foreignKey' => $foreignKey))));
                 }
                 $conditions = array($model . '.' . $Model->{$model}->primaryKey => $result[$foreignKey]);
                 $recursive = -1;
                 $associated = $Model->{$model}->find('first', compact('conditions', 'recursive'));
                 $name = $Model->{$model}->display($result[$foreignKey]);
                 $associated[$model]['display_field'] = $name ? $name : '*missing*';
                 $results[$key][$model] = $associated[$model];
             }
         }
     } elseif (isset($results[$Model->alias][$modelField])) {
         $associated = array();
         $model = Inflector::classify($result[$Model->alias][$modelField]);
         $foreignId = $results[$Model->alias][$foreignKey];
         if ($model && $foreignId) {
             $result = $results[$Model->alias];
             if (!isset($Model->{$model})) {
                 $Model->bindModel(array('belongsTo' => array($model => array('conditions' => array($Model->alias . '.' . $modelField => $model), 'foreignKey' => $foreignKey))));
             }
             $conditions = array($model . '.' . $Model->{$model}->primaryKey => $result[$foreignKey]);
             $recursive = -1;
             $associated = $Model->{$model}->find('first', compact('conditions', 'recursive'));
             $name = $Model->{$model}->display($result[$foreignKey]);
             $associated[$model]['display_field'] = $name ? $name : '*missing*';
             $results[$model] = $associated[$model];
         }
     }
     return $results;
 }
Example #25
0
 /**
  * {@inheritdoc}
  */
 public function setup(Model $Model, $settings = array())
 {
     $defaults = array('class' => 'Common.CommonDetailModel', 'alias' => $Model->alias . 'Detail');
     $this->settings[$Model->alias] = array_merge($defaults, $settings);
     extract($this->settings[$Model->alias]);
     $DetailModel = ClassRegistry::init(compact('class', 'alias'));
     $belongsTo = array('Parent' => array('className' => $Model->className(), 'foreignKey' => 'foreign_key', 'conditions' => array($DetailModel->alias . '.foreign_model' => $Model->className())));
     $DetailModel->bindModel(compact('belongsTo'), false);
     foreach (array('schema', 'validate') as $key) {
         $varDetail = 'detail' . ucfirst($key);
         if (!empty($Model->{$varDetail})) {
             $varSection = 'section' . ucfirst($key);
             $DetailModel->{$varSection} = $Model->{$varDetail};
         }
     }
     if (!empty($Model->detailDefaults)) {
         $DetailModel->defaults = $Model->detailDefaults;
     }
     $hasMany = array($DetailModel->alias => array('className' => 'Common.' . $alias, 'foreignKey' => 'foreign_key', 'conditions' => array($DetailModel->alias . '.foreign_model' => $Model->className()), 'order' => array($DetailModel->alias . '.position' => 'ASC')));
     $Model->bindModel(compact('hasMany'), false);
 }
Example #26
0
 /**
  * Setup the behavior
  *
  * @param Model $Model
  * @param array $settings
  * @return void
  */
 public function setup(Model $Model, $settings = array())
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->_defaults;
     }
     $this->settings[$Model->alias] = am($this->settings[$Model->alias], is_array($settings) ? $settings : array());
     $favoriteClass = $this->settings[$Model->alias]['favoriteClass'];
     $favoriteAlias = $this->settings[$Model->alias]['favoriteAlias'];
     $Model->bindModel(array('hasMany' => array('Favorite' => array('className' => $favoriteClass, 'foreignKey' => $this->settings[$Model->alias]['foreignKey'], 'conditions' => array($favoriteAlias . '.model' => $Model->name), 'fields' => '', 'dependent' => true))), false);
     $Model->{$favoriteAlias}->bindModel(array('belongsTo' => array($Model->alias => array('className' => $Model->name, 'foreignKey' => $this->settings[$Model->alias]['foreignKey'], 'fields' => '', 'counterCache' => $this->settings[$Model->alias]['counterCache']))), false);
     $types = Configure::read('Favorites.types');
     $this->favoriteTypes = array();
     if (!empty($types)) {
         foreach ((array) $types as $key => $type) {
             $this->favoriteTypes[$key] = is_array($type) ? $type : array('model' => $type);
             if (empty($this->favoriteTypes[$key]['limit'])) {
                 $this->favoriteTypes[$key]['limit'] = null;
             }
         }
     }
 }
 public function getCustomFieldValues(Model $model, $options = array())
 {
     $module = $this->settings[$model->alias]['module'];
     $valueModel = $module . 'CustomValue';
     $conditions = array();
     $key = $valueModel . '.' . Inflector::underscore($module . 'Id');
     if (array_key_exists('id', $options)) {
         $conditions[$key] = $options['id'];
     }
     $model->unbindModel(array('belongsTo' => array('ModifiedUser', 'CreatedUser')));
     $model->bindModel(array('hasMany' => array($valueModel => array('conditions' => $conditions))));
     $valuesData = $model->find('all', array('order' => $model->alias . '.order'));
     $processedData = array();
     foreach ($valuesData as $key => $value) {
         if (!empty($value[$valueModel])) {
             array_push($processedData, $value[$valueModel][0]);
         } else {
             array_push($processedData, array());
         }
     }
     return $processedData;
 }
 /**
  * Initiate behaviour for the model using settings.
  *
  * @param Model $Model instance of model
  * @param array $config array of configuration settings.
  * @return void
  */
 public function setup(Model $Model, $config = array())
 {
     $settings = array_merge($this->_defaults, $config);
     $on = array('create' => array(), 'read' => array(), 'update' => array(), 'delete' => array());
     $validFields = array();
     foreach ($settings[$Model->alias]['fields'] as $field => $callbacks) {
         foreach ($callbacks as $callback) {
             if (in_array($field, $on[$callback]) && $Model->hasField($field)) {
                 $validFields[] = $field;
                 $settings[$Model->alias]['event'][$callback][] = $field;
             }
         }
     }
     $settings['required'] = (array) $settings['required'];
     if ($settings[$Model->alias]['auto_bind']) {
         foreach ($validFields as $field) {
             $alias = Inflector::classify($field);
             $commonBelongsTo = array($alias => array('className' => $settings[$Model->alias]['user_model'], 'foreignKey' => $field));
             $Model->bindModel(array('belongsTo' => $commonBelongsTo), false);
         }
     }
     $this->settings[$Model->alias] = $settings;
 }
 /**
  * afterFind method
  *
  * @param mixed $model
  * @param mixed $results
  * @param bool $primary
  * @access public
  * @return void
  */
 function afterFind(Model $model, $results, $primary = false)
 {
     extract($this->settings[$model->alias]);
     if ($primary && isset($results[0][$model->alias][$classField]) && $model->recursive > 0) {
         foreach ($results as $key => $result) {
             $associated = array();
             $class = Inflector::classify($result[$model->alias][$classField]);
             $foreignId = $result[$model->alias][$foreignKey];
             if ($class && $foreignId) {
                 $result = $result[$model->alias];
                 if (!isset($model->{$class})) {
                     $model->bindModel(array('belongsTo' => array($class => array('conditions' => array($model->alias . '.' . $classField => $class), 'foreignKey' => $foreignKey))));
                 }
                 $conditions = array($class . '.id' => $foreignId);
                 $recursive = -1;
                 $associated = $model->{$class}->find('first', compact('conditions', 'recursive'));
                 $name = $model->{$class}->find('list', compact('conditions'));
                 $associated[$class]['display_field'] = $name[$foreignId];
                 $results[$key][$class] = $associated[$class];
             }
         }
     } elseif (isset($results[$model->alias][$classField])) {
         $associated = array();
         $class = $results[$model->alias][$classField];
         $foreignId = $results[$model->alias][$foreignKey];
         if ($class && $foreignId) {
             $result = $results[$model->alias];
             if (!isset($model->{$class})) {
                 $model->bindModel(array('belongsTo' => array($class => array('conditions' => array($model->alias . '.' . $classField => $class), 'foreignKey' => $foreignKey))));
             }
             $associated = $model->{$class}->find(array($class . '.id' => $foreignId), array('recursive' => -1));
             $associated[$class]['display_field'] = $associated[$class][$model->{$class}->displayField];
             $results[$class] = $associated[$class];
         }
     }
     return $results;
 }
 public function setup(Model $model, $config = null)
 {
     $model->bindModel(array('hasOne' => array('Attachment')));
 }