Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#715)
by Alexander
03:36
created

CalendarController::mainAction()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
c 3
b 0
f 0
nc 14
nop 0
dl 0
loc 32
rs 8.0555
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Controller;
14
15
use Kitodo\Dlf\Domain\Model\Document;
16
use TYPO3\CMS\Core\Database\ConnectionPool;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Controller class for the plugin 'Calendar'.
21
 *
22
 * @author Alexander Bigga <[email protected]>
23
 * @author Sebastian Meyer <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 */
28
class CalendarController extends AbstractController
29
{
30
    /**
31
     * This holds all issues for the list view.
32
     *
33
     * @var array
34
     * @access protected
35
     */
36
    protected $allIssues = [];
37
38
    /**
39
     * The main method of the plugin
40
     *
41
     * @return void
42
     */
43
    public function mainAction()
44
    {
45
        // Set initial document (anchor or year file) if configured.
46
        if (empty($this->requestData['id']) && !empty($this->settings['initialDocument'])) {
47
            $this->requestData['id'] = $this->settings['initialDocument'];
48
        }
49
50
        // Load current document.
51
        $this->loadDocument($this->requestData);
52
        if ($this->document === null) {
53
            // Quit without doing anything if required variables are not set.
54
            return;
55
        }
56
57
        $metadata = $this->document->getDoc()->getTitledata();
58
        if (!empty($metadata['type'][0])) {
59
            $type = $metadata['type'][0];
60
        } else {
61
            return;
62
        }
63
64
        switch ($type) {
65
            case 'newspaper':
66
            case 'ephemera':
67
                $this->forward('years', null, null, $this->requestData);
68
                break;
69
            case 'year':
70
                $this->forward('calendar', null, null, $this->requestData);
71
                break;
72
            case 'issue':
73
            default:
74
                break;
75
        }
76
77
    }
78
79
    /**
80
     * The Calendar Method
81
     *
82
     * @access public
83
     *
84
     * @param string $content: The PlugIn content
85
     * @param array $conf: The PlugIn configuration
86
     *
87
     * @return void
88
     */
89
    public function calendarAction()
90
    {
91
        // access arguments passed by the mainAction()
92
        $mainrequestData = $this->request->getArguments();
93
94
        // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win
95
        $this->requestData = array_merge($this->requestData, $mainrequestData);
96
97
        // Load current document.
98
        $this->loadDocument($this->requestData);
99
        if ($this->document === null) {
100
            // Quit without doing anything if required variables are not set.
101
            return;
102
        }
103
104
        $documents = $this->documentRepository->getChildrenOfYearAnchor($this->document->getUid(), 'issue');
105
106
        $issues = [];
107
108
        // Process results.
109
        /** @var Document $document */
110
        foreach ($documents as $document) {
111
            // Set title for display in calendar view.
112
            if (!empty($document->getTitle())) {
113
                $title = $document->getTitle();
114
            } else {
115
                $title = !empty($document->getMetsLabel()) ? $document->getMetsLabel() : $document->getMetsOrderlabel();
116
                if (strtotime($title) !== false) {
117
                    $title = strftime('%x', strtotime($title));
118
                }
119
            }
120
            $issues[] = [
121
                'uid' => $document->getUid(),
122
                'title' => $title,
123
                'year' => $document->getYear()
124
            ];
125
        }
126
127
        //  We need an array of issues with year => month => day number as key.
128
        $calendarIssuesByYear = [];
129
        foreach ($issues as $issue) {
130
            $dateTimestamp = strtotime($issue['year']);
131
            if ($dateTimestamp !== false) {
132
                $_year = date('Y', $dateTimestamp);
133
                $_month = date('n', $dateTimestamp);
134
                $_day = date('j', $dateTimestamp);
135
                $calendarIssuesByYear[$_year][$_month][$_day][] = $issue;
136
            } else {
137
                $this->logger->warning('Document with UID ' . $issue['uid'] . 'has no valid date of publication');
138
            }
139
        }
140
        // Sort by years.
141
        ksort($calendarIssuesByYear);
142
        // Build calendar for year (default) or season.
143
        $iteration = 1;
144
        foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) {
145
            // Sort by months.
146
            ksort($calendarIssuesByMonth);
147
            // Default: First month is January, last month is December.
148
            $firstMonth = 1;
149
            $lastMonth = 12;
150
            // Show calendar from first issue up to end of season if applicable.
151
            if (
152
                empty($this->settings['showEmptyMonths'])
153
                && count($calendarIssuesByYear) > 1
154
            ) {
155
                if ($iteration == 1) {
156
                    $firstMonth = (int) key($calendarIssuesByMonth);
157
                } elseif ($iteration == count($calendarIssuesByYear)) {
158
                    end($calendarIssuesByMonth);
159
                    $lastMonth = (int) key($calendarIssuesByMonth);
160
                }
161
            }
162
            $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth);
163
            $iteration++;
164
        }
