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
Push — master ( 86258e...0ee995 )
by Sebastian
16:29
created

tx_dlf_basket::main()   F

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 283
Code Lines 121

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 121
dl 0
loc 283
rs 0
c 1
b 0
f 0
cc 25
nc 62208
nop 2

How to fix   Long Method    Complexity   

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
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Plugin 'DLF: Basket' for the 'dlf' extension.
14
 *
15
 * @author	Christopher Timm <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_basket extends tx_dlf_plugin {
21
22
    public $scriptRelPath = 'plugins/basket/class.tx_dlf_basket.php';
23
24
    /**
25
     * The main method of the PlugIn
26
     *
27
     * @access	public
28
     *
29
     * @param	string		$content: The PlugIn content
30
     * @param	array		$conf: The PlugIn configuration
31
     *
32
     * @return	string		The content that is displayed on the website
33
     */
34
    public function main($content, $conf) {
35
36
        $this->init($conf);
37
38
        // Don't cache the output.
39
        $this->setCache(FALSE);
40
41
        // Load template file.
42
        if (!empty($this->conf['templateFile'])) {
43
44
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
45
46
        } else {
47
48
            $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/basket/template.tmpl'), '###TEMPLATE###');
49
50
        }
51
52
        $subpartArray['entry'] = $this->cObj->getSubpart($this->template, '###ENTRY###');
1 ignored issue
show
Comprehensibility Best Practice introduced by
$subpartArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subpartArray = array(); before regardless.
Loading history...
53
54
        $markerArray['###JS###'] = '';
1 ignored issue
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
55
56
        // get user session
57
        $sessionId = $GLOBALS['TSFE']->fe_user->id;
58
59
        if ($GLOBALS['TSFE']->loginUser) {
60
61
            $insertArray['fe_user_id'] = $GLOBALS['TSFE']->fe_user->user['uid'];
1 ignored issue
show
Comprehensibility Best Practice introduced by
$insertArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $insertArray = array(); before regardless.
Loading history...
62
63
            $query = $GLOBALS['TYPO3_DB']->SELECTquery(
64
                '*',
65
                'tx_dlf_basket',
66
                'tx_dlf_basket.fe_user_id='.intval($insertArray['fe_user_id']).tx_dlf_helper::whereClause('tx_dlf_basket'),
67
                '',
68
                '',
69
                '1'
70
            );
71
72
        } else {
73
74
            $GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_dlf_basket', '');
75
76
            $GLOBALS['TSFE']->fe_user->sesData_change = TRUE;
77
78
            $GLOBALS['TSFE']->fe_user->storeSessionData();
79
80
            $query = $GLOBALS['TYPO3_DB']->SELECTquery(
81
                '*',
82
                'tx_dlf_basket',
83
                'tx_dlf_basket.session_id='.$GLOBALS['TYPO3_DB']->fullQuoteStr($sessionId, 'tx_dlf_basket').tx_dlf_helper::whereClause('tx_dlf_basket'),
84
                '',
85
                '',
86
                '1'
87
            );
88
89
        }
90
91
        $result = $GLOBALS['TYPO3_DB']->sql_query($query);
92
93
        // session already exists
94
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) == 0) {
95
96
            // create new basket in db
97
            $insertArray['session_id'] = $sessionId;
98
            $insertArray['doc_ids'] = '';
99
            $insertArray['label'] = '';
100
            $insertArray['l18n_diffsource'] = '';
101
102
            $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_dlf_basket', $insertArray);
103
104
            $result = $GLOBALS['TYPO3_DB']->sql_query($query);
105
106
        }
107
108
        $basketData = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
109
110
        $piVars = $this->piVars;
111
112
        // action add to basket
113
        if (!empty($this->piVars['id']) && $this->piVars['addToBasket']) {
114
115
            $returnData = $this->addToBasket($this->piVars, $basketData);
116
117
            $basketData = $returnData['basketData'];
118
119
            $markerArray['###JS###'] = $returnData['jsOutput'];
120
121
        } else {
122
123
            $basketData['doc_ids'] = json_decode($basketData['doc_ids']);
124
125
        }
