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

OSSTimeControl_Calendar_Model::getQuery()   C

Complexity

Conditions 15
Paths 160

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 66
rs 5.4166
ccs 0
cts 35
cp 0
cc 15
nc 160
nop 0
crap 240

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TimeControl 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    Mariusz Krzaczkowski <[email protected]>
11
 */
12
13
/**
14
 * TimeControl calendar model class.
15
 */
16
class OSSTimeControl_Calendar_Model extends Vtiger_Calendar_Model
17
{
18
	/** {@inheritdoc} */
19
	public function getCalendarTypes(): array
20
	{
21
		$calendarTypes = [];
22
		$moduleField = $this->getModule()->getFieldByName('timecontrol_type');
23
		if ($moduleField && $moduleField->isActiveField()) {
24
			$calendarTypes = $moduleField->getPicklistValues();
25
		}
26
		return $calendarTypes;
27
	}
28
29
	/**
30
	 * Get query.
31
	 *
32
	 * @return \App\Db\Query
33
	 */
34
	public function getQuery()
35
	{
36
		$queryGenerator = new App\QueryGenerator($this->getModuleName());
37
		if ($this->has('customFilter')) {
38
			$queryGenerator->initForCustomViewById($this->get('customFilter'));
39
		}
40
		$queryGenerator->setFields(['id', 'date_start', 'time_start', 'time_end', 'due_date', 'timecontrol_type', 'name', 'assigned_user_id']);
41
		if ($types = $this->get('types')) {
42
			$queryGenerator->addCondition('timecontrol_type', implode('##', $types), 'e');
43
		}
44
		if ($this->get('start') && $this->get('end')) {
45
			$dbStartDateOject = DateTimeField::convertToDBTimeZone($this->get('start'));
46
			$dbStartDateTime = $dbStartDateOject->format('Y-m-d H:i:s');
47
			$dbStartDate = $dbStartDateOject->format('Y-m-d');
48
			$dbEndDateObject = DateTimeField::convertToDBTimeZone($this->get('end'));
49
			$dbEndDateTime = $dbEndDateObject->format('Y-m-d H:i:s');
50
			$dbEndDate = $dbEndDateObject->format('Y-m-d');
51
			$queryGenerator->addNativeCondition([
52
				'or',
53
				[
54
					'and',
55
					['>=', new \yii\db\Expression("CONCAT(vtiger_osstimecontrol.date_start, ' ', vtiger_osstimecontrol.time_start)"), $dbStartDateTime],
56
					['<=', new \yii\db\Expression("CONCAT(vtiger_osstimecontrol.date_start, ' ', vtiger_osstimecontrol.time_start)"), $dbEndDateTime],
57
				],
58
				[
59
					'and',
60
					['>=', new \yii\db\Expression("CONCAT(vtiger_osstimecontrol.due_date, ' ', vtiger_osstimecontrol.time_end)"), $dbStartDateTime],
61
					['<=', new \yii\db\Expression("CONCAT(vtiger_osstimecontrol.due_date, ' ', vtiger_osstimecontrol.time_end)"), $dbEndDateTime],
62
				],
63
				[
64
					'and',
65
					['<', 'vtiger_osstimecontrol.date_start', $dbStartDate],
66
					['>', 'vtiger_osstimecontrol.due_date', $dbEndDate],
67
				],
68
			]);
69
		}
70
71
		$query = $queryGenerator->createQuery();
72
		if ($this->has('filters')) {
73
			foreach ($this->get('filters') as $filter) {
74
				$filterClassName = Vtiger_Loader::getComponentClassName('CalendarFilter', $filter['name'], $this->getModuleName());
75
				$filterInstance = new $filterClassName();
76
				if ($filterInstance->checkPermissions() && $conditions = $filterInstance->getCondition($filter['value'])) {
77
					$query->andWhere($conditions);
78
				}
79
			}
80
		}
81
		$conditions = [];
82
		if (!empty($this->get('user')) && isset($this->get('user')['selectedIds'][0])) {
83
			$selectedUsers = $this->get('user');
84
			$selectedIds = $selectedUsers['selectedIds'];
85
			if ('all' !== $selectedIds[0]) {
86
				$conditions[] = ['vtiger_crmentity.smownerid' => $selectedIds];
87
				$subQuery = (new \App\Db\Query())->select(['crmid'])->from('u_#__crmentity_showners')->where(['userid' => $selectedIds]);
88
				$conditions[] = ['vtiger_crmentity.crmid' => $subQuery];
89
			}
90
			if (isset($selectedUsers['excludedIds']) && 'all' === $selectedIds[0]) {
91
				$conditions[] = ['not in', 'vtiger_crmentity.smownerid', $selectedUsers['excludedIds']];
92
			}
93
		}
94
		if ($conditions) {
95
			$query->andWhere(array_merge(['or'], $conditions));
96
		}
97
		$query->orderBy(['vtiger_osstimecontrol.date_start' => SORT_ASC, 'vtiger_osstimecontrol.time_start' => SORT_ASC]);
98
99
		return $query;
100
	}
101
102
	/**
103
	 * Function to get records.
104
	 *
105
	 * @return array
106
	 */
107
	public function getEntity()
108
	{
109
		$dataReader = $this->getQuery()->createCommand()->query();
110
		$result = [];
111
		$moduleModel = $this->getModule();
112
		$isSummaryViewSupported = $moduleModel->isSummaryViewSupported();
113
		$colors = \App\Fields\Picklist::getColors('timecontrol_type', false);
114
		while ($record = $dataReader->read()) {
115
			$item = [];
116
			$item['id'] = $record['id'];
117
			$item['title'] = \App\Purifier::encodeHtml($record['name']);
118
119
			$dateTimeInstance = 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

119
			$dateTimeInstance = new DateTimeField(/** @scrutinizer ignore-type */ $record['date_start'] . ' ' . $record['time_start']);
Loading history...
120
			$item['start'] = DateTimeField::convertToUserTimeZone($record['date_start'] . ' ' . $record['time_start'])->format('Y-m-d') . ' ' . $dateTimeInstance->getFullcalenderTime();
121
			$item['start_display'] = $dateTimeInstance->getDisplayDateTimeValue();
122
123
			$dateTimeInstance = new DateTimeField($record['due_date'] . ' ' . $record['time_end']);
124
			$item['end'] = DateTimeField::convertToUserTimeZone($record['due_date'] . ' ' . $record['time_end'])->format('Y-m-d') . ' ' . $dateTimeInstance->getFullcalenderTime();
125
			$item['end_display'] = $dateTimeInstance->getDisplayDateTimeValue();
126
127
			$item['borderColor'] = $colors[$record['timecontrol_type']] ?? '';
128
			$item['className'] = 'js-popover-tooltip--record ownerCBg_' . $record['assigned_user_id'];
129
			if ($isSummaryViewSupported) {
130
				$item['url'] = 'index.php?module=' . $this->getModuleName() . '&view=Detail&record=' . $record['id'];
131
				$item['className'] .= ' js-show-modal';
132
			} else {
133
				$item['url'] = $moduleModel->getDetailViewUrl($record['id']);
134
			}
135
			$result[] = $item;
136
		}
137
		$dataReader->close();
138
		return $result;
139
	}
140
}
141