Exemple #1
0
 protected function loadModel($value)
 {
     $model = YModel::model($this->modelClass)->findByAttributes([$this->attribute => $value]);
     if (!$model) {
         throw new CHttpException(404, $this->getErrorMessage());
     }
     return $model;
 }
Exemple #2
0
 /**
  * @param $params
  * @param $module
  * @param $user
  * @param null $request
  * @return bool|Comment
  * @throws CException
  */
 public function create($params, $module, $user, $request = null)
 {
     if ($user->isAuthenticated()) {
         $params = CMap::mergeArray($params, ['user_id' => $user->getId(), 'name' => $user->getState('nick_name'), 'email' => $user->getProfileField('email')]);
     }
     $comment = new Comment();
     $comment->setAttributes($params);
     $comment->status = (int) $module->defaultCommentStatus;
     if ($module->autoApprove && $user->isAuthenticated()) {
         $comment->status = Comment::STATUS_APPROVED;
     }
     /**
      * Реализована проверка прав на возможность добавления комментария в конкретную модель
      * Для того чтобы осушествить проверку у модели должен быть public метод: commitValidation()
      * Если метод вернет значение: false, то предполагается что для данной сушности добавление комментария запрещено
      **/
     $model = YModel::model($comment->model)->findByPk($comment->model_id);
     if ($model instanceof ICommentAllowed && $model->isCommentAllowed() === false) {
         throw new CException(Yii::t('CommentModule.comment', 'Not have permission to add a comment!'));
     }
     $transaction = Yii::app()->getDb()->beginTransaction();
     try {
         Yii::app()->eventManager->fire(CommentEvents::BEFORE_ADD_COMMENT, new CommentEvent($comment, $user, $module, $request));
         $root = null;
         $parentId = (int) $comment->parent_id;
         // Если указан parent_id просто добавляем новый комментарий.
         if ($parentId) {
             $root = Comment::model()->approved()->findByPk($parentId);
             if (null === $root) {
                 throw new CException(Yii::t('CommentModule.comment', 'Root comment not found!'));
             }
         } else {
             // Иначе если parent_id не указан...
             $root = $comment->createRootOfCommentsIfNotExists($comment->model, $comment->model_id);
             if (null === $root) {
                 throw new CException(Yii::t('CommentModule.comment', 'Root comment not created!'));
             }
         }
         if ($comment->appendTo($root)) {
             Yii::app()->eventManager->fire(CommentEvents::SUCCESS_ADD_COMMENT, new CommentEvent($comment, $user, $module));
             $transaction->commit();
             return $comment;
         }
         throw new CException(Yii::t('CommentModule.comment', 'Error append comment to root!'));
     } catch (Exception $e) {
         $transaction->rollback();
         Yii::app()->eventManager->fire(CommentEvents::ERROR_ADD_COMMENT, new CommentEvent($comment, $user, $module));
         Yii::log($e->__toString(), CLogger::LEVEL_ERROR, 'comment');
         return false;
     }
 }
Exemple #3
0
 /**
  * Returns the static model of the specified AR class.
  * @return Settings the static model class
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }