Passed
Pull Request — developer (#15952)
by Arkadiusz
31:16 queued 15:09
created

Vtiger_Calendar_Model::changeDateTime()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 4
nc 8
nop 2
1
<?php
2
3
/**
4
 * Calendar model class.
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
 */
12
/**
13
 * Vtiger_Calendar_Model class.
14
 */
15
abstract class Vtiger_Calendar_Model extends App\Base
16
{
17
	/**
18
	 * @var string Module name
19
	 */
20
	public $moduleName;
21
	/**
22
	 * @var \Vtiger_Module_Model Module model
23
	 */
24
	public $module;
25
26
	/**
27
	 * Get module name.
28
	 *
29
	 * @return string
30
	 */
31
	public function getModuleName()
32
	{
33
		return $this->moduleName;
34
	}
35
36
	/**
37
	 * Get module model.
38
	 *
39
	 * @return \Vtiger_Module_Model
40
	 */
41
	public function getModule()
42
	{
43
		if (!isset($this->module)) {
44
			$this->module = Vtiger_Module_Model::getInstance($this->getModuleName());
45
		}
46
		return $this->module;
47
	}
48
49
	/**
50
	 * Get records count for extended calendar left column.
51
	 *
52
	 * @return int|string
53
	 */
54
	public function getEntityRecordsCount()
55
	{
56
		return $this->getQuery()->count();
57
	}
58
59
	/** {@inheritdoc} */
60
	public function getSideBarLinks($linkParams)
61
	{
62
		$links = Vtiger_Link_Model::getAllByType($this->getModule()->getId(), ['SIDEBARWIDGET'], $linkParams)['SIDEBARWIDGET'] ?? [];
63
		$links[] = Vtiger_Link_Model::getInstanceFromValues([
64
			'linktype' => 'SIDEBARWIDGET',
65
			'linklabel' => 'LBL_USERS',
66
			'linkclass' => 'js-calendar__filter--users',
67
			'template' => 'Filters/Users.tpl',
68
			'filterData' => Vtiger_CalendarRightPanel_Model::getUsersList($this->getModuleName()),
69
		]);
70
		$links[] = Vtiger_Link_Model::getInstanceFromValues([
71
			'linktype' => 'SIDEBARWIDGET',
72
			'linklabel' => 'LBL_GROUPS',
73
			'linkclass' => 'js-calendar__filter--groups',
74
			'template' => 'Filters/Groups.tpl',
75
			'filterData' => Vtiger_CalendarRightPanel_Model::getGroupsList($this->getModuleName()),
76
		]);
77
		return $links;
78
	}
79
80
	/**
81
	 * Static Function to get the instance of Vtiger Module Model for the given id or name.
82
	 *
83
	 * @param mixed id or name of the module
84
	 * @param mixed $moduleName
85
	 */
86
	public static function getInstance(string $moduleName)
87
	{
88
		$className = Vtiger_Loader::getComponentClassName('Model', 'Calendar', $moduleName);
89
		$handler = new $className();
90
		$handler->moduleName = $moduleName;
91
		return $handler;
92
	}
93
94
	/**
95
	 * Get public holidays for rendering them on the calendar.
96
	 *
97
	 * @return array
98
	 */
99
	public function getPublicHolidays()
100
	{
101
		$result = [];
102
		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) {
103
			$item = [
104
				'title' => $holiday['name'],
105
				'start' => $holiday['date'],
106
				'display' => 'background',
107
			];
108
			if ('national' === $holiday['type']) {
109
				$item['color'] = '#FFAB91';
110
				$item['icon'] = 'fas fa-flag';
111
			} else {
112
				$item['color'] = '#81D4FA';
113
				$item['icon'] = 'fas fa-church';
114
			}
115
			$result[] = $item;
116
		}
117
		return $result;
118
	}
119
120
	/**
121
	 * Gest query.
122
	 *
123
	 * @return \App\Db\Query
124
	 */
125
	abstract public function getQuery();
126
127
	/**
128
	 * Function to get records.
129
	 *
130
	 * @return array
131
	 */
132
	abstract public function getEntity();
133
134
	/**
135
	 * Get entity count for year view.
136
	 *
137
	 * @return array
138
	 */
139
	public function getEntityYearCount()
140
	{
141
		$currentUser = \App\User::getCurrentUserModel();
142
		$startDate = DateTimeField::convertToDBTimeZone($this->get('start'));
143
		$startDate = strtotime($startDate->format('Y-m-d H:i:s'));
144
		$endDate = DateTimeField::convertToDBTimeZone($this->get('end'));
145
		$endDate = strtotime($endDate->format('Y-m-d H:i:s'));
146
		$dataReader = $this->getQuery()
147
			->createCommand()
148
			->query();
149
		$return = [];
150
		while ($record = $dataReader->read()) {
151
			$dateTimeFieldInstance = new DateTimeField($record['date_start'] . ' ' . $record['time_start']);
0 ignored issues
show
Bug introduced by
$record['date_start'] . .... $record['time_start'] of type string is incompatible with the type type expected by parameter $value of DateTimeField::__construct(). ( Ignorable by Annotation )

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

151
			$dateTimeFieldInstance = new DateTimeField(/** @scrutinizer ignore-type */ $record['date_start'] . ' ' . $record['time_start']);
Loading history...
152
			$userDateTimeString = $dateTimeFieldInstance->getDisplayDateTimeValue();
153
			$dateTimeComponents = explode(' ', $userDateTimeString);
154
			$dateComponent = $dateTimeComponents[0];
155
			$startDateFormated = DateTimeField::__convertToDBFormat($dateComponent, $currentUser->getDetail('date_format'));
156
157
			$dateTimeFieldInstance = new DateTimeField($record['due_date'] . ' ' . $record['time_end']);
158
			$userDateTimeString = $dateTimeFieldInstance->getDisplayDateTimeValue();
159
			$dateTimeComponents = explode(' ', $userDateTimeString);
160
			$dateComponent = $dateTimeComponents[0];
161
			$endDateFormated = DateTimeField::__convertToDBFormat($dateComponent, $currentUser->getDetail('date_format'));
162
163
			$begin = new DateTime($startDateFormated);
164
			$end = new DateTime($endDateFormated);
165
			$end->modify('+1 day');
166
			$interval = DateInterval::createFromDateString('1 day');
167
			foreach (new DatePeriod($begin, $interval, $end) as $dt) {
168
				$date = strtotime($dt->format('Y-m-d'));
169
				if ($date >= $startDate && $date <= $endDate) {
170
					$date = date('Y-m-d', $date);
171
					$return[$date]['date'] = $date;
172
					if (isset($return[$date]['count'])) {
173
						++$return[$date]['count'];
174
					} else {
175
						$return[$date]['count'] = 1;
176
					}
177
				}
178
			}
179
		}
180
		$dataReader->close();
181
182
		return array_values($return);
183
	}
184
185
	/**
186
	 * Update event.
187
	 *
188
	 * @param int    $recordId Record ID
189
	 * @param string $start    Start date
190
	 * @param string $end      End date
191
	 *
192
	 * @return bool
193
	 */
194
	public function updateEvent(int $recordId, string $start, string $end)
195
	{
196
		try {
197
			$recordModel = Vtiger_Record_Model::getInstanceById($recordId, $this->getModuleName());
198
			if ($success = $recordModel->isEditable()) {
199
				$start = DateTimeField::convertToDBTimeZone($start);
200
				$recordModel->set('date_start', $start->format('Y-m-d'));
201
				$recordModel->set('time_start', $start->format('H:i:s'));
202
				$end = DateTimeField::convertToDBTimeZone($end);
203
				$recordModel->set('due_date', $end->format('Y-m-d'));
204
				$recordModel->set('time_end', $end->format('H:i:s'));
205
				$recordModel->save();
206
				$success = true;
207
			}
208
		} catch (Exception $e) {
209
			\App\Log::error($e->__toString());
210
			$success = false;
211
		}
212
		return $success;
213
	}
214
}
215