Completed
Push — master ( 22896e...75e918 )
by vistart
03:54
created

UserProfileSearchTrait.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use Yii;
16
use yii\base\Model;
17
use yii\data\ActiveDataProvider;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait UserProfileSearchTrait
24
{
25
    /**
26
     * @var string 
27
     */
28
    public $createdFrom;
29
    protected $createdFromInUtc;
30
31
    /**
32
     * @var string 
33
     */
34
    public $createdTo;
35
    protected $createdToInUtc;
36
37
    /**
38
     * 
39
     * @return array
40
     */
41
    public function rules()
42
    {
43
        return [
44
            ['id', 'integer'],
45
            [['nickname', 'first_name', 'last_name'], 'string'],
46
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
47
            [['createdFrom', 'createdTo'], 'gmdate'],
48
        ];
49
    }
50
51
    /**
52
     * Convert time attribute to UTC time.
53
     * @param string $attribute
54
     * @param array $params
55
     * @param mixed $validator
56
     */
57
    public function gmdate($attribute, $params, $validator)
58
    {
59
        if (isset($this->$attribute)) {
60
            $timestamp = strtotime($this->$attribute);
61
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
62
        }
63
    }
64
65
    /**
66
     * 
67
     * @return array
68
     */
69
    public function scenarios()
70
    {
71
        // bypass scenarios() implementation in the parent class
72
        return Model::scenarios();
73
    }
74
75
    /**
76
     * Search
77
     * @param array $params
78
     * @return ActiveDataProvider
79
     */
80
    public function search($params)
81
    {
82
        $query = static::find();
83
        $dataProvider = new ActiveDataProvider([
84
            'query' => $query,
85
            'pagination' => [
86
                'pageParam' => 'user-page',
87
                'defaultPageSize' => 20,
88
                'pageSizeParam' => 'user-per-page',
89
            ],
90
            'sort' => [
91
                'sortParam' => 'user-sort',
92
            ],
93
        ]);
94
95
        if (!($this->load($params) && $this->validate())) {
96
            return $dataProvider;
97
        }
98
99
        $query = $query->andFilterWhere([
100
            'LIKE', 'id', $this->id,
101
        ])->andFilterWhere([
102
            'LIKE', 'nickname', $this->nickname,
103
        ])->andFilterWhere([
104
            '>=', 'created_at', $this->createdFromInUtc,
105
        ])->andFilterWhere([
106
            '<=', 'created_at', $this->createdToInUtc,
107
        ])->andFilterWhere([
108
            'LIKE', 'first_name', $this->first_name,
0 ignored issues
show
The property first_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
        ])->andFilterWhere([
110
            'LIKE', 'last_name', $this->last_name,
0 ignored issues
show
The property last_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
111
        ]);
112
        $dataProvider->query = $query;
113
        return $dataProvider;
114
    }
115
116
    /**
117
     * Add `createdFrom` & `createdTo` attributes.
118
     * @return array
119
     */
120
    public function attributeLabels()
121
    {
122
        $attributeLabels = parent::attributeLabels();
123
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
124
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
125
        return $attributeLabels;
126
    }
127
}
128