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

Vtiger_Reference_InventoryField::compare()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * Inventory Reference 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_Reference_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'Reference';
16
	protected $defaultLabel = 'LBL_REFERENCE';
17
	protected $columnName = 'ref';
18
	protected $dbType = 'int';
19
	protected $params = ['modules', 'mandatory'];
20
	protected $maximumLength = '-2147483648,2147483647';
21
	protected $purifyType = \App\Purifier::INTEGER;
22
23
	/** {@inheritdoc} */
24
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
25
	{
26
		if (empty($value)) {
27
			return '';
28
		}
29
		if (!($referenceModule = $this->getReferenceModule($value))) {
30
			return '<i class="color-red-500" title="' . \App\Purifier::encodeHtml($value) . '">' . \App\Language::translate('LBL_RECORD_DOES_NOT_EXIST') . '</i>';
31
		}
32
		$referenceModuleName = $referenceModule->getName();
33
		if ('Users' === $referenceModuleName || 'Groups' === $referenceModuleName) {
0 ignored issues
show
introduced by
The condition 'Groups' === $referenceModuleName is always false.
Loading history...
34
			return \App\Fields\Owner::getLabel($value);
35
		}
36
		if ($rawText) {
37
			return \App\Record::getLabel($value, $rawText);
38
		}
39
		return \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

39
		return \App\Record::getHtmlLink($value, /** @scrutinizer ignore-type */ $referenceModuleName, \App\Config::main('href_max_length'));
Loading history...
40
	}
41
42
	/** {@inheritdoc} */
43
	public function isMandatory()
44
	{
45
		$config = $this->getParamsConfig();
46
		return isset($config['mandatory']) ? 'false' !== $config['mandatory'] : true;
47
	}
48
49
	/**
50
	 * Function to get the Display Value, for the current field type with given DB Insert Value.
51
	 *
52
	 * @param mixed $record
53
	 *
54
	 * @return Vtiger_Module_Model|null
55
	 */
56
	public function getReferenceModule($record): ?Vtiger_Module_Model
57
	{
58
		if (!empty($record)) {
59
			$referenceModuleList = $this->getReferenceModules();
60
			$referenceEntityType = vtlib\Functions::getCRMRecordMetadata($record)['setype'] ?? '';
61
			if (!empty($referenceModuleList) && \in_array($referenceEntityType, $referenceModuleList)) {
62
				return Vtiger_Module_Model::getInstance($referenceEntityType);
63
			}
64
			if (!empty($referenceModuleList) && \in_array('Users', $referenceModuleList)) {
65
				return Vtiger_Module_Model::getInstance('Users');
66
			}
67
		}
68
		return null;
69
	}
70
71
	/**
72
	 * Function to get reference modules.
73
	 *
74
	 * @return array
75
	 */
76
	public function getReferenceModules()
77
	{
78
		$values = $this->getParamsConfig()['modules'] ?? [];
79
		if (\is_string($values)) {
80
			$values = explode(' |##| ', $values);
81
		}
82
83
		return $values;
84
	}
85
86
	/** {@inheritdoc} */
87
	public function getDBValue($value, ?string $name = '')
88
	{
89
		return (int) $value;
90
	}
91
92
	/** {@inheritdoc} */
93
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
94
	{
95
		if ((empty($value) && $this->isMandatory()) || ($value && !is_numeric($value))) {
96
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406);
97
		}
98
		$rangeValues = explode(',', $this->maximumLength);
99
		if (!empty($value) && ($rangeValues[1] < $value || $rangeValues[0] > $value)) {
100
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$value", 406);
101
		}
102
	}
103
104
	/** {@inheritdoc} */
105
	public function getConfigFieldsData(): array
106
	{
107
		$qualifiedModuleName = 'Settings:LayoutEditor';
108
		$data = parent::getConfigFieldsData();
109
110
		$data['modules'] = [
111
			'name' => 'modules',
112
			'label' => 'LBL_PARAMS_MODULES',
113
			'uitype' => 33,
114
			'maximumlength' => '25',
115
			'typeofdata' => 'V~M',
116
			'purifyType' => \App\Purifier::ALNUM,
117
			'picklistValues' => []
118
		];
119
		foreach (Vtiger_Module_Model::getAll([0], [], true) as $module) {
120
			$data['modules']['picklistValues'][$module->getName()] = \App\Language::translate($module->getName(), $module->getName());
121
		}
122
123
		$data['mandatory'] = [
124
			'name' => 'mandatory',
125
			'label' => 'LBL_PARAMS_MANDATORY',
126
			'uitype' => 16,
127
			'maximumlength' => '5',
128
			'typeofdata' => 'V~M',
129
			'purifyType' => \App\Purifier::STANDARD,
130
			'defaultvalue' => 'true',
131
			'picklistValues' => [
132
				'false' => \App\Language::translate('LBL_NO', $qualifiedModuleName),
133
				'true' => \App\Language::translate('LBL_YES', $qualifiedModuleName),
134
			],
135
		];
136
137
		return $data;
138
	}
139
140
	/** {@inheritdoc} */
141
	public function compare($value, $prevValue, string $column): bool
142
	{
143
		return (int) $value === (int) $prevValue;
144
	}
145
}
146