Example #1
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');
 }
 public function dislike(Model $Model, $article_id, $user_id)
 {
     if (!$Model->exists($article_id)) {
         throw new NotFoundException();
     }
     if (!$Model->isLikedBy($article_id, $user_id)) {
         throw new NotLikedException();
     }
     $like = $Model->Like->find('first', array('conditions' => array('Like.article_id' => $article_id, 'Like.user_id' => $user_id)));
     $Model->Like->delete($like['Like']['id']);
     $Model->getEventManager()->dispatch(new CakeEvent('Plugin.Like.dislike', $like));
 }
Example #3
0
 /**
  * 
  * @param Model $Model
  * @param unknown $data
  * 
  * @return boolean|Ambigous <multitype:, NULL>
  */
 protected function _tokenizeResetEmail(Model $Model, $data = array())
 {
     $Model->validator()->remove('password');
     $Model->validator()->remove('captcha');
     $Model->validator()->add('password', array('notempty' => array('rule' => array('notempty'), 'message' => 'Password is required'), 'exists' => array('rule' => array('fieldExists', array($Model->alias . '.id' => AuthComponent::user('id'))), 'message' => 'Password doesnt match')));
     $Model->validator()->add('captcha', array('captcha' => array('rule' => array('captcha'), 'message' => 'Captcha is wrong')));
     $Model->set($data);
     if ($Model->validator()->errors(array('fieldList' => array('password', 'captcha')))) {
         return false;
     }
     $user = $Model->find('first', array('conditions' => array($Model->alias . '.' . $Model->primaryKey => AuthComponent::user('id'))));
     $id = $user[$Model->alias][$Model->primaryKey];
     $Model->Tokenization->deleteAll(array($Model->Tokenization->alias . '.user_id' => $id, $Model->Tokenization->alias . '.field' => 'email', $Model->Tokenization->alias . '.action' => 'reset'));
     $data = array($Model->Tokenization->alias => array('user_id' => $id, 'token' => Security::hash(String::uuid()), 'expire' => date('Y-m-d H:i:s', strtotime($this->settings[$Model->alias]['expire'])), 'action' => 'reset', 'field' => 'email'));
     $Model->Tokenization->create();
     if ($Model->Tokenization->save($data)) {
         $Model->getEventManager()->dispatch(new CakeEvent("Model.{$Model->alias}.afterTokenize", $Model, array('id' => $id, 'field' => 'email', 'action' => 'reset')));
         return $id;
     }
     return false;
 }
 /**
  * Index a document or a list of documents, bypassing Model::save() (much faster)
  *
  * Good for indexing pre-validated data such as data from your DB
  *
  * @param Model $Model
  * @param array $documents
  * @return void
  * @author David Kullmann
  */
 public function index(Model $Model, $documents = array(), $options = array())
 {
     $defaults = array('callbacks' => false);
     $options = array_merge($defaults, $options);
     $geoFields = !empty($this->settings[$Model->alias]['geoFields']) ? $this->settings[$Model->alias]['geoFields'] : false;
     if ($geoFields) {
         extract($geoFields);
     }
     $ds = $Model->getDataSource();
     $ds->begin();
     foreach ($documents as $document) {
         if ($geoFields) {
             $document[$alias][$location] = array('lat' => $document[$Model->alias][$latitude], 'lon' => $document[$Model->alias][$longitude]);
         }
         if ($options['callbacks'] === true || $options['callbacks'] === 'before') {
             $Model->set($document);
             $event = new CakeEvent('Model.beforeSave', $Model, array($options));
             list($event->break, $event->breakOn) = array(true, array(false, null));
             $Model->getEventManager()->dispatch($event);
             if (!$event->result) {
                 return false;
             }
         }
         $ds->addToDocument($Model, $document);
     }
     $ds->commit();
     return $documents;
 }