We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 18 |
| Paths | 2161 |
| Total Lines | 132 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 98 | public function calendar($content, $conf) |
||
| 99 | { |
||
| 100 | $this->init($conf); |
||
| 101 | // Load current document. |
||
| 102 | $this->loadDocument(); |
||
| 103 | if ($this->doc === null) { |
||
| 104 | // Quit without doing anything if required variables are not set. |
||
| 105 | return $content; |
||
| 106 | } |
||
| 107 | // Load template file. |
||
| 108 | $this->getTemplate('###TEMPLATECALENDAR###'); |
||
| 109 | |||
| 110 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 111 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 112 | |||
| 113 | // Get all children of year anchor. |
||
| 114 | $result = $queryBuilder |
||
| 115 | ->select( |
||
| 116 | 'tx_dlf_documents.uid AS uid', |
||
| 117 | 'tx_dlf_documents.title AS title', |
||
| 118 | 'tx_dlf_documents.year AS year', |
||
| 119 | 'tx_dlf_documents.mets_label AS label', |
||
| 120 | 'tx_dlf_documents.mets_orderlabel AS orderlabel' |
||
| 121 | ) |
||
| 122 | ->from('tx_dlf_documents') |
||
| 123 | ->where( |
||
| 124 | $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('issue', 'tx_dlf_structures', $this->doc->cPid)), |
||
| 125 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)), |
||
| 126 | Helper::whereExpression('tx_dlf_documents') |
||
| 127 | ) |
||
| 128 | ->orderBy('tx_dlf_documents.mets_orderlabel') |
||
| 129 | ->execute(); |
||
| 130 | |||
| 131 | $issues = []; |
||
| 132 | |||
| 133 | // Process results. |
||
| 134 | while ($resArray = $result->fetch()) { |
||
| 135 | // Set title for display in calendar view. |
||
| 136 | if (!empty($resArray['title'])) { |
||
| 137 | $title = $resArray['title']; |
||
| 138 | } else { |
||
| 139 | $title = !empty($resArray['label']) ? $resArray['label'] : $resArray['orderlabel']; |
||
| 140 | if (strtotime($title) !== false) { |
||
| 141 | $title = strftime('%x', strtotime($title)); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $issues[] = [ |
||
| 145 | 'uid' => $resArray['uid'], |
||
| 146 | 'title' => $title, |
||
| 147 | 'year' => $resArray['year'] |
||
| 148 | ]; |
||
| 149 | } |
||
| 150 | // We need an array of issues with year => month => day number as key. |
||
| 151 | $calendarIssuesByYear = []; |
||
| 152 | foreach ($issues as $issue) { |
||
| 153 | $dateTimestamp = strtotime($issue['year']); |
||
| 154 | if ($dateTimestamp !== false) { |
||
| 155 | $_year = date('Y', $dateTimestamp); |
||
| 156 | $_month = date('n', $dateTimestamp); |
||
| 157 | $_day = date('j', $dateTimestamp); |
||
| 158 | $calendarIssuesByYear[$_year][$_month][$_day][] = $issue; |
||
| 159 | } else { |
||
| 160 | Helper::devLog('Document with UID ' . $issue['uid'] . 'has no valid date of publication', DEVLOG_SEVERITY_WARNING); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | // Sort by years. |
||
| 164 | ksort($calendarIssuesByYear); |
||
| 165 | // Build calendar for year (default) or season. |
||
| 166 | $subPartContent = ''; |
||
| 167 | $iteration = 1; |
||
| 168 | foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) { |
||
| 169 | // Sort by months. |
||
| 170 | ksort($calendarIssuesByMonth); |
||
| 171 | // Default: First month is January, last month is December. |
||
| 172 | $firstMonth = 1; |
||
| 173 | $lastMonth = 12; |
||
| 174 | // Show calendar from first issue up to end of season if applicable. |
||
| 175 | if ( |
||
| 176 | empty($this->conf['showEmptyMonths']) |
||
| 177 | && count($calendarIssuesByYear) > 1 |
||
| 178 | ) { |
||
| 179 | if ($iteration == 1) { |
||
| 180 | $firstMonth = (int) key($calendarIssuesByMonth); |
||
| 181 | } elseif ($iteration == count($calendarIssuesByYear)) { |
||
| 182 | end($calendarIssuesByMonth); |
||
| 183 | $lastMonth = (int) key($calendarIssuesByMonth); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | $subPartContent .= $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth); |
||
| 187 | $iteration++; |
||
| 188 | } |
||
| 189 | // Prepare list as alternative view. |
||
| 190 | $subPartContentList = ''; |
||
| 191 | // Get subpart templates. |
||
| 192 | $subParts['list'] = $this->templateService->getSubpart($this->template, '###ISSUELIST###'); |
||
| 193 | $subParts['singleday'] = $this->templateService->getSubpart($subParts['list'], '###SINGLEDAY###'); |
||
| 194 | foreach ($this->allIssues as $dayTimestamp => $issues) { |
||
| 195 | $markerArrayDay['###DATE_STRING###'] = strftime('%A, %x', $dayTimestamp); |
||
| 196 | $markerArrayDay['###ITEMS###'] = ''; |
||
| 197 | foreach ($issues as $issue) { |
||
| 198 | $markerArrayDay['###ITEMS###'] .= $issue; |
||
| 199 | } |
||
| 200 | $subPartContentList .= $this->templateService->substituteMarkerArray($subParts['singleday'], $markerArrayDay); |
||
| 201 | } |
||
| 202 | $this->template = $this->templateService->substituteSubpart($this->template, '###SINGLEDAY###', $subPartContentList); |
||
| 203 | // Link to current year. |
||
| 204 | $linkConf = [ |
||
| 205 | 'useCacheHash' => 1, |
||
| 206 | 'parameter' => $this->conf['targetPid'], |
||
| 207 | 'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->uid), |
||
| 208 | ]; |
||
| 209 | $linkTitleData = $this->doc->getTitledata(); |
||
| 210 | $linkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0]; |
||
| 211 | $yearLink = $this->cObj->typoLink($linkTitle, $linkConf); |
||
| 212 | // Link to years overview. |
||
| 213 | $linkConf = [ |
||
| 214 | 'useCacheHash' => 1, |
||
| 215 | 'parameter' => $this->conf['targetPid'], |
||
| 216 | 'additionalParams' => '&' . $this->prefixId . '[id]=' . urlencode($this->doc->parentId), |
||
| 217 | ]; |
||
| 218 | $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', true) . ' ' . $this->doc->getTitle($this->doc->parentId), $linkConf); |
||
| 219 | // Fill marker array. |
||
| 220 | $markerArray = [ |
||
| 221 | '###CALENDARVIEWACTIVE###' => count($this->allIssues) > 5 ? 'active' : '', |
||
| 222 | '###LISTVIEWACTIVE###' => count($this->allIssues) < 6 ? 'active' : '', |
||
| 223 | '###CALYEAR###' => $yearLink, |
||
| 224 | '###CALALLYEARS###' => $allYearsLink, |
||
| 225 | '###LABEL_CALENDAR###' => $this->pi_getLL('label.view_calendar'), |
||
| 226 | '###LABEL_LIST_VIEW###' => $this->pi_getLL('label.view_list'), |
||
| 227 | ]; |
||
| 228 | $this->template = $this->templateService->substituteMarkerArray($this->template, $markerArray); |
||
| 229 | return $this->templateService->substituteSubpart($this->template, '###CALMONTH###', $subPartContent); |
||
| 230 | } |
||
| 439 |