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 ( 79866d...70b5f4 )
by Jan
24:35 queued 17:10
created

ModuleBelegungsplan   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 368
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 368
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 211 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
	 * @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' can never be true.
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)) 
79
		{
80
			return '';
81
		}
82
		// Return if there are no month
83
		if (!is_array($this->belegungsplan_month) || empty($this->belegungsplan_month)) 
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
		$arrObjekteCalender = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $arrObjekteCalender is dead and can be removed.
Loading history...
99
		
100
		// Monate sortieren
101
		$arrBelegungsplanMonth = $this->belegungsplan_month;
102
		sort($arrBelegungsplanMonth, SORT_NUMERIC);
103
		$this->belegungsplan_month = $arrBelegungsplanMonth;
104
		
105
		$blnClearInput = false;
106
		
107
		// wenn der letzte anzuzeigende Monat verstrichen ist automatisch das nächste Jahr anzeigen
108
		$intMax = (int) max($this->belegungsplan_month);
109
		
110
		$intYear = \Input::get('belegyear');
111
		// interner Zaehler
112
		$i = 0;
113
		
114
		// Aktuelle Periode bei Erstaufruf der Seite
115
		if (!isset($_GET['belegyear'])) {	
116
			$intYear = $intMax < (int) date('n') ? (int) date('Y') + 1 : (int) date('Y');
117
			$blnClearInput = true;
118
		} else {
119
			if (!empty($intYear)) {
120
				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];
121
			}
122
		}
123
		$intMinYear = $intMax < (int) date('n') ? (int) date('Y') + 1 : (int) date('Y');
124
		
125
		// wenn $arrInfo hier schon belegt, dann nicht erst weiter machen
