Passed
Push — developer ( 2a0d9f...47d6ff )
by Radosław
18:29
created

getConfigFieldsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.9
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Inventory DiscountMode 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_DiscountMode_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'DiscountMode';
16
	protected $defaultLabel = 'LBL_DISCOUNT_MODE';
17
	protected $defaultValue = '0';
18
	protected $columnName = 'discountmode';
19
	protected $dbType = 'smallint(1) DEFAULT 0';
20
	protected $modes = [
21
		\Vtiger_Inventory_Model::DISCOUT_MODE_GLOBAL => 'LBL_INV_DISCOUNT_MODE_GLOBAL',
22
		\Vtiger_Inventory_Model::DISCOUT_MODE_INDIVIDUAL => 'LBL_INDIVIDUAL',
23
		\Vtiger_Inventory_Model::DISCOUT_MODE_GROUP => 'LBL_INV_DISCOUNT_MODE_GROUP'
24
	];
25
	protected $blocks = [0];
26
	protected $maximumLength = '-32768,32767';
27
	protected $purifyType = \App\Purifier::INTEGER;
28
29
	/** {@inheritdoc} */
30
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
31
	{
32
		return '' !== $value && null !== $value ? \App\Language::translate($this->modes[$value], $this->getModuleName()) : '';
33
	}
34
35
	/** {@inheritdoc} */
36 2
	public function getDBValue($value, ?string $name = '')
37
	{
38 2
		return (int) $value;
39
	}
40
41
	/** {@inheritdoc} */
42
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
43
	{
44 2
		if (!is_numeric($value) || !isset($this->modes[$value])) {
45
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406);
46 2
		}
47
	}
48
49 2
	/** {@inheritdoc} */
50
	public function getEditValue(array $itemData, string $column = '')
51
	{
52
		$value = parent::getEditValue($itemData, $column);
53
		return is_numeric($value) ? (int) $value : Vtiger_Inventory_Model::getDiscountsConfig('default_mode');
54
	}
55
56
	/** {@inheritdoc} */
57
	public function compare($value, $prevValue, string $column): bool
58
	{
59
		return (int) $value === (int) $prevValue;
60
	}
61
62
	/** {@inheritdoc} */
63
	public function getConfigFieldsData(): array
64
	{
65
		$data = parent::getConfigFieldsData();
66
		$data['defaultvalue'] = [
67
			'name' => 'defaultvalue',
68
			'label' => 'LBL_INV_DEFAULT_VALUE',
69
			'uitype' => 16,
70
			'maximumlength' => '1',
71
			'typeofdata' => 'V~M',
72
			'purifyType' => \App\Purifier::INTEGER,
73
			'defaultvalue' => \Vtiger_Inventory_Model::DISCOUT_MODE_INDIVIDUAL,
74
			'picklistValues' => array_map(fn ($label) => \App\Language::translate($label, $this->getModuleName()), $this->modes)
75
		];
76
77
		return $data;
78
	}
79
80
	/**
81
	 * Get available modes.
82
	 *
83
	 * @return array
84
	 */
85
	public function getModes(): array
86
	{
87
		return $this->modes;
88
	}
89
}
90