126
127
        // action remove from basket
128
        if ($this->piVars['basket_action'] == 'remove') {
129
130
            // remove entry from list
131
            unset($piVars['basket_action']);
132
133
            if (isset($this->piVars['selected'])) {
134
135
                $basketData = $this->removeFromBasket($piVars, $basketData);
136
137
            }
138
139
        }
140
141
        // action remove from basket
142
        if ($this->piVars['basket_action'] == 'open') {
143
144
            // open selected documents
145
            unset($piVars['basket_action']);
146
147
            if (isset($this->piVars['selected'])) {
148
149
                $basketData = $this->openFromBasket($piVars, $basketData);
150
151
            }
152
153
        }
154
155
        // action print from basket
156
        if ($this->piVars['print_action']) {
157
158
            // open selected documents
159
            unset($piVars['print_action']);
160
161
            if (isset($this->piVars['selected'])) {
162
163
                $basketData = $this->printDocument($piVars, $basketData);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $basketData is correct as $this->printDocument($piVars, $basketData) targeting tx_dlf_basket::printDocument() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to tx_dlf_basket::printDocument() has too many arguments starting with $piVars. ( Ignorable by Annotation )

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

163
                /** @scrutinizer ignore-call */ 
164
                $basketData = $this->printDocument($piVars, $basketData);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
164
165
            }
166
167
        }
168
169
        // action send mail
170
        if ($this->piVars['mail_action']) {
171
172
            if (isset($this->piVars['selected'])) {
173
174
                $this->sendMail($this->piVars);
0 ignored issues
show
Unused Code introduced by
The call to tx_dlf_basket::sendMail() has too many arguments starting with $this->piVars. ( Ignorable by Annotation )

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

174
                $this->/** @scrutinizer ignore-call */ 
175
                       sendMail($this->piVars);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
175
176
            }
177
178
        }
179
180
        // set marker
181
        $markerArray['###ACTION###'] = $this->pi_getPageLink($GLOBALS['TSFE']->id);
182
183
        $markerArray['###LISTTITLE###'] = $this->pi_getLL('basket', '', TRUE);
184
185
        if ($basketData['doc_ids']) {
186
187
            if (is_object($basketData['doc_ids'])) {
188
189
                $basketData['doc_ids'] = get_object_vars($basketData['doc_ids']);
190
191
            }
192
193
            $markerArray['###COUNT###'] = sprintf($this->pi_getLL('count'), count($basketData['doc_ids']));
194
195
        } else {
196
197
            $markerArray['###COUNT###'] = sprintf($this->pi_getLL('count'), 0);
198
199
        }
200
201
        // get mail addresses
202
        $resultMail = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
203
            '*',
204
            'tx_dlf_mail',
205
            '1'.tx_dlf_helper::whereClause('tx_dlf_mail'),
206
            '',
207
            'tx_dlf_mail.sorting',
208
            ''
209
        );
210
211
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($resultMail) > 0) {
212
213
            $mails = array ();
214
215
            $mailForm = '<select name="tx_dlf[mail_action]">';
216
217
            $mailForm .= '<option value="">'.$this->pi_getLL('chooseMail', '', TRUE).'</option>';
218
219
            while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultMail)) {
220
221
                $mails[] = $row;
222
223
                $mailForm .= '<option value="'.$row['uid'].'">'.$row['name'].' ('.$row['mail'].')</option>';
224
225
            }
226
227
            $mailForm .= '</select><input type="submit">';
228
229
        }
230
231
        // mail action form
232
        $markerArray['###MAILACTION###'] = $mailForm;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mailForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
233
234
        // remove action form
235
        $markerArray['###REMOVEACTION###'] = '
236
			<select name="tx_dlf[basket_action]">