165
        // Prepare list as alternative view.
166
        $issueData = [];
167
        foreach ($this->allIssues as $dayTimestamp => $issues) {
168
            $issueData[$dayTimestamp]['dateString'] = strftime('%A, %x', $dayTimestamp);
169
            $issueData[$dayTimestamp]['items'] = [];
170
            foreach ($issues as $issue) {
171
                $issueData[$dayTimestamp]['items'][] = $issue;
172
            }
173
        }
174
        $this->view->assign('issueData', $issueData);
175
176
        // Link to current year.
177
        $linkTitleData = $this->document->getDoc()->getTitledata();
178
        $yearLinkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0];
179
180
        $this->view->assign('documentId', $this->document->getUid());
181
        $this->view->assign('yearLinkTitle', $yearLinkTitle);
182
        $this->view->assign('parentDocumentId', $this->document->getPartof());
183
        $this->view->assign('allYearDocTitle', $this->document->getDoc()->getTitle($this->document->getPartof()));
184
    }
185
186
    /**
187
     * The Years Method
188
     *
189
     * @access public
190
     *
191
     * @return void
192
     */
193
    public function yearsAction()
194
    {
195
        // access arguments passed by the mainAction()
196
        $mainrequestData = $this->request->getArguments();
197
198
        // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win
199
        $this->requestData = array_merge($this->requestData, $mainrequestData);
200
201
        // Load current document.
202
        $this->loadDocument($this->requestData);
203
        if ($this->document === null) {
204
            // Quit without doing anything if required variables are not set.
205
            return;
206
        }
207
208
        // Get all children of anchor. This should be the year anchor documents
209
        $documents = $this->documentRepository->getChildrenOfYearAnchor($this->document->getUid(), 'year');
210
211
        $years = [];
212
        // Process results.
213
        /** @var Document $document */
214
        foreach ($documents as $document) {
215
            $years[] = [
216
                'title' => !empty($document->getMetsLabel()) ? $document->getMetsLabel() : (!empty($document->getMetsOrderlabel()) ? $document->getMetsOrderlabel() : $document->getTitle()),
217
                'uid' => $document->getUid()
218
            ];
219
        }
220
221
        $yearArray = [];
222
        if (count($years) > 0) {
223
            foreach ($years as $year) {
224
                $yearArray[] = [
225
                    'documentId' => $year['uid'],
226
                    'title' => $year['title']
227
                ];
228
            }
229
            $this->view->assign('yearName', $yearArray);
230
        }
231
232
        $this->view->assign('documentId', $this->document->getUid());
233
        $this->view->assign('allYearDocTitle', $this->document->getDoc()->getTitle($this->document->getPartof()));
234
    }
235
236
    /**
237
     * Build calendar for a certain year
238
     *
239
     * @access protected
240
     *
241
     * @param array $calendarIssuesByMonth All issues sorted by month => day
242
     * @param int $year Gregorian year
243
     * @param int $firstMonth 1 for January, 2 for February, ... 12 for December
244
     * @param int $lastMonth 1 for January, 2 for February, ... 12 for December
245
     *
246
     * @return string Content for template subpart
247
     */
248
    protected function getCalendarYear($calendarIssuesByMonth, $year, $firstMonth = 1, $lastMonth = 12)
