GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 7a6e83...48c884 )
by Jan
43:51 queued 36:29
created

ModuleBelegungsplan   F

Complexity

Total Complexity 67

Size/Duplication

Total Lines 400
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 178
dl 0
loc 400
rs 3.04
c 0
b 0
f 0
wmc 67

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 29 6
B dataMonth() 0 27 7
F compile() 0 237 44
A sortNachWizard() 0 16 3
B includeCalender() 0 21 7

How to fix   Complexity   

Complex Class

Complex classes like ModuleBelegungsplan often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ModuleBelegungsplan, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
* Contao Open Source CMS
4
*
5
* Copyright (c) Jan Karai
6
*
7
* @license LGPL-3.0+
8
*/
9
namespace Mailwurm\Belegung;
10
use Psr\Log\LogLevel;
11
use Contao\CoreBundle\Monolog\ContaoContext;
12
use Patchwork\Utf8;
13
/**
14
* Class ModuleBelegungsplan
15
*
16
* @property array $belegungsplan_categories
17
* @property array $belegungsplan_month
18
*
19
* @author Jan Karai <https://www.sachsen-it.de>
20
*/
21
class ModuleBelegungsplan extends \Module
22
{	
23
	/**
24
	 * Template
25
	 * @var string
26
	 */
27
	protected $strTemplate = 'mod_belegungsplan';
28
	
29
	/**
30
	 * @var array
31
	 */
32
	protected $belegungsplan_category = array();
33
	
34
	/**
35
	 * @var string
36
	 */
37
	protected $strUrl;
38
	
39
	/**
40
	 * @var integer
41
	 */
42
	protected $intStartAuswahl;
43
	
44
	/**
45
	 * @var integer
46
	 */
47
	protected $intEndeAuswahl;
48
	
49
	/**
50
	 * @var integer
51
	 */
52
	protected $intAnzahlJahre;
53
	
54
	/**
55
	 * Display a wildcard in the back end
56
	 *
57
	 * @return string
58
	 */
59
	public function generate() 
60
	{
61
		if (TL_MODE == 'BE') 
0 ignored issues
show
introduced by
The condition Mailwurm\Belegung\TL_MODE == 'BE' is always false.
Loading history...
62
		{
63
			/** @var BackendTemplate|object $objTemplate */
64
			$objTemplate = new \BackendTemplate('be_wildcard');
65
			$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['belegungsplan'][0]) . ' ###';
66
			$objTemplate->title = $this->headline;
67
			$objTemplate->id = $this->id;
68
			$objTemplate->link = $this->name;
69
			$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
70
			return $objTemplate->parse();
71
		}
72
		$this->belegungsplan_category = \StringUtil::deserialize($this->belegungsplan_categories);
73
		$this->belegungsplan_month = \StringUtil::deserialize($this->belegungsplan_month);
74
		// aktuelle Seiten URL
75
		$this->strUrl = preg_replace('/\?.*$/', '', \Environment::get('request'));
76
		
77
		// Return if there are no categories
78
		if (!is_array($this->belegungsplan_category) || empty($this->belegungsplan_category)) 
0 ignored issues
show
introduced by
The condition is_array($this->belegungsplan_category) is always true.
Loading history...
79
		{
80
			return '';
81
		}
82
		// Return if there are no month
83
		if (!is_array($this->belegungsplan_month) || empty($this->belegungsplan_month)) 
0 ignored issues
show
introduced by
The condition is_array($this->belegungsplan_month) is always true.
Loading history...
84
		{
85
			return '';
86
		}
87
		return parent::generate();
88
	}
89
	/**
90
	 * Generate the module
91
	 */
92
	protected function compile() 
