Completed
Push — master ( 9eb8f3...27c704 )
by vistart
05:33
created

OrganizationSearch::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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\organization;
14
15
use rhosocial\organization\queries\OrganizationQuery;
16
use Yii;
17
use yii\base\Model;
18
use yii\data\ActiveDataProvider;
19
20
/**
21
 * Class OrganizationSearch
22
 * @package rhosocial\organization
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class OrganizationSearch extends Model
27
{
28
    public $organizationClass = Organization::class;
29
    public $memberClass = Member::class;
30
    public function getQuery()
31
    {
32
        $class = $this->organizationClass;
33
        if (empty($class)) {
34
            return null;
35
        }
36
        return $class::find();
37
    }
38
    public $organizationAlias = 'o_alias';
39
    public $memberAlias = 'm_alias';
40
    public $memberUserAlias = 'u_alias';
41
    public $profileAlias = 'op_alias';
42
    public $id;
43
    public $name;
44
    public $nickname;
45
    public $type;
46
    public $parentId;
47
    /**
48
     * @var string
49
     */
50
    public $createdFrom;
51
    protected $createdFromInUtc;
52
53
    /**
54
     * @var string
55
     */
56
    public $createdTo;
57
    protected $createdToInUtc;
58
    /**
59
     * @var OrganizationQuery;
60
     */
61
    public $query;
62
63
    public function init()
64
    {
65
        if (!isset($this->query)) {
66
            $this->query = $this->prepareQuery();
67
        }
68
    }
69
70
    public static function getTypesWithEmpty()
71
    {
72
        return [
73
            '' => Yii::t('user', 'All'),
74
            Organization::TYPE_ORGANIZATION => Yii::t('organization', 'Organization'),
75
            Organization::TYPE_DEPARTMENT => Yii::t('organization', 'Department'),
76
        ];
77
    }
78
79
    public function rules()
80
    {
81
        return [
82
            [['id', 'parentId'], 'integer', 'min' => 0],
83
            [['name', 'nickname'], 'string'],
84
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
85
            [['createdFrom', 'createdTo'], 'gmdate'],
86
            ['type', 'in', 'range' => array_keys(static::getTypesWithEmpty())],
87
        ];
88
    }
89
90
    /**
91
     * Convert time attribute to UTC time.
92
     * @param string $attribute
93
     * @param array $params
94
     * @param mixed $validator
95
     */
96
    public function gmdate($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    {
98
        if (isset($this->$attribute)) {
99
            $timestamp = strtotime($this->$attribute);
100
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
101
        }
102
    }
103
104
    /**
105
     * @param OrganizationQuery $query
106
     * @return null|OrganizationQuery
107
     */
108
    protected function prepareQuery($query = null)
109
    {
110
        if (!$query) {
111
            $query = $this->getQuery();
112
        }
113
        /* @var $query OrganizationQuery */
114
        $class = $this->organizationClass;
115
        $query = $query->from("{$class::tableName()} {$this->organizationAlias}");
116
        $noInitOrg = new $class;
117
        /* @var $noInitOrg Organization */
118
        $profileClass = $noInitOrg->profileClass;
119
        if (!empty($profileClass)) {
120
            $query = $query->joinWith(["profile {$this->profileAlias}"]);
121
        }
122
        $memberClass = $noInitOrg->memberClass;
123
        if (!empty($memberClass)) {
124
            $query = $query->joinWith(["members {$this->memberAlias}"]);
125
        }
126
        $memberUserClass = $noInitOrg->getNoInitMember()->memberUserClass;
127
        if (!empty($memberUserClass)) {
128
            $query = $query->joinWith(["members.memberUser {$this->memberUserAlias}"]);
129
        }
130
        $query = $query->select($this->organizationAlias . '.guid')->distinct()
131
            ->addSelect([
132
                $this->organizationAlias . '.id',
133
                $this->organizationAlias . '.ip',
134
                $this->organizationAlias . '.ip_type',
135
                $this->organizationAlias . '.parent_guid',
136
                $this->organizationAlias . '.created_at',
137
                $this->organizationAlias . '.updated_at',
138
                $this->organizationAlias . '.status',
139
                $this->organizationAlias . '.type',
140
            ]);
141
        return $query;
142
    }
143
144
    public function search($params)
145
    {
146
        $query = $this->query;
147
        $dataProvider = new ActiveDataProvider([
148
            'query' => $query,
149
            'pagination' => [
150
                'pageParam' => 'organization-page',
151
                'defaultPageSize' => 20,
152
                'pageSizeParam' => 'organization-per-page',
153
            ],
154
            'sort' => [
155
                'sortParam' => 'organization-sort',
156
                'attributes' => [
157
                    'id',
158
                    'nickname' => [
159
                        'asc' => [$this->profileAlias . '.nickname' => SORT_ASC],
160
                        'desc' => [$this->profileAlias . '.nickname' => SORT_DESC],
161
                        'default' => SORT_ASC,
162
                        'label' => Yii::t('user', 'Nickname'),
163
                    ],
164
                    'name',
165
                    'type' => [
166
                        'asc' => [$this->organizationAlias . '.type' => SORT_ASC],
167
                        'desc' => [$this->organizationAlias . '.type' => SORT_DESC],
168
                        'default' => SORT_ASC,
169
                        'label' => Yii::t('user', 'Type'),
170
                    ],
171
                    'created_at' => [
172
                        'asc' => [$this->organizationAlias . '.created_at' => SORT_ASC],
173
                        'desc' => [$this->organizationAlias . '.created_at' => SORT_DESC],
174
                        'default' => SORT_ASC,
175
                        'label' => Yii::t('user', 'Creation Time'),
176
                    ],
177
                    'updated_at' => [
178
                        'asc' => [$this->organizationAlias . '.updated_at' => SORT_ASC],
179
                        'desc' => [$this->organizationAlias . '.updated_at' => SORT_DESC],
180
                        'default' => SORT_ASC,
181
                        'label' => Yii::t('user', 'Last Updated Time'),
182
                    ],
183
                ],
184
            ],
185
        ]);
186
187
        if (!($this->load($params) && $this->validate())) {
188
            return $dataProvider;
189
        }
190
191
        $query = $query->andFilterWhere([
192
            'LIKE', $this->organizationAlias . '.id', $this->id,
193
        ])->andFilterWhere([
194
            'LIKE', $this->profileAlias . '.nickname', $this->nickname,
195
        ])->andFilterWhere([
196
            '>=', $this->organizationAlias . '.created_at', $this->createdFromInUtc,
197
        ])->andFilterWhere([
198
            '<=', $this->organizationAlias . '.created_at', $this->createdToInUtc,
199
        ])->andFilterWhere([
200
            'LIKE', $this->profileAlias . '.name', $this->name,
201
        ])->andFilterWhere([
202
            $this->organizationAlias . '.type' => $this->type,
203
        ]);
204
        $dataProvider->query = $query;
205
        return $dataProvider;
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    public function attributeLabels()
212
    {
213
        $attributeLabels = parent::attributeLabels();
214
        $attributeLabels['id'] = Yii::t('user', 'ID');
215
        $attributeLabels['parentId'] = Yii::t('organization', 'Parent ID');
216
        $attributeLabels['name'] = Yii::t('organization', 'Name');
217
        $attributeLabels['nickname'] = Yii::t('user', 'Nickname');
218
        $attributeLabels['type'] = Yii::t('user', 'Type');
219
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
220
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
221
        return $attributeLabels;
222
    }
223
}
224