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
02:50
created

BasketController::sendMail()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 68
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 41
c 2
b 0
f 0
nc 24
nop 1
dl 0
loc 68
rs 8.3306

How to fix   Long Method   

Long Method

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:

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\Model\Basket;
19
use Kitodo\Dlf\Domain\Repository\ActionLogRepository;
20
use Kitodo\Dlf\Domain\Repository\MailRepository;
21
use Kitodo\Dlf\Domain\Repository\BasketRepository;
22
use Kitodo\Dlf\Domain\Repository\PrinterRepository;
23
use TYPO3\CMS\Core\Database\Connection;
24
use TYPO3\CMS\Core\Database\ConnectionPool;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
27
28
class BasketController extends AbstractController
29
{
30
    /**
31
     * @var BasketRepository
32
     */
33
    protected $basketRepository;
34
35
    /**
36
     * @param BasketRepository $basketRepository
37
     */
38
    public function injectBasketRepository(BasketRepository $basketRepository)
39
    {
40
        $this->basketRepository = $basketRepository;
41
    }
42
43
    /**
44
     * @var MailRepository
45
     */
46
    protected $mailRepository;
47
48
    /**
49
     * @param MailRepository $mailRepository
50
     */
51
    public function injectMailRepository(MailRepository $mailRepository)
52
    {
53
        $this->mailRepository = $mailRepository;
54
    }
55
56
    /**
57
     * @var PrinterRepository
58
     */
59
    protected $printerRepository;
60
61
    /**
62
     * @param PrinterRepository $printerRepository
63
     */
64
    public function injectPrinterRepository(PrinterRepository $printerRepository)
65
    {
66
        $this->printerRepository = $printerRepository;
67
    }
68
69
    /**
70
     * @var ActionLogRepository
71
     */
72
    protected $actionLogRepository;
73
74
    /**
75
     * @param ActionLogRepository $actionLogRepository
76
     */
77
    public function injectActionLogRepository(ActionLogRepository $actionLogRepository)
78
    {
79
        $this->actionLogRepository = $actionLogRepository;
80
    }
81
82
    /**
83
     * Different actions which depends on the choosen action (form)
84
     *
85
     * @return void
86
     */
87
    public function basketAction()
88
    {
89
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
90
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
91
92
        $basket = $this->getBasketData();
93
94
        // action remove from basket
95
        if ($requestData['basket_action'] === 'remove') {
96
            // remove entry from list
97
            if (isset($requestData['selected'])) {
98
                $basket = $this->removeFromBasket($requestData, $basket);
99
            }
100
        }
101
        // action remove from basket
102
        if ($requestData['basket_action'] == 'download') {
103
            // open selected documents
104
            if (isset($requestData['selected'])) {
105
                $pdfUrl = $this->settings['pdfgenerate'];
106
                foreach ($requestData['selected'] as $docValue) {
107
                    if ($docValue['id']) {
108
                        $docData = $this->getDocumentData($docValue['id'], $docValue);
109
                        $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
110
                        $this->redirectToUri($pdfUrl);
111
                    }
112
                }
113
            }
114
        }
115
        // action print from basket
116
        if ($requestData['print_action']) {
117
            // open selected documents
118
            if (isset($requestData['selected'])) {
119
                $this->printDocument($requestData, $basket);
120
            }
121
        }
122
        // action send mail
123
        if ($requestData['mail_action']) {
124
            if (isset($requestData['selected'])) {
125
                $this->sendMail($requestData);
126
            }
127
        }
128
129
        $this->redirect('main');
130
    }
131
132
    /**
133
     * Add documents to the basket
134
     *
135
     * @return void
136
     */
137
    public function addAction()
138
    {
139
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
140
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
141
142
        $basket = $this->getBasketData();
143
144
        if (
145
            !empty($requestData['id'])
146
            && $requestData['addToBasket']
147
        ) {
148
            $basket = $this->addToBasket($requestData, $basket);
0 ignored issues
show
Unused Code introduced by
The assignment to $basket is dead and can be removed.
Loading history...
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
        $basket = $this->getBasketData();
165
166
        if ($basket->getDocIds()) {
167
            $count = sprintf(LocalizationUtility::translate('basket.count', 'dlf'), count(json_decode($basket->getDocIds())));
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

167
            $count = sprintf(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.count', 'dlf'), count(json_decode($basket->getDocIds())));
Loading history...
168
        } else {
169
            $count = sprintf(LocalizationUtility::translate('basket.count', 'dlf'), 0);
170
        }
171
        $this->view->assign('count', $count);
172
173
        $allMails = $this->mailRepository->findAllWithPid($this->settings['pages']);
174
175
        $mailSelect = [];
176
        if ($allMails->count() > 0) {
177
            $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

177
            $mailSelect[0] = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.chooseMail', 'dlf'));
Loading history...
178
            foreach ($allMails as $mail) {
179
                $mailSelect[$mail->getUid()] = htmlspecialchars($mail->getName()) . ' (' . htmlspecialchars($mail->getMail()) . ')';
180
            }
181
            $this->view->assign('mailSelect', $mailSelect);
182
        }
183
184
        $allPrinter = $this->printerRepository->findAll();
185
186
        $printSelect = [];
187
        if ($allPrinter->count() > 0) {
188
            $printSelect[0] = htmlspecialchars(LocalizationUtility::translate('basket.choosePrinter', 'dlf'));
189
            foreach ($allPrinter as $printer) {
190
                $printSelect[$printer->getUid()] = htmlspecialchars($printer->getLabel());
191
            }
192
            $this->view->assign('printSelect', $printSelect);
193
        }
194
195
        $entries = [];
196
        if ($basket->getDocIds()) {
197
            // get each entry
198
            foreach (json_decode($basket->getDocIds()) as $value) {
199
                $entries[] = $this->getEntry($value);
200
            }
201
            $this->view->assign('entries', $entries);
202
        }
203
    }
204
205
    /**
206
     * The basket data from user session.
207
     *
208
     * @return Basket The found data from user session.
209
     */
210
    protected function getBasketData()
211
    {
212
        // get user session
213
        $sessionId = $GLOBALS['TSFE']->fe_user->id;
214
215
        if ($GLOBALS['TSFE']->loginUser) {
216
            $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

216
            /** @scrutinizer ignore-call */ 
217
            $basket = $this->basketRepository->findOneByFeUserId((int) $GLOBALS['TSFE']->fe_user->user['uid']);
Loading history...
217
        } else {
218
            $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_dlf_basket', '');
219
            $GLOBALS['TSFE']->fe_user->sesData_change = true;
220
            $GLOBALS['TSFE']->fe_user->storeSessionData();
221
222
            $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

222
            /** @scrutinizer ignore-call */ 
223
            $basket = $this->basketRepository->findOneBySessionId($sessionId);
Loading history...
223
        }
224
        // session doesnt exists
225
        if ($basket === null) {
226
            // create new basket in db
227
            $basket = new Basket();
228
            $basket->setSessionId($sessionId);
229
            $basket->setFeUserId($GLOBALS['TSFE']->loginUser ? $GLOBALS['TSFE']->fe_user->user['uid'] : 0);
230
231
            $this->basketRepository->add($basket);
232
233
            $persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
234
            $persistenceManager->persistAll();
235
236
            return $basket;
237
        }
238
239
        return $basket;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $basket also could return the type TYPO3\CMS\Extbase\Persis...Interface|array|integer which is incompatible with the documented return type Kitodo\Dlf\Domain\Model\Basket.
Loading history...
240
    }
241
242
    /**
243
     * Return one basket entry
244
     *
245
     * @access protected
246
     *
247
     * @param array $data: DocumentData
248
     * @param array $template: Template information
249
     *
250
     * @return string One basket entry
251
     */
252
    protected function getEntry($data)
253
    {
254
        if (is_object($data)) {
255
            $data = get_object_vars($data);
256
        }
257
        $id = $data['id'];
258
        $startpage = $data['startpage'];
259
        $endpage = $data['endpage'];
260
        $startX = $data['startX'];
261
        $startY = $data['startY'];
262
        $endX = $data['endX'];
263
        $endY = $data['endY'];
264
        $rotation = $data['rotation'];
265
266
        $docData = $this->getDocumentData($id, $data);
267
268
        $entryArray['BASKETDATA'] = $docData;
269
270
        $entryKey = $id . '_' . $startpage;
271
        if (!empty($startX)) {
272
            $entryKey .= '_' . $startX;
273
        }
274
        if (!empty($endX)) {
275
            $entryKey .= '_' . $endX;
276
        }
277
278
        $entryArray['id'] = $id;
279
        $entryArray['CONTROLS'] = [
280
            'startpage' => $startpage,
281
            'endpage' => $endpage,
282
            'startX' => $startX,
283
            'startY' => $startY,
284
            'endX' => $endX,
285
            'endY' => $endY,
286
            'rotation' => $rotation,
287
        ];
288
289
        $entryArray['NUMBER'] = $docData['record_id'];
290
        $entryArray['key'] = $entryKey;
291
292
        // return one entry
293
        return $entryArray;
294
    }
295
296
    /**
297
     * Returns the downloadurl configured in the basket
298
     *
299
     * @access protected
300
     *
301
     * @param int $id: Document id
302
     *
303
     * @return mixed download url or false
304
     */
305
    protected function getDocumentData($id, $data)
306
    {
307
        // get document instance to load further information
308
        $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

308
        $document = Document::getInstance($id, /** @scrutinizer ignore-type */ 0);
Loading history...
309
        if ($document) {
0 ignored issues
show
introduced by
$document is of type Kitodo\Dlf\Common\Document, thus it always evaluated to true.
Loading history...
310
            // replace url param placeholder
311
            $urlParams = str_replace("##page##", (int) $data['page'], $this->settings['pdfparams']);
312
            $urlParams = str_replace("##docId##", $document->recordId, $urlParams);
313
            $urlParams = str_replace("##startpage##", (int) $data['startpage'], $urlParams);
314
            if ($data['startpage'] != $data['endpage']) {
315
                $urlParams = str_replace("##endpage##", $data['endpage'] === "" ? "" : (int) $data['endpage'], $urlParams);
316
            } else {
317
                // remove parameter endpage
318
                $urlParams = str_replace(",##endpage##", '', $urlParams);
319
            }
320
            $urlParams = str_replace("##startx##", $data['startX'] === "" ? "" : (int) $data['startX'], $urlParams);
321
            $urlParams = str_replace("##starty##", $data['startY'] === "" ? "" : (int) $data['startY'], $urlParams);
322
            $urlParams = str_replace("##endx##", $data['endX'] === "" ? "" : (int) $data['endX'], $urlParams);
323
            $urlParams = str_replace("##endy##", $data['endY'] === "" ? "" : (int) $data['endY'], $urlParams);
324
            $urlParams = str_replace("##rotation##", $data['rotation'] === "" ? "" : (int) $data['rotation'], $urlParams);
325
326
            $downloadUrl = $this->settings['pdfgenerate'] . $urlParams;
327
328
            $title = $document->getTitle($id, true);
329
            if (empty($title)) {
330
                $title = LocalizationUtility::translate('basket.noTitle', 'dlf') ? : '';
331
            }
332
333
            // Set page and cutout information
334
            $info = '';
335
            if ($data['startX'] != '' && $data['endX'] != '') {
336
                // cutout
337
                $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

337
                $info .= htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.cutout', 'dlf')) . ' ';
Loading history...
338
            }
339
            if ($data['startpage'] == $data['endpage']) {
340
                // One page
341
                $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage'];
342
            } else {
343
                $info .= htmlspecialchars(LocalizationUtility::translate('page', 'dlf')) . ' ' . $data['startpage'] . '-' . $data['endpage'];
344
            }
345
            $downloadLink = '<a href="' . $downloadUrl . '" target="_blank">' . htmlspecialchars($title) . '</a> (' . $info . ')';
346
            if ($data['startpage'] == $data['endpage']) {
347
                $pageNums = 1;
348
            } else {
349
                $pageNums = $data['endpage'] - $data['startpage'];
350
            }
351
            return [
352
                'downloadUrl' => $downloadUrl,
353
                'title' => $title,
354
                'info' => $info,
355
                'downloadLink' => $downloadLink,
356
                'pageNums' => $pageNums,
357
                'urlParams' => $urlParams,
358
                'record_id' => $document->recordId,
359
            ];
360
        }
361
        return false;
362
    }
363
364
    /**
365
     * Adds documents to the basket
366
     *
367
     * @access protected
368
     *
369
     * @param array $_piVars: piVars
370
     * @param Basket $basket: basket object
371
     *
372
     * @return array Basket data and Javascript output
373
     */
374
    protected function addToBasket($_piVars, $basket)
375
    {
376
        $output = '';
377
        if (!$_piVars['startpage']) {
378
            $page = 0;
379
        } else {
380
            $page = (int) $_piVars['startpage'];
381
        }
382
        if ($page != null || $_piVars['addToBasket'] == 'list') {
383
            $documentItem = [
384
                'id' => (int) $_piVars['id'],
385
                'startpage' => (int) $_piVars['startpage'],
386
                'endpage' => !isset($_piVars['endpage']) || $_piVars['endpage'] === "" ? "" : (int) $_piVars['endpage'],
387
                'startX' => !isset($_piVars['startX']) || $_piVars['startX'] === "" ? "" : (int) $_piVars['startX'],
388
                'startY' => !isset($_piVars['startY']) || $_piVars['startY'] === "" ? "" : (int) $_piVars['startY'],
389
                'endX' => !isset($_piVars['endX']) || $_piVars['endX'] === "" ? "" : (int) $_piVars['endX'],
390
                'endY' => !isset($_piVars['endY']) || $_piVars['endY'] === "" ? "" : (int) $_piVars['endY'],
391
                'rotation' => !isset($_piVars['rotation']) || $_piVars['rotation'] === "" ? "" : (int) $_piVars['rotation']
392
            ];
393
394
            // update basket
395
            if (!empty(json_decode($basket->getDocIds()))) {
396
                $items = json_decode($basket->getDocIds());
397
                $items = get_object_vars($items);
398
            } else {
399
                $items = [];
400
            }
401
            // get document instance to load further information
402
            $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

402
            $document = Document::getInstance($documentItem['id'], /** @scrutinizer ignore-type */ 0);
Loading history...
403
            // set endpage for toc and subentry based on logid
404
            if (($_piVars['addToBasket'] == 'subentry') or ($_piVars['addToBasket'] == 'toc')) {
405
                $smLinks = $document->smLinks;
406
                $pageCounter = sizeof($smLinks['l2p'][$_piVars['logId']]);
407
                $documentItem['endpage'] = ($documentItem['startpage'] + $pageCounter) - 1;
408
            }
409
            // add whole document
410
            if ($_piVars['addToBasket'] == 'list') {
411
                $documentItem['endpage'] = $document->numPages;
412
            }
413
            $arrayKey = $documentItem['id'] . '_' . $page;
414
            if (!empty($documentItem['startX'])) {
415
                $arrayKey .= '_' . $documentItem['startX'];
416
            }
417
            if (!empty($documentItem['endX'])) {
418
                $arrayKey .= '_' . $documentItem['endX'];
419
            }
420
            // do not add more than one identical object
421
            if (!in_array($arrayKey, $items)) {
422
                $items[$arrayKey] = $documentItem;
423
                // replace url param placeholder
424
                $pdfParams = str_replace("##startpage##", $documentItem['startpage'], $this->settings['pdfparams']);
425
                $pdfParams = str_replace("##docId##", $document->recordId, $pdfParams);
426
                $pdfParams = str_replace("##startx##", $documentItem['startX'], $pdfParams);
427
                $pdfParams = str_replace("##starty##", $documentItem['startY'], $pdfParams);
428
                $pdfParams = str_replace("##endx##", $documentItem['endX'], $pdfParams);
429
                $pdfParams = str_replace("##endy##", $documentItem['endY'], $pdfParams);
430
                $pdfParams = str_replace("##rotation##", $documentItem['rotation'], $pdfParams);
431
                if ($documentItem['startpage'] != $documentItem['endpage']) {
432
                    $pdfParams = str_replace("##endpage##", $documentItem['endpage'], $pdfParams);
433
                } else {
434
                    // remove parameter endpage
435
                    $pdfParams = str_replace(",##endpage##", '', $pdfParams);
436
                }
437
                $pdfGenerateUrl = $this->settings['pdfgenerate'] . $pdfParams;
438
                if ($this->settings['pregeneration']) {
439
                    // send ajax request to webapp
440
                    $output .= '
441
     <script>
442
      $(document).ready(function(){
443
       $.ajax({
444
         url: "' . $pdfGenerateUrl . '",
445
       }).done(function() {
446
       });
447
      });
448
     </script>';
449
                }
450
            }
451
452
            $basket->setDocIds(json_encode($items));
453
            $this->basketRepository->update($basket);
454
        }
455
        $this->view->assign('pregenerateJs', $output);
456
457
        return $basket;
458
    }
459
460
    /**
461
     * Removes selected documents from basket
462
     *
463
     * @access protected
464
     *
465
     * @param array $_piVars: plugin variables
466
     * @param Basket $basket: basket object
467
     *
468
     * @return Basket basket
469
     */
470
    protected function removeFromBasket($_piVars, $basket)
471
    {
472
        if (!empty($basket->getDocIds())) {
473
            $items = json_decode($basket->getDocIds());
474
            $items = get_object_vars($items);
475
        }
476
        foreach ($_piVars['selected'] as $value) {
477
            if (isset($value['id'])) {
478
                $arrayKey = $value['id'] . '_' . $value['startpage'];
479
                if (!empty($value['startX'])) {
480
                    $arrayKey .= '_' . $value['startX'];
481
                }
482
                if (!empty($value['endX'])) {
483
                    $arrayKey .= '_' . $value['endX'];
484
                }
485
                if (isset($items[$arrayKey])) {
486
                    unset($items[$arrayKey]);
487
                }
488
            }
489
        }
490
        if (empty($items)) {
491
            $update = '';
492
        } else {
493
            $update = json_encode($items);
494
        }
495
496
        $basket->setDocIds($update);
497
        $this->basketRepository->update($basket);
498
499
        return $basket;
500
    }
501
502
    /**
503
     * Send mail with pdf download url
504
     *
505
     * @access protected
506
     *
507
     * @return void
508
     */
509
    protected function sendMail($requestData)
510
    {
511
        // send mail
512
        $mailId = $requestData['mail_action'];
513
514
        $mailObject = $this->mailRepository->findByUid(intval($mailId))->getFirst();
515
516
        $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

516
        $mailText = htmlspecialchars(/** @scrutinizer ignore-type */ LocalizationUtility::translate('basket.mailBody', 'dlf')) . "\n";
Loading history...
517
        $numberOfPages = 0;
518
        $pdfUrl = $this->settings['pdfdownload'];
519
        // prepare links
520
        foreach ($requestData['selected'] as $docValue) {
521
            if ($docValue['id']) {
522
                $explodeId = explode("_", $docValue['id']);
523
                $docData = $this->getDocumentData($explodeId[0], $docValue);
524
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
525
                $pages = (abs(intval($docValue['startpage']) - intval($docValue['endpage'])));
526
                if ($pages === 0) {
527
                    $numberOfPages = $numberOfPages + 1;
528
                } else {
529
                    $numberOfPages = $numberOfPages + $pages;
530
                }
531
            }
532
        }
533
        // Remove leading/tailing pdfparamseperator
534
        $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
535
        $mailBody = $mailText . $pdfUrl;
536
        // Get hook objects.
537
        $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...
538
        // Hook for getting a customized mail body.
539
        foreach ($hookObjects as $hookObj) {
540
            if (method_exists($hookObj, 'customizeMailBody')) {
541
                $mailBody = $hookObj->customizeMailBody($mailText, $pdfUrl);
542
            }
543
        }
544
        $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
545
        // send mail
546
        $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
547
        // Prepare and send the message
548
        $mail
549
            // subject
550
            ->setSubject(LocalizationUtility::translate('basket.mailSubject', 'dlf'))
551
            // Set the From address with an associative array
552
            ->setFrom($from)
553
            // Set the To addresses with an associative array
554
            ->setTo([$mailObject->getMail() => $mailObject->getName()])
555
            ->setBody($mailBody, 'text/html')
556
            ->send();
557
558
        // create entry for action log
559
        $newActionLog = $this->objectManager->get(ActionLog::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

559
        $newActionLog = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ActionLog::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
560
        $newActionLog->setFileName($pdfUrl);
561
        $newActionLog->setCountPages($numberOfPages);
562
        $newActionLog->setLabel('Mail: ' . $mailObject->getMail());
563
564
        if ($GLOBALS["TSFE"]->loginUser) {
565
            // internal user
566
            $newActionLog->setUserId($GLOBALS["TSFE"]->fe_user->user['uid']);
567
            $newActionLog->setName($GLOBALS["TSFE"]->fe_user->user['username']);
568
        } else {
569
            // external user
570
            $newActionLog->setUserId(0);
571
            $newActionLog->setName('n/a');
572
        }
573
574
        $this->actionLogRepository->add($newActionLog);
575
576
        $this->redirect('main');
577
    }
578
579
    /**
580
     * Sends document information to an external printer (url)
581
     *
582
     * @access protected
583
     * @param Basket basket object
584
     *
585
     * @return void
586
     */
587
    protected function printDocument($requestData, $basket)
0 ignored issues
show
Unused Code introduced by
The parameter $basket 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

587
    protected function printDocument($requestData, /** @scrutinizer ignore-unused */ $basket)

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...
588
    {
589
        $pdfUrl = $this->settings['pdfprint'];
590
        $numberOfPages = 0;
591
        foreach ($requestData['selected'] as $docId => $docValue) {
592
            if ($docValue['id']) {
593
                $docData = $this->getDocumentData($docValue['id'], $docValue);
594
                $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
595
                $numberOfPages += $docData['pageNums'];
596
            }
597
        }
598
        // get printer data
599
        $printerId = $requestData['print_action'];
600
601
        // get id from db and send selected doc download link
602
        $printer = $this->printerRepository->findOneByUid($printerId);
0 ignored issues
show
Bug introduced by
The method findOneByUid() does not exist on Kitodo\Dlf\Domain\Repository\PrinterRepository. 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

602
        /** @scrutinizer ignore-call */ 
603
        $printer = $this->printerRepository->findOneByUid($printerId);
Loading history...
603
604
        // printer is selected
605
        if ($printer) {
606
            $pdfUrl = $printer->getPrint();
0 ignored issues
show
Bug introduced by
The method getPrint() 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

606
            /** @scrutinizer ignore-call */ 
607
            $pdfUrl = $printer->getPrint();

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...
607
            $numberOfPages = 0;
608
            foreach ($requestData['selected'] as $docId => $docValue) {
609
                if ($docValue['id']) {
610
                    $explodeId = explode("_", $docId);
611
                    $docData = $this->getDocumentData($explodeId[0], $docValue);
612
                    $pdfUrl .= $docData['urlParams'] . $this->settings['pdfparamseparator'];
613
                    $numberOfPages += $docData['pageNums'];
614
                }
615
            }
616
            $pdfUrl = trim($pdfUrl, $this->settings['pdfparamseparator']);
617
        }
618
619
        $actionLog = new ActionLog();
620
        // protocol
621
        $actionLog->setPid($this->settings['pages']);
622
        $actionLog->setFileName($pdfUrl);
623
        $actionLog->setCountPages($numberOfPages);
624
625
        if ($GLOBALS["TSFE"]->loginUser) {
626
            // internal user
627
            $actionLog->setUserId($GLOBALS["TSFE"]->fe_user->user['uid']);
628
            $actionLog->setName($GLOBALS["TSFE"]->fe_user->user['username']);
629
            $actionLog->setLabel('Print: ' . $printer->getLabel());
630
        } else {
631
            // external user
632
            $actionLog->setUserId(0);
633
            $actionLog->setName('n/a');
634
            $actionLog->setLabel('Print: ' . $printer->getLabel());
635
        }
636
        // add action to protocol
637
        $this->actionLogRepository->add($actionLog);
638
639
        $this->redirectToUri($pdfUrl);
640
    }
641
}
642