tx_dlf_newspaper   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 401
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 401
rs 8.3157
c 2
b 0
f 0
wmc 43

3 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 4 1
F calendar() 0 264 36
B years() 0 91 6

How to fix   Complexity   

Complex Class

Complex classes like tx_dlf_newspaper 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 tx_dlf_newspaper, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Plugin 'DLF: Newspaper' for the 'dlf' extension.
14
 *
15
 * @author	Alexander Bigga <[email protected]>
16
 * @copyright	Copyright (c) 2016, Alexander Bigga, SLUB Dresden
17
 * @package	TYPO3
18
 * @subpackage	tx_dlf
19
 * @access	public
20
 */
21
class tx_dlf_newspaper extends tx_dlf_plugin {
22
23
    public $extKey = 'dlf';
24
25
    public $scriptRelPath = 'plugins/newspaper/class.tx_dlf_newspaper.php';
26
27
    /**
28
     * The main method of the PlugIn
29
     *
30
     * @access	public
31
     *
32
     * @param	string		$content: The PlugIn content
33
     * @param	array		$conf: The PlugIn configuration
34
     *
35
     * @return	string		The content that is displayed on the website
36
     */
37
    public function main($content, $conf) {
38
39
        // Nothing to do here.
40
        return $content;
41
42
    }
43
44
    /**
45
     * The Calendar Method
46
     *
47
     * @access	public
48
     *
49
     * @param	string		$content: The PlugIn content
50
     * @param	array		$conf: The PlugIn configuration
51
     *
52
     * @return	string		The content that is displayed on the website
53
     */
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) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $issues does not seem to be defined for all execution paths leading up to this point.
Loading history...
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###');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$subparts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subparts = array(); before regardless.
Loading history...
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###' => '&nbsp;',
150
                    '###DAYTUE###' => '&nbsp;',
151
                    '###DAYWED###' => '&nbsp;',
152
                    '###DAYTHU###' => '&nbsp;',
153
                    '###DAYFRI###' => '&nbsp;',
154
                    '###DAYSAT###' => '&nbsp;',
155
                    '###DAYSUN###' => '&nbsp;',
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dayLinkDiv does not seem to be defined for all execution paths leading up to this point.
Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subPartContent does not seem to be defined for all execution paths leading up to this point.
Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subPartContentList does not exist. Did you maybe mean $subPartContent?
Loading history...
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
319
    }
320
321
    /**
322
     * The Year 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) {
332
333
        $this->init($conf);
334
335
        // Load current document.
336
        $this->loadDocument();
337
338
        if ($this->doc === NULL) {
339
340
            // Quit without doing anything if required variables are not set.
341
            return $content;
342
343
        }
344
345
        // Load template file.
346
        if (!empty($this->conf['templateFile'])) {
347
348
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATEYEAR###');
349
350
        } else {
351
352
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/newspaper/template.tmpl'), '###TEMPLATEYEAR###');
353
354
        }
355
356
        // Get subpart templates
357
        $subparts['year'] = $this->cObj->getSubpart($this->template, '###LISTYEAR###');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$subparts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subparts = array(); before regardless.
Loading history...
358
359
        // get the title of the anchor file
360
        $titleAnchor = $this->doc->getTitle($this->doc->uid);
361
362
        // get all children of anchor. this should be the year anchor documents
363
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
364
            'tx_dlf_documents.uid AS uid, tx_dlf_documents.title AS title',
365
            'tx_dlf_documents',
366
            '(tx_dlf_documents.structure='.tx_dlf_helper::getIdFromIndexName('year', 'tx_dlf_structures', $this->doc->pid).' AND tx_dlf_documents.partof='.intval($this->doc->uid).')'.tx_dlf_helper::whereClause('tx_dlf_documents'),
367
            '',
368
            'title ASC',
369
            ''
370
        );
371
372
        // Process results.
373
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
374
375
            $years[] = array (
376
                'title' => $resArray['title'],
377
                'uid' => $resArray['uid']
378
            );
379
380
        }
381
382
        $subYearPartContent = '';
383
384
        if (count($years) > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $years does not seem to be defined for all execution paths leading up to this point.
Loading history...
385
386
            foreach ($years as $id => $year) {
387
388
                $linkConf = array (
389
                    'useCacheHash' => 1,
390
                    'parameter' => $this->conf['targetPid'],
391
                    'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($year['uid']),
392
                    'title' => $titleAnchor.': '.$year['title']
393
                );
394
395
                $yearArray = array (
396
                    '###YEARNAME###' => $this->cObj->typoLink($year['title'], $linkConf),
397
                );
398
399
                $subYearPartContent .= $this->cObj->substituteMarkerArray($subparts['year'], $yearArray);
400
401
            }
402
        }
403
404
        // link to years overview (should be itself here)
405
        $linkConf = array (
406
            'useCacheHash' => 1,
407
            'parameter' => $this->conf['targetPid'],
408
            'additionalParams' => '&'.$this->prefixId.'[id]='.$this->doc->uid,
409
        );
410
        $allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', TRUE).' '.$this->doc->getTitle($this->doc->uid), $linkConf);
411
412
        // Fill markers.
413
        $markerArray = array (
414
            '###LABEL_CHOOSE_YEAR###' => $this->pi_getLL('label.please_choose_year'),
415
            '###CALALLYEARS###' => $allYearsLink
416
        );
417
418
        $this->template = $this->cObj->substituteMarkerArray($this->template, $markerArray);
419
420
        // fill the week markers
421
        return $this->cObj->substituteSubpart($this->template, '###LISTYEAR###', $subYearPartContent);
422
423
    }
424
425
}
426