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:00
created

BasketController::injectPrinterRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\Common\Document;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Domain\Model\ActionLog;
18
use Kitodo\Dlf\Domain\Repository\ActionLogRepository;
19
use Kitodo\Dlf\Domain\Repository\MailRepository;
20
use Kitodo\Dlf\Domain\Repository\BasketRepository;
21
use Kitodo\Dlf\Domain\Repository\PrinterRepository;
22
use TYPO3\CMS\Core\Database\Connection;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
26
27
class BasketController extends AbstractController
28
{
29
    /**
30
     * @var BasketRepository
31
     */
32
    protected $basketRepository;
33
34
    /**
35
     * @param BasketRepository $basketRepository
36
     */
37
    public function injectBasketRepository(BasketRepository $basketRepository)
38
    {
39
        $this->basketRepository = $basketRepository;
40
    }
41
42
    /**
43
     * @var MailRepository
44
     */
45
    protected $mailRepository;
46
47
    /**
48
     * @param MailRepository $mailRepository
49
     */
50
    public function injectMailRepository(MailRepository $mailRepository)
51
    {
52
        $this->mailRepository = $mailRepository;
53
    }
54
55
    /**
56
     * @var PrinterRepository
57
     */
58
    protected $printerRepository;
59
60
    /**
61
     * @param PrinterRepository $printerRepository
62
     */
63
    public function injectPrinterRepository(PrinterRepository $printerRepository)
64
    {
65
        $this->printerRepository = $printerRepository;
66
    }
67
68
    /**
69
     * @var ActionLogRepository
70
     */
71
    protected $actionLogRepository;
72
73
    /**
74
     * @param ActionLogRepository $actionLogRepository
75
     */
76
    public function injectActionLogRepository(ActionLogRepository $actionLogRepository)
77
    {
78
        $this->actionLogRepository = $actionLogRepository;
79
    }
80
81
    /**
82
     * Different actions which depends on the choosen action (form)
83
     *
84
     * @return void
85
     */
86
    public function basketAction()
87
    {
88
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
89
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
90
91
        $basketData = $this->getBasketData();
92
93
        // action remove from basket
94
        if ($requestData['basket_action'] === 'remove') {
95
            // remove entry from list
96
            if (isset($requestData['selected'])) {
97
                $basketData = $this->removeFromBasket($requestData, $basketData);
98
            }
99
        }
100
        // action remove from basket
101
        if ($requestData['basket_action'] == 'download') {
102
            // open selected documents
103
            if (isset($requestData['selected'])) {
104
                $pdfUrl = $this->settings['pdfgenerate'];
105
                foreach ($requestData['selected'] as $docValue) {
106
                    if ($docValue['id']) {
107
                        $docData = $this->getDocumentData($docValue['id'], $docValue);
108
                        $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
109
                        $this->redirectToUri($pdfUrl);
110
                    }
111
                }
112
            }
113
        }
114
        // action print from basket
115
        if ($requestData['print_action']) {
116
            // open selected documents
117
            if (isset($requestData['selected'])) {
118
                $this->printDocument($requestData, $basketData);
119
            }
120
        }
121
        // action send mail
122
        if ($requestData['mail_action']) {
123
            if (isset($requestData['selected'])) {
124
                $this->sendMail($requestData);
125
            }
126
        }
127
128
        $this->redirect('main');
129
    }
130
131
    /**
132
     * Add documents to the basket
133
     *
134
     * @return void
135
     */
136
    public function addAction()
137
    {
138
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
139
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
140
141
        $basketData = $this->getBasketData();
142
143
        if (
144
            !empty($requestData['id'])
145
            && $requestData['addToBasket']
146
        ) {
147
            $returnData = $this->addToBasket($requestData, $basketData);
148
            $this->view->assign('pregenerateJs', $returnData['jsOutput']);
149
        }
150
151
        $this->redirect('main');
152
    }
153
154
    /**
155
     * The main method of the plugin
156
     *
157
     * @return void
158
     */
159
    public function mainAction()
160
    {
161
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
162
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
163
164
        $basketData = $this->getBasketData();
165
166
        if ($basketData['doc_ids']) {
167
            if (is_object($basketData['doc_ids'])) {
168
                $basketData['doc_ids'] = get_object_vars($basketData['doc_ids']);
169
            }
170
            $count = sprintf(LocalizationUtility::translate('basket.count', 'dlf'), count($basketData['doc_ids']));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...('basket.count', 'dlf') can also be of type null; however, parameter $format of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
            $count = sprintf(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.count', 'dlf'), count($basketData['doc_ids']));
Loading history...
171
        } else {
172
            $count = sprintf(LocalizationUtility::translate('basket.count', 'dlf'), 0);
173
        }
174
        $this->view->assign('count', $count);
175
176
        $allMails = $this->mailRepository->findAllWithPid($this->settings['pages']);
177
178
        $mailSelect = [];
179
        if ($allMails->count() > 0) {
180
            $mailSelect[0] = htmlspecialchars(LocalizationUtility::translate('basket.chooseMail', 'dlf'));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...ket.chooseMail', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

180
            $mailSelect[0] = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.chooseMail', 'dlf'));
Loading history...
181
            foreach ($allMails as $mail) {
182
                $mailSelect[$mail->getUid()] = htmlspecialchars($mail->getName()) . ' (' . htmlspecialchars($mail->getMail()) . ')';
183
            }
184
            $this->view->assign('mailSelect', $mailSelect);
185
        }
186
187
        $allPrinter = $this->printerRepository->findAll();
188
189
        $printSelect = [];
190
        if ($allPrinter->count() > 0) {
191
            $printSelect[0] = htmlspecialchars(LocalizationUtility::translate('basket.choosePrinter', 'dlf'));
192
            foreach ($allPrinter as $printer) {
193
                $printSelect[$printer->getUid()] = htmlspecialchars($printer->getLabel());
194
            }
195
            $this->view->assign('printSelect', $printSelect);
196
        }
197
198
        $entries = [];
199
        if (isset($basketData['doc_ids'])) {
200
            // get each entry
201
            foreach ($basketData['doc_ids'] as $value) {
202
                $entries[] = $this->getEntry($value);
203
            }
204
            $this->view->assign('entries', $entries);
205
        }
206
    }
207
208
    /**
209
     * The basket data from user session.
210
     *
211
     * @return array The found data from user session.
212
     */
213
    protected function getBasketData()
214
    {
215
        // get user session
216
        $sessionId = $GLOBALS['TSFE']->fe_user->id;
217
218
        if ($GLOBALS['TSFE']->loginUser) {
219
            $basket = $this->basketRepository->findOneByFeUserId((int) $GLOBALS['TSFE']->fe_user->user['uid']);
0 ignored issues
show
Bug introduced by
The method findOneByFeUserId() does not exist on Kitodo\Dlf\Domain\Repository\BasketRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            /** @scrutinizer ignore-call */ 
220
            $basket = $this->basketRepository->findOneByFeUserId((int) $GLOBALS['TSFE']->fe_user->user['uid']);
Loading history...
220
        } else {
221
            $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_dlf_basket', '');
222
            $GLOBALS['TSFE']->fe_user->sesData_change = true;
223
            $GLOBALS['TSFE']->fe_user->storeSessionData();
224
225
            $basket = $this->basketRepository->findOneBySessionId($sessionId);
0 ignored issues
show
Bug introduced by
The method findOneBySessionId() does not exist on Kitodo\Dlf\Domain\Repository\BasketRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
            /** @scrutinizer ignore-call */ 
226
            $basket = $this->basketRepository->findOneBySessionId($sessionId);
Loading history...
226
        }
227
        // session already exists
228
        if ($basket === null) {
229
            // create new basket in db
230
            $insertArray['fe_user_id'] = $GLOBALS['TSFE']->loginUser ? $GLOBALS['TSFE']->fe_user->user['uid'] : 0;
231
            $insertArray['session_id'] = $sessionId;
232
            $insertArray['doc_ids'] = '';
233
            $insertArray['label'] = '';
234
            $insertArray['l18n_diffsource'] = '';
235
            GeneralUtility::makeInstance(ConnectionPool::class)
236
                ->getConnectionForTable('tx_dlf_basket')
237
                ->insert(
238
                    'tx_dlf_basket',
239
                    $insertArray
240
                );
241
242
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type array.
Loading history...
243
        }
244
245
        $basketData['uid'] = $basket->getUid();
0 ignored issues
show
Bug introduced by
The method getUid() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
        /** @scrutinizer ignore-call */ 
246
        $basketData['uid'] = $basket->getUid();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
246
        $basketData['doc_ids'] = json_decode($basket->getDocIds());
0 ignored issues
show
Bug introduced by
The method getDocIds() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
        $basketData['doc_ids'] = json_decode($basket->/** @scrutinizer ignore-call */ getDocIds());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
        return $basketData;
248
    }
249
250
    /**
251
     * Return one basket entry
252
     *
253
     * @access protected
254
     *
255
     * @param array $data: DocumentData
256
     * @param array $template: Template information
257
     *
258
     * @return string One basket entry
259
     */
260
    protected function getEntry($data)
261
    {
262
        if (is_object($data)) {
263
            $data = get_object_vars($data);
264
        }
265
        $id = $data['id'];
266
        $startpage = $data['startpage'];
267
        $endpage = $data['endpage'];
268
        $startX = $data['startX'];
269
        $startY = $data['startY'];
270
        $endX = $data['endX'];
271
        $endY = $data['endY'];
272
        $rotation = $data['rotation'];
273
274
        $docData = $this->getDocumentData($id, $data);
275
276
        $entryArray['BASKETDATA'] = $docData;
277
278
        $entryKey = $id . '_' . $startpage;
279
        if (!empty($startX)) {
280
            $entryKey .= '_' . $startX;
281
        }
282
        if (!empty($endX)) {
283
            $entryKey .= '_' . $endX;
284
        }
285
286
        $entryArray['id'] = $id;
287
        $entryArray['CONTROLS'] = [
288
            'startpage' => $startpage,
289
            'endpage' => $endpage,
290
            'startX' => $startX,
291
            'startY' => $startY,
292
            'endX' => $endX,
293
            'endY' => $endY,
294
            'rotation' => $rotation,
295
        ];
296
297
        $entryArray['NUMBER'] = $docData['record_id'];
298
        $entryArray['key'] = $entryKey;
299
300
        // return one entry
301
        return $entryArray;
302
    }
303
304
    /**
305
     * Returns the downloadurl configured in the basket
306
     *
307
     * @access protected
308
     *
309
     * @param int $id: Document id
310
     *
311
     * @return mixed download url or false
312
     */
313
    protected function getDocumentData($id, $data)
314
    {
315
        // get document instance to load further information
316
        $document = Document::getInstance($id, 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $settings of Kitodo\Dlf\Common\Document::getInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

316
        $document = Document::getInstance($id, /** @scrutinizer ignore-type */ 0);
Loading history...
317
        if ($document) {
0 ignored issues
show
introduced by
$document is of type Kitodo\Dlf\Common\Document, thus it always evaluated to true.
Loading history...
318
            // replace url param placeholder
319
            $urlParams = str_replace("##page##", (int) $data['page'], $this->settings['pdfparams']);
320
            $urlParams = str_replace("##docId##", $document->recordId, $urlParams);
321
            $urlParams = str_replace("##startpage##", (int) $data['startpage'], $urlParams);
322
            if ($data['startpage'] != $data['endpage']) {
323
                $urlParams = str_replace("##endpage##", $data['endpage'] === "" ? "" : (int) $data['endpage'], $urlParams);
324
            } else {
325
                // remove parameter endpage
326
                $urlParams = str_replace(",##endpage##", '', $urlParams);
327
            }
328
            $urlParams = str_replace("##startx##", $data['startX'] === "" ? "" : (int) $data['startX'], $urlParams);
329
            $urlParams = str_replace("##starty##", $data['startY'] === "" ? "" : (int) $data['startY'], $urlParams);
330
            $urlParams = str_replace("##endx##", $data['endX'] === "" ? "" : (int) $data['endX'], $urlParams);
331
            $urlParams = str_replace("##endy##", $data['endY'] === "" ? "" : (int) $data['endY'], $urlParams);
332
            $urlParams = str_replace("##rotation##", $data['rotation'] === "" ? "" : (int) $data['rotation'], $urlParams);
333
334
            $downloadUrl = $this->settings['pdfgenerate'] . $urlParams;
335
336
            $title = $document->getTitle($id, true);
337
            if (empty($title)) {
338
                $title = LocalizationUtility::translate('basket.noTitle', 'dlf') ? : '';
339
            }
340
341
            // Set page and cutout information
342
            $info = '';
343
            if ($data['startX'] != '' && $data['endX'] != '') {
344
                // cutout
345
                $info .= htmlspecialchars(LocalizationUtility::translate('basket.cutout', 'dlf')) . ' ';
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...'basket.cutout', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

345
                $info .= htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.cutout', 'dlf')) . ' ';
Loading history...
346
            }
347
            if ($data['startpage'] == $data['endpage']) {
348
                // One page
349
                $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage'];
350
            } else {
351
                $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage'] . '-' . $data['endpage'];
352
            }
353
            $downloadLink = '<a href="' . $downloadUrl . '" target="_blank">' . htmlspecialchars($title) . '</a> (' . $info . ')';
354
            if ($data['startpage'] == $data['endpage']) {
355
                $pageNums = 1;
356
            } else {
357
                $pageNums = $data['endpage'] - $data['startpage'];
358
            }
359
            return [
360
                'downloadUrl' => $downloadUrl,
361
                'title' => $title,
362
                'info' => $info,
363
                'downloadLink' => $downloadLink,
364
                'pageNums' => $pageNums,
365
                'urlParams' => $urlParams,
366
                'record_id' => $document->recordId,
367
            ];
368
        }
369
        return false;
370
    }
371
372
    /**
373
     * Adds documents to the basket
374
     *
375
     * @access protected
376
     *
377
     * @param array $_piVars: piVars
378
     * @param array $basketData: basket data
379
     *
380
     * @return array Basket data and Javascript output
381
     */
382
    protected function addToBasket($_piVars, $basketData)
383
    {
384
        $output = '';
385
        if (!$_piVars['startpage']) {
386
            $page = 0;
387
        } else {
388
            $page = (int) $_piVars['startpage'];
389
        }
390
        if ($page != null || $_piVars['addToBasket'] == 'list') {
391
            $documentItem = [
392
                'id' => (int) $_piVars['id'],
393
                'startpage' => (int) $_piVars['startpage'],
394
                'endpage' => !isset($_piVars['endpage']) || $_piVars['endpage'] === "" ? "" : (int) $_piVars['endpage'],
395
                'startX' => !isset($_piVars['startX']) || $_piVars['startX'] === "" ? "" : (int) $_piVars['startX'],
396
                'startY' => !isset($_piVars['startY']) || $_piVars['startY'] === "" ? "" : (int) $_piVars['startY'],
397
                'endX' => !isset($_piVars['endX']) || $_piVars['endX'] === "" ? "" : (int) $_piVars['endX'],
398
                'endY' => !isset($_piVars['endY']) || $_piVars['endY'] === "" ? "" : (int) $_piVars['endY'],
399
                'rotation' => !isset($_piVars['rotation']) || $_piVars['rotation'] === "" ? "" : (int) $_piVars['rotation']
400
            ];
401
            // update basket
402
            if (!empty($basketData['doc_ids'])) {
403
                $items = $basketData['doc_ids'];
404
                $items = get_object_vars($items);
405
            } else {
406
                $items = [];
407
            }
408
            // get document instance to load further information
409
            $document = Document::getInstance($documentItem['id'], 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $settings of Kitodo\Dlf\Common\Document::getInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

409
            $document = Document::getInstance($documentItem['id'], /** @scrutinizer ignore-type */ 0);
Loading history...
410
            // set endpage for toc and subentry based on logid
411
            if (($_piVars['addToBasket'] == 'subentry') or ($_piVars['addToBasket'] == 'toc')) {
412
                $smLinks = $document->smLinks;
413
                $pageCounter = sizeof($smLinks['l2p'][$_piVars['logId']]);
414
                $documentItem['endpage'] = ($documentItem['startpage'] + $pageCounter) - 1;
415
            }
416
            // add whole document
417
            if ($_piVars['addToBasket'] == 'list') {
418
                $documentItem['endpage'] = $document->numPages;
419
            }
420
            $arrayKey = $documentItem['id'] . '_' . $page;
421
            if (!empty($documentItem['startX'])) {
422
                $arrayKey .= '_' . $documentItem['startX'];
423
            }
424
            if (!empty($documentItem['endX'])) {
425
                $arrayKey .= '_' . $documentItem['endX'];
426
            }
427
            // do not add more than one identical object
428
            if (!in_array($arrayKey, $items)) {
429
                $items[$arrayKey] = $documentItem;
430
                // replace url param placeholder
431
                $pdfParams = str_replace("##startpage##", $documentItem['startpage'], $this->settings['pdfparams']);
432
                $pdfParams = str_replace("##docId##", $document->recordId, $pdfParams);
433
                $pdfParams = str_replace("##startx##", $documentItem['startX'], $pdfParams);
434
                $pdfParams = str_replace("##starty##", $documentItem['startY'], $pdfParams);
435
                $pdfParams = str_replace("##endx##", $documentItem['endX'], $pdfParams);
436
                $pdfParams = str_replace("##endy##", $documentItem['endY'], $pdfParams);
437
                $pdfParams = str_replace("##rotation##", $documentItem['rotation'], $pdfParams);
438
                if ($documentItem['startpage'] != $documentItem['endpage']) {
439
                    $pdfParams = str_replace("##endpage##", $documentItem['endpage'], $pdfParams);
440
                } else {
441
                    // remove parameter endpage
442
                    $pdfParams = str_replace(",##endpage##", '', $pdfParams);
443
                }
444
                $pdfGenerateUrl = $this->settings['pdfgenerate'] . $pdfParams;
445
                if ($this->settings['pregeneration']) {
446
                    // send ajax request to webapp
447
                    $output .= '
448
     <script>
449
      $(document).ready(function(){
450
       $.ajax({
451
         url: "' . $pdfGenerateUrl . '",
452
       }).done(function() {
453
       });
454
      });
455
     </script>';
456
                }
457
            }
458
            $update = ['doc_ids' => json_encode($items)];
459
            GeneralUtility::makeInstance(ConnectionPool::class)
460
                ->getConnectionForTable('tx_dlf_basket')
461
                ->update(
462
                    'tx_dlf_basket',
463
                    $update,
464
                    ['uid' => (int) $basketData['uid']]
465
                );
466
            $basketData['doc_ids'] = $items;
467
        }
468
        return ['basketData' => $basketData, 'jsOutput' => $output];
469
    }
470
471
    /**
472
     * Removes selected documents from basket
473
     *
474
     * @access protected
475
     *
476
     * @param array $_piVars: plugin variables
477
     * @param array $basketData: array with document information
478
     *
479
     * @return array basket data
480
     */
481
    protected function removeFromBasket($_piVars, $basketData)
482
    {
483
        if (!empty($basketData['doc_ids'])) {
484
            $items = $basketData['doc_ids'];
485
            $items = get_object_vars($items);
486
        }
487
        foreach ($_piVars['selected'] as $value) {
488
            if (isset($value['id'])) {
489
                $arrayKey = $value['id'] . '_' . $value['startpage'];
490
                if (!empty($value['startX'])) {
491
                    $arrayKey .= '_' . $value['startX'];
492
                }
493
                if (!empty($value['endX'])) {
494
                    $arrayKey .= '_' . $value['endX'];
495
                }
496
                if (isset($items[$arrayKey])) {
497
                    unset($items[$arrayKey]);
498
                }
499
            }
500
        }
501
        if (empty($items)) {
502
            $update = ['doc_ids' => ''];
503
        } else {
504
            $update = ['doc_ids' => json_encode($items)];
505
        }
506
507
        GeneralUtility::makeInstance(ConnectionPool::class)
508
            ->getConnectionForTable('tx_dlf_basket')
509
            ->update(
510
                'tx_dlf_basket',
511
                    $update,
512
                ['uid' => (int) $basketData['uid']]
513
            );
514
        $basketData['doc_ids'] = $items;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $items does not seem to be defined for all execution paths leading up to this point.
Loading history...
515
        return $basketData;
516
    }
517
518
    /**
519
     * Send mail with pdf download url
520
     *
521
     * @access protected
522
     *
523
     * @return void
524
     */
525
    protected function sendMail($requestData)
526
    {
527
        // send mail
528
        $mailId = $requestData['mail_action'];
529
530
        $mailObject = $this->mailRepository->findByUid(intval($mailId))->getFirst();
531
532
        $mailText = htmlspecialchars(LocalizationUtility::translate('basket.mailBody', 'dlf')) . "\n";
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Extbase\Utilit...asket.mailBody', 'dlf') can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

532
        $mailText = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.mailBody', 'dlf')) . "\n";
Loading history...
533
        $numberOfPages = 0;
534
        $pdfUrl = $this->settings['pdfdownload'];
535
        // prepare links
536
        foreach ($requestData['selected'] as $docValue) {
537
            if ($docValue['id']) {
538
                $explodeId = explode("_", $docValue['id']);
539
                $docData = $this->getDocumentData($explodeId[0], $docValue);
540
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
541
                $pages = (abs(intval($docValue['startpage']) - intval($docValue['endpage'])));
542
                if ($pages === 0) {
543
                    $numberOfPages = $numberOfPages + 1;
544
                } else {
545
                    $numberOfPages = $numberOfPages + $pages;
546
                }
547
            }
548
        }
549
        // Remove leading/tailing pdfparamseperator
550
        $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
551
        $mailBody = $mailText . $pdfUrl;
552
        // Get hook objects.
553
        $hookObjects = Helper::getHookObjects($this->scriptRelPath);
0 ignored issues
show
Bug Best Practice introduced by
The property scriptRelPath does not exist on Kitodo\Dlf\Controller\BasketController. Did you maybe forget to declare it?
Loading history...
554
        // Hook for getting a customized mail body.
555
        foreach ($hookObjects as $hookObj) {
556
            if (method_exists($hookObj, 'customizeMailBody')) {
557
                $mailBody = $hookObj->customizeMailBody($mailText, $pdfUrl);
558
            }
559
        }
560
        $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
561
        // send mail
562
        $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
563
        // Prepare and send the message
564
        $mail
565
            // subject
566
            ->setSubject(LocalizationUtility::translate('basket.mailSubject', 'dlf'))
567
            // Set the From address with an associative array
568
            ->setFrom($from)
569
            // Set the To addresses with an associative array
570
            ->setTo([$mailObject->getMail() => $mailObject->getName()])
571
            ->setBody($mailBody, 'text/html')
572
            ->send();
573
574
        // create entry for action log
575
        $newActionLog = $this->objectManager->get(ActionLog::class);
576
        $newActionLog->setFileName($pdfUrl);
577
        $newActionLog->setCountPages($numberOfPages);
578
        $newActionLog->setLabel('Mail: ' . $mailObject->getMail());
579
580
        if ($GLOBALS["TSFE"]->loginUser) {
581
            // internal user
582
            $newActionLog->setUserId($GLOBALS["TSFE"]->fe_user->user['uid']);
583
            $newActionLog->setName($GLOBALS["TSFE"]->fe_user->user['username']);
584
        } else {
585
            // external user
586
            $newActionLog->setUserId(0);
587
            $newActionLog->setName('n/a');
588
        }
589
590
        $this->actionLogRepository->add($newActionLog);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Kitodo\Dlf\Domain\Repository\ActionLogRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

590
        $this->actionLogRepository->/** @scrutinizer ignore-call */ 
591
                                    add($newActionLog);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
591
592
        $this->redirect('main');
593
    }
594
595
    /**
596
     * Sends document information to an external printer (url)
597
     *
598
     * @access protected
599
     *
600
     * @return void
601
     */
602
    protected function printDocument($requestData, $basketData)
0 ignored issues
show
Unused Code introduced by
The parameter $basketData is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

602
    protected function printDocument($requestData, /** @scrutinizer ignore-unused */ $basketData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
603
    {
604
        $pdfUrl = $this->settings['pdfprint'];
605
        $numberOfPages = 0;
606
        foreach ($requestData['selected'] as $docId => $docValue) {
607
            if ($docValue['id']) {
608
                $docData = $this->getDocumentData($docValue['id'], $docValue);
609
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
610
                $numberOfPages += $docData['pageNums'];
611
            }
612
        }
613
        // get printer data
614
        $printerId = $requestData['print_action'];
615
616
        // get id from db and send selected doc download link
617
        $printer = $this->printerRepository->findByUid($printerId)->getFirst();
618
619
        // printer is selected
620
        if ($printer) {
621
            $pdfUrl = $printer->getPrint();
622
            $numberOfPages = 0;
623
            foreach ($requestData['selected'] as $docId => $docValue) {
624
                if ($docValue['id']) {
625
                    $explodeId = explode("_", $docId);
626
                    $docData = $this->getDocumentData($explodeId[0], $docValue);
627
                    $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
628
                    $numberOfPages += $docData['pageNums'];
629
                }
630
            }
631
            $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
632
        }
633
        // protocol
634
        $insertArray = [
635
            'pid' => $this->settings['pages'],
636
            'file_name' => $pdfUrl,
637
            'count_pages' => $numberOfPages,
638
            'crdate' => time(),
639
        ];
640
        if ($GLOBALS["TSFE"]->loginUser) {
641
            // internal user
642
            $insertArray['user_id'] = $GLOBALS["TSFE"]->fe_user->user['uid'];
643
            $insertArray['name'] = $GLOBALS["TSFE"]->fe_user->user['username'];
644
            $insertArray['label'] = 'Print: ' . $printer->getLabel();
645
        } else {
646
            // external user
647
            $insertArray['user_id'] = 0;
648
            $insertArray['name'] = 'n/a';
649
            $insertArray['label'] = 'Print: ' . $printer->getLabel();
650
        }
651
        // add action to protocol
652
        GeneralUtility::makeInstance(ConnectionPool::class)
653
            ->getConnectionForTable('tx_dlf_actionlog')
654
            ->insert(
655
                'tx_dlf_actionlog',
656
                $insertArray
657
            );
658
659
        $this->redirectToUri($pdfUrl);
660
    }
661
}
662