Commit ce440bf9f77f0955c916892387a95c31275d4a7e

Authored by Alexey Boroda
1 parent 1317fa68

-Comments upgrade ready

controllers/ManageController.php
... ... @@ -15,6 +15,7 @@
15 15  
16 16 /**
17 17 * Returns a list of behaviors that this component should behave as.
  18 + *
18 19 * @return array
19 20 */
20 21 public function behaviors()
... ... @@ -36,6 +37,7 @@
36 37  
37 38 /**
38 39 * Lists all comments.
  40 + *
39 41 * @return mixed
40 42 */
41 43 public function actionIndex()
... ... @@ -44,11 +46,14 @@
44 46 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
45 47 $commentModel = Yii::$app->getModule(Module::$name)->commentModelClass;
46 48  
47   - return $this->render('index', [
48   - 'dataProvider' => $dataProvider,
49   - 'searchModel' => $searchModel,
50   - 'commentModel' => $commentModel,
51   - ]);
  49 + return $this->render(
  50 + 'index',
  51 + [
  52 + 'dataProvider' => $dataProvider,
  53 + 'searchModel' => $searchModel,
  54 + 'commentModel' => $commentModel,
  55 + ]
  56 + );
52 57 }
53 58  
54 59 /**
... ... @@ -63,16 +68,44 @@
63 68 {
64 69 $model = $this->findModel($id);
65 70  
66   - if($model->load(Yii::$app->request->post()) && $model->save()) {
67   - Yii::$app->session->setFlash('artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/
68   - 'Comment has been saved.');
  71 + if ($model->load(Yii::$app->request->post()) && $model->save()) {
  72 + Yii::$app->session->setFlash(
  73 + 'artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/
  74 + 'Comment has been saved.'
  75 + );
69 76 return $this->redirect([ 'index' ]);
70 77 }
71 78  
72   - return $this->render('update', [
73   - 'model' => $model,
74   - ]);
  79 + return $this->render(
  80 + 'update',
  81 + [
  82 + 'model' => $model,
  83 + ]
  84 + );
  85 +
  86 + }
  87 +
  88 + public function actionAnswer($id)
  89 + {
  90 + $model = $this->findModel($id);
  91 +
  92 + $answer = new CommentModel();
  93 +
  94 + if ($answer->load(Yii::$app->request->post()) && $answer->save()) {
  95 + Yii::$app->session->setFlash(
  96 + 'artbox_comment_success',
  97 + 'Comment has been saved.'
  98 + );
  99 + return $this->redirect([ 'index' ]);
  100 + }
75 101  
  102 + return $this->render(
  103 + 'answer',
  104 + [
  105 + 'answer' => $answer,
  106 + 'model' => $model,
  107 + ]
  108 + );
76 109 }
77 110  
78 111 /**
... ... @@ -87,7 +120,10 @@
87 120 {
88 121 $this->findModel($id)
89 122 ->delete();
90   - Yii::$app->session->setFlash('artbox_comment_success', Yii::t('artbox-comment', 'Comment has been deleted.'));
  123 + Yii::$app->session->setFlash(
  124 + 'artbox_comment_success',
  125 + Yii::t('artbox-comment', 'Comment has been deleted.')
  126 + );
91 127 return $this->redirect([ 'index' ]);
92 128 }
93 129  
... ... @@ -102,11 +138,12 @@
102 138 */
103 139 protected function findModel($id)
104 140 {
105   - if(( $model = CommentModel::findOne($id) ) !== NULL) {
  141 + if (( $model = CommentModel::findOne($id) ) !== NULL) {
106 142 return $model;
107 143 } else {
108 144 throw new NotFoundHttpException(/*Yii::t('yii2mod.comments', 'The requested page does not exist.')*/
109   - 'The requested page does not exist.');
  145 + 'The requested page does not exist.'
  146 + );
110 147 }
111 148 }
112 149 }
113 150 \ No newline at end of file
... ...
models/CommentModel.php
... ... @@ -3,32 +3,36 @@
3 3  
4 4 use artweb\artbox\comment\behaviors\ParentBehavior;
5 5 use artweb\artbox\comment\models\interfaces\CommentInterface;
6   - use yii\base\InvalidConfigException;
  6 + use artweb\artbox\ecommerce\models\Product;
