Passed
Push — developer ( 5f736c...9195d5 )
by Mariusz
18:14
created

Vtiger_Calendar_Model   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 91
c 0
b 0
f 0
dl 0
loc 221
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtraSourcesCount() 0 13 3
A getPublicHolidays() 0 19 3
A getInstance() 0 6 1
A getModuleName() 0 3 1
A updateEvent() 0 19 3
A getModule() 0 6 2
A getCalendarTypes() 0 3 1
A getEntityRecordsCount() 0 3 1
A getExtraSources() 0 13 3
A getSideBarLinks() 0 39 4
1
<?php
2
3
/**
4
 * Calendar model file.
5
 *
6
 * @package Model
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 * @author   Mariusz Krzaczkowski <[email protected]>
12
 */
13
/**
14
 * Calendar model class.
15
 */
16
abstract class Vtiger_Calendar_Model extends App\Base
17
{
18
	/** @var string Module name */
19
	public $moduleName;
20
	/** @var \Vtiger_Module_Model Module model */
21
	public $module;
22
23
	/**
24
	 * Get module name.
25
	 *
26
	 * @return string
27
	 */
28
	public function getModuleName()
29
	{
30
		return $this->moduleName;
31
	}
32
33
	/**
34
	 * Get module model.
35
	 *
36
	 * @return \Vtiger_Module_Model
37
	 */
38
	public function getModule()
39
	{
40
		if (!isset($this->module)) {
41
			$this->module = Vtiger_Module_Model::getInstance($this->getModuleName());
42
		}
43
		return $this->module;
44
	}
45
46
	/**
47
	 * Get records count for extended calendar left column.
48
	 *
49
	 * @return int|string
50
	 */
51
	public function getEntityRecordsCount()
52
	{
53
		return $this->getQuery()->count();
54
	}
55
56
	/**
57
	 * Function to get the right side bar links for the module.
58
	 *
59
	 * @param array $linkParams
60
	 *
61
	 * @return Vtiger_Link_Model[]
62
	 */
63
	public function getSideBarLinks(array $linkParams): array
64
	{
65
		$links = Vtiger_Link_Model::getAllByType($this->getModule()->getId(), ['SIDEBARWIDGET'], $linkParams)['SIDEBARWIDGET'] ?? [];
66
		if ($types = $this->getCalendarTypes()) {
67
			$links[] = Vtiger_Link_Model::getInstanceFromValues([
68
				'linktype' => 'SIDEBARWIDGET',
69
				'linklabel' => 'LBL_TYPE',
70
				'linkdata' => ['cache' => 'calendar-types', 'name' => 'types'],
71
				'template' => 'Filters/ActivityTypes.tpl',
72
				'filterData' => $types,
73
			]);
74
		}
75
		$request = \App\Request::init();
76
		$historyUsers = $request->has('user') ? $request->get('user') : [];
77
		$links[] = Vtiger_Link_Model::getInstanceFromValues([
78
			'linktype' => 'SIDEBARWIDGET',
79
			'linkclass' => 'js-users-form usersForm ',
80
			'template' => 'Filters/Users.tpl',
81
			'filterData' => Vtiger_CalendarRightPanel_Model::getUsersList($this->getModuleName()),
82
			'historyUsers' => $historyUsers,
83
		]);
84
		$links[] = Vtiger_Link_Model::getInstanceFromValues([
85
			'linktype' => 'SIDEBARWIDGET',
86
			'linkclass' => 'js-group-form groupForm',
87
			'template' => 'Filters/Groups.tpl',
88
			'filterData' => Vtiger_CalendarRightPanel_Model::getGroupsList($this->getModuleName()),
89
			'historyUsers' => $historyUsers,
90
		]);
91
		$privileges = Users_Privileges_Model::getCurrentUserPrivilegesModel();
92
		if ($privileges->hasModuleActionPermission($this->getModuleName(), 'CalendarExtraSources')) {
93
			$links[] = Vtiger_Link_Model::getInstanceFromValues([
94
				'linktype' => 'SIDEBARWIDGET',
95
				'linklabel' => 'LBL_EXTRA_SOURCES',
96
				'linkclass' => 'js-extra-sources-form',
97
				'template' => 'Filters/ExtraSources.tpl',
98
				'filterData' => Vtiger_CalendarExtSource_Model::getByModule($this->getModule()->getId())
99
			]);
100
		}
101
		return $links;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $links could return the type Vtiger_Link_Model which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
102
	}
103
104
	/**
105
	 * Static Function to get the instance of Vtiger Module Model for the given id or name.
106
	 *
107
	 * @param mixed id or name of the module
108
	 * @param mixed $moduleName
109
	 */
110
	public static function getInstance(string $moduleName)
111
	{
112
		$className = Vtiger_Loader::getComponentClassName('Model', 'Calendar', $moduleName);
113
		$handler = new $className();
114
		$handler->moduleName = $moduleName;
115
		return $handler;
116
	}
117
118
	/**
119
	 * Get calendar types list.
120
	 *
121
	 * @return string[]
122
	 */
123
	public function getCalendarTypes(): array
124
	{
125
		return [];
126
	}
127
128
	/**
129
	 * Get public holidays for rendering them on the calendar.
130
	 *
131
	 * @return array
132
	 */
133
	public function getPublicHolidays()
134
	{
135
		$result = [];
136
		foreach (App\Fields\Date::getHolidays(DateTimeField::convertToDBTimeZone($this->get('start'))->format('Y-m-d'), DateTimeField::convertToDBTimeZone($this->get('end'))->format('Y-m-d')) as $holiday) {
137
			$item = [
138
				'title' => $holiday['name'],
139
				'start' => $holiday['date'],
140
				'display' => 'background',
141
			];
142
			if ('national' === $holiday['type']) {
143
				$item['color'] = '#FFAB91';
144
				$item['icon'] = 'fas fa-flag';
145
			} else {
146
				$item['color'] = '#81D4FA';
147
				$item['icon'] = 'fas fa-church';
148
			}
149
			$result[] = $item;
150
		}
151
		return $result;
152
	}
153
154
	/**
155
	 * Gest query.
156
	 *
157
	 * @return \App\Db\Query
158
	 */
159
	abstract public function getQuery();
160
161
	/**
162
	 * Function to get records.
163
	 *
164
	 * @return array
165
	 */
166
	abstract public function getEntity();
167
168
	/**
169
	 * Update event.
170
	 *
171
	 * @param int          $recordId Record ID
172
	 * @param string       $start    Start date
173
	 * @param string       $end      End date
174
	 * @param \App\Request $request  Request instance
175
	 *
176
	 * @return bool
177
	 */
178
	public function updateEvent(int $recordId, string $start, string $end, App\Request $request): bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

178
	public function updateEvent(int $recordId, string $start, string $end, /** @scrutinizer ignore-unused */ App\Request $request): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
	{
180
		try {
181
			$recordModel = Vtiger_Record_Model::getInstanceById($recordId, $this->getModuleName());
182
			if ($success = $recordModel->isEditable()) {
183
				$start = DateTimeField::convertToDBTimeZone($start);
184
				$recordModel->set('date_start', $start->format('Y-m-d'));
185
				$recordModel->set('time_start', $start->format('H:i:s'));
186
				$end = DateTimeField::convertToDBTimeZone($end);
187
				$recordModel->set('due_date', $end->format('Y-m-d'));
188
				$recordModel->set('time_end', $end->format('H:i:s'));
189
				$recordModel->save();
190
				$success = true;
191
			}
192
		} catch (Exception $e) {
193
			\App\Log::error($e->__toString());
194
			$success = false;
195
		}
196
		return $success;
197
	}
198
199
	/**
200
	 * Get calendar extra sources rows.
201
	 *
202
	 * @return array
203
	 */
204
	public function getExtraSources(): array
205
	{
206
		$result = [];
207
		if ($this->get('extraSources')) {
208
			foreach ($this->get('extraSources') as $sourceId) {
209
				$source = Vtiger_CalendarExtSource_Model::getInstanceById($sourceId);
210
				$source->set('start', $this->get('start'));
211
				$source->set('end', $this->get('end'));
212
				$source->set('user', $this->get('user'));
213
				$result = array_merge($result, $source->getRows());
214
			}
215
		}
216
		return $result;
217
	}
218
219
	/**
220
	 * Get calendar extra sources counter.
221
	 *
222
	 * @return int
223
	 */
224
	public function getExtraSourcesCount(): int
225
	{
226
		$counter = 0;
227
		if ($this->get('extraSources')) {
228
			foreach ($this->get('extraSources') as $sourceId) {
229
				$source = Vtiger_CalendarExtSource_Model::getInstanceById($sourceId);
230
				$source->set('start', $this->get('start'));
231
				$source->set('end', $this->get('end'));
232
				$source->set('user', $this->get('user'));
233
				$counter += $source->getExtraSourcesCount();
234
			}
235
		}
236
		return $counter;
237
	}
238
}
239