Passed
Push — developer ( 410514...630a8e )
by Radosław
15:06
created

getFieldInstanceByName()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 29
rs 8.9457
cc 6
nc 5
nop 1
1
<?php
2
/**
3
 * Calendar activities 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    Adrian Kon <[email protected]>
10
 */
11
12
/**
13
 * Calendar activities model for dashboard - class.
14
 */
15
class Vtiger_CalendarActivitiesModel_Dashboard extends Vtiger_Widget_Model
16
{
17
	/** @var string Module name */
18
	protected $moduleName = 'Calendar';
19
20
	/** {@inheritdoc} */
21
	public $customFields = [
22
		'filterid' => ['label' => 'LBL_SELECT_BASE_MODULE_FILTER', 'purifyType' => \App\Purifier::TEXT],
23
		'customFilters' => ['label' => 'LBL_SELECT_CUSTOM_FILTERS', 'purifyType' => App\Purifier::TEXT]
24
	];
25
26
	/** {@inheritdoc} */
27
	public function getEditFields(): array
28
	{
29
		$fields['title'] = ['label' => 'LBL_WIDGET_NAME', 'purifyType' => \App\Purifier::TEXT];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fields = array(); before regardless.
Loading history...
30
		return $fields + parent::getEditFields();
31
	}
32
33
	/** {@inheritdoc} */
34
	public function getFieldInstanceByName($name)
35
	{
36
		if (!isset($this->customFields[$name])) {
37
			return parent::getFieldInstanceByName($name);
38
		}
39
		$params = [
40
			'name' => $name,
41
			'label' => $this->getEditFields()[$name]['label'],
42
			'tooltip' => $this->getEditFields()[$name]['tooltip'] ?? ''
43
		];
44
		if ('filterid' === $name) {
45
			$params['uitype'] = 16;
46
			$params['typeofdata'] = 'V~O';
47
			$params['picklistValues'] = $this->getFilters();
48
			$params['fieldvalue'] = $this->get('filterid') ?: '';
49
		} elseif ('customFilters' === $name) {
50
			$params['uitype'] = 33;
51
			$params['typeofdata'] = 'V~O';
52
			$params['picklistValues'] = [
53
				'activitytype' => App\Language::translate('Activity Type', $this->moduleName),
54
				'taskpriority' => App\Language::translate('Priority', $this->moduleName),
55
				'owner' => App\Language::translate('LBL_ASSIGNED_TO', $this->moduleName),
56
			];
57
			$dataValue = $this->get('data') ? \App\Json::decode($this->get('data')) : [];
58
			$value = $dataValue[$name] ?? [];
59
			$params['fieldvalue'] = implode(' |##| ', $value);
60
		}
61
62
		return \Vtiger_Field_Model::init('Settings:WidgetsManagement', $params, $name);
63
	}
64
65
	/**
66
	 * Get calendar module filters.
67
	 *
68
	 * @return array
69
	 */
70
	protected function getFilters(): array
71
	{
72
		$filtersForPicklist = [];
73
		foreach (App\CustomView::getFiltersByModule('Calendar') as $filterId => $filter) {
74
			if ($filter['setmetrics']) {
75
				$filtersForPicklist[$filterId] = App\Language::translate($filter['viewname'], $this->moduleName);
76
			}
77
		}
78
		return $filtersForPicklist;
79
	}
80
81
	/** {@inheritdoc} */
82
	public function setDataFromRequest(App\Request $request)
83
	{
84
		foreach ($this->customFields as $fieldName => $fieldInfo) {
85
			if ($request->has($fieldName)) {
86
				$value = $request->getByType($fieldName, $fieldInfo['purifyType']);
87
				$fieldModel = $this->getFieldInstanceByName($fieldName)->getUITypeModel();
88
				$fieldModel->validate($value, true);
89
				$value = $fieldModel->getDBValue($value);
90
				if ('filterid' === $fieldName) {
91
					$this->set('filterid', (int) $value);
92
				} elseif ('customFilters' === $fieldName) {
93
					$value = $value ? explode(' |##| ', $value) : [];
94
					$data = $this->get('data') ? \App\Json::decode($this->get('data')) : [];
95
					$data[$fieldName] = $value;
96
					$this->set('data', \App\Json::encode($data));
97
				}
98
			}
99
		}
100
		parent::setDataFromRequest($request);
101
	}
102
103
	/** {@inheritdoc} */
104
	public function isViewable(): bool
105
	{
106
		$userPrivModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
107
		$isPermittedToCustomView = true;
108
		if ($filterId = $this->get('filterid')) {
109
			$isPermittedToCustomView = \App\CustomView::getInstance($this->moduleName)->isPermittedCustomView((int) $filterId);
110
		}
111
		return $userPrivModel->hasModulePermission($this->moduleName) && $isPermittedToCustomView;
0 ignored issues
show
Bug introduced by
$this->moduleName of type string is incompatible with the type integer expected by parameter $mixed of Users_Privileges_Model::hasModulePermission(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
		return $userPrivModel->hasModulePermission(/** @scrutinizer ignore-type */ $this->moduleName) && $isPermittedToCustomView;
Loading history...
112
	}
113
}
114