7 7 use yii\behaviors\AttributeBehavior;
8 8 use yii\behaviors\BlameableBehavior;
9 9 use yii\behaviors\TimestampBehavior;
10 10 use yii\data\ActiveDataProvider;
11 11 use yii\db\ActiveRecord;
  12 + use yii\helpers\Html;
  13 + use yii\helpers\VarDumper;
12 14  
13 15 /**
14 16 * Class CommentModel
15 17 *
16   - * @property int $artbox_comment_id
17   - * @property string $text
18   - * @property int $user_id
19   - * @property string $username
20   - * @property string $email
21   - * @property int $created_at
22   - * @property int $updated_at
23   - * @property int $deleted_at
24   - * @property int $status
25   - * @property int $artbox_comment_pid
26   - * @property int $related_id
27   - * @property string $ip
28   - * @property string $info
29   - * @property string $entity
30   - * @property int $entity_id
  18 + * @property int $artbox_comment_id
  19 + * @property string $text
  20 + * @property int $user_id
  21 + * @property string $username
  22 + * @property string $email
  23 + * @property int $created_at
  24 + * @property int $updated_at
  25 + * @property int $deleted_at
  26 + * @property int $status
  27 + * @property int $artbox_comment_pid
  28 + * @property int $related_id
  29 + * @property string $ip
  30 + * @property string $info
  31 + * @property string $entity
  32 + * @property int $entity_id
31 33 * @package artweb\artbox\comment\models
  34 + * @property ActiveRecord $entityModel
  35 + * @property string $link
32 36 */
33 37 class CommentModel extends ActiveRecord implements CommentInterface
34 38 {
... ... @@ -44,8 +48,6 @@
44 48  
45 49 public $entityId;
46 50  
47   - private $entityModel;
48   -
49 51 public function scenarios()
50 52 {
51 53 $scenarios = parent::scenarios();
... ... @@ -66,7 +68,7 @@
66 68 ];
67 69 return $scenarios;
68 70 }
69   -
  71 +
