Passed
Push — developer ( 3f9034...c0f71b )
by Radosław
17:01
created

getDbConditionBuilderValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * Inventory Name Field Class.
5
 *
6
 * @package   InventoryField
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 * @author    Radosław Skrzypczak <[email protected]>
12
 */
13
class Vtiger_Name_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'Name';
16
	protected $defaultLabel = 'LBL_ITEM_NAME';
17
	protected $columnName = 'name';
18
	protected $dbType = 'int DEFAULT 0';
19
	protected $params = ['modules', 'limit', 'mandatory'];
20
	protected $colSpan = 30;
21
	protected $maximumLength = '-2147483648,2147483647';
22
	protected $purifyType = \App\Purifier::INTEGER;
23
	/** {@inheritdoc} */
24
	protected $searchable = true;
25
	/** {@inheritdoc} */
26
	protected $queryOperators = ['e', 'n', 's', 'ew', 'c', 'k', 'y', 'ny'];
27
	/** {@inheritdoc} */
28
	protected $recordOperators = ['e', 'n', 's', 'ew', 'c', 'k', 'y', 'ny'];
29
30
	/** {@inheritdoc} */
31
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
32
	{
33
		if (empty($value)) {
34
			return '';
35
		}
36
		if (!($referenceModule = $this->getReferenceModule($value))) {
37
			return '<i class="color-red-500" title="' . \App\Purifier::encodeHtml($value) . '">' . \App\Language::translate('LBL_RECORD_DOES_NOT_EXIST', 'ModTracker') . '</i>';
38
		}
39
		$referenceModuleName = $referenceModule->getName();
40
		if ('Users' === $referenceModuleName || 'Groups' === $referenceModuleName) {
0 ignored issues
show
introduced by
The condition 'Groups' === $referenceModuleName is always false.
Loading history...
41
			return \App\Fields\Owner::getLabel($value);
42
		}
43
		if ($rawText) {
44
			return \App\Record::getLabel($value, $rawText);
45
		}
46
		return "<span class=\"yfm-{$referenceModuleName} mr-1\"></span>" . \App\Record::getHtmlLink($value, $referenceModuleName, \App\Config::main('href_max_length'));
0 ignored issues
show
Bug introduced by
$referenceModuleName of type boolean is incompatible with the type null|string expected by parameter $moduleName of App\Record::getHtmlLink(). ( Ignorable by Annotation )

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

46
		return "<span class=\"yfm-{$referenceModuleName} mr-1\"></span>" . \App\Record::getHtmlLink($value, /** @scrutinizer ignore-type */ $referenceModuleName, \App\Config::main('href_max_length'));
Loading history...
47
	}
48
49
	/** {@inheritdoc} */
50
	public function getDbConditionBuilderValue($value, string $operator)
51
	{
52
		return \App\Purifier::decodeHtml($value);
53
	}
54
55
	/**
56
	 * Function to get the Display Value, for the current field type with given DB Insert Value.
57
	 *
58
	 * @param mixed $record
59
	 *
60
	 * @return Vtiger_Module_Model|null
61
	 */
62
	public function getReferenceModule($record): ?Vtiger_Module_Model
63
	{
64
		if (!empty($record)) {
65
			$referenceModuleList = $this->getModules(false);
66
			$referenceModuleList = !\is_array($referenceModuleList) ? [$referenceModuleList] : $referenceModuleList;
0 ignored issues
show
introduced by
The condition is_array($referenceModuleList) is always true.
Loading history...
67
			$referenceEntityType = vtlib\Functions::getCRMRecordMetadata($record)['setype'] ?? '';
68
			if (!empty($referenceModuleList) && \in_array($referenceEntityType, $referenceModuleList)) {
69
				return Vtiger_Module_Model::getInstance($referenceEntityType);
70
			}
71
			if (!empty($referenceModuleList) && \in_array('Users', $referenceModuleList)) {
72
				return Vtiger_Module_Model::getInstance('Users');
73
			}
74
		}
75
		return null;
76
	}
77
78
	/** {@inheritdoc} */
79
	public function isMandatory()
80
	{
81
		return true;
82
	}
83
84
	/** {@inheritdoc} */
85
	public function getDBValue($value, ?string $name = '')
86
	{
87
		return (int) $value;
88
	}
89
90
	/** {@inheritdoc} */
91
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
92
	{
93
		if ((empty($value) && $this->isMandatory()) || ($value && !is_numeric($value))) {
94
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406);
95
		}
