| Total Complexity | 55 |
| Total Lines | 368 |
| 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 |
||
| 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() |
||
| 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(); |
||
| 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)) {
|
||
| 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) |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Ausgabe fuer Kalender |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | protected function includeCalender($intBuchungsStartJahr, $intBuchungsEndeJahr, $intY, $arrCategoriesObjekte, $z) |
||
| 391 |