70 72 public static function tableName()
71 73 {
72 74 return '{{%artbox_comment}}';
... ... @@ -142,7 +144,7 @@
142 144 'attributes' => [
143 145 ActiveRecord::EVENT_BEFORE_INSERT => 'ip',
144 146 ],
145   - 'value' => function ($event) {
  147 + 'value' => function($event) {
146 148 return \Yii::$app->request->userIP;
147 149 },
148 150 ],
... ... @@ -200,7 +202,7 @@
200 202 'entity' => $entity,
201 203 'entity_id' => $entityId,
202 204 'status' => 1,
203   - 'artbox_comment_pid' => null,
  205 + 'artbox_comment_pid' => NULL,
204 206 ]
205 207 ),
206 208 'pagination' => [
... ... @@ -217,7 +219,7 @@
217 219  
218 220 public function deleteComment(): bool
219 221 {
220   - if (\Yii::$app->user->id != null && \Yii::$app->user->id == $this->user_id) {
  222 + if (\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) {
221 223 if ($this->delete()) {
222 224 return true;
223 225 }
... ... @@ -260,10 +262,48 @@
260 262 ->andWhere(
261 263 [
262 264 'or',
263   - [ 'artbox_comment_rating.model' => null ],
  265 + [ 'artbox_comment_rating.model' => NULL ],
264 266 [ 'artbox_comment_rating.model' => self::className() ],
265 267 ]
266 268 );
267 269 }
  270 +
  271 + public function getEntityModel()
  272 + {
  273 + $model = call_user_func_array(
  274 + [
  275 + $this->entity,
  276 + 'findOne',
  277 + ],
  278 + [ $this->entity_id ]
  279 + );
  280 + return $model;
  281 + }
  282 +
  283 + public function getLink()
  284 + {
  285 + $model = $this->getEntityModel();
  286 + if (empty($model)) {
  287 + return Html::a('Страница не найдена', '#');
  288 + }
  289 + if (Product::className() == $this->entity) {
  290 + return Html::a(
  291 + 'Перейти на товар',
  292 + \Yii::$app->urlManagerFrontend->createAbsoluteUrl(
  293 + [
  294 + 'catalog/product',
  295 + 'product' => $model->lang->alias,
  296 + 'variant' => $model->variant->sku,
  297 + ]
  298 + ),
  299 + [
  300 + 'target' => '_blank',
  301 + 'data-pjax' => '0',
  302 + ]
  303 + );
  304 + } else {
  305 + return Html::a('Неизвестная модель', '#');
  306 + }
  307 + }
268 308 }
269 309  
270 310 \ No newline at end of file
... ...
views/manage/answer.php 0 → 100644
  1 +<?php
  2 + use artweb\artbox\comment\models\CommentModel;
  3 + use yii\helpers\Html;
  4 + use yii\web\View;
  5 + use yii\widgets\ActiveForm;
  6 +
  7 + /**
  8 + * @var View $this
  9 + * @var CommentModel $model
  10 + * @var CommentModel $answer
  11 + */
  12 +
  13 + $this->title = 'Ответить на комментарий# ' . $model->artbox_comment_id;
  14 +
  15 + $this->params[ 'breadcrumbs' ][] = [
  16 + 'label' => 'Комментарии',
  17 + 'url' => [ 'index' ],
  18 + ];
  19 + $this->params[ 'breadcrumbs' ][] = $this->title;
  20 +
  21 +?>
  22 +
  23 +<div class="comment-answer">
  24 +
  25 + <?php $form = ActiveForm::begin(); ?>
  26 + <div class="form-group">
  27 + <?= $form->field($answer, 'text')
  28 + ->textarea() ?>
  29 +
  30 + <?= $form->field($answer, 'artbox_comment_pid')
  31 + ->hiddenInput(
  32 + [
  33 + 'value' => $model->artbox_comment_id,
  34 + ]
  35 + )
  36 + ->label(false) ?>
  37 +
  38 + <?= $form->field($answer, 'user_id')
  39 + ->hiddenInput(
  40 + [
  41 + 'value' => 1,
  42 + ]
  43 + )
  44 + ->label(false) ?>
  45 +
  46 + <?= $form->field($answer, 'entity')
  47 + ->hiddenInput(
  48 + [
  49 + 'value' => $model->entity,
  50 + ]
  51 + )
  52 + ->label(false) ?>
  53 +
  54 + <?= $form->field($answer, 'entity_id')
  55 + ->hiddenInput(
  56 + [
  57 + 'value' => $model->entity_id,
  58 + ]
  59 + )
  60 + ->label(false) ?>
  61 +
  62 + <?= Html::submitButton(
  63 + \Yii::t('app', 'Ответить'),
  64 + [ 'class' => 'btn btn-primary' ]
  65 + ) ?>
  66 + </div>
  67 +
  68 + <?php ActiveForm::end(); ?>
  69 +
  70 +</div>
... ...
views/manage/index.php
... ... @@ -3,82 +3,104 @@
3 3 use artweb\artbox\comment\models\CommentModelSearch;
4 4 use yii\data\ActiveDataProvider;
5 5 use yii\grid\GridView;
6   - use yii\grid\SerialColumn;
7 6 use yii\helpers\Html;
8 7 use yii\helpers\StringHelper;
  8 + use yii\web\View;
9 9 use yii\widgets\Pjax;
10 10  
11 11 /**
12 12 * @var ActiveDataProvider $dataProvider
13 13 * @var CommentModelSearch $searchModel
14 14 * @var string $commentModel
  15 + * @var View $this
15 16 */
  17 + $this->title = 'Комментарии';
  18 +
  19 + $this->params[ 'breadcrumbs' ][] = $this->title;
  20 +
16 21 $statuses = [
17 22 $searchModel::STATUS_ACTIVE => 'Активный',
18 23 $searchModel::STATUS_HIDDEN => 'Скрытый',
19 24 $searchModel::STATUS_DELETED => 'Удаленный',
20 25 ];
21 26 Pjax::begin();
22   - if(( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) {
  27 + if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) {
23 28 echo Html::tag('p', $success);
24 29 }
25   - echo GridView::widget([
26   - 'dataProvider' => $dataProvider,
27   - 'filterModel' => $searchModel,
28   - 'columns' => [
29   - [
30   - 'class' => SerialColumn::className(),
31   - ],
32   - [
33   - 'attribute' => 'created_at',
34   - 'format' => [
35   - 'date',
36   - 'php:d.m.Y',
  30 + echo GridView::widget(
  31 + [
  32 + 'dataProvider' => $dataProvider,
  33 + 'filterModel' => $searchModel,
  34 + 'columns' => [
  35 + [
  36 + 'attribute' => 'artbox_comment_id',
  37 + 'label' => 'Id',
  38 + ],
  39 + [
  40 + 'label' => 'url',
  41 + 'content' => function (CommentModel $model) {
  42 + return $model->getLink();
  43 + },
  44 + ],
  45 + [
  46 + 'label' => 'Ссылка',
  47 + ],
  48 + [
  49 + 'attribute' => 'created_at',
  50 + 'format' => [
  51 + 'date',
  52 + 'php:d.m.Y',
  53 + ],
  54 + 'filter' => false,
  55 + ],
  56 + [
  57 + 'label' => 'Комментарий',
  58 + 'content' => function(CommentModel $model) {
  59 + return StringHelper::truncate($model->text, 40, '...');
  60 + },
  61 + ],
  62 + [
  63 + 'attribute' => 'user_id',
  64 + 'value' => function($model) {
  65 + if (!empty($model->user_id)) {
  66 + return $model->user->username . ' (id:' . $model->user->id . ')';
  67 + } else {
  68 + return $model->username . ' ' . $model->email . ' (Гость)';
  69 + }
  70 + },
  71 + ],
  72 + [
  73 + 'attribute' => 'status',
  74 + 'filter' => $statuses,
  75 + 'value' => function($model) use ($statuses) {
  76 + return $statuses[ $model->status ];
  77 + },
  78 + ],
  79 + [
  80 + 'attribute' => 'ratingValue',
  81 + 'value' => function($model) {
  82 + if (!empty($model->rating)) {
  83 + return $model->rating->value;
  84 + }
  85 + return NULL;
  86 + },
  87 + ],
  88 + [
  89 + 'attribute' => 'childrenCount',
  90 + 'value' => function($model) {
  91 + return count($model->children);
  92 + },
  93 + ],
  94 + [
  95 + 'class' => 'yii\grid\ActionColumn',
  96 + 'buttons' => [
  97 + 'answer' => function(string $url) {
  98 + return Html::a(Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-bullhorn' ]), $url);
  99 + },
  100 + ],
  101 + 'template' => '{update} {answer} {delete}',
37 102 ],
38   - 'filter' => false,
39   - ],
40   - [
41   - 'label' => 'Комментарий',
42   - 'content' => function(CommentModel $model) {
43   - return StringHelper::truncate($model->text, 40, '...');
44   - }
45   - ],
46   - [
47   - 'attribute' => 'user_id',
48   - 'value' => function($model) {
49   - if(!empty( $model->user_id )) {
50   - return $model->user->username . ' (id:' . $model->user->id . ')';
51   - } else {
52   - return $model->username . ' ' . $model->email . ' (Гость)';
53   - }
54   - },
55   - ],
56   - [
57   - 'attribute' => 'status',
58   - 'filter' => $statuses,
59   - 'value' => function($model) use ($statuses) {
60   - return $statuses[ $model->status ];
61   - },
62   - ],
63   - [
64   - 'attribute' => 'ratingValue',
65   - 'value' => function($model) {
66   - if(!empty( $model->rating )) {
67   - return $model->rating->value;
68   - }
69   - return NULL;
70   - },
71   - ],
72   - [
73   - 'attribute' => 'childrenCount',
74   - 'value' => function($model) {
75   - return count($model->children);
76   - },
77   - ],
78   - [
79   - 'class' => 'yii\grid\ActionColumn',
80   - 'template' => '{update} {delete}',
81 103 ],
82   - ],
83   - ]);
  104 + ]
  105 + );
84 106 Pjax::end();
85 107 \ No newline at end of file
... ...