Completed
Push — master ( 6f5d59...afd2c7 )
by Andrey
06:35
created

FeedbackSearch::deleteSelected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use yii\base\Model;
6
use yii\helpers\ArrayHelper;
7
use yii\data\{ActiveDataProvider, Pagination};
8
9
/**
10
 * FeedbackSearch represents the model behind the search form of `app\models\Feedback`.
11
 *
12
 * @package app\models
13
 */
14
class FeedbackSearch extends Feedback
15
{
16
    /**
17
     * @var array
18
     */
19
    public $delete_items = [];
20
21
    const SCENARIO_DELETE_SELECTED = 'delete_selected';
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function rules()
27
    {
28
        return [
29
            [
30
                'read',
31
                'integer',
32
            ],
33
            [
34
                [
35
                    'name',
36
                    'phone',
37
                    'subject',
38
                ],
39
                'string'
40
            ],
41
            [
42
                'email',
43
                'email'
44
            ],
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function scenarios()
52
    {
53
        return ArrayHelper::merge(
54
            Model::scenarios(),
55
            parent::scenarios(),
56
            [
57
                self::SCENARIO_DELETE_SELECTED => ['delete_items']
58
            ]
59
        );
60
    }
61
62
    /**
63
     * Creates data provider instance with search query applied
64
     *
65
     * @param array $params
66
     *
67
     * @return ActiveDataProvider
68
     */
69
    public function search($params)
70
    {
71
        $query = Feedback::find();
72
73
        // add conditions that should always apply here
74
75
        $dataProvider = new ActiveDataProvider([
76
            'query' => $query,
77
        ]);
78
79
        $this->load($params);
80
81
        if (!$this->validate()) {
82
            // uncomment the following line if you do not want to return any records when validation fails
83
            // $query->where('0=1');
84
            return $dataProvider;
85
        }
86
87
        // grid filtering conditions
88
        $query->andFilterWhere([
89
            'read' => $this->read,
90
            'email' => $this->email,
91
            'phone' => $this->phone,
92
        ]);
93
94
        $query->andFilterWhere([
95
            'like',
96
            'name',
97
            $this->name
98
        ]);
99
100
        $query->andFilterWhere([
101
            'like',
102
            'subject',
103
            $this->subject
104
        ]);
105
106
        $pagination = new Pagination([
107
            'defaultPageSize' => 20,
108
            'totalCount' => $query->count(),
109
        ]);
110
111
        $dataProvider->setPagination($pagination);
112
113
        return $dataProvider;
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function deleteSelected()
120
    {
121
        return static::deleteAll(['id' => $this->delete_items]);
122
    }
123
}
124