126
		if (empty($arrInfo)) {
127
			// Anfang und Ende des Anzeigezeitraumes je nach GET
128
			if (!empty($intYear)) {
129
				$this->intStartAuswahl = (int) mktime(0, 0, 0, 1, 1, $intYear);
130
				$this->intEndeAuswahl = (int) mktime(23, 59, 59, 12, 31, $intYear);
131
			}
132
			
133
			// Hole alle aktiven Objekte inklusive dazugehoeriger Kategorie
134
			$objCategoryObjekte = $this->Database->prepare("SELECT 	tbc.id as CategoryID,
135
										tbc.title as CategoryTitle,
136
										tbo.id as ObjektID,
137
										tbo.name as ObjektName,
138
										tbo.infotext as ObjektInfoText,
139
										tbo.sorting as ObjektSortierung
140
									FROM 	tl_belegungsplan_category tbc,
141
										tl_belegungsplan_objekte tbo
142
									WHERE	tbo.pid = tbc.id
143
									AND	tbo.published = 1")
144
									->execute();
145
			if ($objCategoryObjekte->numRows > 0) {
146
				while ($objCategoryObjekte->next()) {
147
					// Nicht anzuzeigende Kategorien aussortieren
148
					if (in_array($objCategoryObjekte->CategoryID, $this->belegungsplan_category)) {
149
						$arrHelper = array();
150
						$arrHelper['ObjektID'] = (int) $objCategoryObjekte->ObjektID;
151
						$arrHelper['ObjektName'] = \StringUtil::specialchars($objCategoryObjekte->ObjektName);
152
						$arrHelper['ObjektInfoText'] = $objCategoryObjekte->ObjektInfoText;
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		tbc.startDate < tbc.endDate
205
						AND 	((tbc.startDate < ? AND tbc.endDate > ?) OR (tbc.startDate >= ? AND tbc.endDate <= ?) OR (tbc.startDate < ? AND tbc.endDate > ?))")
206
						->execute($this->intStartAuswahl, $this->intStartAuswahl, $this->intStartAuswahl, $this->intEndeAuswahl, $this->intEndeAuswahl, $this->intEndeAuswahl);
207
						
208
			if ($objObjekteCalender->numRows > 0) {
209
				while ($objObjekteCalender->next()) {
210
					$intEndeMonat = (int) date('t', mktime(0, 0, 0, (int) $objObjekteCalender->StartMonat, (int) $objObjekteCalender->StartTag, (int) $objObjekteCalender->StartJahr));
211
					for ($d = (int) $objObjekteCalender->StartTag, $m = (int) $objObjekteCalender->StartMonat, $e = $intEndeMonat, $y = (int) $objObjekteCalender->StartJahr, $z = 0; ;) {
212
						// erster Tag der Buchung und weitere
213
						if (empty($z)) {
0 ignored issues
show
introduced by
The condition empty($z) can never be false.
Loading history...
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], 0);
215
						} elseif ($y === (int) $objObjekteCalender->EndeJahr && $m === (int) $objObjekteCalender->EndeMonat && $d === (int) $objObjekteCalender->EndeTag) {
216
							$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);
217
							break;
218
						} else {
219
							$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#1';
220
						}
221
						if ($d === $e) {
222
							if ((int) $objObjekteCalender->StartMonat === (int) $objObjekteCalender->EndeMonat) {
223
								$arrCategorieObjekte[$objObjekteCalender->CategoryID]['Objekte'][$objObjekteCalender->ObjektSortierung]['Calender'][$m][$d] = '1#0';
224
								break;
225
							}
226
							$m++;
227
							$d = 0;
228
							$e = (int) date('t', mktime(0, 0, 0, $m, $d + 1, $y));
229
						}
230
						$d++;
231
						$z++;
232
					}
233
				}
234
			}
235
			
236
			// Hole alle Jahre fuer die bereits Buchungen vorhanden sind ab dem aktuellen Jahr
237
			$objJahre = $this->Database->prepare("	SELECT YEAR(FROM_UNIXTIME(tbc.startDate)) as Start 
238
								FROM tl_belegungsplan_calender tbc,
239
									tl_belegungsplan_objekte tbo
240
								WHERE YEAR(FROM_UNIXTIME(tbc.startDate)) >= ? 
241
								AND tbc.pid = tbo.id
242
								AND tbo.published = 1
243
								GROUP BY YEAR(FROM_UNIXTIME(tbc.startDate))
244
								ORDER BY YEAR(FROM_UNIXTIME(tbc.startDate)) ASC")
245
								->execute($intMinYear);
246
			$this->intAnzahlJahre = $objJahre->numRows;
247
			if ($this->intAnzahlJahre > 0) {
248
				while ($objJahre->next()) {
249
					$arrJahre[] = array('single_year' => $objJahre->Start, 'year_href' => $this->strUrl . '?belegyear=' . $objJahre->Start, 'active' => $objJahre->Start == $intYear ? 1 : 0);
250
				}
251
			}
252
			
253
			// Hole alle Feiertage
254
			$objFeiertage = $this->Database->prepare("SELECT DAY(FROM_UNIXTIME(startDate)) as Tag,
255
									MONTH(FROM_UNIXTIME(startDate)) as Monat,
256
									YEAR(FROM_UNIXTIME(startDate)) as Jahr,
257
									title
258
							FROM 	tl_belegungsplan_feiertage
259
							WHERE 	startDate >= " . $this->intStartAuswahl . "
260
							AND 	startDate <= " . $this->intEndeAuswahl)
261
							->execute();
262
			if ($objFeiertage->numRows > 0) {
263
				while ($objFeiertage->next()) {
264
					$arrFeiertage[$objFeiertage->Jahr][$objFeiertage->Monat][$objFeiertage->Tag] = $objFeiertage->title;
265
				}
266
			}
267
		}
268
		
269
		$this->Template = new \FrontendTemplate($this->strTemplate);
270
		// Info-Array zur Ausgabe von Fehlern, Warnings und Defaults
271
		$this->Template->info = $arrInfo;
272
		// aktuell anzuzeigendes Jahr, wenn \Input::get('belegyear');
273
		$this->Template->display_year = $intYear;
274
		// Anzahl der anzuzeigenden Jahre fuer welche Reservierungen vorliegen
275
		$this->Template->number_year = $this->intAnzahlJahre;
276
		// Jahreszahlen fuer die Auswahlbox
277
		$this->Template->selectable_year = $arrJahre;
278
		// Anzahl anzuzeigender Objekte
279
		$this->Template->number_objekte = $i;
280
		// Kategorien sortieren wie im Checkboxwizard ausgewaehlt -> Elterntabelle
281
		$this->Template->CategorieObjekteCalender = $this->sortNachWizard($arrCategorieObjekte, $this->belegungsplan_category);
282
		// Array mit den Monatsdaten
283
		$this->Template->Month = $this->dataMonth($arrBelegungsplanMonth, $this->intStartAuswahl, $arrFeiertage);
284
		// Array mit den Feiertagen
285
		#$this->Template->Feiertage = $arrFeiertage;
286
		#$this->Template->Start = $this->intStartAuswahl;
287
		#$this->Template->Ende = $this->intEndeAuswahl;
288
		#$this->Template->Monate = $arrBelegungsplanMonth;
289
		#$this->Template->MaxMonat = $intMax;
290
		
291
		if (!empty($arrCategorieObjekte)) {
292
			unset($arrCategorieObjekte);
293
		}
294
		if (!empty($arrInfo)) {
295
			unset($arrInfo);
296
		}
297
		if (!empty($arrFeiertage)) {
298
			unset($arrFeiertage);
299
		}
300
		// Clear the $_GET array (see #2445)
301
		if ($blnClearInput) {
302
			\Input::setGet('belegyear', null);
303
		}
304
	}
305
	
306
	/**
307
	 * Sortiert die Kategorien nach Auswahl im Checkbox-Wizard
308
	 *
309
	 * @return array
310
	 */
311
	protected function sortNachWizard($arrCategorieObjekte, $arrBelegungsplanCategory)
312
	{	
313
		// Schluessel und Werte tauschen
314
		$arrHelper = array_flip($arrBelegungsplanCategory);
315
		
316
		foreach ($arrHelper as $key => $value) {
317
			if (array_key_exists($key, $arrCategorieObjekte)) {
318
				$arrHelper[$key] = $arrCategorieObjekte[$key];
319
				// Objekte in der Kategorie gleich mit nach DB sortieren
320
				ksort($arrHelper[$key]['Objekte']);
321
			} else {
322
				unset($arrHelper[$key]);
323
			}
324
		}
325
		// leere Einträge entfernen
326
		return $arrHelper;
327
	}
328
	
329
	/**
330
	 * Fuegt den Monaten Daten hinzu
331
	 *
332
	 * @return array
333
	 */
334
	protected function dataMonth($arrMonth, $intStartAuswahl, $arrFeiertage)
335
	{
336
		$arrHelper = array();
337
		$intJahr = date('Y', $intStartAuswahl);
338
		foreach ($arrMonth as $key => $value) {
339
			$arrHelper[$value]['Name'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['month'][$value];
340
			$arrHelper[$value]['TageMonat'] = (int) date('t', mktime(0, 0, 0, (int) $value, 1, (int) $intJahr));
341
			$arrHelper[$value]['ColSpan'] = $arrHelper[$value]['TageMonat'] + 1;
342
			$intFirstDayInMonth = (int) date('N', mktime(0, 0, 0, (int) $value, 1, (int) $intJahr));
343
			for ($f = 1, $i = $intFirstDayInMonth; $f <= $arrHelper[$value]['TageMonat']; $f++) {
344
				$strClass = '';
345
				$arrHelper[$value]['Days'][$f]['Day'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['day'][$i];
346
				$arrHelper[$value]['Days'][$f]['DayCut'] = $GLOBALS['TL_LANG']['mailwurm_belegung']['short_cut_day'][$i];
347
				$arrHelper[$value]['Days'][$f]['DayWeekNum'] = $i;
348
				$i === 6 ? $strClass .= ' saturday' : '';
349
				$i === 7 ? $strClass .= ' sunday' : '';
350
				if (!empty($arrFeiertage[$intJahr][$value][$f])) {
351
					$strClass .= ' holiday';
352
					$arrHelper[$value]['Days'][$f]['Holiday'] = $arrFeiertage[$intJahr][$value][$f];
353
				}
354
				$arrHelper[$value]['Days'][$f]['Class'] = trim($strClass);
355
				$i === 7 ? $i = 1 : $i++;
356
			}
357
		}
358
		unset($intJahr);
359
		unset($arrFeiertage);
360
		return $arrHelper;
361
	}
362
	
363
	/**
364
	 * Ausgabe fuer Kalender
365
	 *
366
	 * @return string
367
	 */
368
	protected function includeCalender($intBuchungsStartJahr, $intBuchungsEndeJahr, $intY, $arrCategoriesObjekte, $z)
369
	{
370
		$strReturn = '';
371
		$intBuchungJahr = empty($z) ? (int) $intBuchungsStartJahr : (int) $intBuchungsEndeJahr;
372
		// bei Jahresuebergreifender Buchung
373
		if ((int) $intBuchungsStartJahr != (int) $intBuchungsEndeJahr) {
374
			// bei Jahresuebergreifender Buchung
375
			if ($intY === $intBuchungJahr) {
376
				$strReturn = empty($z) ? '0#1' : '1#0';
377
			} else {
378
				$strReturn = '1#1';
379
			}
380
		} else {
381
			// wenn letzter Tag einer Buchung gleich dem ersten Tag einer neuer Buchung
382
			if (isset($arrCategoriesObjekte)) {
383
				$strReturn = '1#1';
384
			} else {
385
				$strReturn = empty($z) ? '0#1' : '1#0';
386
			}
387
		}
388
		return $strReturn;
389
	}
390
}
391