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

OwnerField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
eloc 44
c 0
b 0
f 0
dl 0
loc 102
ccs 0
cts 4
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A operatorWr() 0 3 1
A getMemberValue() 0 21 5
A operatorN() 0 13 4
A operatorOgu() 0 13 4
A operatorE() 0 13 4
A operatorNwr() 0 3 1
1
<?php
2
3
namespace App\Conditions\RecordFields;
4
5
/**
6
 * Owner condition record 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
 */
14
class OwnerField extends BaseField
15
{
16
	/**
17
	 * Is watching record operator.
18
	 *
19
	 * @return array
20
	 */
21
	public function operatorWr()
22
	{
23
		return \Vtiger_Watchdog_Model::getInstanceById($this->recordModel->getId(), $this->recordModel->getModuleName())->isWatchingRecord();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Vtiger_Watchdog_M...())->isWatchingRecord() returns the type boolean which is incompatible with the documented return type array.
Loading history...
24
	}
25
26
	/**
27
	 * Users who belong to the same group as the currently logged in user.
28
	 *
29
	 * @return bool
30
	 */
31
	public function operatorOgu(): bool
32
	{
33
		$result = false;
34
		$groups = \App\Fields\Owner::getInstance($this->recordModel->getModuleName())->getGroups(false, 'private');
35
		if ($groups) {
36
			foreach (array_keys($groups) as $groupId) {
37
				if (in_array($this->getValue(), \App\PrivilegeUtil::getUsersByGroup((int) $groupId))) {
38
					$result = true;
39
					break;
40
				}
41
			}
42
		}
43
		return $result;
44
	}
45
46
	/**
47
	 * Is not watching record operator.
48
	 *
49
	 * @return array
50
	 */
51
	public function operatorNwr()
52
	{
53
		return !\Vtiger_Watchdog_Model::getInstanceById($this->recordModel->getId(), $this->recordModel->getModuleName())->isWatchingRecord();
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! Vtiger_Watchdog...())->isWatchingRecord() returns the type boolean which is incompatible with the documented return type array.
Loading history...
54
	}
55
56
	/** {@inheritdoc} */
57
	public function operatorE()
58
	{
59
		if (!\is_array($this->value)) {
60
			$this->value = explode('##', $this->value);
61
		}
62
		$result = false;
63
		foreach ($this->value as $value) {
64
			if (\in_array($this->getValue(), $this->getMemberValue($value))) {
65
				$result = true;
66
				break;
67
			}
68
		}
69
		return $result;
70
	}
71
72
	/** {@inheritdoc} */
73
	public function operatorN()
74
	{
75
		if (!\is_array($this->value)) {
76
			$this->value = explode('##', $this->value);
77
		}
78
		$result = true;
79
		foreach ($this->value as $value) {
80
			if (\in_array($this->getValue(), $this->getMemberValue($value))) {
81
				$result = false;
82
				break;
83
			}
84
		}
85
		return $result;
86
	}
87
88
	/**
89
	 * Gets conditions for member.
90
	 *
91
	 * @param int|string $member
92
	 *
93
	 * @return int[]
94
	 */
95
	public function getMemberValue($member): array
96
	{
97
		if (is_numeric($member)) {
98
			return [$member];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($member) returns an array which contains values of type string which are incompatible with the documented value type integer.
Loading history...
99
		}
100
		[$type, $id] = explode(':', $member);
101
		switch ($type) {
102
			case \App\PrivilegeUtil::MEMBER_TYPE_GROUPS:
103
				$value = (new \App\Db\Query())->select(['userid'])->from(["condition_{$type}_{$id}_" . \App\Layout::getUniqueId() => \App\PrivilegeUtil::getQueryToUsersByGroup((int) $id)])->column();
104
				break;
105
			case \App\PrivilegeUtil::MEMBER_TYPE_ROLES:
106
				$value = \App\PrivilegeUtil::getQueryToUsersByRole($id)->column();
107
				break;
108
			case \App\PrivilegeUtil::MEMBER_TYPE_ROLE_AND_SUBORDINATES:
109
				$value = \App\PrivilegeUtil::getQueryToUsersByRoleAndSubordinate($id)->column();
110
				break;
111
			default:
112
				$value = [-1];
113
				break;
114
		}
115
		return $value;
116
	}
117
}
118