| Total Complexity | 42 |
| Total Lines | 286 |
| 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 | * Display a wildcard in the back end |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function generate() |
||
| 37 | {
|
||
| 38 | if (TL_MODE == 'BE') |
||
| 39 | {
|
||
| 40 | /** @var BackendTemplate|object $objTemplate */ |
||
| 41 | $objTemplate = new \BackendTemplate('be_wildcard');
|
||
| 42 | $objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['belegungsplan'][0]) . ' ###'; |
||
| 43 | $objTemplate->title = $this->headline; |
||
| 44 | $objTemplate->id = $this->id; |
||
| 45 | $objTemplate->link = $this->name; |
||
| 46 | $objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id; |
||
| 47 | return $objTemplate->parse(); |
||
| 48 | } |
||
| 49 | $this->belegungsplan_category = \StringUtil::deserialize($this->belegungsplan_categories); |
||
| 50 | $this->belegungsplan_month = \StringUtil::deserialize($this->belegungsplan_month); |
||
| 51 | // aktuelle Seiten URL |
||
| 52 | $this->strUrl = preg_replace('/\?.*$/', '', \Environment::get('request'));
|
||
| 53 | |||
| 54 | // Return if there are no categories |
||
| 55 | if (!is_array($this->belegungsplan_category) || empty($this->belegungsplan_category)) |
||
| 56 | {
|
||
| 57 | return ''; |
||
| 58 | } |
||
| 59 | // Return if there are no month |
||
| 60 | if (!is_array($this->belegungsplan_month) || empty($this->belegungsplan_month)) |
||
| 61 | {
|
||
| 62 | return ''; |
||
| 63 | } |
||
| 64 | return parent::generate(); |
||
| 65 | } |
||
| 66 | /** |
||
| 67 | * Generate the module |
||
| 68 | */ |
||
| 69 | protected function compile() |
||
| 70 | {
|
||
| 71 | $arrInfo = array(); |
||
| 72 | $arrCategorieObjekte = array(); |
||
| 73 | $arrJahre = array(); |
||
| 74 | $arrObjekteCalender = array(); |
||
| 75 | $intStartAuswahl = 0; |
||
| 76 | $intEndeAuswahl = 0; |
||
| 77 | // Monate sortieren |
||
| 78 | $arrBelegungsplanMonth = $this->belegungsplan_month; |
||
| 79 | sort($arrBelegungsplanMonth, SORT_NUMERIC); |
||
| 80 | $this->belegungsplan_month = $arrBelegungsplanMonth; |
||
| 81 | |||
| 82 | $blnClearInput = false; |
||
| 83 | |||
| 84 | // wenn der letzte anzuzeigende Monat verstrichen ist automatisch das nächste Jahr anzeigen |
||
| 85 | $intMax = (int)max($this->belegungsplan_month); |
||
| 86 | |||
| 87 | $intYear = \Input::get('belegyear');
|
||
| 88 | // interner Zaehler |
||
| 89 | $i = 0; |
||
| 90 | |||
| 91 | // Aktuelle Periode bei Erstaufruf der Seite |
||
| 92 | if (!isset($_GET['belegyear'])) {
|
||
| 93 | $intYear = $intMax < (int)date('n') ? (int)date('Y') + 1 : (int)date('Y');
|
||
| 94 | $blnClearInput = true; |
||
| 95 | } else {
|
||
| 96 | if(!empty($intYear)) {
|
||
| 97 | 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];
|
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // wenn $arrInfo hier schon belegt, dann nicht erst weiter machen |
||
| 102 | if(empty($arrInfo)) {
|
||
| 103 | // Anfang und Ende des Anzeigezeitraumes je nach GET |
||
| 104 | if(!empty($intYear)) {
|
||
| 105 | $intStartAuswahl = mktime(0, 0, 0, 1, 1, $intYear); |
||
| 106 | $intEndeAuswahl = mktime(23, 59, 59, 12, 31, $intYear); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Hole alle Calenderdaten zur Auswahl |
||
| 110 | $objObjekteCalender = $this->Database->prepare("SELECT tbo.id as ObjektID,
|
||
| 111 | (CASE |
||
| 112 | WHEN tbc.startDate < ".$intStartAuswahl." THEN DAY(FROM_UNIXTIME(".$intStartAuswahl."))
|
||
|
|
|||
| 113 | ELSE DAY(FROM_UNIXTIME(tbc.startDate)) |
||
| 114 | END) as StartTag, |
||
| 115 | (CASE |
||
| 116 | WHEN tbc.startDate < ".$intStartAuswahl." THEN MONTH(FROM_UNIXTIME(".$intStartAuswahl."))
|
||
| 117 | ELSE MONTH(FROM_UNIXTIME(tbc.startDate)) |
||
| 118 | END) as StartMonat, |
||
| 119 | (CASE |
||
| 120 | WHEN tbc.startDate < ".$intStartAuswahl." THEN YEAR(FROM_UNIXTIME(".$intStartAuswahl."))
|
||
| 121 | ELSE YEAR(FROM_UNIXTIME(tbc.startDate)) |
||
| 122 | END) as StartJahr, |
||
| 123 | (CASE |
||
| 124 | WHEN tbc.endDate > ".$intEndeAuswahl." THEN DAY(FROM_UNIXTIME(".$intEndeAuswahl."))
|
||
| 125 | ELSE DAY(FROM_UNIXTIME(tbc.endDate)) |
||
| 126 | END) as EndeTag, |
||
| 127 | (CASE |
||
| 128 | WHEN tbc.endDate > ".$intEndeAuswahl." THEN MONTH(FROM_UNIXTIME(".$intEndeAuswahl."))
|
||
| 129 | ELSE MONTH(FROM_UNIXTIME(tbc.endDate)) |
||
| 130 | END) as EndeMonat, |
||
| 131 | (CASE |
||
| 132 | WHEN tbc.endDate > ".$intEndeAuswahl." THEN YEAR(FROM_UNIXTIME(".$intEndeAuswahl."))
|
||
| 133 | ELSE YEAR(FROM_UNIXTIME(tbc.endDate)) |
||
| 134 | END) as EndeJahr |
||
| 135 | FROM tl_belegungsplan_calender tbc, |
||
| 136 | tl_belegungsplan_objekte tbo |
||
| 137 | WHERE tbc.pid = tbo.id |
||
| 138 | AND tbo.published = 1 |
||
| 139 | AND ((startDate < ".$intStartAuswahl." AND endDate > ".$intStartAuswahl.") OR (startDate >= ".$intStartAuswahl." AND endDate <= ".$intEndeAuswahl.") OR (startDate < ".$intEndeAuswahl." AND endDate > ".$intEndeAuswahl."))") |
||
| 140 | ->execute(); |
||
| 141 | if($objObjekteCalender->numRows > 0) {
|
||
| 142 | while($objObjekteCalender->next()) {
|
||
| 143 | $arrHelper = array(); |
||
| 144 | $arrHelper['ObjektID'] = $objObjekteCalender->ObjektID; |
||
| 145 | $arrHelper['StartTag'] = (int)$objObjekteCalender->StartTag; |
||
| 146 | $arrHelper['StartMonat'] = (int)$objObjekteCalender->StartMonat; |
||
| 147 | $arrHelper['StartJahr'] = (int)$objObjekteCalender->StartJahr; |
||
| 148 | $arrHelper['EndeTag'] = (int)$objObjekteCalender->EndeTag; |
||
| 149 | $arrHelper['EndeMonat'] = (int)$objObjekteCalender->EndeMonat; |
||
| 150 | $arrHelper['EndeJahr'] = (int)$objObjekteCalender->EndeJahr; |
||
| 151 | $intEndeMonat = (int)date('t', mktime(0, 0, 0, $arrHelper['StartMonat'], $arrHelper['StartTag'], $arrHelper['StartJahr']));
|
||
| 152 | for($d = $arrHelper['StartTag'], $m = $arrHelper['StartMonat'], $e = $intEndeMonat, $y = $arrHelper['StartJahr'], $z = 0; ; ) {
|
||
| 153 | // erster Tag der Buchung und weitere |
||
| 154 | empty($z) ? $arrObjekteCalender[$objObjekteCalender->ObjektID][$m][$d] = '0/1' : $arrObjekteCalender[$objObjekteCalender->ObjektID][$m][$d] = '1/1'; |
||
| 155 | if($d === $e) {
|
||
| 156 | if($arrHelper['StartMonat'] === $arrHelper['EndeMonat']) {
|
||
| 157 | break; |
||
| 158 | } |
||
| 159 | $m++; |
||
| 160 | $d = 0; |
||
| 161 | $e = (int)date('t', mktime(0, 0, 0, $m, $d + 1, $y));
|
||
| 162 | } |
||
| 163 | if($y === $arrHelper['EndeJahr'] && $m === $arrHelper['EndeMonat'] && $d === $arrHelper['EndeTag']) {
|
||
| 164 | // letzter Tag der Buchung |
||
| 165 | $arrObjekteCalender[$objObjekteCalender->ObjektID][$m][$d] = '1/0'; |
||
| 166 | break; |
||
| 167 | } |
||
| 168 | $d++; |
||
| 169 | $z++; |
||
| 170 | } |
||
| 171 | unset($arrHelper); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | // Hole alle aktiven Objekte inklusive dazugehoeriger Kategorie |
||
| 176 | $objCategoryObjekte = $this->Database->prepare("SELECT tbc.id as CategoryID,
|
||
| 177 | tbc.title as CategoryTitle, |
||
| 178 | tbo.id as ObjektID, |
||
| 179 | tbo.name as ObjektName, |
||
| 180 | tbo.infotext as ObjektInfoText, |
||
| 181 | tbo.sorting as ObjektSortierung |
||
| 182 | FROM tl_belegungsplan_category tbc, |
||
| 183 | tl_belegungsplan_objekte tbo |
||
| 184 | WHERE tbo.pid = tbc.id |
||
| 185 | AND tbo.published = 1") |
||
| 186 | ->execute(); |
||
| 187 | if($objCategoryObjekte->numRows > 0) {
|
||
| 188 | while($objCategoryObjekte->next()) {
|
||
| 189 | // Nicht anzuzeigende Kategorien aussortieren |
||
| 190 | if(in_array($objCategoryObjekte->CategoryID, $this->belegungsplan_category)) {
|
||
| 191 | $arrHelper = array(); |
||
| 192 | $arrHelper['ObjektID'] = (int)$objCategoryObjekte->ObjektID; |
||
| 193 | $arrHelper['ObjektName'] = $objCategoryObjekte->ObjektName; |
||
| 194 | $arrHelper['ObjektInfoText'] = $objCategoryObjekte->ObjektInfoText; |
||
| 195 | // Calender anfügen wenn vorhanden |
||
| 196 | if(array_key_exists($arrHelper['ObjektID'], $arrObjekteCalender)) {
|
||
| 197 | $arrHelper['Calender'] = $arrObjekteCalender[$arrHelper['ObjektID']]; |
||
| 198 | unset($arrObjekteCalender[$arrHelper['ObjektID']]); |
||
| 199 | } |
||
| 200 | if(array_key_exists($objCategoryObjekte->CategoryID, $arrCategorieObjekte)) {
|
||
| 201 | $arrCategorieObjekte[$objCategoryObjekte->CategoryID]['Objekte'][$objCategoryObjekte->ObjektSortierung] = $arrHelper; |
||
| 202 | $i++; |
||
| 203 | } else {
|
||
| 204 | $arrCategorieObjekte[$objCategoryObjekte->CategoryID]['CategoryTitle'] = \StringUtil::specialchars($objCategoryObjekte->CategoryTitle); |
||
| 205 | $arrCategorieObjekte[$objCategoryObjekte->CategoryID]['Objekte'][$objCategoryObjekte->ObjektSortierung] = $arrHelper; |
||
| 206 | $i++; |
||
| 207 | } |
||
| 208 | unset($arrHelper); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } else {
|
||
| 212 | $arrInfo[] = '3. ' . $GLOBALS['TL_LANG']['mailwurm_belegung']['info'][0]; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Hole alle Jahre fuer die bereits Buchungen vorhanden sind ab dem aktuellen Jahr |
||
| 216 | $objJahre = $this->Database->prepare(" SELECT YEAR(FROM_UNIXTIME(startDate)) as Start
|
||
| 217 | FROM tl_belegungsplan_calender tbc, |
||
| 218 | tl_belegungsplan_objekte tbo |
||
| 219 | WHERE YEAR(FROM_UNIXTIME(startDate)) >= ? |
||
| 220 | AND tbc.pid = tbo.id |
||
| 221 | AND tbo.published = 1 |
||
| 222 | GROUP BY YEAR(FROM_UNIXTIME(startDate)) |
||
| 223 | ORDER BY YEAR(FROM_UNIXTIME(startDate)) ASC") |
||
| 224 | ->execute(date("Y"));
|
||
| 225 | if($objJahre->numRows > 0) {
|
||
| 226 | while($objJahre->next()) {
|
||
| 227 | $arrJahre[] = array('single_year' => $objJahre->Start, 'year_href' => $this->strUrl . '?belegyear=' . $objJahre->Start, 'active' => $objJahre->Start == $intYear ? 1 : 0);
|
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | $this->Template = new \FrontendTemplate($this->strTemplate); |
||
| 233 | // Info-Array zur Ausgabe von Fehlern, Warnings und Defaults |
||
| 234 | $this->Template->info = $arrInfo; |
||
| 235 | // aktuell anzuzeigendes Jahr, wenn \Input::get('year');
|
||
| 236 | $this->Template->display_year = $intYear; |
||
| 237 | // Anzahl der anzuzeigenden Jahre fuer welche Reservierungen vorliegen |
||
| 238 | $this->Template->number_year = $objJahre->numRows; |
||
| 239 | // Jahreszahlen fuer die Auswahlbox |
||
| 240 | $this->Template->selectable_year = $arrJahre; |
||
| 241 | // Anzahl anzuzeigender Objekte |
||
| 242 | $this->Template->number_objekte = $i; |
||
| 243 | // Kategorien sortieren wie im Checkboxwizard ausgewaehlt -> Elterntabelle |
||
| 244 | $this->Template->CategorieObjekteCalender = $this->sortNachWizard($arrCategorieObjekte, $this->belegungsplan_category); |
||
| 245 | // Array mit den Monatsdaten |
||
| 246 | $this->Template->Month = $this->dataMonth($arrBelegungsplanMonth, $intStartAuswahl); |
||
| 247 | #$this->Template->Start = $intStartAuswahl; |
||
| 248 | #$this->Template->Ende = $intEndeAuswahl; |
||
| 249 | #$this->Template->Monate = $arrBelegungsplanMonth; |
||
| 250 | #$this->Template->MaxMonat = $intMax; |
||
| 251 | |||
| 252 | if(!empty($arrCategorieObjekte)) {
|
||
| 253 | unset($arrCategorieObjekte); |
||
| 254 | } |
||
| 255 | if(!empty($arrInfo)) {
|
||
| 256 | unset($arrInfo); |
||
| 257 | } |
||
| 258 | // Clear the $_GET array (see #2445) |
||
| 259 | if($blnClearInput) {
|
||
| 260 | \Input::setGet('belegyear', null);
|
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Sortiert die Kategorien nach Auswahl im Checkbox-Wizard |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | protected function sortNachWizard($arrCategorieObjekte, $arrBelegungsplanCategory) |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Fuegt den Monaten Daten hinzu |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | protected function dataMonth($arrMonth, $intStartAuswahl) |
||
| 309 | } |
||
| 310 | } |
||
| 311 |