CommentModelSearch.php 7.13 KB
<?php
    
    namespace artbox\webcomment\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * CommentModelSearch represents the model behind the search form about
     * `artbox\webcomment\models\CommentModel`.
     */
    class CommentModelSearch extends CommentModel
    {
        
        public $ratingValue;
        
        public $childrenCount;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                        'created_at',
                        'updated_at',
                        'deleted_at',
                        'status',
                        'parent_id',
                        'related_id',
                        'entity_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'childrenCount',
                    ],
                    'integer',
                    'min' => 0,
                ],
                [
                    [
                        'ratingValue',
                    ],
                    'number',
                    'min' => 1,
                    'max' => 5,
                ],
                [
                    [
                        'customer_id',
                        'text',
                        'username',
                        'email',
                        'ip',
                        'entity',
                        'info',
                    ],
                    'safe',
                ],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return array_merge(
                parent::attributeLabels(),
                [
                    'ratingValue'   => \Yii::t('artbox-comment', 'Рейтинг'),
                    'childrenCount' => \Yii::t('artbox-comment', 'Количество ответов'),
                ]
            );
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            // bypass scenarios() implementation in the parent class
            return Model::scenarios();
        }
        
        /**
         * Creates data provider instance with search query applied
         *
         * @param array $params
         *
         * @return ActiveDataProvider
         */
        public function search($params)
        {
            $query = CommentModel::find()
                                 ->joinWith(
                                     [
                                         'rating',
                                         'customer',
                                     ]
                                 );
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                    'sort'  => [
                        'attributes'   => [
                            'ratingValue' => [
                                'asc'  => [ 'artbox_comment_rating.value' => SORT_ASC ],
                                'desc' => [ 'artbox_comment_rating.value' => SORT_DESC ],
                            ],
                            'id',
                            'text',
                            'customer_id',
                            'status',
                            'entity',
                            'entity_id',
                            'created_at',
                        ],
                        'defaultOrder' => [
                            'created_at' => SORT_DESC,
                        ],
                    ],
                ]
            );
            
            $this->load($params);
            
            if (!$this->validate()) {
                // uncomment the following line if you do not want to return any records when validation fails
                // $query->where('0=1');
                return $dataProvider;
            }
            
            // grid filtering conditions
            $query->andFilterWhere(
                [
                    'artbox_comment.id'                    => $this->id,
                    'created_at'            => $this->created_at,
                    'updated_at'            => $this->updated_at,
                    'deleted_at'            => $this->deleted_at,
                    'artbox_comment.status' => $this->status,
                    'parent_id'             => $this->parent_id,
                    'related_id'            => $this->related_id,
                    'entity_id'             => $this->entity_id,
                ]
            );
            
            $query->andFilterWhere(
                [
                    'like',
                    'artbox_comment.text',
                    $this->text,
                ]
            )
                  ->andFilterWhere(
                      [
                          'like',
                          'artbox_comment.username',
                          $this->username,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'artbox_comment.email',
                          $this->email,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'artbox_comment.ip',
                          $this->ip,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'artbox_comment.entity',
                          $this->entity,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'artbox_comment.info',
                          $this->info,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'artbox_comment_rating.value' => $this->ratingValue,
                      ]
                  );
    
            if (!empty($this->customer_id)) {
                $query->andWhere(
                    [
                        'or',
                        [ 'artbox_comment.customer_id' => (int) $this->customer_id ],
                        [
                            'like',
                            'customer.username',
                            $this->customer_id,
                        ],
                        [
                            'like',
                            'artbox_comment.username',
                            $this->customer_id,
                        ],
                        [
                            'like',
                            'artbox_comment.email',
                            $this->customer_id,
                        ],
                    ]
                );
            }
            
            return $dataProvider;
        }
    }