Passed
Pull Request — developer (#16340)
by Arkadiusz
14:53
created

setDataFromRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 16
rs 9.4888
cc 5
nc 5
nop 1
1
<?php
2
/**
3
 * Widget model for dashboard - file.
4
 *
5
 * @package   Dashboard
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Arkadiusz Sołek <[email protected]>
10
 */
11
12
/**
13
 * Widget model for dashboard - class.
14
 */
15
class OSSTimeControl_TimeCounterModel_Dashboard extends Vtiger_Widget_Model
16
{
17
	/** {@inheritdoc} */
18
	public $customFields = [
19
		'default_time' => ['label' => 'LBL_DEFAULT_TIME', 'purifyType' => \App\Purifier::TEXT],
20
	];
21
22
	/** {@inheritdoc} */
23
	public $editFields = [
24
		'isdefault' => ['label' => 'LBL_MANDATORY_WIDGET', 'purifyType' => \App\Purifier::BOOL],
25
		'width' => ['label' => 'LBL_WIDTH', 'purifyType' => \App\Purifier::INTEGER],
26
		'height' => ['label' => 'LBL_HEIGHT', 'purifyType' => \App\Purifier::INTEGER],
27
	];
28
29
	/** {@inheritdoc} */
30
	public function getEditFields(): array
31
	{
32
		return $this->editFields + $this->customFields;
33
	}
34
35
	/** {@inheritdoc} */
36
	public function getFieldInstanceByName($name)
37
	{
38
		$moduleName = 'Settings:WidgetsManagement';
39
		if (!isset($this->customFields[$name])) {
40
			return parent::getFieldInstanceByName($name);
41
		}
42
		$params = [
43
			'name' => $name,
44
			'label' => $this->getEditFields()[$name]['label'],
45
			'tooltip' => $this->getEditFields()[$name]['tooltip'] ?? ''
46
		];
47
		switch ($name) {
48
				case 'default_time':
49
					$data = $this->get('data') ? \App\Json::decode($this->get('data')) : [];
50
					$params['uitype'] = 33;
51
					$params['typeofdata'] = 'V~O';
52
					$picklistValue = [
53
						'15' => 'PLL_FIFTEEN_MINUTES',
54
						'30' => 'PLL_THIRTY_MINUTES',
55
						'40' => 'PLL_FORTY_MINUTES',
56
						'60' => 'PLL_SIXTY_MINUTES',
57
						'90' => 'PLL_NINETY_MINUTES',
58
					];
59
					foreach ($picklistValue as $key => $label) {
60
						$picklistValue[$key] = \App\Language::translate($label, $moduleName);
61
					}
62
					$params['picklistValues'] = $picklistValue;
63
					$value = $data[$name] ?? [];
64
					$params['fieldvalue'] = implode(' |##| ', $value);
65
					break;
66
			default: break;
67
		}
68
		return \Vtiger_Field_Model::init($moduleName, $params, $name);
69
	}
70
71
	/** {@inheritdoc} */
72
	public function setDataFromRequest(App\Request $request)
73
	{
74
		parent::setDataFromRequest($request);
75
		foreach ($this->customFields as $fieldName => $fieldInfo) {
76
			if ($request->has($fieldName)) {
77
				$value = $request->getByType($fieldName, $fieldInfo['purifyType']);
78
				$fieldModel = $this->getFieldInstanceByName($fieldName)->getUITypeModel();
79
				$fieldModel->validate($value, true);
80
				$value = $fieldModel->getDBValue($value);
81
				switch ($fieldName) {
82
					case 'default_time':
83
						$value = $value ? explode(' |##| ', $value) : [];
84
						$data[$fieldName] = $value;
85
						$this->set('data', \App\Json::encode($data));
86
						break;
87
					default: break;
88
				}
89
			}
90
		}
91
	}
92
93
	/** {@inheritdoc} */
94
	public function getTitle()
95
	{
96
		return \App\Language::translate($this->get('linklabel'), 'Home');
97
	}
98
}
99