237
				<option value="">'.$this->pi_getLL('chooseAction', '', TRUE).'</option>
238
				<option value="open">'.$this->pi_getLL('download', '', TRUE).'</option>
239
				<option value="remove">'.$this->pi_getLL('remove', '', TRUE).'</option>
240
			</select>
241
			<input type="submit">
242
		';
243
244
        // get mail addresses
245
        $resultPrinter = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
246
            '*',
247
            'tx_dlf_printer',
248
            '1'.tx_dlf_helper::whereClause('tx_dlf_printer'),
249
            '',
250
            '',
251
            ''
252
        );
253
254
        $printForm = '';
255
256
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($resultPrinter) > 0) {
257
258
            $printers = array ();
259
260
            $printForm = '<select name="tx_dlf[print_action]">';
261
262
            $printForm .= '<option value="">'.$this->pi_getLL('choosePrinter', '', TRUE).'</option>';
263
264
            while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultPrinter)) {
265
266
                $printers[] = $row;
267
268
                $printForm .= '<option value="'.$row['uid'].'">'.$row['label'].'</option>';
269
270
            }
271
272
            $printForm .= '</select><input type="submit" />';
273
274
        }
275
276
        // print action form
277
        $markerArray['###PRINTACTION###'] = $printForm;
278
279
        $entries = '';
280
281
        if (isset($basketData['doc_ids'])) {
282
283
            // get each entry
284
            foreach ($basketData['doc_ids'] as $key => $value) {
285
286
                $entries .= $this->getEntry($value, $subpartArray);
287
288
            }
289
290
        } else {
291
292
            $entries = '';
293
294
        }
295
296
        // basket go to
297
        if ($this->conf['targetBasket'] && $this->conf['basketGoToButton'] && $this->piVars['id']) {
298
299
            $label = $this->pi_getLL('goBasket', '', TRUE);
300
301
            $basketConf = array (
302
                'parameter' => $this->conf['targetBasket'],
303
                'title' => $label
304
            );
305
306
            $markerArray['###BASKET###'] = $this->cObj->typoLink($label, $basketConf);
307
308
        } else {
309
310
            $markerArray['###BASKET###'] = '';
311
312
        }
313
314
        $content = $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($this->template, '###ENTRY###', $entries, TRUE), $markerArray);
315
316
        return $this->pi_wrapInBaseClass($content);
317
318
    }
319
320
    /**
321
     * Return one basket entry
322
     * @param  array $data     DocumentData
323
     * @param  array $template Template information
324
     * @return Template
0 ignored issues
show
Bug introduced by
The type Template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
325
     */
