OrganizationSearch::getQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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
    /**
71
     * @return array
72
     */
73
    public static function getTypesWithEmpty()
74
    {
75
        return [
76
            '' => Yii::t('user', 'All'),
77
            Organization::TYPE_ORGANIZATION => Yii::t('organization', 'Organization'),
78
            Organization::TYPE_DEPARTMENT => Yii::t('organization', 'Department'),
79
        ];
80
    }
81
82
    public function rules()
83
    {
84
        return [
85
            [['id', 'parentId'], 'integer', 'min' => 0],
86
            [['name', 'nickname'], 'string'],
87
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
88
            [['createdFrom', 'createdTo'], 'gmdate'],
89
            ['type', 'in', 'range' => array_keys(static::getTypesWithEmpty())],
90
        ];
91
    }
92
93
    /**
94
     * Convert time attribute to UTC time.
95
     * @param string $attribute
96
     * @param array $params
97
     * @param mixed $validator
98
     */
99
    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...
100
    {
101
        if (isset($this->$attribute)) {
102
            $timestamp = strtotime($this->$attribute);
103
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
104
        }
105
    }
106
107
    /**
108
     * @param OrganizationQuery $query
109
     * @return null|OrganizationQuery
110
     */
111
    protected function prepareQuery($query = null)
112
    {
113
        if (!$query) {
114
            $query = $this->getQuery();
115
        }
116
        /* @var $query OrganizationQuery */
117
        $class = $this->organizationClass;
118
        $query = $query->from("{$class::tableName()} {$this->organizationAlias}");
119
        $noInitOrg = new $class;
120
        /* @var $noInitOrg Organization */
121
        $profileClass = $noInitOrg->profileClass;
122
        if (!empty($profileClass)) {
123
            $query = $query->joinWith(["profile {$this->profileAlias}"]);
124
        }
125
        $memberClass = $noInitOrg->memberClass;
126
        if (!empty($memberClass)) {
127
            $query = $query->joinWith(["members {$this->memberAlias}"]);
128
        }
129
        $memberUserClass = $noInitOrg->getNoInitMember()->memberUserClass;
130
        if (!empty($memberUserClass)) {
131
            $query = $query->joinWith(["members.memberUser {$this->memberUserAlias}"]);
132
        }
133
        $query = $query->select($this->organizationAlias . '.guid')->distinct()
134
            ->addSelect([
135
                $this->organizationAlias . '.id',
136
                $this->organizationAlias . '.ip',
137
                $this->organizationAlias . '.ip_type',
138
                $this->organizationAlias . '.parent_guid',
139
                $this->organizationAlias . '.created_at',
140
                $this->organizationAlias . '.updated_at',
141
                $this->organizationAlias . '.status',
142
                $this->organizationAlias . '.type',
143
            ]);
144
        //In MySQL 5.7 and earlier versions, it is necessary to specify the table name.
145
        if (!empty($profileClass)) {
146
            $query = $query->addSelect([
147
                $this->profileAlias . '.name',
148
                $this->profileAlias . '.nickname',
149
            ]);
150
        }
151
        return $query;
152
    }
153
154
    /**
155
     * @param $params
156
     * @return ActiveDataProvider
157
     */
158
    public function search($params)
159
    {
160
        $query = $this->query;
161
        $dataProvider = new ActiveDataProvider([
162
            'query' => $query,
163
            'pagination' => [
164
                'pageParam' => 'organization-page',
165
                'defaultPageSize' => 20,
166
                'pageSizeParam' => 'organization-per-page',
167
            ],
168
            'sort' => [
169
                'sortParam' => 'organization-sort',
170
                'attributes' => [
171
                    'id',
172
                    'nickname' => [
173
                        'asc' => [$this->profileAlias . '.nickname' => SORT_ASC],
174
                        'desc' => [$this->profileAlias . '.nickname' => SORT_DESC],
175
                        'default' => SORT_ASC,
176
                        'label' => Yii::t('user', 'Nickname'),
177
                    ],
178
                    'name',
179
                    'type' => [
180
                        'asc' => [$this->organizationAlias . '.type' => SORT_ASC],
181
                        'desc' => [$this->organizationAlias . '.type' => SORT_DESC],
182
                        'default' => SORT_ASC,
183
                        'label' => Yii::t('user', 'Type'),
184
                    ],
185
                    'created_at' => [
186
                        'asc' => [$this->organizationAlias . '.created_at' => SORT_ASC],
187
                        'desc' => [$this->organizationAlias . '.created_at' => SORT_DESC],
188
                        'default' => SORT_ASC,
189
                        'label' => Yii::t('user', 'Creation Time'),
190
                    ],
191
                    'updated_at' => [
192
                        'asc' => [$this->organizationAlias . '.updated_at' => SORT_ASC],
193
                        'desc' => [$this->organizationAlias . '.updated_at' => SORT_DESC],
194
                        'default' => SORT_ASC,
195
                        'label' => Yii::t('user', 'Last Updated Time'),
196
                    ],
197
                ],
198
            ],
199
        ]);
200
201
        if (!($this->load($params) && $this->validate())) {
202
            return $dataProvider;
203
        }
204
205
        $query = $query->andFilterWhere([
206
            'LIKE', $this->organizationAlias . '.id', $this->id,
207
        ])->andFilterWhere([
208
            'LIKE', $this->profileAlias . '.nickname', $this->nickname,
209
        ])->andFilterWhere([
210
            '>=', $this->organizationAlias . '.created_at', $this->createdFromInUtc,
211
        ])->andFilterWhere([
212
            '<=', $this->organizationAlias . '.created_at', $this->createdToInUtc,
213
        ])->andFilterWhere([
214
            'LIKE', $this->profileAlias . '.name', $this->name,
215
        ])->andFilterWhere([
216
            $this->organizationAlias . '.type' => $this->type,
217
        ]);
218
        $dataProvider->query = $query;
219
        return $dataProvider;
220
    }
221
222
    /**
223
     * @return array
224
     */
225
    public function attributeLabels()
226
    {
227
        $attributeLabels = parent::attributeLabels();
228
        $attributeLabels['id'] = Yii::t('user', 'ID');
229
        $attributeLabels['parentId'] = Yii::t('organization', 'Parent ID');
230
        $attributeLabels['name'] = Yii::t('organization', 'Name');
231
        $attributeLabels['nickname'] = Yii::t('user', 'Nickname');
232
        $attributeLabels['type'] = Yii::t('user', 'Type');
233
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
234
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
235
        return $attributeLabels;
236
    }
237
}
238