AuditTrailSearch::search()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 0
Metric Value
cc 7
nc 14
nop 2
dl 0
loc 38
ccs 22
cts 24
cp 0.9167
crap 7.0283
rs 8.3786
c 0
b 0
f 0
1
<?php
2
3
namespace bedezign\yii2\audit\models;
4
5
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
9
/**
10
 * AuditTrailSearch
11
 * @package bedezign\yii2\audit\models
12
 */
13
class AuditTrailSearch extends AuditTrail
14
{
15
    /**
16
     * @return array
17
     */
18 9
    public function rules()
19
    {
20
        // Note: The model is used by both the entry and the trail index pages, hence the separate use of `id` and `entry_id`
21
        return [
22 9
            [['id', 'user_id', 'entry_id', 'action', 'model', 'model_id', 'field', 'created'], 'safe'],
23 9
        ];
24
    }
25
26
    /**
27
     * @return array
28
     */
29 9
    public function scenarios()
30
    {
31
        // bypass scenarios() implementation in the parent class
32 9
        return Model::scenarios();
33
    }
34
35
    /**
36
     * @param $params
37
     * @param null $query
38
     * @return ActiveDataProvider
39
     */
40 9
    public function search($params, $query = null)
41
    {
42 9
        $query = $query ? $query : $this->find();
43
44 9
        $dataProvider = new ActiveDataProvider([
45 9
            'query' => $query,
46
            'sort' => [
47
                'defaultOrder' => [
48
                    'id' => SORT_DESC
49 9
                ]
50 9
            ]
51 9
        ]);
52
53
        // load the search form data and validate
54 9
        if (!($this->load($params) && $this->validate())) {
55 6
            return $dataProvider;
56
        }
57
58
        // adjust the query by adding the filters
59 3
        $userId = $this->user_id;
60 3
        if (strlen($this->user_id))
61 3
            $userId = intval($this->user_id) ?: 0;
62
63 3
        $query->andFilterWhere(['id' => $this->id]);
64 3
        $query->andFilterWhere(['entry_id' => $this->entry_id]);
65 3
        $query->andFilterWhere(['user_id' => $userId]);
66 3
        $query->andFilterWhere(['action' => $this->action]);
67 3
        $query->andFilterWhere(['like', 'model', $this->model]);
68 3
        $query->andFilterWhere(['model_id' => $this->model_id]);
69 3
        if (is_array($this->field)) {
70
            $query->andFilterWhere(['in', 'field', $this->field]);
71
        } else {
72 3
            $query->andFilterWhere(['field' => $this->field]);
73
        }
74 3
        $query->andFilterWhere(['like', 'created', $this->created]);
75
76 3
        return $dataProvider;
77
    }
78
79
    /**
80
     * @return array
81
     */
82 9
    static public function actionFilter()
83
    {
84 9
        return \yii\helpers\ArrayHelper::map(
85 9
            self::find()->select('action')->groupBy('action')->orderBy('action ASC')->all(),
86 9
            'action',
87
            'action'
88 9
        );
89
    }
90
}
91