Blame view

common/modules/comment/widgets/CommentWidget.php 7.77 KB
b82db04a   Yarik   test
1
2
3
4
5
6
7
8
9
10
  <?php
      namespace common\modules\comment\widgets;
  
      use \yii\helpers\ArrayHelper;
      use \yii\helpers\Html;
  
      class CommentWidget extends \yii\base\Widget
      {
  
          /**
2d107e9e   Yarik   test
11
12
13
14
15
           * @var null|\yii\web\View
           */
          public $context = NULL;
  
          /**
b82db04a   Yarik   test
16
17
           * @var array Parts of widgets that can be rendered
           */
2d107e9e   Yarik   test
18
19
20
21
22
          public $parts = [ ];
  
          public $rating_class = NULL;
  
          public $rating_options = [ ];
b82db04a   Yarik   test
23
24
25
26
27
28
29
30
31
  
          /**
           * @var string|\common\modules\comment\models\Comment
           */
          public $comment_class;
  
          /**
           * @var array
           */
2d107e9e   Yarik   test
32
          public $class_options = [ ];
b82db04a   Yarik   test
33
34
35
36
37
38
39
  
          /**
           * @var bool Wheather to display comment list
           */
          public $display_comment_list = true;
  
          /**
2d107e9e   Yarik   test
40
           * @var bool Whether to display comment form
b82db04a   Yarik   test
41
42
43
           */
          public $display_comment_form = true;
  
2d107e9e   Yarik   test
44
45
46
          /**
           * @var bool Whether to display success text
           */
b82db04a   Yarik   test
47
48
          public $display_comment_success = true;
  
2d107e9e   Yarik   test
49
50
51
          /**
           * @var bool Whether to allow one user post multiple comments
           */
b82db04a   Yarik   test
52
53
          public $allow_multiple = true;
  
2d107e9e   Yarik   test
54
55
56
          /**
           * @var array Options sent to list part
           */
b82db04a   Yarik   test
57
          public $list_options = [
2d107e9e   Yarik   test
58
59
              'tag'   => 'div',
              'view'  => 'list-comment',
b82db04a   Yarik   test
60
61
62
              'class' => 'test-class',
          ];
  
2d107e9e   Yarik   test
63
64
65
          /**
           * @var array Options sent to success part
           */
b82db04a   Yarik   test
66
          public $success_options = [
2d107e9e   Yarik   test
67
68
69
              'tag'     => 'div',
              'content' => NULL,
              'class'   => 'test-class-success',
b82db04a   Yarik   test
70
71
          ];
  
2d107e9e   Yarik   test
72
73
74
          /**
           * @var array Options sent to form part
           */
b82db04a   Yarik   test
75
          public $form_options = [
2d107e9e   Yarik   test
76
77
              'tag'   => 'div',
              'view'  => 'form-comment',
b82db04a   Yarik   test
78
79
80
              'class' => 'test-class-form',
          ];
  
2d107e9e   Yarik   test
81
82
83
          /**
           * @var bool Indicates whether any successful action happened
           */
b82db04a   Yarik   test
84
85
          protected $isSuccess = false;
  
2d107e9e   Yarik   test
86
87
88
          public $success_text = 'Comment successfully added';
  
          /**
2fd40ee7   Yarik   test
89
           * @var string $model Model, to which comments attached
2d107e9e   Yarik   test
90
           */
2fd40ee7   Yarik   test
91
92
93
94
95
96
          public $model;
  
          /**
           * @var integer $model_id Model id, to which comments attached
           */
          public $model_id;
b82db04a   Yarik   test
97
  
2d107e9e   Yarik   test
98
99
100
101
          /**
           * @var string Template of the widget. You may use <code>{success}, {form}, {list}</code>
           *      to render particular parts. You are also able to use common HTML here.
           */
b82db04a   Yarik   test
102
103
          public $template = "{success}\n{form}\n{list}";
  
2d107e9e   Yarik   test
104
105
106
107
          /**
           * @var array Widget options
           */
          public $options = [ ];
b82db04a   Yarik   test
108
109
  
          /**
2d107e9e   Yarik   test
110
           * @var \yii\data\DataProviderInterface Data provider of comments
b82db04a   Yarik   test
111
112
113
114
115
116
117
118
119
           */
          public $dataProvider;
  
          /**
           * @inheritdoc
           */
          public function init()
          {
              parent::init();
2d107e9e   Yarik   test
120
              \common\modules\comment\assets\CommentAsset::register($this->view);
b82db04a   Yarik   test
121
122
              if(is_string($this->comment_class)) {
                  $this->comment_class = new $this->comment_class($this->class_options);
2d107e9e   Yarik   test
123
124
125
126
127
128
129
              } else {
                  throw new \yii\base\InvalidConfigException(__CLASS__ . '->comment_class must be defined as object full class name string.');
              }
              if(!empty( $this->rating_class ) && is_string($this->rating_class)) {
                  $this->rating_class = new $this->rating_class($this->rating_options);
              } elseif(!empty( $this->rating_class )) {
                  throw new \yii\base\InvalidConfigException(__CLASS__ . '->rating_class must be defined as object full class name string.');
b82db04a   Yarik   test
130
              }
2fd40ee7   Yarik   test
131
132
              $this->comment_class->model = $this->model;
              $this->comment_class->model_id = $this->model_id;
b82db04a   Yarik   test
133
              $this->createDataProvider();
2d107e9e   Yarik   test
134
              $this->process();
b82db04a   Yarik   test
135
136
137
138
139
140
141
142
143
144
145
146
147
148
              ob_start();
          }
  
          /**
           * @inheritdoc
           * @return string
           */
          public function run()
          {
              $content = ob_get_clean();
              $this->createParts();
              return $this->renderWidget();
          }
  
2d107e9e   Yarik   test
149
150
          public function createParts()
          {
b82db04a   Yarik   test
151
152
              if($this->display_comment_success && $this->isSuccess) {
                  $tag = ArrayHelper::remove($this->success_options, 'tag', 'div');
2d107e9e   Yarik   test
153
                  if(is_callable($this->success_options[ 'content' ])) {
b82db04a   Yarik   test
154
                      $result = call_user_func(ArrayHelper::remove($this->success_options, 'content'), $this->success_text);
2d107e9e   Yarik   test
155
                  } elseif($this->success_options[ 'content' ] != NULL) {
b82db04a   Yarik   test
156
157
158
159
                      $result = Html::encode(ArrayHelper::remove($this->success_options, 'content', $this->success_text));
                  } else {
                      $result = Html::encode($this->success_text);
                  }
2d107e9e   Yarik   test
160
161
                  $this->parts[ 'success' ] = Html::tag($tag, $result, $this->success_options);
                  unset( $tag, $result );
b82db04a   Yarik   test
162
163
164
165
166
              }
  
              if($this->display_comment_list) {
                  $tag = ArrayHelper::remove($this->list_options, 'tag', 'div');
                  $view = ArrayHelper::remove($this->list_options, 'view');
2d107e9e   Yarik   test
167
                  $this->parts[ 'list' ] = Html::tag($tag, $this->renderItems($view), $this->list_options);
b82db04a   Yarik   test
168
169
              }
  
38a6e1dd   Yarik   test
170
              if($this->display_comment_form && $this->comment_class->checkCreate()) {
b82db04a   Yarik   test
171
172
                  $tag = ArrayHelper::remove($this->form_options, 'tag', 'div');
                  $view = ArrayHelper::remove($this->form_options, 'view');
2d107e9e   Yarik   test
173
                  $this->parts[ 'form' ] = Html::tag($tag, $this->renderForm($view), $this->form_options);
b82db04a   Yarik   test
174
175
176
177
178
179
              }
          }
  
          public function createDataProvider()
          {
              $this->dataProvider = new \yii\data\ActiveDataProvider([
2fd40ee7   Yarik   test
180
                  'query'      => $this->comment_class->getComments($this->model, $this->model_id),
b82db04a   Yarik   test
181
182
183
184
185
186
                  'pagination' => [
                      'pageSize' => 10,
                  ],
              ]);
          }
  
2d107e9e   Yarik   test
187
188
189
          public function renderItems($view)
          {
              if(empty( $view )) {
b82db04a   Yarik   test
190
191
                  throw new \yii\base\InvalidConfigException("list_options[view] must be set");
              }
2d107e9e   Yarik   test
192
              return $this->render($view, [ 'dataProvider' => $this->dataProvider ]);
b82db04a   Yarik   test
193
194
          }
  
2d107e9e   Yarik   test
195
196
197
          public function renderForm($view)
          {
              if(empty( $view )) {
b82db04a   Yarik   test
198
199
                  throw new \yii\base\InvalidConfigException("form_options[view] must be set");
              }
4ff3ca88   Yarik   test
200
201
202
203
204
205
206
207
208
209
              if($this->comment_class->guestComment || !empty(\Yii::$app->user->identity)) {
                  return $this->render($view, [
                      'model'        => $this->comment_class,
                      'rating'       => $this->rating_class,
                      'user'         => \Yii::$app->user->identity,
                      'dataProvider' => $this->dataProvider,
                  ]);
              } else {
                  return '';
              }
b82db04a   Yarik   test
210
211
          }
  
2d107e9e   Yarik   test
212
213
          public function renderWidget()
          {
b82db04a   Yarik   test
214
215
216
217
218
219
220
221
222
223
              $template = $this->template;
              $parts = $this->parts;
              $options = $this->options;
              $template = preg_replace('/{success}/', ArrayHelper::remove($parts, 'success', ''), $template);
              $template = preg_replace('/{list}/', ArrayHelper::remove($parts, 'list', ''), $template);
              $template = preg_replace('/{form}/', ArrayHelper::remove($parts, 'form', ''), $template);
              $tag = ArrayHelper::remove($options, 'tag', 'div');
              return Html::tag($tag, $template, $options);
          }
  
2d107e9e   Yarik   test
224
          public function process()
b82db04a   Yarik   test
225
226
          {
              $data = \Yii::$app->request->post();
2d107e9e   Yarik   test
227
228
229
230
231
              if($this->comment_class->load($data) && $this->comment_class->postComment()) {
                  if(is_object($this->rating_class) && $this->comment_class->rating->load($data) && $this->comment_class->rating->save()) {
                      $this->isSuccess = true;
                  }
              }
b82db04a   Yarik   test
232
233
          }
      }