93
	{
94
		$arrInfo = array();
95
		$arrCategorieObjekte = array();
96
		$arrJahre = array();
97
		$arrFeiertage = array();
98
		
99
		// Monate sortieren
100
		$arrBelegungsplanMonth = $this->belegungsplan_month;
101
		sort($arrBelegungsplanMonth, SORT_NUMERIC);
102
		$this->belegungsplan_month = $arrBelegungsplanMonth;
103
		
104
		$blnClearInput = false;
105
		
106
		// wenn der letzte anzuzeigende Monat verstrichen ist automatisch das nächste Jahr anzeigen
107
		$intMax = (int) max($this->belegungsplan_month);
108
		
109
		$intYear = \Input::get('belegyear');
110
		// interner Zaehler
111
		$i = 0;
112
		
113
		// Aktuelle Periode bei Erstaufruf der Seite
114
		if (!isset($_GET['belegyear'])) {	
115
			$intYear = $intMax < (int) date('n') ? (int) date('Y') + 1 : (int) date('Y');
116
			$blnClearInput = true;
117
		} else {
118
			if (!empty($intYear)) {
119
				is_numeric($intYear) && strlen($intYear) === 4 ? ($intYear >= (int) date('Y') ? $intYear = (int) $intYear : $arrInfo[] = '4. ' . $GLOBALS['TL_LANG']['mailwurm_belegung']['info'][2]) : $arrInfo[] = '1. ' . $GLOBALS['TL_LANG']['mailwurm_belegung']['info'][1];
120
			}
121
		}
122
		$intMinYear = $intMax < (int) date('n') ? (int) date('Y') + 1 : (int) date('Y');
123
		
124
		// wenn $arrInfo hier schon belegt, dann nicht erst weiter machen
125
		if (empty($arrInfo)) {
126
			// Anfang und Ende des Anzeigezeitraumes je nach GET
127
			if (!empty($intYear)) {
128
				$this->intStartAuswahl = (int) mktime(0, 0, 0, 1, 1, $intYear);
129
				$this->intEndeAuswahl = (int) mktime(23, 59, 59, 12, 31, $intYear);
130
			}
131
			
132
			// Hole alle aktiven Objekte inklusive dazugehoeriger Kategorie
133
			$objCategoryObjekte = $this->Database->prepare("SELECT 	tbc.id as CategoryID,
134
										tbc.title as CategoryTitle,
135
										tbo.id as ObjektID,
136
										tbo.name as ObjektName,
137
										tbo.infotext as ObjektInfoText,
138
										tbo.sorting as ObjektSortierung
139
									FROM 	tl_belegungsplan_category tbc,
140
										tl_belegungsplan_objekte tbo
141
									WHERE	tbo.pid = tbc.id
142
									AND	tbo.published = 1")
143
									->execute();
144
			if ($objCategoryObjekte->numRows > 0) {
145
				while ($objCategoryObjekte->next()) {
146
					// Nicht anzuzeigende Kategorien aussortieren
147
					if (in_array($objCategoryObjekte->CategoryID, $this->belegungsplan_category)) {
148
						$arrHelper = array();
149
						$arrHelper['ObjektID'] = (int) $objCategoryObjekte->ObjektID;
150
						$arrHelper['ObjektName'] = \StringUtil::specialchars($objCategoryObjekte->ObjektName);
151
						$arrHelper['ObjektInfoText'] = $objCategoryObjekte->ObjektInfoText;
152
						if (array_key_exists($objCategoryObjekte->CategoryID, $arrCategorieObjekte)) {
153
							$arrCategorieObjekte[$objCategoryObjekte->CategoryID]['Objekte'][$objCategoryObjekte->ObjektSortierung] = $arrHelper;
154
							$i++;
155
						} else {
156
							$arrCategorieObjekte[$objCategoryObjekte->CategoryID]['CategoryTitle'] = \StringUtil::specialchars($objCategoryObjekte->CategoryTitle);
157
							$arrCategorieObjekte[$objCategoryObjekte->CategoryID]['Objekte'][$objCategoryObjekte->ObjektSortierung] = $arrHelper;
158
							$i++;
159
						}
160
						unset($arrHelper);
161
					}
162
				}
163
			} else {
164
				$arrInfo[] = '3. ' . $GLOBALS['TL_LANG']['mailwurm_belegung']['info'][0];
165
			}
166
			
167
			// Hole alle Calenderdaten zur Auswahl
168
			$objObjekteCalender = $this->Database->prepare("SELECT tbo.id as ObjektID,
169
							tbo.sorting as ObjektSortierung,
170
							tbcat.id as CategoryID,
171
							(CASE
172
								WHEN tbc.startDate < " . $this->intStartAuswahl . " THEN DAY(FROM_UNIXTIME(" . $this->intStartAuswahl . "))
173
								ELSE DAY(FROM_UNIXTIME(tbc.startDate))
174
							 END) as StartTag,
175
							 (CASE
176
								WHEN tbc.startDate < " . $this->intStartAuswahl . " THEN MONTH(FROM_UNIXTIME(" . $this->intStartAuswahl . "))
177
								ELSE MONTH(FROM_UNIXTIME(tbc.startDate))
178
							 END) as StartMonat,
179
							 (CASE
180
								WHEN tbc.startDate < " . $this->intStartAuswahl . " THEN YEAR(FROM_UNIXTIME(" . $this->intStartAuswahl . "))
181
								ELSE YEAR(FROM_UNIXTIME(tbc.startDate))
182
							 END) as StartJahr,
183
							 YEAR(FROM_UNIXTIME(tbc.startDate)) as BuchungsStartJahr,
184
							 (CASE
185
								WHEN tbc.endDate > " . $this->intEndeAuswahl . " THEN DAY(FROM_UNIXTIME(" . $this->intEndeAuswahl . "))
186
								ELSE DAY(FROM_UNIXTIME(tbc.endDate))
187
							 END) as EndeTag,
188
							 (CASE
189
								WHEN tbc.endDate > " . $this->intEndeAuswahl . " THEN MONTH(FROM_UNIXTIME(" . $this->intEndeAuswahl . "))
190
								ELSE MONTH(FROM_UNIXTIME(tbc.endDate))
191
							 END) as EndeMonat,
192
							 (CASE
193
								WHEN tbc.endDate > " . $this->intEndeAuswahl . " THEN YEAR(FROM_UNIXTIME(" . $this->intEndeAuswahl . "))
194
								ELSE YEAR(FROM_UNIXTIME(tbc.endDate))
195
							 END) as EndeJahr,
196
							 YEAR(FROM_UNIXTIME(tbc.endDate)) as BuchungsEndeJahr
197
						FROM 	tl_belegungsplan_calender tbc,
198
							tl_belegungsplan_objekte tbo,
199
							tl_belegungsplan_category tbcat
200
						WHERE 	tbc.pid = tbo.id
201
						AND		tbo.pid = tbcat.id
202
						AND 	tbo.published = 1
203
						AND		tbc.startDate < tbc.endDate
204
						AND 	((tbc.startDate < ? AND tbc.endDate >= ?) OR (tbc.startDate >= ? AND tbc.endDate <= ?) OR (tbc.startDate < ? AND tbc.endDate > ?))")
205
						->execute($this->intStartAuswahl, $this->intStartAuswahl, $this->intStartAuswahl, $this->intEndeAuswahl, $this->intEndeAuswahl, $this->intEndeAuswahl);
206
						
207
			if ($objObjekteCalender->numRows > 0) {
208
				while ($objObjekteCalender->next()) {
209
					// Ermittlung Anzahl der Tage des angegebenen Monats (28 - 31)
210
					$intEndeMonat = (int) date('t', mktime(0, 0, 0, (int) $objObjekteCalender->StartMonat, (int) $objObjekteCalender->StartTag, (int) $objObjekteCalender->StartJahr));
211
					// d = 1, m = 1, e = 31, y = 2021, z = 0
212
					for ($d = (int) $objObjekteCalender->StartTag, $m = (int) $objObjekteCalender->StartMonat, $e = $intEndeMonat, $y = (int) $objObjekteCalender->StartJahr, $z = 0; ;) {
213
						// erster Tag der Buchung und weitere
214
						if ($z === 0) {
215
							// nur anzuzeigende Monate auswaehlen
216
							if (in_array($m, $this->belegungsplan_month)) {
217
								// Sonderfall letzter Buchungstag faellt auf Neujahr
218
								if ((int) $objObjekteCalender->BuchungsStartJahr < (int) $objObjekteCalender->BuchungsEndeJahr && (int) $objObjekteCalender->EndeTag === 1 && (int) $objObjekteCalender->EndeMonat === 1 && (int) $objObjekteCalender->StartTag === 1 && (int) $objObjekteCalender->StartMonat === 1)
219
								{
220
									if ($arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d])
221
									{
222
										$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#1';
223
									} else {
224
										$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#0';
225
									}
226
									break;
227
								}
228
								$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = $this->includeCalender($objObjekteCalender->BuchungsStartJahr, $objObjekteCalender->BuchungsEndeJahr, $y, $arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d], 0);
229
							}
230
							// Sonderfall Sylvester
231
							if ($d === 31 && $m === 12)
232
							{
233
								break;
234
							}
235
						} elseif ($y === (int) $objObjekteCalender->EndeJahr && $m === (int) $objObjekteCalender->EndeMonat && $d === (int) $objObjekteCalender->EndeTag) {
236
							// nur anzuzeigende Monate auswaehlen
237
							if (in_array($m, $this->belegungsplan_month)) {
238
								$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = $this->includeCalender($objObjekteCalender->BuchungsStartJahr, $objObjekteCalender->BuchungsEndeJahr, $y, $arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d], 1);
239
							}
240
							break;
241
						} else {
242
							// nur anzuzeigende Monate auswaehlen
243
							if (in_array($m, $this->belegungsplan_month)) {
244
								$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#1';
245
							}
246
						}
247
						if ($d === $e) {
248
							if ((int) $objObjekteCalender->StartMonat === (int) $objObjekteCalender->EndeMonat) {
249
								// nur anzuzeigende Monate auswaehlen
250
								if (in_array($m, $this->belegungsplan_month)) {
251
									$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#0';
252
								}
253
								break;
254
							}
255
							$m++;
256
							$d = 0;
257
							$e = (int) date('t', mktime(0, 0, 0, $m, $d + 1, $y));
258
						}
259
						$d++;
260
						$z++;
261
					}
262
				}
263
			}
264
			
265
			// Hole alle Jahre fuer die bereits Buchungen vorhanden sind ab dem aktuellen Jahr
266
			$objJahre = $this->Database->prepare("	SELECT YEAR(FROM_UNIXTIME(tbc.startDate)) as Start 
267
								FROM tl_belegungsplan_calender tbc,
268
									tl_belegungsplan_objekte tbo
269
								WHERE YEAR(FROM_UNIXTIME(tbc.startDate)) >= ? 
270
								AND tbc.pid = tbo.id
271
								AND tbo.published = 1
272
								GROUP BY YEAR(FROM_UNIXTIME(tbc.startDate))
273
								ORDER BY YEAR(FROM_UNIXTIME(tbc.startDate)) ASC")
274
								->execute($intMinYear);
275
			$this->intAnzahlJahre = $objJahre->numRows;
276
			if ($this->intAnzahlJahre > 0) {
277
				while ($objJahre->next()) {
278
					$arrJahre[] = array('single_year' => $objJahre->Start, 'year_href' => $this->strUrl . '?belegyear=' . $objJahre->Start, 'active' => $objJahre->Start == $intYear ? 1 : 0);
279
				}
280
			}
281
			
282
			// Hole alle Feiertage
283
			$objFeiertage = $this->Database->prepare("SELECT DAY(FROM_UNIXTIME(startDate)) as Tag,
284
									MONTH(FROM_UNIXTIME(startDate)) as Monat,
285
									YEAR(FROM_UNIXTIME(startDate)) as Jahr,
286
									title
287
							FROM 	tl_belegungsplan_feiertage
288
							WHERE 	startDate >= " . $this->intStartAuswahl . "
289
							AND 	startDate <= " . $this->intEndeAuswahl)
290
							->execute();
291
			if ($objFeiertage->numRows > 0) {
292
				while ($objFeiertage->next()) {
293
					$arrFeiertage[$objFeiertage->Jahr][$objFeiertage->Monat][$objFeiertage->Tag] = $objFeiertage->title;
294
				}
295
			}
296
		}
297
		
298
		$this->Template = new \FrontendTemplate($this->strTemplate);
299
		// Info-Array zur Ausgabe von Fehlern, Warnings und Defaults
300
		$this->Template->info = $arrInfo;
301
		// aktuell anzuzeigendes Jahr, wenn \Input::get('belegyear');
302
		$this->Template->display_year = $intYear;
303
		// Anzahl der anzuzeigenden Jahre fuer welche Reservierungen vorliegen
304
		$this->Template->number_year = $this->intAnzahlJahre;
305
		// Jahreszahlen fuer die Auswahlbox
306
		$this->Template->selectable_year = $arrJahre;
307
		// Anzahl anzuzeigender Objekte
308
		$this->Template->number_objekte = $i;
309
		// Kategorien sortieren wie im Checkboxwizard ausgewaehlt -> Elterntabelle
310
		$this->Template->CategorieObjekteCalender = $this->sortNachWizard($arrCategorieObjekte, $this->belegungsplan_category);
311
		// Array mit den Monatsdaten
312
		$this->Template->Month = $this->dataMonth($arrBelegungsplanMonth, $this->intStartAuswahl, $arrFeiertage);
313
		// Text fuer Legende
314
		$this->Template->Frei = $GLOBALS['TL_LANG']['mailwurm_belegung']['legende']['frei'];
315
		$this->Template->Belegt = $GLOBALS['TL_LANG']['mailwurm_belegung']['legende']['belegt'];
316
		
317
		if (!empty($arrCategorieObjekte)) {
318
			unset($arrCategorieObjekte);
319
		}
320
		if (!empty($arrInfo)) {
321
			unset($arrInfo);
322
		}
323
		if (!empty($arrFeiertage)) {
324
			unset($arrFeiertage);
325
		}
326
		// Clear the $_GET array (see #2445)
327
		if ($blnClearInput) {
328
			\Input::setGet('belegyear', null);
329
		}
330
	}
331
	
332
	/**
333
	 * Sortiert die Kategorien nach Auswahl im Checkbox-Wizard
334
	 *
335
	 * @return array
336
	 */
337
	protected function sortNachWizard($arrCategorieObjekte, $arrBelegungsplanCategory)
338
	{	
339
		// Schluessel und Werte tauschen
340
		$arrHelper = array_flip($arrBelegungsplanCategory);
341
		
342
		foreach ($arrHelper as $key => $value) {
343
			if (array_key_exists($key, $arrCategorieObjekte)) {
344
				$arrHelper[$key] = $arrCategorieObjekte[$key];
345
				// Objekte in der Kategorie gleich mit nach DB sortieren
346
				ksort($arrHelper[$key]['Objekte']);
347
			} else {
348
				unset($arrHelper[$key]);
349
			}
350
		}
351
		// leere Einträge entfernen
352
		return $arrHelper;
353
	}
354
	
355
	/**
356
	 * Fuegt den Monaten Daten hinzu
357
	 *
358
	 * @return array
359
	 */
360
	protected function dataMonth($arrMonth, $intStartAuswahl, $arrFeiertage)
361
	{
362
		$arrHelper = array();
363
		$intJahr = date('Y', $intStartAuswahl);
364
		foreach ($arrMonth as $key => $value) {
365
			$arrHelper[$value]['Name'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['month'][$value];
366
			$arrHelper[$value]['TageMonat'] = (int) date('t', mktime(0, 0, 0, (int) $value, 1, (int) $intJahr));
367
			$arrHelper[$value]['ColSpan'] = $arrHelper[$value]['TageMonat'] + 1;
368
			$intFirstDayInMonth = (int) date('N', mktime(0, 0, 0, (int) $value, 1, (int) $intJahr));
369
			for ($f = 1, $i = $intFirstDayInMonth; $f <= $arrHelper[$value]['TageMonat']; $f++) {
370
				$strClass = '';
371
				$arrHelper[$value]['Days'][$f]['Day'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['day'][$i];
372
				$arrHelper[$value]['Days'][$f]['DayCut'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['short_cut_day'][$i];
373
				$arrHelper[$value]['Days'][$f]['DayWeekNum'] = $i;
374
				$i === 6 ? $strClass .= ' saturday' : '';
375
				$i === 7 ? $strClass .= ' sunday' : '';
376
				if (!empty($arrFeiertage[$intJahr][$value][$f])) {
377
					$strClass .= ' holiday';
378
					$arrHelper[$value]['Days'][$f]['Holiday'] = $arrFeiertage[$intJahr][$value][$f];
379
				}
380
				$arrHelper[$value]['Days'][$f]['Class'] = trim($strClass);
381
				$i === 7 ? $i = 1 : $i++;
382
			}
383
		}
384
		unset($intJahr);
385
		unset($arrFeiertage);
386
		return $arrHelper;
387
	}
388
	
389
	/**
390
	 * Ausgabe fuer Kalender
391
	 *
392
	 * @param integer $intBuchungsStartJahr
393
	 * @param integer $intBuchungsEndeJahr
394
	 * @param integer $intY
395
	 * @param array $arrCategoriesObjekte
396
	 * @param integer $z
397
	 *
398
	 * @return string
399
	 */
400
	protected function includeCalender($intBuchungsStartJahr, $intBuchungsEndeJahr, $intY, $arrCategoriesObjekte, $z)
401
	{
402
		$strReturn = '';
403
		$intBuchungJahr = empty($z) ? (int) $intBuchungsStartJahr : (int) $intBuchungsEndeJahr;
404
		// bei Jahresuebergreifender Buchung
405
		if ((int) $intBuchungsStartJahr != (int) $intBuchungsEndeJahr) {
406
			// bei Jahresuebergreifender Buchung
407
			if ($intY === $intBuchungJahr) {
408
				$strReturn = empty($z) ? '0#1' : '1#0';
409
			} else {
410
				$strReturn = '1#1';
411
			}
412
		} else {
413
			// wenn letzter Tag einer Buchung gleich dem ersten Tag einer neuer Buchung
414
			if (isset($arrCategoriesObjekte)) {
415
				$strReturn = '1#1';
416
			} else {
417
				$strReturn = empty($z) ? '0#1' : '1#0';
418
			}
419
		}
420
		return $strReturn;
421
	}
422
}
423