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