326
    public function getEntry($data, $template) {
327
328
        if (is_object($data)) {
0 ignored issues
show
introduced by
The condition is_object($data) is always false.
Loading history...
329
330
            $data = get_object_vars($data);
331
332
        }
333
334
        $id = $data['id'];
335
336
        $startpage = $data['startpage'];
337
338
        $endpage = $data['endpage'];
339
340
        $startX = $data['startX'];
341
342
        $startY = $data['startY'];
343
344
        $endX = $data['endX'];
345
346
        $endY = $data['endY'];
347
348
        $rotation = $data['rotation'];
349
350
        $docData = $this->getDocumentData($id, $data);
351
352
        $markerArray['###BASKETDATA###'] = $docData['downloadLink'];
1 ignored issue
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
353
354
        $arrayKey = $id.'_'.$startpage;
355
356
        if (isset($startX)) {
357
358
            $arrayKey .= '_'.$startX;
359
360
        }
361
362
        if (isset($endX)) {
363
364
            $arrayKey .= '_'.$endX;
365
366
        }
367
368
        $controlMark = '<input value="'.$id.'" name="tx_dlf[selected]['.$arrayKey.'][id]" type="checkbox">';
369
370
        $controlMark .= '<input value="'.$startpage.'" name="tx_dlf[selected]['.$arrayKey.'][startpage]" type="hidden">';
371
372
        $controlMark .= '<input value="'.$endpage.'" name="tx_dlf[selected]['.$arrayKey.'][endpage]" type="hidden">';
373
374
        // add hidden fields for detail information
375
        if ($startX) {
376
377
            $controlMark .= '<input type="hidden" name="tx_dlf[selected]['.$arrayKey.'][startX]" value="'.$startX.'">';
378
379
            $controlMark .= '<input type="hidden" name="tx_dlf[selected]['.$arrayKey.'][startY]"  value="'.$startY.'">';
380
381
            $controlMark .= '<input type="hidden" name="tx_dlf[selected]['.$arrayKey.'][endX]"  value="'.$endX.'">';
382
383
            $controlMark .= '<input type="hidden" name="tx_dlf[selected]['.$arrayKey.'][endY]"  value="'.$endY.'">';
384
385
            $controlMark .= '<input type="hidden" name="tx_dlf[selected]['.$arrayKey.'][rotation]"  value="'.$rotation.'">';
386
387
        }
388
389
        // return one entry
390
        $markerArray['###CONTROLS###'] = $controlMark;
391
392
        $markerArray['###NUMBER###'] = $docData['record_id'];
393
394
        return $this->cObj->substituteMarkerArray($this->cObj->substituteSubpart($template['entry'], '###ENTRY###', $subpart, TRUE), $markerArray);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cObj->subs...t, TRUE), $markerArray) returns the type string which is incompatible with the documented return type Template.
Loading history...
Comprehensibility Best Practice introduced by
The variable $subpart seems to be never defined.
Loading history...
395
396
    }
397
398
    /**
399
     * Adds documents to the basket
400
     * @param array $_piVars    piVars
401
     * @param array $basketData basket data
402
     */
