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
07:45
created

CalendarController::mainAction()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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