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

Vtiger_Reference_InventoryField::getDBValue()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
	/** {@inheritdoc} */
23
	protected $onlyOne = false;
24
	/** {@inheritdoc} */
25
	protected $searchable = true;
26
	/** {@inheritdoc} */
27
	protected $queryOperators = ['e', 'n', 's', 'ew', 'c', 'k', 'y', 'ny'];
28
	/** {@inheritdoc} */
29
	protected $recordOperators = ['e', 'n', 's', 'ew', 'c', 'k', 'y', 'ny'];
30
31
	/** {@inheritdoc} */
32
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
33
	{
34
		if (empty($value)) {
35
			return '';
36
		}
37
		if (!($referenceModule = $this->getReferenceModule($value))) {
38
			return '<i class="color-red-500" title="' . \App\Purifier::encodeHtml($value) . '">' . \App\Language::translate('LBL_RECORD_DOES_NOT_EXIST') . '</i>';
39
		}
40
		$referenceModuleName = $referenceModule->getName();
41
		if ('Users' === $referenceModuleName || 'Groups' === $referenceModuleName) {
0 ignored issues
show
introduced by
The condition 'Groups' === $referenceModuleName is always false.
Loading history...
42
			return \App\Fields\Owner::getLabel($value);
43
		}
44
		if ($rawText) {
45
			return \App\Record::getLabel($value, $rawText);
46
		}
47
		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

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