Passed
Pull Request — developer (#15979)
by Arkadiusz
18:25
created

OwnerField::operatorOgu()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 2
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
namespace App\Conditions\QueryFields;
4
5
/**
6
 * Owner Query Field Class.
7
 *
8
 * @package UIType
9
 *
10
 * @copyright YetiForce S.A.
11
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
12
 * @author    Mariusz Krzaczkowski <[email protected]>
13
 * @author    Radosław Skrzypczak <[email protected]>
14
 */
15
class OwnerField extends BaseField
16
{
17
	/**
18
	 * Equals operator.
19
	 *
20
	 * @return array
21
	 */
22
	public function operatorE(): array
23
	{
24
		if (!\is_array($this->value)) {
25
			$this->value = explode('##', $this->value);
26
		}
27
		$condition = ['or'];
28
		foreach ($this->value as $value) {
29
			$condition[] = [$this->getColumnName() => $this->getMemberValue($value)];
30
		}
31
		return $condition;
32
	}
33
34
	/**
35
	 * Not equal operator.
36
	 *
37
	 * @return array
38
	 */
39
	public function operatorN(): array
40
	{
41
		if (!\is_array($this->value)) {
42
			$this->value = explode('##', $this->value);
43
		}
44
		$condition = ['and'];
45
		foreach ($this->value as $value) {
46
			$condition[] = ['not in', $this->getColumnName(), $this->getMemberValue($value)];
47
		}
48
		return $condition;
49
	}
50
51
	/**
52
	 * Gets conditions for member.
53
	 *
54
	 * @param int|string $member
55
	 *
56
	 * @return \App\Db\Query|int
57
	 */
58
	public function getMemberValue($member)
59
	{
60
		if (is_numeric($member)) {
61
			return $member;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $member also could return the type string which is incompatible with the documented return type App\Db\Query|integer.
Loading history...
62
		}
63
		[$type, $id] = explode(':', $member);
64
		switch ($type) {
65
			case \App\PrivilegeUtil::MEMBER_TYPE_GROUPS:
66
				$value = (new \App\Db\Query())->select(['userid'])->from(["condition_{$type}_{$id}_" . \App\Layout::getUniqueId() => \App\PrivilegeUtil::getQueryToUsersByGroup((int) $id)]);
67
				break;
68
			case \App\PrivilegeUtil::MEMBER_TYPE_ROLES:
69
				$value = \App\PrivilegeUtil::getQueryToUsersByRole($id);
70
				break;
71
			case \App\PrivilegeUtil::MEMBER_TYPE_ROLE_AND_SUBORDINATES:
72
				$value = \App\PrivilegeUtil::getQueryToUsersByRoleAndSubordinate($id);
73
				break;
74
			default:
75
				$value = -1;
76
				break;
77
		}
78
		return $value;
79
	}
80
81
	/**
82
	 * Currently logged user.
83
	 *
84
	 * @return array
85
	 */
86
	public function operatorOm()
87
	{
88
		return [$this->getColumnName() => \App\User::getCurrentUserId()];
89
	}
90
91
	/**
92
	 * Currently logged-in user groups.
93
	 *
94
	 * @return array
95
	 */
96
	public function operatorOgr(): array
97
	{
98
		$groups = \App\Fields\Owner::getInstance($this->getModuleName())->getGroups(false, 'private');
99
		return [$this->getColumnName() => \array_keys($groups)];
100
	}
101
102
	/**
103
	 * Users who belong to the same group as the currently logged in user.
104
	 *
105
	 * @return array
106
	 */
107
	public function operatorOgu(): array
108
	{
109
		$groups = \App\Fields\Owner::getInstance($this->getModuleName())->getGroups(false, 'private');
110
		if ($groups) {
111
			$condition = ['or'];
112
			foreach (array_keys($groups)  as $idGroup) {
113
				$condition[] = [$this->getColumnName() => (new \App\Db\Query())->select(['userid'])->from(["condition_groups_{$idGroup}_" . \App\Layout::getUniqueId() => \App\PrivilegeUtil::getQueryToUsersByGroup((int) $idGroup)])];
114
			}
115
		} else {
116
			$condition = [$this->getColumnName() => (new \yii\db\Expression('0=1'))];
117
		}
118
		return $condition;
119
	}
120
121
	/**
122
	 * Watched record.
123
	 *
124
	 * @return array
125
	 */
126
	public function operatorWr()
127
	{
128
		$watchdog = \Vtiger_Watchdog_Model::getInstance($this->getModuleName());
129
		$condition = [];
130
		if ($watchdog->isActive()) {
131
			$this->queryGenerator->addJoin(['LEFT JOIN', 'u_#__watchdog_record', 'vtiger_crmentity.crmid = u_#__watchdog_record.record']);
132
			if ($watchdog->isWatchingModule()) {
133
				$condition = ['or', ['u_#__watchdog_record.record' => null], ['not', ['u_#__watchdog_record.userid' => $watchdog->get('userId'), 'u_#__watchdog_record.state' => 0]]];
134
			} else {
135
				$condition = ['u_#__watchdog_record.state' => 1, 'u_#__watchdog_record.userid' => $watchdog->get('userId')];
136
			}
137
		}
138
		return $condition;
139
	}
140
141
	/**
142
	 * Watched record not.
143
	 *
144
	 * @return array
145
	 */
146
	public function operatorNwr()
147
	{
148
		$watchdog = \Vtiger_Watchdog_Model::getInstance($this->getModuleName());
149
		$condition = [];
150
		if ($watchdog->isActive()) {
151
			$this->queryGenerator->addJoin(['LEFT JOIN', 'u_#__watchdog_record', 'vtiger_crmentity.crmid = u_#__watchdog_record.record']);
152
			if ($watchdog->isWatchingModule()) {
153
				$condition = ['u_#__watchdog_record.userid' => $watchdog->get('userId'), 'u_#__watchdog_record.state' => 0];
154
			} else {
155
				$condition = ['or', ['u_#__watchdog_record.record' => null], ['not', ['u_#__watchdog_record.userid' => $watchdog->get('userId'), 'u_#__watchdog_record.state' => 1]]];
156
			}
157
		}
158
		return $condition;
159
	}
160
161
	/**
162
	 * Get order by.
163
	 *
164
	 * @param mixed $order
165
	 *
166
	 * @return array
167
	 */
168
	public function getOrderBy($order = false): array
169
	{
170
		$this->queryGenerator->addJoin(['LEFT JOIN', 'vtiger_users', 'vtiger_users.id = ' . $this->getColumnName()]);
171
		$this->queryGenerator->addJoin(['LEFT JOIN', 'vtiger_groups', 'vtiger_groups.groupid = ' . $this->getColumnName()]);
172
		if ($order && 'DESC' === strtoupper($order)) {
173
			return ['vtiger_users.last_name' => SORT_DESC, 'vtiger_users.first_name' => SORT_DESC, 'vtiger_groups.groupname' => SORT_DESC];
174
		}
175
		return ['vtiger_users.last_name' => SORT_ASC, 'vtiger_users.first_name' => SORT_ASC, 'vtiger_groups.groupname' => SORT_ASC];
176
	}
177
178
	/**
179
	 * Is not empty operator.
180
	 *
181
	 * @return array
182
	 */
183
	public function operatorNy(): array
184
	{
185
		return ['and',
186
			['not', [$this->getColumnName() => null]],
187
			['<>', $this->getColumnName(), 0],
188
		];
189
	}
190
191
	/**
192
	 * Is empty operator.
193
	 *
194
	 * @return array
195
	 */
196
	public function operatorY(): array
197
	{
198
		return ['or',
199
			[$this->getColumnName() => null],
200
			['=', $this->getColumnName(), 0],
201
		];
202
	}
203
204
	/**
205
	 * Not Currently logged user.
206
	 *
207
	 * @return array
208
	 */
209
	public function operatorNom()
210
	{
211
		return ['<>', $this->getColumnName(), \App\User::getCurrentUserId()];
212
	}
213
}
214