Passed
Push — developer ( a6dfc2...610d41 )
by Radosław
18:24
created

getConfigFieldsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 1
cts 1
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Inventory Currency 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_Currency_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'Currency';
16
	protected $defaultLabel = 'LBL_CURRENCY';
17
	protected $columnName = 'currency';
18
	protected $dbType = [\yii\db\Schema::TYPE_INTEGER, 11];
19
	protected $customColumn = [
20
		'currencyparam' => [\yii\db\Schema::TYPE_STRING, 1024],
21
	];
22
	protected $blocks = [0];
23
	protected $maximumLength = '-2147483648,2147483647';
24
	protected $customMaximumLength = [
25
		'currencyparam' => 1024
26
	];
27
	protected $purifyType = \App\Purifier::INTEGER;
28
	protected $customPurifyType = [
29
		'currencyparam' => App\Purifier::TEXT
30
	];
31
	/** {@inheritdoc} */
32
	protected $params = ['reset_currency'];
33
34
	/** {@inheritdoc} */
35
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
36
	{
37
		if (empty($value)) {
38
			return '';
39
		}
40
		return \App\Fields\Currency::getById($value)['currency_name'];
41
	}
42
43
	/**
44
	 * Gets currency param.
45
	 *
46
	 * @param array  $currencies
47
	 * @param string $param
48
	 *
49
	 * @throws \App\Exceptions\AppException
50
	 *
51
	 * @return array
52
	 */
53
	public function getCurrencyParam(array $currencies, $param = '')
54
	{
55
		$params = [];
56
		if ($param) {
57
			$params = \App\Json::decode($param);
58
		}
59
		foreach ($currencies as $currency) {
60
			if (!isset($params[$currency['id']])) {
61
				$params[$currency['id']] = vtlib\Functions::getConversionRateInfo($currency['id']);
62
			}
63
		}
64
		return $params;
65
	}
66
67
	/** {@inheritdoc} */
68
	public function getDBValue($value, ?string $name = '')
69
	{
70
		if ($name === $this->getColumnName()) {
71
			$value = (int) $value;
72
		}
73
		return $value;
74
	}
75 2
76
	/** {@inheritdoc} */
77 2
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
78 2
	{
79
		if ($columnName === $this->getColumnName()) {
80 2
			if (!is_numeric($value) || !isset(\App\Fields\Currency::getAll()[$value])) {
81
				throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||" . print_r($value, true), 406);
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true 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

81
				throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||" . /** @scrutinizer ignore-type */ print_r($value, true), 406);
Loading history...
82
			}
83
		} elseif (!\is_array($value) && \App\TextUtils::getTextLength($value) > $this->customMaximumLength[$columnName]) {
84
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||" . print_r($value, true), 406);
85
		}
86 2
	}
87
88 2
	/** {@inheritdoc} */
89 2
	public function compare($value, $prevValue, string $column): bool
90 2
	{
91
		return $column === $this->getColumnName() ? (int) $value === (int) $prevValue : parent::compare($value, $prevValue, $column);
92 2
	}
93
94
	/** {@inheritdoc} */
95 2
	public function getConfigFieldsData(): array
96
	{
97
		$data = parent::getConfigFieldsData();
98
		$data['reset_currency'] = [
99
			'name' => 'reset_currency',
100
			'label' => 'LBL_INV_CURRENCY_RESET',
101
			'uitype' => 56,
102
			'maximumlength' => '1',
103
			'typeofdata' => 'C~O',
104
			'purifyType' => \App\Purifier::INTEGER,
105
			'defaultvalue' => 0,
106
			'tooltip' => 'LBL_INV_CURRENCY_RESET_DESC'
107
		];
108
109
		return $data;
110
	}
111
}
112