Completed
Push — master ( af05a7...eda6ec )
by Nekrasov
02:11
created

UserQuery::groupsMustBeSelected()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 3
eloc 2
nc 3
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Queries;
4
5
use Arrilot\BitrixModels\Collection;
6
use Arrilot\BitrixModels\Models\UserModel;
7
8
/**
9
 * @method UserQuery fromGroup($groupId)
10
 */
11
class UserQuery extends BaseQuery
12
{
13
    /**
14
     * Query sort.
15
     *
16
     * @var array
17
     */
18
    public $sort = ['last_name' => 'asc'];
19
20
    /**
21
     * List of standard entity fields.
22
     *
23
     * @var array
24
     */
25
    protected $standardFields = [
26
        'ID',
27
        'PERSONAL_WWW',
28
        'PERSONAL_ZIP',
29
        'IS_ONLINE',
30
        'ACTIVE',
31
        'PERSONAL_ICQ',
32
        'PERSONAL_COUNTRY',
33
        'WORK_CITY',
34
        'LAST_LOGIN',
35
        'PERSONAL_GENDER',
36
        'PERSONAL_NOTES',
37
        'WORK_STATE',
38
        'LOGIN',
39
        'PERSONAL_PHOTO',
40
        'WORK_COMPANY',
41
        'WORK_ZIP',
42
        'EMAIL',
43
        'PERSONAL_PHONE',
44
        'WORK_DEPARTMENT',
45
        'WORK_COUNTRY',
46
        'NAME',
47
        'PERSONAL_FAX',
48
        'WORK_POSITION',
49
        'WORK_PROFILE',
50
        'LAST_NAME',
51
        'PERSONAL_MOBILE',
52
        'WORK_WWW',
53
        'WORK_NOTES',
54
        'SECOND_NAME',
55
        'PERSONAL_PAGER',
56
        'WORK_PHONE',
57
        'ADMIN_NOTES',
58
        'TIMESTAMP_X',
59
        'PERSONAL',
60
        'STREET',
61
        'WORK_FAX',
62
        'XML_ID',
63
        'PERSONAL_BIRTHDAY',
64
        'PERSONAL_MAILBOX',
65
        'WORK_PAGER',
66
        'LAST_NAME',
67
        'DATE_REGISTER',
68
        'PERSONAL_CITY',
69
        'WORK_STREET',
70
        'SECOND_NAME',
71
        'PERSONAL_PROFESSION',
72
        'PERSONAL_STATE',
73
        'WORK_MAILBOX',
74
        'STORED_HASH',
75
        'CHECKWORD_TIME',
76
        'EXTERNAL_AUTH_ID',
77
        'CONFIRM_CODE',
78
        'LOGIN_ATTEMPTS',
79
        'LAST_ACTIVITY_DATE',
80
        'AUTO_TIME_ZONE',
81
        'TIME_ZONE',
82
        'PASSWORD',
83
        'CHECKWORD',
84
        'LID',
85
    ];
86
87
    /**
88
     * Get the collection of users according to the current query.
89
     *
90
     * @return Collection
91
     */
92
    public function getList()
93
    {
94
        if ($this->queryShouldBeStopped) {
95
            return new Collection();
96
        }
97
98
        $params = [
99
            'SELECT'     => $this->propsMustBeSelected() ? ['UF_*'] : false,
100
            'NAV_PARAMS' => $this->navigation,
101
            'FIELDS'     => $this->normalizeSelect(),
102
        ];
103
104
        $users = [];
105
        $rsUsers = $this->bxObject->getList($this->sort, $sortOrder = false, $this->normalizeFilter(), $params);
106
        while ($arUser = $rsUsers->fetch()) {
107
            if ($this->groupsMustBeSelected()) {
108
                $arUser['GROUP_ID'] = $this->bxObject->getUserGroup($arUser['ID']);
109
            }
110
111
            $this->addItemToResultsUsingKeyBy($users, new $this->modelName($arUser['ID'], $arUser));
112
        }
113
114
        return new Collection($users);
115
    }
116
117
    /**
118
     * Get the first user with a given login.
119
     *
120
     * @param string $login
121
     *
122
     * @return UserModel
123
     */
124
    public function getByLogin($login)
125
    {
126
        $this->filter['LOGIN_EQUAL_EXACT'] = $login;
127
128
        return $this->first();
129
    }
130
131
    /**
132
     * Get the first user with a given email.
133
     *
134
     * @param string $email
135
     *
136
     * @return UserModel
137
     */
138
    public function getByEmail($email)
139
    {
140
        $this->filter['EMAIL'] = $email;
141
142
        return $this->first();
143
    }
144
145
    /**
146
     * Get count of users according the current query.
147
     *
148
     * @return int
149
     */
150
    public function count()
151
    {
152
        if ($this->queryShouldBeStopped) {
153
            return 0;
154
        }
155
156
        return (int) $this->bxObject->getList($order = 'ID', $by = 'ASC', $this->normalizeFilter(), [
157
            'NAV_PARAMS' => [
158
                'nTopCount' => 0,
159
            ],
160
        ])->NavRecordCount;
161
    }
162
163
    /**
164
     * Determine if groups must be selected.
165
     *
166
     * @return bool
167
     */
168
    protected function groupsMustBeSelected()
169
    {
170
        return in_array('GROUPS', $this->select) || in_array('GROUP_ID', $this->select) || in_array('GROUPS_ID', $this->select);
171
    }
172
173
    /**
174
     * Normalize filter before sending it to getList.
175
     * This prevents some inconsistency.
176
     *
177
     * @return array
178
     */
179
    protected function normalizeFilter()
180
    {
181
        $this->substituteField($this->filter, 'GROUPS', 'GROUPS_ID');
182
        $this->substituteField($this->filter, 'GROUP_ID', 'GROUPS_ID');
183
184
        return $this->filter;
185
    }
186
187
    /**
188
     * Normalize select before sending it to getList.
189
     * This prevents some inconsistency.
190
     *
191
     * @return array
192
     */
193
    protected function normalizeSelect()
194
    {
195
        if ($this->fieldsMustBeSelected()) {
196
            $this->select = array_merge($this->standardFields, $this->select);
197
        }
198
199
        return $this->clearSelectArray();
200
    }
201
}
202