ActivityLogSearch::search()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
use yii\data\DataProviderInterface;
15
16
class ActivityLogSearch extends Model
17
{
18
    /**
19
     * @var string
20
     */
21
    public $entityName;
22
    /**
23
     * @var int|string
24
     */
25
    public $entityId;
26
    /**
27
     * @var int|string
28
     */
29
    public $userId;
30
    /**
31
     * @var string
32
     */
33
    public $env;
34
    /**
35
     * @var string
36
     */
37
    public $date;
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function rules()
43
    {
44
        return [
45
            [['entityName', 'entityId', 'userId', 'env'], 'string', 'max' => 32],
46
            [['date'], 'date', 'format' => 'dd.MM.yyyy'],
47
        ];
48
    }
49
50
    /**
51
     * For beautiful links in the browser bar when filtering and searching
52
     * @return string
53
     */
54
    public function formName()
55
    {
56
        return '';
57
    }
58
59
    /**
60
     * Creates data provider instance with search query applied
61
     */
62
    public function search(): DataProviderInterface
63
    {
64
        $query = ActivityLog::find()
65
            ->orderBy(['id' => SORT_DESC]);
66
67
        if (!empty($this->date)) {
68
            $timeZone = Yii::$app->getFormatter()->timeZone;
69
            $dateFrom = strtotime("{$this->date} 00:00:00 {$timeZone}");
70
            $dateTo = $dateFrom + 86399; // + 23:59:59
71
            $query->andWhere(['between', 'created_at', $dateFrom, $dateTo]);
72
        }
73
74
        $query->andFilterWhere([
75
            'entity_name' => $this->entityName,
76
            'entity_id' => $this->entityId,
77
            'user_id' => $this->userId,
78
            'env' => $this->env,
79
        ]);
80
81
        return new ActiveDataProvider([
82
            'query' => $query,
83
            'sort' => false,
84
        ]);
85
    }
86
}