Passed
Push — master ( 27c83c...3b806d )
by Loban
03:03
created

ActivityLogSearch   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 28
c 0
b 0
f 0
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formName() 0 3 1
A search() 0 29 4
A rules() 0 5 1
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-activity-logger
4
 * @copyright Copyright (c) 2017 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\activityLogger\module\models;
10
11
use Yii;
12
use yii\base\Model;
13
use yii\data\ActiveDataProvider;
14
15
class ActivityLogSearch extends Model
16
{
17
    /**
18
     * @var string
19
     */
20
    public $entityName;
21
    /**
22
     * @var int|string
23
     */
24
    public $entityId;
25
    /**
26
     * @var int|string
27
     */
28
    public $userId;
29
    /**
30
     * @var string
31
     */
32
    public $env;
33
    /**
34
     * @var string
35
     */
36
    public $date;
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function rules()
42
    {
43
        return [
44
            [['entityName', 'entityId', 'userId', 'env'], 'string', 'max' => 32],
45
            [['date'], 'date', 'format' => 'dd.MM.yyyy'],
46
        ];
47
    }
48
49
    /**
50
     * For beautiful links in the browser bar when filtering and searching
51
     * @return string
52
     */
53
    public function formName()
54
    {
55
        return '';
56
    }
57
58
    /**
59
     * Creates data provider instance with search query applied
60
     * @param array $params
61
     * @return ActiveDataProvider
62
     */
63
    public function search($params)
64
    {
65
        $query = ActivityLogViewModel::find()
66
            ->orderBy(['id' => SORT_DESC]);
67
68
        $dataProvider = new ActiveDataProvider([
69
            'query' => $query,
70
            'sort' => false,
71
        ]);
72
73
        if (!($this->load($params) && $this->validate())) {
74
            return $dataProvider;
75
        }
76
77
        if (!empty($this->date)) {
78
            $time_zone = Yii::$app->getTimeZone();
79
            $date_from = strtotime("{$this->date} 00:00:00 {$time_zone}");
80
            $date_to = $date_from + 86399; // + 23:59:59
81
            $query->andWhere(['between', 'created_at', $date_from, $date_to]);
82
        }
83
84
        $query->andFilterWhere([
85
            'entity_name' => $this->entityName,
86
            'entity_id' => $this->entityId,
87
            'user_id' => $this->userId,
88
            'env' => $this->env,
89
        ]);
90
91
        return $dataProvider;
92
    }
93
}