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; + +?> + +
+ + +
+ field($answer, 'text') + ->textarea() ?> + + field($answer, 'artbox_comment_pid') + ->hiddenInput( + [ + 'value' => $model->artbox_comment_id, + ] + ) + ->label(false) ?> + + field($answer, 'user_id') + ->hiddenInput( + [ + 'value' => 1, + ] + ) + ->label(false) ?> + + field($answer, 'entity') + ->hiddenInput( + [ + 'value' => $model->entity, + ] + ) + ->label(false) ?> + + field($answer, 'entity_id') + ->hiddenInput( + [ + 'value' => $model->entity_id, + ] + ) + ->label(false) ?> + + 'btn btn-primary' ] + ) ?> +
+ + + +
diff --git a/views/manage/index.php b/views/manage/index.php index 88791a6..9b303aa 100755 --- a/views/manage/index.php +++ b/views/manage/index.php @@ -3,82 +3,104 @@ use artweb\artbox\comment\models\CommentModelSearch; use yii\data\ActiveDataProvider; use yii\grid\GridView; - use yii\grid\SerialColumn; use yii\helpers\Html; use yii\helpers\StringHelper; + use yii\web\View; use yii\widgets\Pjax; /** * @var ActiveDataProvider $dataProvider * @var CommentModelSearch $searchModel * @var string $commentModel + * @var View $this */ + $this->title = 'Комментарии'; + + $this->params[ 'breadcrumbs' ][] = $this->title; + $statuses = [ $searchModel::STATUS_ACTIVE => 'Активный', $searchModel::STATUS_HIDDEN => 'Скрытый', $searchModel::STATUS_DELETED => 'Удаленный', ]; Pjax::begin(); - if(( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) { + if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) { echo Html::tag('p', $success); } - echo GridView::widget([ - 'dataProvider' => $dataProvider, - 'filterModel' => $searchModel, - 'columns' => [ - [ - 'class' => SerialColumn::className(), - ], - [ - 'attribute' => 'created_at', - 'format' => [ - 'date', - 'php:d.m.Y', + echo GridView::widget( + [ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + [ + 'attribute' => 'artbox_comment_id', + 'label' => 'Id', + ], + [ + 'label' => 'url', + 'content' => function (CommentModel $model) { + return $model->getLink(); + }, + ], + [ + 'label' => 'Ссылка', + ], + [ + 'attribute' => 'created_at', + 'format' => [ + 'date', + 'php:d.m.Y', + ], + 'filter' => false, + ], + [ + 'label' => 'Комментарий', + 'content' => function(CommentModel $model) { + return StringHelper::truncate($model->text, 40, '...'); + }, + ], + [ + 'attribute' => 'user_id', + 'value' => function($model) { + if (!empty($model->user_id)) { + return $model->user->username . ' (id:' . $model->user->id . ')'; + } else { + return $model->username . ' ' . $model->email . ' (Гость)'; + } + }, + ], + [ + 'attribute' => 'status', + 'filter' => $statuses, + 'value' => function($model) use ($statuses) { + return $statuses[ $model->status ]; + }, + ], + [ + 'attribute' => 'ratingValue', + 'value' => function($model) { + if (!empty($model->rating)) { + return $model->rating->value; + } + return NULL; + }, + ], + [ + 'attribute' => 'childrenCount', + 'value' => function($model) { + return count($model->children); + }, + ], + [ + 'class' => 'yii\grid\ActionColumn', + 'buttons' => [ + 'answer' => function(string $url) { + return Html::a(Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-bullhorn' ]), $url); + }, + ], + 'template' => '{update} {answer} {delete}', ], - 'filter' => false, - ], - [ - 'label' => 'Комментарий', - 'content' => function(CommentModel $model) { - return StringHelper::truncate($model->text, 40, '...'); - } - ], - [ - 'attribute' => 'user_id', - 'value' => function($model) { - if(!empty( $model->user_id )) { - return $model->user->username . ' (id:' . $model->user->id . ')'; - } else { - return $model->username . ' ' . $model->email . ' (Гость)'; - } - }, - ], - [ - 'attribute' => 'status', - 'filter' => $statuses, - 'value' => function($model) use ($statuses) { - return $statuses[ $model->status ]; - }, - ], - [ - 'attribute' => 'ratingValue', - 'value' => function($model) { - if(!empty( $model->rating )) { - return $model->rating->value; - } - return NULL; - }, - ], - [ - 'attribute' => 'childrenCount', - 'value' => function($model) { - return count($model->children); - }, - ], - [ - 'class' => 'yii\grid\ActionColumn', - 'template' => '{update} {delete}', ], - ], - ]); + ] + ); Pjax::end(); \ No newline at end of file -- libgit2 0.21.4