403
    public function addToBasket($_piVars, $basketData) {
404
405
        if (!$_piVars['startpage']) {
406
407
            $page = 0;
408
409
        } else {
410
411
            $page = intval($_piVars['startpage']);
412
413
        }
414
415
        if ($page != NULL || $_piVars['addToBasket'] == 'list') {
416
417
            $documentItem = array (
418
                'id' => intval($_piVars['id']),
419
                'startpage' => intval($_piVars['startpage']),
420
                'endpage' => !isset($_piVars['endpage']) || $_piVars['endpage'] === "" ? "" : intval($_piVars['endpage']),
421
                'startX' => !isset($_piVars['startX']) || $_piVars['startX'] === "" ? "" : intval($_piVars['startX']),
422
                'startY' => !isset($_piVars['startY']) || $_piVars['startY'] === "" ? "" : intval($_piVars['startY']),
423
                'endX' => !isset($_piVars['endX']) || $_piVars['endX'] === "" ? "" : intval($_piVars['endX']),
424
                'endY' => !isset($_piVars['endY']) || $_piVars['endY'] === "" ? "" : intval($_piVars['endY']),
425
                'rotation' => !isset($_piVars['rotation']) || $_piVars['rotation'] === "" ? "" : intval($_piVars['rotation'])
426
            );
427
428
            // update basket
429
            if (!empty($basketData['doc_ids'])) {
430
431
                $items = json_decode($basketData['doc_ids']);
432
433
                $items = get_object_vars($items);
434
435
            } else {
436
437
                $items = array ();
438
439
            }
440
441
            // get document instance to load further information
442
            $document = tx_dlf_document::getInstance($documentItem['id'], 0);
443
444
            // set endpage for toc and subentry based on logid
445
            if (($_piVars['addToBasket'] == 'subentry') or ($_piVars['addToBasket'] == 'toc')) {
446
447
                $smLinks = $document->smLinks;
0 ignored issues
show
Bug Best Practice introduced by
The property $smLinks is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
448
449
                $pageCounter = sizeof($smLinks['l2p'][$_piVars['logId']]);
450
451
                $documentItem['endpage'] = ($documentItem['startpage'] + $pageCounter) - 1;
452
453
            }
454
455
            // add whole document
456
            if ($_piVars['addToBasket'] == 'list') {
457
458
                $documentItem['endpage'] = $document->numPages;
0 ignored issues
show
Bug Best Practice introduced by
The property $numPages is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
459
460
            }
461
462
            $arrayKey = $documentItem['id'].'_'.$page;
463
464
            if (!empty($documentItem['startX'])) {
465
466
                $arrayKey .= '_'.$documentItem['startX'];
467
468
            }
469
470
            if (!empty($documentItem['endX'])) {
471
472
                $arrayKey .= '_'.$documentItem['endX'];
473
474
            }
475
476
            // do not add more than one identical object
477
            if (!in_array($arrayKey, $items)) {
478
479
                $items[$arrayKey] = $documentItem;
480
481
                // replace url param placeholder
482
                $pdfParams = str_replace("##startpage##", $documentItem['startpage'], $this->conf['pdfparams']);
483
484
                $pdfParams = str_replace("##docId##", $document->recordId, $pdfParams);
0 ignored issues
show
Bug Best Practice introduced by
The property $recordId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
485
486
                $pdfParams = str_replace("##startx##", $documentItem['startX'], $pdfParams);
487
488
                $pdfParams = str_replace("##starty##", $documentItem['startY'], $pdfParams);
489
490
                $pdfParams = str_replace("##endx##", $documentItem['endX'], $pdfParams);
491
492
                $pdfParams = str_replace("##endy##", $documentItem['endY'], $pdfParams);
493
494
                $pdfParams = str_replace("##rotation##", $documentItem['rotation'], $pdfParams);
495
496
                if ($documentItem['startpage'] != $documentItem['endpage']) {
497
498
                    $pdfParams = str_replace("##endpage##", $documentItem['endpage'], $pdfParams);
499
500
                } else {
501
502
                    // remove parameter endpage
503
                    $pdfParams = str_replace(",##endpage##", '', $pdfParams);
504
505
                }
506
507
                $pdfGenerateUrl = $this->conf['pdfgenerate'].$pdfParams;
508
509
                if ($this->conf['pregeneration']) {
510
511
                    // send ajax request to webapp
512
                    $output .= '
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
513
					<script>
514
						$(document).ready(function(){
515
							$.ajax({
516
							  url: "'.$pdfGenerateUrl.'",
517
							}).done(function() {
518
							});
519
						});
520
					</script>';
521
522
                }
523
524
            }
525
526
            $update = array ('doc_ids' => json_encode($items));
527
528
            $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_dlf_basket', 'uid='.intval($basketData['uid']), $update);
529
530
            $basketData['doc_ids'] = $items;
531
532
        }
533
534
        return array ('basketData' => $basketData, 'jsOutput' => $output);
535
536
    }
537
538
    /**
539
     * Removes selected documents from basket
540
     * @param  array $_piVars    plugin variables
541
     * @param  array $basketData array with document information
542
     * @return array             basket data
543
     */
544
    public function removeFromBasket($_piVars, $basketData) {
545
546
        if (!empty($basketData['doc_ids'])) {
547
548
            $items = $basketData['doc_ids'];
549
550
            $items = get_object_vars($items);
551
552
        }
553
554
        foreach ($_piVars['selected'] as $key => $value) {
555
556
            if (isset($value['id'])) {
557
558
                $arrayKey = $value['id'].'_'.$value['startpage'];
559
560
                if (isset($value['startX'])) {
561
562
                    $arrayKey .= '_'.$value['startX'];
563
564
                }
565
566
                if (isset($value['endX'])) {
567
568
                    $arrayKey .= '_'.$value['endX'];
569
570
                }
571
572
                if (isset($items[$arrayKey])) {
573
574
                    unset($items[$arrayKey]);
575
576
                }
577
578
            }
579
580
        }
581
582
        if (empty($items)) {
583
584
            $update = array ('doc_ids' => '');
585
586
        } else {
587
588
            $update = array ('doc_ids' => json_encode($items));
589
590
        }
591
592
        $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_dlf_basket', 'uid='.intval($basketData['uid']), $update);
593
594
        $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...
595
596
        return $basketData;
597
598
    }
