Passed
Push — developer ( 762b64...a1cff0 )
by Radosław
20:19
created

ReferenceField::operatorE()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Reference query inventory field file.
4
 *
5
 * @package UIType
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author Radosław Skrzypczak <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Conditions\QueryFields\Inventory;
15
16
/**
17
 * Reference query inventory field class.
18
 */
19
class ReferenceField extends BaseField
20
{
21
	/**
22
	 * Get related column name.
23
	 *
24
	 * @return string[]
25
	 */
26
	public function getRelatedTableName(): array
27
	{
28
		return $this->getRelatedTables($this->fieldModel->getReferenceModules(), $this->fieldModel->getName());
0 ignored issues
show
Bug introduced by
The method getReferenceModules() does not exist on Vtiger_Basic_InventoryField. It seems like you code against a sub-type of Vtiger_Basic_InventoryField such as Vtiger_Reference_InventoryField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		return $this->getRelatedTables($this->fieldModel->/** @scrutinizer ignore-call */ getReferenceModules(), $this->fieldModel->getName());
Loading history...
29
	}
30
31
	/**
32
	 * Get formatted column references from related records.
33
	 *
34
	 * @param array  $modules
35
	 * @param string $fieldName
36
	 *
37
	 * @return string[]
38
	 */
39
	public function getRelatedTables(array $modules, string $fieldName): array
40
	{
41
		$relatedTableName = [];
42
		if ($modules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $modules 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...
43
			$moduleName = $this->queryGenerator->getModuleModel();
44
			$invTableName = $moduleName->getInventoryModel()->getDataTableName();
45
			$tableName = $moduleName->getBaseTableName();
46
			$tableIndex = $moduleName->getBaseTableIndex();
47
			$this->queryGenerator->addJoin(['LEFT JOIN', $invTableName, "{$tableName}.{$tableIndex} = {$invTableName}.crmid"]);
48
		}
49
		foreach ($modules as $moduleName) {
50
			$formattedTables = [];
51
			$entityFieldInfo = \App\Module::getEntityInfo($moduleName);
52
			$relatedModuleModel = \Vtiger_Module_Model::getInstance($moduleName);
53
			$relTableIndexes = $relatedModuleModel->getEntityInstance()->tab_name_index;
54
			foreach ($entityFieldInfo['fieldnameArr'] as $column) {
55
				if ($relField = $relatedModuleModel->getFieldByColumn($column)) {
56
					$referenceTable = $relField->getTableName() . $fieldName;
57
					$this->queryGenerator->addJoin(['LEFT JOIN',
58
						"{$relField->getTableName()} {$referenceTable}",
59
						"{$this->getColumnName()} = {$referenceTable}.{$relTableIndexes[$relField->getTableName()]}",
60
					]);
61
					$formattedTables[] = "{$referenceTable}.{$column}";
62
				}
63
			}
64
			$relatedTableName[$moduleName] = \count($formattedTables) > 1 ? new \yii\db\Expression('CONCAT(' . implode(",' ',", $formattedTables) . ')') : current($formattedTables);
65
		}
66
		return $relatedTableName;
67
	}
68
69
	/**
70
	 * Auto operator.
71
	 *
72
	 * @return array
73
	 */
74
	public function operatorA(): array
75
	{
76
		if (\App\Config::performance('SEARCH_REFERENCE_BY_AJAX')) {
77
			if (false === strpos($this->value, '##')) {
78
				return [$this->getColumnName() => $this->value];
79
			}
80
			$condition = ['or'];
81
			foreach (explode('##', $this->value) as $value) {
82
				$condition[] = [$this->getColumnName() => $value];
83
			}
84
			return $condition;
85
		}
86
		return parent::operatorA();
87
	}
88
89
	/**
90
	 * Equals operator.
91
	 *
92
	 * @return array
93
	 */
94
	public function operatorE(): array
95
	{
96
		$condition = ['or'];
97
		foreach ($this->getRelatedTableName() as $formattedName) {
98
			$condition[] = ['=', $formattedName, $this->getValue()];
99
		}
100
		return $condition;
101
	}
102
103
	/**
104
	 * Equals Id operator.
105
	 *
106
	 * @return array
107
	 */
108
	public function operatorEid()
109
	{
110
		return [$this->getColumnName() => $this->getValue()];
111
	}
112
113
	/**
114
	 * Not equal operator.
115
	 *
116
	 * @return array
117
	 */
118
	public function operatorN(): array
119
	{
120
		$condition = ['or'];
121
		foreach ($this->getRelatedTableName() as $formattedName) {
122
			$condition[] = ['<>', $formattedName, $this->getValue()];
123
		}
124
		return $condition;
125
	}
126
127
	/**
128
	 * Starts with operator.
129
	 *
130
	 * @return array
131
	 */
132
	public function operatorS()
133
	{
134
		$condition = ['or'];
135
		foreach ($this->getRelatedTableName() as $formattedName) {
136
			$condition[] = ['like', $formattedName, $this->getValue() . '%', false];
0 ignored issues
show
Bug introduced by
Are you sure $this->getValue() of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
			$condition[] = ['like', $formattedName, /** @scrutinizer ignore-type */ $this->getValue() . '%', false];
Loading history...
137
		}
138
		return $condition;
139
	}
140
141
	/**
142
	 * Ends with operator.
143
	 *
144
	 * @return array
145
	 */
146
	public function operatorEw()
147
	{
148
		$condition = ['or'];
149
		foreach ($this->getRelatedTableName() as $formattedName) {
150
			$condition[] = ['like', $formattedName, '%' . $this->getValue(), false];
0 ignored issues
show
Bug introduced by
Are you sure $this->getValue() of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
			$condition[] = ['like', $formattedName, '%' . /** @scrutinizer ignore-type */ $this->getValue(), false];
Loading history...
151
		}
152
		return $condition;
153
	}
154
155
	/**
156
	 * Contains operator.
157
	 *
158
	 * @return array
159
	 */
160
	public function operatorC(): array
161
	{
162
		$condition = ['or'];
163
		foreach ($this->getRelatedTableName() as $formattedName) {
164
			$condition[] = ['like', $formattedName, $this->getValue()];
165
		}
166
		return $condition;
167
	}
168
169
	/**
170
	 * Does not contain operator.
171
	 *
172
	 * @return array
173
	 */
174
	public function operatorK(): array
175
	{
176
		$condition = ['or'];
177
		foreach ($this->getRelatedTableName() as $formattedName) {
178
			$condition[] = ['not like', $formattedName, $this->getValue()];
179
		}
180
		return $condition;
181
	}
182
183
	/**
184
	 * Is empty operator.
185
	 *
186
	 * @return array
187
	 */
188
	public function operatorY(): array
189
	{
190
		return ['or',
191
			[$this->getColumnName() => null],
192
			['=', $this->getColumnName(), ''],
193
			['=', $this->getColumnName(), 0],
194
		];
195
	}
196
197
	/**
198
	 * Is not empty operator.
199
	 *
200
	 * @return array
201
	 */
202
	public function operatorNy(): array
203
	{
204
		return ['and',
205
			['not', [$this->getColumnName() => null]],
206
			['<>', $this->getColumnName(), ''],
207
			['<>', $this->getColumnName(), 0],
208
		];
209
	}
210
211
	/**
212
	 * Get order by.
213
	 *
214
	 * @param mixed $order
215
	 *
216
	 * @return array
217
	 */
218
	public function getOrderBy($order = false): array
219
	{
220
		$condition = [];
221
		if ($order && 'DESC' === strtoupper($order)) {
222
			foreach ($this->getRelatedTableName() as $formattedName) {
223
				$condition[(string) $formattedName] = SORT_DESC;
224
			}
225
		} else {
226
			foreach ($this->getRelatedTableName() as $formattedName) {
227
				$condition[(string) $formattedName] = SORT_ASC;
228
			}
229
		}
230
		return $condition;
231
	}
232
}
233