96
		if (!\App\Record::isExists($value)) {
97 2
			throw new \App\Exceptions\AppException("ERR_RECORD_NOT_FOUND||$value||$columnName", 406);
98
		}
99 2
		$rangeValues = explode(',', $this->maximumLength);
100
		if ($value && ($rangeValues[1] < $value || $rangeValues[0] > $value)) {
101
			throw new \App\Exceptions\AppException("ERR_VALUE_IS_TOO_LONG||$columnName||$value", 406);
102
		}
103
	}
104
105 2
	/** {@inheritdoc} */
106
	public function isRequired()
107 2
	{
108
		$config = $this->getParamsConfig();
109
		return isset($config['mandatory']) ? 'false' !== $config['mandatory'] : true;
110 2
	}
111 2
112
	/**
113
	 * Gets URL for mass selection.
114 2
	 *
115
	 * @param string $moduleName
116
	 *
117
	 * @return string
118
	 */
119 2
	public function getUrlForMassSelection(string $moduleName): string
120
	{
121 2
		$view = 'RecordsList';
122 2
		if (\App\Config::module($moduleName, 'inventoryMassAddEntriesTreeView') && ($field = current(Vtiger_Module_Model::getInstance($moduleName)->getFieldsByType('tree', true))) && $field->isViewable()) {
123
			$view = 'TreeInventoryModal';
124
		}
125
		return "index.php?module={$moduleName}&view={$view}&src_module={$this->getModuleName()}&multi_select=true";
126
	}
127
128
	/**
129
	 * Get modules.
130
	 *
131
	 * @param bool $permissions
132
	 *
133
	 * @return array
134
	 */
135
	public function getModules(bool $permissions = true): array
136
	{
137
		$modules = $this->getParamsConfig()['modules'] ?? [];
138
		if (\is_string($modules)) {
139
			$modules = explode(' |##| ', $modules);
140
		}
141
		return $permissions ? array_filter($modules, fn ($moduleName) => \App\Privilege::isPermitted($moduleName)) : $modules;
142
	}
143
144
	/** {@inheritdoc} */
145
	public function getConfigFieldsData(): array
146
	{
147
		$qualifiedModuleName = 'Settings:LayoutEditor';
148
		$data = parent::getConfigFieldsData();
149
150
		$data['modules'] = [
151
			'name' => 'modules',
152
			'label' => 'LBL_PARAMS_MODULES',
153
			'uitype' => 33,
154
			'maximumlength' => '25',
155
			'typeofdata' => 'V~M',
156
			'purifyType' => \App\Purifier::ALNUM,
157
			'picklistValues' => []
158
		];
159
		foreach (Vtiger_Module_Model::getAll([0], [], true) as $module) {
160
			$data['modules']['picklistValues'][$module->getName()] = \App\Language::translate($module->getName(), $module->getName());
161
		}
162
163
		$grossLabel = self::getInstance($this->getModuleName(), 'GrossPrice')->getDefaultLabel();
164
		$data['limit'] = [
165
			'name' => 'limit',
166
			'label' => 'LBL_PARAMS_LIMIT',
167
			'uitype' => 16,
168
			'maximumlength' => '1',
169
			'typeofdata' => 'V~M',
170
			'purifyType' => \App\Purifier::INTEGER,
171
			'tooltip' => \App\Language::translate('LBL_PARAMS_LIMIT_CONDITIONS', $qualifiedModuleName) . ': ' . \App\Language::translate($grossLabel, $qualifiedModuleName),
172
			'defaultvalue' => '0',
173
			'picklistValues' => [
174
				0 => \App\Language::translate('LBL_NO', $qualifiedModuleName),
175
				1 => \App\Language::translate('LBL_YES', $qualifiedModuleName)
176
			],
177
		];
178
		$data['mandatory'] = [
179
			'name' => 'mandatory',
180
			'label' => 'LBL_PARAMS_MANDATORY',
181
			'uitype' => 16,
182
			'maximumlength' => '5',
183
			'typeofdata' => 'V~M',
184
			'purifyType' => \App\Purifier::STANDARD,
185
			'tooltip' => 'LBL_EDIT_MANDATORY_INFO',
186
			'defaultvalue' => 'true',
187
			'picklistValues' => [
188
				'false' => \App\Language::translate('LBL_NO', $qualifiedModuleName),
189
				'true' => \App\Language::translate('LBL_YES', $qualifiedModuleName),
190
			],
191
		];
192
		return $data;
193
	}
194
195
	/** {@inheritdoc} */
196
	public function compare($value, $prevValue, string $column): bool
197
	{
198
		return (int) $value === (int) $prevValue;
199
	}
200
}
201