599
600
    /**
601
     * Open selected documents from basket
602
     * @param  array $_piVars    plugin variables
603
     * @param  array $basketData array with document information
604
     * @return array             basket data
605
     */
606
    public function openFromBasket($_piVars, $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

606
    public function openFromBasket($_piVars, /** @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...
Unused Code introduced by
The parameter $_piVars 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

606
    public function openFromBasket(/** @scrutinizer ignore-unused */ $_piVars, $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...
607
608
        $pdfUrl = $this->conf['pdfgenerate'];
609
610
        foreach ($this->piVars['selected'] as $docId => $docValue) {
611
612
            if ($docValue['id']) {
613
614
                $filename .= $docValue['id'].'_';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename seems to be never defined.
Loading history...
615
616
                $docData = $this->getDocumentData($docValue['id'], $docValue);
617
618
                $pdfUrl .= $docData['urlParams'].$this->conf['pdfparamseparator'];
619
620
                $numberOfPages += $docData['pageNums'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $numberOfPages seems to be never defined.
Loading history...
621
622
            }
623
624
        }
625
626
        header('Location: '.$pdfUrl);
627
628
        ob_end_flush();
629
630
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
631
632
    }
633
634
    /**
635
     * Returns the downloadurl configured in the basket
636
     * @param  integer $id Document id
637
     * @return mixed     download url or false
638
     */
639
    public function getDocumentData($id, $data) {
640
641
        // get document instance to load further information
642
        $document = tx_dlf_document::getInstance($id, 0);
643
644
        if ($document) {
645
646
            // replace url param placeholder
647
            $urlParams = str_replace("##page##", intval($data['page']), $this->conf['pdfparams']);
648
649
            $urlParams = str_replace("##docId##", $document->recordId, $urlParams);
0 ignored issues
show
Bug Best Practice introduced by
The property $recordId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
650
651
            $urlParams = str_replace("##startpage##", intval($data['startpage']), $urlParams);
652
653
            if ($data['startpage'] != $data['endpage']) {
654
655
                $urlParams = str_replace("##endpage##", $data['endpage'] === "" ? "" : intval($data['endpage']), $urlParams);
656
657
            } else {
658
659
                // remove parameter endpage
660
                $urlParams = str_replace(",##endpage##", '', $urlParams);
661
662
            }
663
664
            $urlParams = str_replace("##startx##", $data['startX'] === "" ? "" : intval($data['startX']), $urlParams);
665
666
            $urlParams = str_replace("##starty##", $data['startY'] === "" ? "" : intval($data['startY']), $urlParams);
667
668
            $urlParams = str_replace("##endx##", $data['endX'] === "" ? "" : intval($data['endX']), $urlParams);
669
670
            $urlParams = str_replace("##endy##", $data['endY'] === "" ? "" : intval($data['endY']), $urlParams);
671
672
            $urlParams = str_replace("##rotation##", $data['rotation'] === "" ? "" : intval($data['rotation']), $urlParams);
673
674
            $downloadUrl = $this->conf['pdfgenerate'].$urlParams;
675
676
            $title = $document->getTitle($id, TRUE);
677
678
            if (empty($title)) {
679
680
                $title = $this->pi_getLL('noTitle', '', TRUE);
681
682
            }
683
684
            // Set page and cutout information
685
            $info = '';
686
687
            if ($data['startX'] != '' && $data['endX'] != '') {
688
689
                // cutout
690
                $info .= $this->pi_getLL('cutout', '', TRUE).' ';
691
692
            }
693
694
            if ($data['startpage'] == $data['endpage']) {
695
696
                // One page
697
                $info .= $this->pi_getLL('page', '', TRUE).' '.$data['startpage'];
698
699
            } else {
700
701
                $info .= $this->pi_getLL('page', '', TRUE).' '.$data['startpage'].'-'.$data['endpage'];
702
703
            }
704
705
            $downloadLink = '<a href="'.$downloadUrl.'" target="_blank">'.$title.'</a> ('.$info.')';
706
707
            if ($data['startpage'] == $data['endpage']) {
708
709
                $pageNums = 1;
710
711
            } else {
712
713
                $pageNums = $data['endpage'] - $data['startpage'];
714
715
            }
716
717
            return array (
718
                'downloadUrl' => $downloadUrl,
719
                'downloadLink' => $downloadLink,
720
                'pageNums'	=> $pageNums,
721
                'urlParams' => $urlParams,
722
                'record_id' => $document->recordId,
723
            );
724
725
        }
726
727
        return FALSE;
728
729
    }
730
731
    /**
732
     * Send mail with pdf download url
733
     */
734
    public function sendMail() {
735
736
        // send mail
737
        $mailId = $this->piVars['mail_action'];
738
739
        // get id from db and send selected doc downloadlink
740
        $resultMail = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
741
            '*',
742
            'tx_dlf_mail',
743
            'tx_dlf_mail.uid="'.intval($mailId).'"'.tx_dlf_helper::whereClause('tx_dlf_mail'),
744
            '',
745
            '',
746
            '1'
747
        );
748
749
        $mailData = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultMail);
750
751
        $body = $this->pi_getLL('mailBody', '', TRUE)."\n";
752
753
        $numberOfPages = 0;
754
755
        $pdfUrl = $this->conf['pdfdownload'];
756
757
        // prepare links
758
        foreach ($this->piVars['selected'] as $docId => $docValue) {
759
760
            if ($docValue['id']) {
761
762
                $explodeId = explode("_", $docValue['id']);
763
764
                $docData = $this->getDocumentData($explodeId[0], $docValue);
0 ignored issues
show
Bug introduced by
$explodeId[0] of type string is incompatible with the type integer expected by parameter $id of tx_dlf_basket::getDocumentData(). ( Ignorable by Annotation )

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

764
                $docData = $this->getDocumentData(/** @scrutinizer ignore-type */ $explodeId[0], $docValue);
Loading history...
765
766
                $pdfUrl .= $docData['urlParams'].$this->conf['pdfparamseparator'];
767
768
                $pages = (abs(intval($docValue['startpage']) - intval($docValue['endpage'])));
769
770
                if ($pages === 0) {
771
772
                    $numberOfPages = $numberOfPages + 1;
773
774
                } else {
775
776
                    $numberOfPages = $numberOfPages + $pages;
777
778
                }
779
780
            }
781
782
        }
783
784
        // Remove leading/tailing pdfparamseperator
785
        $pdfUrl = trim($pdfUrl, $this->conf['pdfparamseparator']);
786
787
        $body .= $pdfUrl;
788
789
        $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
790
791
        // send mail
792
        $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
793
794
        // Prepare and send the message
795
        $mail
796
            // subject
797
            ->setSubject($this->pi_getLL('mailSubject', '', TRUE))
798
799
            // Set the From address with an associative array
800
            ->setFrom($from)
801
802
            // Set the To addresses with an associative array
803
            ->setTo(array ($mailData['mail'] => $mailData['name']))
804
805
            ->setBody($body, 'text/html')
806
807
            ->send()
808
        ;
809
810
        // protocol
811
        $insertArray = array (
812
            'pid' => $this->conf['pages'],
813
            'file_name' => $pdfUrl,
814
            'count_pages' => $numberOfPages,
815
            'crdate' => time(),
816
        );
817
818
        if ($GLOBALS["TSFE"]->loginUser) {
819
820
            // internal user
821
            $insertArray['user_id'] = $GLOBALS["TSFE"]->fe_user->user['uid'];
822
823
            $insertArray['name'] = $GLOBALS["TSFE"]->fe_user->user['username'];
824
825
            $insertArray['label'] = 'Mail: '.$mailData['mail'];
826
827
        } else {
828
829
            // external user
830
            $insertArray['user_id'] = 0;
831
832
            $insertArray['name'] = 'n/a';
833
834
            $insertArray['label'] = 'Mail: '.$mailData['mail'];
835
836
        }
837
838
        // add action to protocol
839
        $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_dlf_actionlog', $insertArray);
840
841
    }