249
    {
250
        $calendarData = [];
251
        for ($i = $firstMonth; $i <= $lastMonth; $i++) {
252
            $calendarData[$i] = [
253
                'DAYMON_NAME' => strftime('%a', strtotime('last Monday')),
254
                'DAYTUE_NAME' => strftime('%a', strtotime('last Tuesday')),
255
                'DAYWED_NAME' => strftime('%a', strtotime('last Wednesday')),
256
                'DAYTHU_NAME' => strftime('%a', strtotime('last Thursday')),
257
                'DAYFRI_NAME' => strftime('%a', strtotime('last Friday')),
258
                'DAYSAT_NAME' => strftime('%a', strtotime('last Saturday')),
259
                'DAYSUN_NAME' => strftime('%a', strtotime('last Sunday')),
260
                'MONTHNAME'  => strftime('%B', strtotime($year . '-' . $i . '-1')) . ' ' . $year,
261
                'CALYEAR' => ($i == $firstMonth) ? $year : ''
262
            ];
263
264
            $firstOfMonth = strtotime($year . '-' . $i . '-1');
265
            $lastOfMonth = strtotime('last day of', ($firstOfMonth));
266
            $firstOfMonthStart = strtotime('last Monday', $firstOfMonth);
267
            // There are never more than 6 weeks in a month.
268
            for ($j = 0; $j <= 5; $j++) {
269
                $firstDayOfWeek = strtotime('+ ' . $j . ' Week', $firstOfMonthStart);
270
271
                $calendarData[$i]['week'][$j] = [
272
                    'DAYMON' => ['dayValue' => '&nbsp;'],
273
                    'DAYTUE' => ['dayValue' => '&nbsp;'],
274
                    'DAYWED' => ['dayValue' => '&nbsp;'],
275
                    'DAYTHU' => ['dayValue' => '&nbsp;'],
276
                    'DAYFRI' => ['dayValue' => '&nbsp;'],
277
                    'DAYSAT' => ['dayValue' => '&nbsp;'],
278
                    'DAYSUN' => ['dayValue' => '&nbsp;'],
279
                ];
280
                // Every week has seven days. ;-)
281
                for ($k = 0; $k <= 6; $k++) {
282
                    $currentDayTime = strtotime('+ ' . $k . ' Day', $firstDayOfWeek);
283
                    if (
284
                        $currentDayTime >= $firstOfMonth
285
                        && $currentDayTime <= $lastOfMonth
286
                    ) {
287
                        $dayLinks = '';
288
                        $dayLinksText = [];
289
                        $dayLinkDiv = [];
290
                        $currentMonth = date('n', $currentDayTime);
291
                        if (is_array($calendarIssuesByMonth[$currentMonth])) {
292
                            foreach ($calendarIssuesByMonth[$currentMonth] as $id => $day) {
293
                                if ($id == date('j', $currentDayTime)) {
294
                                    $dayLinks = $id;
295
                                    foreach ($day as $issue) {
296
                                        $dayLinkLabel = empty($issue['title']) ? strftime('%x', $currentDayTime) : $issue['title'];
297
298
                                        $dayLinksText[] = [
299
                                            'documentId' => $issue['uid'],
300
                                            'text' => $dayLinkLabel
301
                                        ];
302
303
                                        // Save issue for list view.
304
                                        $this->allIssues[$currentDayTime][] = [
305
                                            'documentId' => $issue['uid'],
306
                                            'text' => $dayLinkLabel
307
                                        ];
308
                                    }
309
                                }
310
                            }
311
                            $dayLinkDiv = $dayLinksText;
312
                        }
313
                        switch (strftime('%w', strtotime('+ ' . $k . ' Day', $firstDayOfWeek))) {
314
                            case '0':
315
                                $calendarData[$i]['week'][$j]['DAYSUN']['dayValue'] = strftime('%d', $currentDayTime);
316
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
317
                                    $calendarData[$i]['week'][$j]['DAYSUN']['issues'] = $dayLinkDiv;
318
                                }
319
                                break;
320
                            case '1':
321
                                $calendarData[$i]['week'][$j]['DAYMON']['dayValue'] = strftime('%d', $currentDayTime);
322
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
323
                                    $calendarData[$i]['week'][$j]['DAYMON']['issues'] = $dayLinkDiv;
324
                                }
325
                                break;
326
                            case '2':
327
                                $calendarData[$i]['week'][$j]['DAYTUE']['dayValue'] = strftime('%d', $currentDayTime);
328
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
329
                                    $calendarData[$i]['week'][$j]['DAYTUE']['issues'] = $dayLinkDiv;
330
                                }
331
                                break;
332
                            case '3':
333
                                $calendarData[$i]['week'][$j]['DAYWED']['dayValue'] = strftime('%d', $currentDayTime);
334
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
335
                                    $calendarData[$i]['week'][$j]['DAYWED']['issues'] = $dayLinkDiv;
336
                                }
337
                                break;
338
                            case '4':
339
                                $calendarData[$i]['week'][$j]['DAYTHU']['dayValue'] = strftime('%d', $currentDayTime);
340
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
341
                                    $calendarData[$i]['week'][$j]['DAYTHU']['issues'] = $dayLinkDiv;
342
                                }
343
                                break;
344
                            case '5':
345
                                $calendarData[$i]['week'][$j]['DAYFRI']['dayValue'] = strftime('%d', $currentDayTime);
346
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
347
                                    $calendarData[$i]['week'][$j]['DAYFRI']['issues'] = $dayLinkDiv;
348
                                }
349
                                break;
350
                            case '6':
351
                                $calendarData[$i]['week'][$j]['DAYSAT']['dayValue'] = strftime('%d', $currentDayTime);
352
                                if ((int) $dayLinks === (int) date('j', $currentDayTime)) {
353
                                    $calendarData[$i]['week'][$j]['DAYSAT']['issues'] = $dayLinkDiv;
354
                                }
355
                                break;
356
                        }
357
                    }
358
                }
359
            }
360
        }
361
        $this->view->assign('calendarData', $calendarData);
362
    }
363
}
364