diff --git a/controllers/ManageController.php b/controllers/ManageController.php index a49dad1..5001066 100755 --- a/controllers/ManageController.php +++ b/controllers/ManageController.php @@ -15,6 +15,7 @@ /** * Returns a list of behaviors that this component should behave as. + * * @return array */ public function behaviors() @@ -36,6 +37,7 @@ /** * Lists all comments. + * * @return mixed */ public function actionIndex() @@ -44,11 +46,14 @@ $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $commentModel = Yii::$app->getModule(Module::$name)->commentModelClass; - return $this->render('index', [ - 'dataProvider' => $dataProvider, - 'searchModel' => $searchModel, - 'commentModel' => $commentModel, - ]); + return $this->render( + 'index', + [ + 'dataProvider' => $dataProvider, + 'searchModel' => $searchModel, + 'commentModel' => $commentModel, + ] + ); } /** @@ -63,16 +68,44 @@ { $model = $this->findModel($id); - if($model->load(Yii::$app->request->post()) && $model->save()) { - Yii::$app->session->setFlash('artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/ - 'Comment has been saved.'); + if ($model->load(Yii::$app->request->post()) && $model->save()) { + Yii::$app->session->setFlash( + 'artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/ + 'Comment has been saved.' + ); return $this->redirect([ 'index' ]); } - return $this->render('update', [ - 'model' => $model, - ]); + return $this->render( + 'update', + [ + 'model' => $model, + ] + ); + + } + + public function actionAnswer($id) + { + $model = $this->findModel($id); + + $answer = new CommentModel(); + + if ($answer->load(Yii::$app->request->post()) && $answer->save()) { + Yii::$app->session->setFlash( + 'artbox_comment_success', + 'Comment has been saved.' + ); + return $this->redirect([ 'index' ]); + } + return $this->render( + 'answer', + [ + 'answer' => $answer, + 'model' => $model, + ] + ); } /** @@ -87,7 +120,10 @@ { $this->findModel($id) ->delete(); - Yii::$app->session->setFlash('artbox_comment_success', Yii::t('artbox-comment', 'Comment has been deleted.')); + Yii::$app->session->setFlash( + 'artbox_comment_success', + Yii::t('artbox-comment', 'Comment has been deleted.') + ); return $this->redirect([ 'index' ]); } @@ -102,11 +138,12 @@ */ protected function findModel($id) { - if(( $model = CommentModel::findOne($id) ) !== NULL) { + if (( $model = CommentModel::findOne($id) ) !== NULL) { return $model; } else { throw new NotFoundHttpException(/*Yii::t('yii2mod.comments', 'The requested page does not exist.')*/ - 'The requested page does not exist.'); + 'The requested page does not exist.' + ); } } } \ No newline at end of file diff --git a/models/CommentModel.php b/models/CommentModel.php index 9a0b6ae..0fc5182 100755 --- a/models/CommentModel.php +++ b/models/CommentModel.php @@ -3,32 +3,36 @@ use artweb\artbox\comment\behaviors\ParentBehavior; use artweb\artbox\comment\models\interfaces\CommentInterface; - use yii\base\InvalidConfigException; + use artweb\artbox\ecommerce\models\Product; use yii\behaviors\AttributeBehavior; use yii\behaviors\BlameableBehavior; use yii\behaviors\TimestampBehavior; use yii\data\ActiveDataProvider; use yii\db\ActiveRecord; + use yii\helpers\Html; + use yii\helpers\VarDumper; /** * Class CommentModel * - * @property int $artbox_comment_id - * @property string $text - * @property int $user_id - * @property string $username - * @property string $email - * @property int $created_at - * @property int $updated_at - * @property int $deleted_at - * @property int $status - * @property int $artbox_comment_pid - * @property int $related_id - * @property string $ip - * @property string $info - * @property string $entity - * @property int $entity_id + * @property int $artbox_comment_id + * @property string $text + * @property int $user_id + * @property string $username + * @property string $email + * @property int $created_at + * @property int $updated_at + * @property int $deleted_at + * @property int $status + * @property int $artbox_comment_pid + * @property int $related_id + * @property string $ip + * @property string $info + * @property string $entity + * @property int $entity_id * @package artweb\artbox\comment\models + * @property ActiveRecord $entityModel + * @property string $link */ class CommentModel extends ActiveRecord implements CommentInterface { @@ -44,8 +48,6 @@ public $entityId; - private $entityModel; - public function scenarios() { $scenarios = parent::scenarios(); @@ -66,7 +68,7 @@ ]; return $scenarios; } - + public static function tableName() { return '{{%artbox_comment}}'; @@ -142,7 +144,7 @@ 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'ip', ], - 'value' => function ($event) { + 'value' => function($event) { return \Yii::$app->request->userIP; }, ], @@ -200,7 +202,7 @@ 'entity' => $entity, 'entity_id' => $entityId, 'status' => 1, - 'artbox_comment_pid' => null, + 'artbox_comment_pid' => NULL, ] ), 'pagination' => [ @@ -217,7 +219,7 @@ public function deleteComment(): bool { - if (\Yii::$app->user->id != null && \Yii::$app->user->id == $this->user_id) { + if (\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) { if ($this->delete()) { return true; } @@ -260,10 +262,48 @@ ->andWhere( [ 'or', - [ 'artbox_comment_rating.model' => null ], + [ 'artbox_comment_rating.model' => NULL ], [ 'artbox_comment_rating.model' => self::className() ], ] ); } + + public function getEntityModel() + { + $model = call_user_func_array( + [ + $this->entity, + 'findOne', + ], + [ $this->entity_id ] + ); + return $model; + } + + public function getLink() + { + $model = $this->getEntityModel(); + if (empty($model)) { + return Html::a('Страница не найдена', '#'); + } + if (Product::className() == $this->entity) { + return Html::a( + 'Перейти на товар', + \Yii::$app->urlManagerFrontend->createAbsoluteUrl( + [ + 'catalog/product', + 'product' => $model->lang->alias, + 'variant' => $model->variant->sku, + ] + ), + [ + 'target' => '_blank', + 'data-pjax' => '0', + ] + ); + } else { + return Html::a('Неизвестная модель', '#'); + } + } } \ No newline at end of file diff --git a/views/manage/answer.php b/views/manage/answer.php new file mode 100644 index 0000000..9e3a828 --- /dev/null +++ b/views/manage/answer.php @@ -0,0 +1,70 @@ +title = 'Ответить на комментарий# ' . $model->artbox_comment_id; + + $this->params[ 'breadcrumbs' ][] = [ + 'label' => 'Комментарии', + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = $this->title; + +?> + +