Completed
Push — master ( efd6c7...e843b5 )
by vistart
04:27
created

OrganizationSearch::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
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 static function find()
31
    {
32
        $noInit = new static;
33
        $class = $noInit->organizationClass;
34
        if (empty($class)) {
35
            return null;
36
        }
37
        return $class::find();
38
    }
39
    public $organizationAlias = 'o_alias';
40
    public $memberAlias = 'm_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
    public static function getTypesWithEmpty()
60
    {
61
        return [
62
            '' => Yii::t('user', 'All'),
63
            Organization::TYPE_ORGANIZATION => Yii::t('organization', 'Organization'),
64
            Organization::TYPE_DEPARTMENT => Yii::t('organization', 'Department'),
65
        ];
66
    }
67
68
    public function rules()
69
    {
70
        return [
71
            [['id', 'parentId'], 'integer', 'min' => 0],
72
            [['name', 'nickname'], 'string'],
73
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
74
            [['createdFrom', 'createdTo'], 'gmdate'],
75
            ['type', 'in', 'range' => array_keys(static::getTypesWithEmpty())],
76
        ];
77
    }
78
79
    /**
80
     * Convert time attribute to UTC time.
81
     * @param string $attribute
82
     * @param array $params
83
     * @param mixed $validator
84
     */
85
    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...
86
    {
87
        if (isset($this->$attribute)) {
88
            $timestamp = strtotime($this->$attribute);
89
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
90
        }
91
    }
92
93
    public function search($params)
94
    {
95
        $query = static::find();
96
        /* @var $query OrganizationQuery */
97
        $class = $this->organizationClass;
98
        $query = $query->from("{$class::tableName()} {$this->organizationAlias}");
99
        $noInitOrg = new $class;
100
        /* @var $noInitOrg Organization */
101
        $profileClass = $noInitOrg->profileClass;
102
        if (!empty($profileClass)) {
103
            $query = $query->joinWith(["profile {$this->profileAlias}"]);
104
        }
105
        $dataProvider = new ActiveDataProvider([
106
            'query' => $query,
107
            'pagination' => [
108
                'pageParam' => 'organization-page',
109
                'defaultPageSize' => 20,
110
                'pageSizeParam' => 'organization-per-page',
111
            ],
112
            'sort' => [
113
                'sortParam' => 'organization-sort',
114
                'attributes' => [
115
                    'id',
116
                    'nickname',
117
                    'name',
118
                    'createdAt' => [
119
                        'asc' => [$this->organizationAlias . '.created_at' => SORT_ASC],
120
                        'desc' => [$this->organizationAlias . '.created_at' => SORT_DESC],
121
                        'default' => SORT_ASC,
122
                        'label' => Yii::t('user', 'Creation Time'),
123
                    ],
124
                    'updatedAt' => [
125
                        'asc' => [$this->organizationAlias . '.updated_at' => SORT_ASC],
126
                        'desc' => [$this->organizationAlias . '.updated_at' => SORT_DESC],
127
                        'default' => SORT_ASC,
128
                        'label' => Yii::t('user', 'Last Updated Time'),
129
                    ],
130
                ],
131
            ],
132
        ]);
133
134
        if (!($this->load($params) && $this->validate())) {
135
            return $dataProvider;
136
        }
137
138
        $query = $query->andFilterWhere([
139
            'LIKE', $this->organizationAlias . '.id', $this->id,
140
        ])->andFilterWhere([
141
            'LIKE', $this->profileAlias . '.nickname', $this->nickname,
142
        ])->andFilterWhere([
143
            '>=', $this->organizationAlias . '.created_at', $this->createdFromInUtc,
144
        ])->andFilterWhere([
145
            '<=', $this->organizationAlias . '.created_at', $this->createdToInUtc,
146
        ])->andFilterWhere([
147
            'LIKE', $this->profileAlias . '.name', $this->name,
148
        ])->andFilterWhere([
149
            'type' => $this->type,
150
        ]);
151
        $dataProvider->query = $query;
152
        return $dataProvider;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function attributeLabels()
159
    {
160
        $attributeLabels = parent::attributeLabels();
161
        $attributeLabels['id'] = Yii::t('user', 'ID');
162
        $attributeLabels['parentId'] = Yii::t('organization', 'Parent ID');
163
        $attributeLabels['name'] = Yii::t('organization', 'Name');
164
        $attributeLabels['nickname'] = Yii::t('user', 'Nickname');
165
        $attributeLabels['type'] = Yii::t('user', 'Type');
166
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
167
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
168
        return $attributeLabels;
169
    }
170
}
171