We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 54 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Calendar 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 Calendar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Calendar extends \Kitodo\Dlf\Common\AbstractPlugin |
||
| 29 | { |
||
| 30 | public $scriptRelPath = 'Classes/Plugin/Calendar.php'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * This holds all issues for the list view. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected $allIssues = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The main method of the PlugIn |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * |
||
| 45 | * @param string $content: The PlugIn content |
||
| 46 | * @param array $conf: The PlugIn configuration |
||
| 47 | * |
||
| 48 | * @return string The content that is displayed on the website |
||
| 49 | */ |
||
| 50 | public function main($content, $conf) |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The Calendar Method |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * |
||
| 61 | * @param string $content: The PlugIn content |
||
| 62 | * @param array $conf: The PlugIn configuration |
||
| 63 | * |
||
| 64 | * @return string The content that is displayed on the website |
||
| 65 | */ |
||
| 66 | public function calendar($content, $conf) |
||
| 67 | { |
||
| 68 | $this->init($conf); |
||
| 69 | // Load current document. |
||
| 70 | $this->loadDocument(); |
||
| 71 | if ($this->doc === null) { |
||
| 72 | // Quit without doing anything if required variables are not set. |
||
| 73 | return $content; |
||
| 74 | } |
||
| 75 | // Load template file. |
||
| 76 | $this->getTemplate('###TEMPLATECALENDAR###'); |
||
| 77 | |||
| 78 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 79 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 80 | |||
| 81 | // Get all children of year anchor. |
||
| 82 | $result = $queryBuilder |
||
| 83 | ->select( |
||
| 84 | 'tx_dlf_documents.uid AS uid', |
||
| 85 | 'tx_dlf_documents.title AS title', |
||
| 86 | 'tx_dlf_documents.year AS year', |
||
| 87 | 'tx_dlf_documents.mets_label AS label', |
||
| 88 | 'tx_dlf_documents.mets_orderlabel AS orderlabel' |
||
| 89 | ) |
||
| 90 | ->from('tx_dlf_documents') |
||
| 91 | ->where( |
||
| 92 | $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('issue', 'tx_dlf_structures', $this->doc->pid)), |
||
| 93 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)), |
||
| 94 | Helper::whereExpression('tx_dlf_documents') |
||
| 95 | ) |
||
| 96 | ->orderBy('tx_dlf_documents.mets_orderlabel') |
||
| 97 | ->execute(); |
||
| 98 | |||
| 99 | $issues = []; |
||
| 100 | |||
| 101 | // Process results. |
||
| 102 | while ($resArray = $result->fetch()) { |
||
| 103 | // Set title for display in calendar view. |
||
| 104 | if (!empty($resArray['title'])) { |
||
| 105 | $title = $resArray['title']; |
||
| 106 | } else { |
||
| 107 | $title = !empty($resArray['label']) ? $resArray['label'] : $resArray['orderlabel']; |
||
| 108 | if (strtotime($title) !== false) { |
||
| 109 | $title = strftime('%x', strtotime($title)); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $issues[] = [ |
||
| 113 | 'uid' => $resArray['uid'], |
||
| 114 | 'title' => $title, |
||
| 115 | 'year' => $resArray['year'] |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | // We need an array of issues with year => month => day number as key. |
||
| 119 | $calendarIssuesByYear = []; |
||
| 120 | foreach ($issues as $issue) { |
||
| 121 | $dateTimestamp = strtotime($issue['year']); |
||
| 122 | if ($dateTimestamp !== false) { |
||
| 123 | $_year = date('Y', $dateTimestamp); |
||
| 124 | $_month = date('n', $dateTimestamp); |
||
| 125 | $_day = date('j', $dateTimestamp); |
||
| 126 | $calendarIssuesByYear[$_year][$_month][$_day][] = $issue; |
||
| 127 | } else { |
||
| 128 | Helper::devLog('Document with UID ' . $issue['uid'] . 'has no valid date of publication', DEVLOG_SEVERITY_WARNING); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | // Sort by years. |
||
| 132 | ksort($calendarIssuesByYear); |
||
| 133 | // Build calendar for year (default) or season. |
||
| 134 | $subPartContent = ''; |
||
| 135 | $iteration = 1; |
||
| 136 | foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) { |
||
| 137 | // Sort by months. |
||
| 138 | ksort($calendarIssuesByMonth); |
||
| 139 | // Default: First monath is January, last month is December. |
||
| 140 | $firstMonth = 1; |
||
| 141 | $lastMonth = 12; |
||
| 142 | // Show calendar from first issue up to end of season if applicable. |
||
| 143 | if ( |
||
| 144 | empty($this->conf['showEmptyMonths']) |
||
| 145 | && count($calendarIssuesByYear) > 1 |
||
| 146 | ) { |
||
| 147 | if ($iteration == 1) { |
||
| 148 | $firstMonth = (int) key($calendarIssuesByMonth); |
||
| 149 | } elseif ($iteration == count($calendarIssuesByYear)) { |
||
| 150 | end($calendarIssuesByMonth); |
||
| 151 | $lastMonth = (int) key($calendarIssuesByMonth); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $subPartContent .= $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth); |
||
| 155 | $iteration++; |
||
| 156 | } |
||
| 157 | // Prepare list as alternative view. |
||
| 158 | $subPartContentList = ''; |
||
| 159 | // Get subpart templates. |
||
| 160 | $subParts['list'] = $this->templateService->getSubpart($this->template, '###ISSUELIST###'); |
||
| 161 | $subParts['singleday'] = $this->templateService->getSubpart($subParts['list'], '###SINGLEDAY###'); |
||
| 162 | foreach ($this->allIssues as $dayTimestamp => $issues) { |
||
| 163 | $markerArrayDay['###DATE_STRING###'] = strftime('%A, %x', $dayTimestamp); |
||
| 164 | $markerArrayDay['###ITEMS###'] = ''; |
||
| 165 | foreach ($issues as $issue) { |
||
| 166 | $markerArrayDay['###ITEMS###'] .= $issue; |
||
| 167 | } |
||
| 168 | $subPartContentList .= $this->templateService->substituteMarkerArray($subParts['singleday'], $markerArrayDay); |
||
| 169 | } |
||
| 170 | $this->template = $this->templateService->substituteSubpart($this->template, '###SINGLEDAY###', $subPartContentList); |
||
| 171 | // Link to current year. |
||
| 172 | $linkConf = [ |
||
| 173 | 'useCacheHash' => 1, |
||
| 174 | 'parameter' => $this->conf['targetPid'], |
||
| 175 | 'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->uid), |
||
| 176 | ]; |
||
| 177 | $linkTitleData = $this->doc->getTitledata(); |
||
| 178 | $linkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0]; |
||
| 179 | $yearLink = $this->cObj->typoLink($linkTitle, $linkConf); |
||
| 180 | // Link to years overview. |
||
| 181 | $linkConf = [ |
||
| 182 | 'useCacheHash' => 1, |
||
| 183 | 'parameter' => $this->conf['targetPid'], |
||
| 184 | 'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->parentId), |
||
| 185 | ]; |
||
| 186 | $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', true) . ' ' . $this->doc->getTitle($this->doc->parentId), $linkConf); |
||
| 187 | // Fill marker array. |
||
| 188 | $markerArray = [ |
||
| 189 | '###CALENDARVIEWACTIVE###' => count($this->allIssues) > 5 ? 'active' : '', |
||
| 190 | '###LISTVIEWACTIVE###' => count($this->allIssues) < 6 ? 'active' : '', |
||
| 191 | '###CALYEAR###' => $yearLink, |
||
| 192 | '###CALALLYEARS###' => $allYearsLink, |
||
| 193 | '###LABEL_CALENDAR###' => $this->pi_getLL('label.view_calendar'), |
||
| 194 | '###LABEL_LIST_VIEW###' => $this->pi_getLL('label.view_list'), |
||
| 195 | ]; |
||
| 196 | $this->template = $this->templateService->substituteMarkerArray($this->template, $markerArray); |
||
| 197 | return $this->templateService->substituteSubpart($this->template, '###CALMONTH###', $subPartContent); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Build calendar for a certain year |
||
| 202 | * |
||
| 203 | * @access protected |
||
| 204 | * |
||
| 205 | * @param array $calendarIssuesByMonth All issues sorted by month => day |
||
| 206 | * @param int $year Gregorian year |
||
| 207 | * @param int $firstMonth 1 for January, 2 for February, ... 12 for December |
||
| 208 | * @param int $lastMonth 1 for January, 2 for February, ... 12 for December |
||
| 209 | * |
||
| 210 | * @return string Content for template subpart |
||
| 211 | */ |
||
| 212 | protected function getCalendarYear($calendarIssuesByMonth, $year, $firstMonth = 1, $lastMonth = 12) |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * The Years Method |
||
| 323 | * |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @param string $content: The PlugIn content |
||
| 327 | * @param array $conf: The PlugIn configuration |
||
| 328 | * |
||
| 329 | * @return string The content that is displayed on the website |
||
| 330 | */ |
||
| 331 | public function years($content, $conf) |
||
| 405 | } |
||
| 406 | } |
||
| 407 |