/**
  * Create comment.
  */
 public function actionCreate()
 {
     $model = new Comment(['scenario' => 'create']);
     Yii::$app->response->format = Response::FORMAT_JSON;
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             $modelClass = Model::findOne(['id' => Yii::$app->request->post('Comment')['model_class']]);
             //Проверяем может ли пользователь оставлять комментарий
             if (method_exists($modelClass->name, 'checkCanComment')) {
                 $canComment = call_user_func_array([$modelClass->name, 'checkCanComment'], [Yii::$app->request->post('Comment')['model_id'], Yii::$app->getUser()->id, $model->parent_id]);
             } else {
                 $canComment = true;
             }
             if ($canComment && $model->save(false)) {
                 if (Yii::$app->getModule('comments')->allowRate && Yii::$app->request->post('rating') !== null) {
                     if (!Yii::$app->request->post('Comment')['parent_id']) {
                         $modelDestination = call_user_func([$modelClass->name, 'findOne'], [Yii::$app->request->post('Comment')['model_id']]);
                         call_user_func_array([Yii::$app->getModule('comments')->ratePath . 'Rate', 'setRating'], [$modelDestination, Yii::$app->request->post('rating'), $model]);
                         /*Rate::setRating($modelDestination,
                           Yii::$app->request->post('rating'), $model);*/
                     }
                 }
                 return $this->tree($model);
             } else {
                 Yii::$app->response->setStatusCode(500);
                 return Module::t('comments', 'FRONTEND_FLASH_FAIL_CREATE');
             }
         } elseif (Yii::$app->request->isAjax) {
             Yii::$app->response->setStatusCode(400);
             return ActiveForm::validate($model);
         }
     }
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function scenarios()
 {
     $scenarios = parent::scenarios();
     $scenarios['admin-create'] = ['name'];
     $scenarios['admin-update'] = ['name'];
     return $scenarios;
 }
Пример #3
0
 /**
  * Вызываем пользовательский метод - определяется интерфейсом commentableinterface.php
  * @param bool $insert
  * @param array $changedAttributes
  */
 public function afterSave($insert, $changedAttributes)
 {
     $comments_models = Model::find()->where(['id' => $this->model_class])->asArray()->one();
     if (!empty($comments_models['name']) && method_exists($comments_models['name'], 'afterSaveComment')) {
         call_user_func_array([$comments_models['name'], 'afterSaveComment'], [$insert, $changedAttributes, $this->getAttributes(), $comments_models]);
     }
     parent::afterSave($insert, $changedAttributes);
 }