842
843
    /**
844
     * Sends document information to an external printer (url)
845
     */
846
    public function printDocument() {
847
848
        $pdfUrl = $this->conf['pdfprint'];
849
850
        foreach ($this->piVars['selected'] as $docId => $docValue) {
851
852
            if ($docValue['id']) {
853
854
                $docData = $this->getDocumentData($docValue['id'], $docValue);
855
856
                $pdfUrl .= $docData['urlParams'].$this->conf['pdfparamseparator'];
857
858
                $numberOfPages += $docData['pageNums'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $numberOfPages seems to be never defined.
Loading history...
859
860
            }
861
862
        }
863
864
        // get printer data
865
        $printerId = $this->piVars['print_action'];
866
867
        // get id from db and send selected doc downloadlink
868
        $resultPrinter = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
869
            '*',
870
            'tx_dlf_printer',
871
            'tx_dlf_printer.uid="'.intval($printerId).'"'.tx_dlf_helper::whereClause('tx_dlf_basket'),
872
            '',
873
            '',
874
            '1'
875
        );
876
877
        $printerData = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resultPrinter);
878
879
        // printer is selected
880
        if ($printerData) {
881
882
            $pdfUrl = $printerData['print'];
883
884
            $filename = 'Document_';
885
886
            $numberOfPages = 0;
887
888
            foreach ($this->piVars['selected'] as $docId => $docValue) {
889
890
                if ($docValue['id']) {
891
892
                    $filename .= $docValue['id'].'_';
893
894
                    $explodeId = explode("_", $docId);
895
896
                    $docData = $this->getDocumentData($explodeId[0], $docValue);
0 ignored issues
show
Bug introduced by
$explodeId[0] of type string is incompatible with the type integer expected by parameter $id of tx_dlf_basket::getDocumentData(). ( Ignorable by Annotation )

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

896
                    $docData = $this->getDocumentData(/** @scrutinizer ignore-type */ $explodeId[0], $docValue);
Loading history...
897
898
                    $pdfUrl .= $docData['urlParams'].$this->conf['pdfparamseparator'];
899
900
                    $numberOfPages += $docData['pageNums'];
901
902
                }
903
904
            }
905
906
            $pdfUrl = trim($pdfUrl, $this->conf['pdfparamseparator']);
907
908
        }
909
910
        // protocol
911
        $insertArray = array (
912
            'pid' => $this->conf['pages'],
913
            'file_name' => $pdfUrl,
914
            'count_pages' => $numberOfPages,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $numberOfPages does not seem to be defined for all execution paths leading up to this point.
Loading history...
915
            'crdate' => time(),
916
        );
917
918
        if ($GLOBALS["TSFE"]->loginUser) {
919
920
            // internal user
921
            $insertArray['user_id'] = $GLOBALS["TSFE"]->fe_user->user['uid'];
922
923
            $insertArray['name'] = $GLOBALS["TSFE"]->fe_user->user['username'];
924
925
            $insertArray['label'] = 'Print: '.$printerData['label'];
926
927
        } else {
928
929
            // external user
930
            $insertArray['user_id'] = 0;
931
932
            $insertArray['name'] = 'n/a';
933
934
            $insertArray['label'] = 'Print: '.$printerData['label'];
935
936
        }
937
938
        // add action to protocol
939
        $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_dlf_actionlog', $insertArray);
940
941
        header('Location: '.$pdfUrl);
942
943
        ob_end_flush();
944
945
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
946
947
    }
948
949
}