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

SharedOwnerField   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
eloc 46
c 0
b 0
f 0
dl 0
loc 130
ccs 0
cts 42
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A operatorOm() 0 4 1
A operatorE() 0 6 2
A operatorOgr() 0 4 1
A getColumnName() 0 11 3
A operatorN() 0 6 2
A operatorC() 0 3 1
A operatorNy() 0 12 2
A operatorOgu() 0 13 3
A operatorY() 0 12 2
1
<?php
2
3
namespace App\Conditions\QueryFields;
4
5
/**
6
 * Shared 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 SharedOwnerField extends BaseField
16
{
17
	/** {@inheritdoc} */
18
	public function getColumnName(): string
19
	{
20
		if ($this->fullColumnName) {
21
			return $this->fullColumnName;
22
		}
23
		if ($this->related) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->related of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
24
			$fieldModel = $this->queryGenerator->getModuleField($this->related['sourceField']);
25
			return $this->fullColumnName = "{$fieldModel->getTableName()}.{$fieldModel->getColumnName()}";
26
		}
27
		$focus = $this->queryGenerator->getEntityModel();
28
		return $this->fullColumnName = "{$focus->table_name}.{$focus->table_index}";
29
	}
30
31
	/**
32
	 * Equals operator.
33
	 *
34
	 * @return array
35
	 */
36
	public function operatorE(): array
37
	{
38
		if (empty($this->value)) {
39
			return [];
40
		}
41
		return [$this->getColumnName() => (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')->where(['userid' => explode('##', $this->value)])];
42
	}
43
44
	/**
45
	 * Not equal operator.
46
	 *
47
	 * @return array
48
	 */
49
	public function operatorN(): array
50
	{
51
		if (empty($this->value)) {
52
			return [];
53
		}
54
		return ['NOT IN', $this->getColumnName(), (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')->where(['userid' => explode('##', $this->value)])];
55
	}
56
57
	/**
58
	 * Currently logged user.
59
	 *
60
	 * @return array
61
	 */
62
	public function operatorOm()
63
	{
64
		$this->value = \App\User::getCurrentUserId();
65
		return $this->operatorE();
66
	}
67
68
	/**
69
	 * Currently logged-in user groups.
70
	 *
71
	 * @return array
72
	 */
73
	public function operatorOgr(): array
74
	{
75
		$this->value = implode('##', array_keys(\App\Fields\Owner::getInstance($this->getModuleName())->getGroups(false, 'private')));
76
		return $this->operatorE();
77
	}
78
79
	/**
80
	 * Users who belong to the same group as the currently logged in user.
81
	 *
82
	 * @return array
83
	 */
84
	public function operatorOgu(): array
85
	{
86
		$groups = \App\Fields\Owner::getInstance($this->getModuleName())->getGroups(false, 'private');
87
		if ($groups) {
88
			$condition = ['or'];
89
			$query = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners');
90
			foreach (array_keys($groups) as $groupId) {
91
				$condition[] = [$this->getColumnName() => $query->where(['u_#__crmentity_showners.userid' => (new \App\Db\Query())->select(['userid'])->from(["condition_groups_{$groupId}_" . \App\Layout::getUniqueId() => \App\PrivilegeUtil::getQueryToUsersByGroup((int) $groupId)])])];
92
			}
93
		} else {
94
			$condition = [$this->getColumnName() => (new \yii\db\Expression('0=1'))];
95
		}
96
		return $condition;
97
	}
98
99
	/**
100
	 * Contains operator.
101
	 *
102
	 * @return array
103
	 */
104
	public function operatorC(): array
105
	{
106
		return $this->operatorE();
107
	}
108
109
	/**
110
	 * Is not empty operator.
111
	 *
112
	 * @return array
113
	 */
114
	public function operatorNy(): array
115
	{
116
		if ($this->related) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->related of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
			$fieldModel = $this->queryGenerator->getModuleField($this->related['sourceField']);
118
			$query = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')
119
				->innerJoin($fieldModel->getTableName(), "u_#__crmentity_showners.crmid={$fieldModel->getTableName()}.{$fieldModel->getColumnName()}");
120
		} else {
121
			$focus = $this->queryGenerator->getEntityModel();
122
			$query = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')
123
				->innerJoin($focus->table_name, "u_#__crmentity_showners.crmid={$focus->table_name}.{$focus->table_index}");
124
		}
125
		return [$this->getColumnName() => $query];
126
	}
127
128
	/**
129
	 * Is empty operator.
130
	 *
131
	 * @return array
132
	 */
133
	public function operatorY(): array
134
	{
135
		if ($this->related) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->related of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
			$fieldModel = $this->queryGenerator->getModuleField($this->related['sourceField']);
137
			$query = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')
138
				->innerJoin($fieldModel->getTableName(), "u_#__crmentity_showners.crmid={$fieldModel->getTableName()}.{$fieldModel->getColumnName()}");
139
		} else {
140
			$focus = $this->queryGenerator->getEntityModel();
141
			$query = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')
142
				->innerJoin($focus->table_name, "u_#__crmentity_showners.crmid={$focus->table_name}.{$focus->table_index}");
143
		}
144
		return ['NOT IN', $this->getColumnName(), $query];
145
	}
146
}
147