Conditions | 36 |
Paths | 9409 |
Total Lines | 264 |
Code Lines | 127 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
54 | public function calendar($content, $conf) { |
||
55 | |||
56 | $this->init($conf); |
||
57 | |||
58 | // Load current document. |
||
59 | $this->loadDocument(); |
||
60 | |||
61 | if ($this->doc === NULL) { |
||
62 | |||
63 | // Quit without doing anything if required variables are not set. |
||
64 | return $content; |
||
65 | |||
66 | } |
||
67 | |||
68 | // Load template file. |
||
69 | if (!empty($this->conf['templateFile'])) { |
||
70 | |||
71 | $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATECALENDAR###'); |
||
72 | |||
73 | } else { |
||
74 | |||
75 | $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/newspaper/template.tmpl'), '###TEMPLATECALENDAR###'); |
||
76 | |||
77 | } |
||
78 | |||
79 | // Get all children of year anchor. |
||
80 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
81 | 'tx_dlf_documents.uid AS uid, tx_dlf_documents.title AS title, tx_dlf_documents.year AS year', |
||
82 | 'tx_dlf_documents', |
||
83 | '(tx_dlf_documents.structure='.tx_dlf_helper::getIdFromIndexName('issue', 'tx_dlf_structures', $this->doc->pid).' AND tx_dlf_documents.partof='.intval($this->doc->uid).')'.tx_dlf_helper::whereClause('tx_dlf_documents'), |
||
84 | '', |
||
85 | 'title ASC', |
||
86 | '' |
||
87 | ); |
||
88 | |||
89 | // Process results. |
||
90 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
91 | |||
92 | $issues[] = array ( |
||
93 | 'uid' => $resArray['uid'], |
||
94 | 'title' => $resArray['title'], |
||
95 | 'year' => $resArray['year'] |
||
96 | ); |
||
97 | |||
98 | } |
||
99 | |||
100 | // We need an array of issues with month number as key. |
||
101 | $calendarIssues = array (); |
||
102 | |||
103 | foreach ($issues as $issue) { |
||
|
|||
104 | |||
105 | $calendarIssues[date('n', strtotime($issue['year']))][date('j', strtotime($issue['year']))][] = $issue; |
||
106 | |||
107 | } |
||
108 | |||
109 | $allIssues = array (); |
||
110 | |||
111 | // Get subpart templates. |
||
112 | $subparts['list'] = $this->cObj->getSubpart($this->template, '###ISSUELIST###'); |
||
113 | |||
114 | $subparts['month'] = $this->cObj->getSubpart($this->template, '###CALMONTH###'); |
||
115 | |||
116 | $subparts['week'] = $this->cObj->getSubpart($subparts['month'], '###CALWEEK###'); |
||
117 | |||
118 | $subparts['singleday'] = $this->cObj->getSubpart($subparts['list'], '###SINGLEDAY###'); |
||
119 | |||
120 | // Build calendar for given year. |
||
121 | $year = date('Y', strtotime($issues[0]['year'])); |
||
122 | |||
123 | for ($i = 0; $i <= 11; $i++) { |
||
124 | |||
125 | $markerArray = array ( |
||
126 | '###DAYMON_NAME###' => strftime('%a', strtotime('last Monday')), |
||
127 | '###DAYTUE_NAME###' => strftime('%a', strtotime('last Tuesday')), |
||
128 | '###DAYWED_NAME###' => strftime('%a', strtotime('last Wednesday')), |
||
129 | '###DAYTHU_NAME###' => strftime('%a', strtotime('last Thursday')), |
||
130 | '###DAYFRI_NAME###' => strftime('%a', strtotime('last Friday')), |
||
131 | '###DAYSAT_NAME###' => strftime('%a', strtotime('last Saturday')), |
||
132 | '###DAYSUN_NAME###' => strftime('%a', strtotime('last Sunday')), |
||
133 | '###MONTHNAME###' => strftime('%B', strtotime($year.'-'.($i + 1).'-1')) |
||
134 | ); |
||
135 | |||
136 | // Reset week content of new month. |
||
137 | $subWeekPartContent = ''; |
||
138 | |||
139 | $firstOfMonth = strtotime($year.'-'.($i + 1).'-1'); |
||
140 | $lastOfMonth = strtotime('last day of', ($firstOfMonth)); |
||
141 | $firstOfMonthStart = strtotime('last Monday', $firstOfMonth); |
||
142 | |||
143 | // There are never more than 6 weeks in a month. |
||
144 | for ($j = 0; $j <= 5; $j++) { |
||
145 | |||
146 | $firstDayOfWeek = strtotime('+ '.$j.' Week', $firstOfMonthStart); |
||
147 | |||
148 | $weekArray = array ( |
||
149 | '###DAYMON###' => ' ', |
||
150 | '###DAYTUE###' => ' ', |
||
151 | '###DAYWED###' => ' ', |
||
152 | '###DAYTHU###' => ' ', |
||
153 | '###DAYFRI###' => ' ', |
||
154 | '###DAYSAT###' => ' ', |
||
155 | '###DAYSUN###' => ' ', |
||
156 | ); |
||
157 | |||
158 | // Every week has seven days. ;-) |
||
159 | for ($k = 0; $k <= 6; $k++) { |
||
160 | |||
161 | $currentDayTime = strtotime('+ '.$k.' Day', $firstDayOfWeek); |
||
162 | |||
163 | if ($currentDayTime >= $firstOfMonth && $currentDayTime <= $lastOfMonth) { |
||
164 | |||
165 | $dayLinks = ''; |
||
166 | |||
167 | $dayLinksText = array (); |
||
168 | |||
169 | $dayLinksList = ''; |
||
170 | |||
171 | $currentMonth = date('n', $currentDayTime); |
||
172 | |||
173 | if (is_array($calendarIssues[$currentMonth])) { |
||
174 | |||
175 | foreach ($calendarIssues[$currentMonth] as $id => $day) { |
||
176 | |||
177 | if ($id == date('j', $currentDayTime)) { |
||
178 | |||
179 | $dayLinks = $id; |
||
180 | |||
181 | foreach ($day as $issue) { |
||
182 | |||
183 | $dayLinkLabel = empty($issue['title']) ? strftime('%x', $currentDayTime) : $issue['title']; |
||
184 | |||
185 | $linkConf = array ( |
||
186 | 'useCacheHash' => 1, |
||
187 | 'parameter' => $this->conf['targetPid'], |
||
188 | 'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($issue['uid']), |
||
189 | 'ATagParams' => ' class="title"', |
||
190 | ); |
||
191 | |||
192 | $dayLinksText[] = $this->cObj->typoLink($dayLinkLabel, $linkConf); |
||
193 | |||
194 | // Save issues for list view. |
||
195 | $allIssues[$currentDayTime][] = $this->cObj->typoLink($dayLinkLabel, $linkConf); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | } |
||
200 | |||
201 | if (!empty($dayLinksText)) { |
||
202 | |||
203 | $dayLinksList = '<ul>'; |
||
204 | |||
205 | foreach ($dayLinksText as $link) { |
||
206 | |||
207 | $dayLinksList .= '<li>'.$link.'</li>'; |
||
208 | |||
209 | } |
||
210 | |||
211 | $dayLinksList .= '</ul>'; |
||
212 | |||
213 | } |
||
214 | |||
215 | $dayLinkDiv = '<div class="issues"><h4>'.strftime('%d', $currentDayTime).'</h4><div>'.$dayLinksList.'</div></div>'; |
||
216 | } |
||
217 | |||
218 | switch (strftime('%w', strtotime('+ '.$k.' Day', $firstDayOfWeek))) { |
||
219 | |||
220 | case '0': $weekArray['###DAYSUN###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
221 | break; |
||
222 | |||
223 | case '1': $weekArray['###DAYMON###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
224 | break; |
||
225 | |||
226 | case '2': $weekArray['###DAYTUE###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
227 | break; |
||
228 | |||
229 | case '3': $weekArray['###DAYWED###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
230 | break; |
||
231 | |||
232 | case '4': $weekArray['###DAYTHU###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
233 | break; |
||
234 | |||
235 | case '5': $weekArray['###DAYFRI###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
236 | break; |
||
237 | |||
238 | case '6': $weekArray['###DAYSAT###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime); |
||
239 | break; |
||
240 | |||
241 | } |
||
242 | |||
243 | } |
||
244 | |||
245 | } |
||
246 | |||
247 | // Fill the weeks. |
||
248 | $subWeekPartContent .= $this->cObj->substituteMarkerArray($subparts['week'], $weekArray); |
||
249 | |||
250 | } |
||
251 | |||
252 | // Fill the month markers. |
||
253 | $subPartContent .= $this->cObj->substituteMarkerArray($subparts['month'], $markerArray); |
||
254 | |||
255 | // Fill the week markers with the week entries. |
||
256 | $subPartContent = $this->cObj->substituteSubpart($subPartContent, '###CALWEEK###', $subWeekPartContent); |
||
257 | } |
||
258 | |||
259 | // Link to years overview |
||
260 | $linkConf = array ( |
||
261 | 'useCacheHash' => 1, |
||
262 | 'parameter' => $this->conf['targetPid'], |
||
263 | 'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($this->doc->parentId), |
||
264 | ); |
||
265 | |||
266 | $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', TRUE).' '.$this->doc->getTitle($this->doc->parentId), $linkConf); |
||
267 | |||
268 | // Link to current year. |
||
269 | $linkConf = array ( |
||
270 | 'useCacheHash' => 1, |
||
271 | 'parameter' => $this->conf['targetPid'], |
||
272 | 'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($this->doc->uid), |
||
273 | ); |
||
274 | |||
275 | $yearLink = $this->cObj->typoLink($year, $linkConf); |
||
276 | |||
277 | // Prepare list as alternative of the calendar view. |
||
278 | foreach ($allIssues as $dayTime => $issues) { |
||
279 | |||
280 | $markerArrayDay['###DATE_STRING###'] = strftime('%A, %x', $dayTime); |
||
281 | |||
282 | $markerArrayDay['###ITEMS###'] = ''; |
||
283 | |||
284 | foreach ($issues as $issue) { |
||
285 | |||
286 | $markerArrayDay['###ITEMS###'] .= $issue; |
||
287 | |||
288 | } |
||
289 | |||
290 | $subPartContentList .= $this->cObj->substituteMarkerArray($subparts['singleday'], $markerArrayDay); |
||
291 | |||
292 | } |
||
293 | |||
294 | $this->template = $this->cObj->substituteSubpart($this->template, '###SINGLEDAY###', $subPartContentList); |
||
295 | |||
296 | if (count($allIssues) < 6) { |
||
297 | |||
298 | $listViewActive = TRUE; |
||
299 | |||
300 | } else { |
||
301 | |||
302 | $listViewActive = FALSE; |
||
303 | |||
304 | } |
||
305 | |||
306 | $markerArray = array ( |
||
307 | '###CALENDARVIEWACTIVE###' => $listViewActive ? '' : 'active', |
||
308 | '###LISTVIEWACTIVE###' => $listViewActive ? 'active' : '', |
||
309 | '###CALYEAR###' => $yearLink, |
||
310 | '###CALALLYEARS###' => $allYearsLink, |
||
311 | '###LABEL_CALENDAR###' => $this->pi_getLL('label.view_calendar'), |
||
312 | '###LABEL_LIST_VIEW###' => $this->pi_getLL('label.view_list'), |
||
313 | ); |
||
314 | |||
315 | $this->template = $this->cObj->substituteMarkerArray($this->template, $markerArray); |
||
316 | |||
317 | return $this->cObj->substituteSubpart($this->template, '###CALMONTH###', $subPartContent); |
||
318 | |||
426 |