LogSearch::search()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 38
rs 9.552
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace backend\models\search;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
use backend\models\Log;
9
10
/**
11
 * LogSearch represents the model behind the search form of `backend\models\Log`.
12
 */
13
class LogSearch extends Log
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function rules()
19
    {
20
        return [
21
            [['id', 'item_type', 'item_id', 'action'], 'integer'],
22
            [['log_date'], 'date', 'format' => 'php:Y-m-d'],
23
            [['user_ip'], 'ip'],
24
            [['user'], 'string'],
25
        ];
26
    }
27
28
    /**
29
     * Creates data provider instance with search query applied
30
     * @param array $params
31
     * @return ActiveDataProvider
32
     */
33
    public function search($params)
34
    {
35
        $query = Log::find();
36
37
        // add conditions that should always apply here
38
39
        $dataProvider = new ActiveDataProvider([
40
            'query' => $query,
41
            'sort'=> [
42
                'defaultOrder' => [
43
                    'id' => SORT_DESC,
44
                ],
45
            ],
46
        ]);
47
48
        $this->load($params);
49
        if (!$this->validate()) {
50
            return $dataProvider;
51
        }
52
        // grid filtering conditions
53
        $query->andFilterWhere([
54
            'id' => $this->id,
55
            'user_ip' => $this->user_ip ? ip2long($this->user_ip) : null,
56
            'item_type' => $this->item_type,
57
            'item_id' => $this->item_id,
58
            'action' => $this->action,
59
            'user' => $this->user,
60
        ]);
61
        if ($this->log_date) {
62
            $query->andFilterWhere([
63
                'BETWEEN',
64
                'log_date',
65
                strtotime($this->log_date . ' 00:00:00'),
66
                strtotime($this->log_date . ' 23:59:59'),
67
            ]);
68
        }
69
70
        return $dataProvider;
71
    }
72
}
73