Passed
Push — developer ( 5a7d78...6a35f4 )
by Radosław
17:06
created

Vtiger_Name_InventoryField::isRequired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
24
	/** {@inheritdoc} */
25
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
26
	{
27
		if (empty($value)) {
28
			return '';
29
		}
30
		if (!($referenceModule = $this->getReferenceModule($value))) {
31
			return '<i class="color-red-500" title="' . \App\Purifier::encodeHtml($value) . '">' . \App\Language::translate('LBL_RECORD_DOES_NOT_EXIST', 'ModTracker') . '</i>';
32
		}
33
		$referenceModuleName = $referenceModule->getName();
34
		if ('Users' === $referenceModuleName || 'Groups' === $referenceModuleName) {
0 ignored issues
show
introduced by
The condition 'Groups' === $referenceModuleName is always false.
Loading history...
35
			return \App\Fields\Owner::getLabel($value);
36
		}
37
		if ($rawText) {
38
			return \App\Record::getLabel($value, $rawText);
39
		}
40
		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

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