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 ( 0e63d2...55ecae )
by Jan
06:52 queued 16s
created

ModuleBelegungsplan   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 365
rs 6
c 0
b 0
f 0
wmc 55

5 Methods

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