Passed
Push — hypernext ( affafd...433812 )
by Nico
13:20
created

MakeQrPdf::readAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * @author Nicolas CARPi <[email protected]>
4
 * @copyright 2012 Nicolas CARPi
5
 * @see https://www.elabftw.net Official website
6
 * @license AGPL-3.0
7
 * @package elabftw
8
 */
9
10
namespace Elabftw\Services;
11
12
use Elabftw\Elabftw\DisplayParams;
13
use Elabftw\Elabftw\Tools;
14
use Elabftw\Interfaces\FileMakerInterface;
15
use Elabftw\Interfaces\MpdfProviderInterface;
16
use Elabftw\Models\AbstractEntity;
17
use Elabftw\Models\Config;
18
use Elabftw\Traits\PdfTrait;
19
use Elabftw\Traits\TwigTrait;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * Make a PDF from several experiments or db items showing only minimal info with QR codes
24
 */
25
class MakeQrPdf extends AbstractMake implements FileMakerInterface
26
{
27
    use TwigTrait;
28
29
    use PdfTrait;
30
31
    // the input ids but in an array
32
    private array $idArr = array();
33
34
    /**
35
     * The idList is a space separated string of ids
36
     */
37
    public function __construct(MpdfProviderInterface $mpdfProvider, AbstractEntity $entity, string $idList)
38
    {
39
        parent::__construct($entity);
40
41
        $this->idArr = explode(' ', $idList);
42
        $this->mpdf = $mpdfProvider->getInstance();
43
    }
44
45
    /**
46
     * Get the name of the generated file
47
     */
48
    public function getFileName(): string
49
    {
50
        return 'qr-codes.elabftw.pdf';
51
    }
52
53
    public function getFileContent(): string
54
    {
55
        $renderArr = array(
56
            'css' => $this->getCss(),
57
            'entityArr' => $this->readAll(),
58
        );
59
        $html = $this->getTwig(Config::getConfig())->render('qr-pdf.html', $renderArr);
60
        $this->mpdf->WriteHTML($html);
61
        return $this->mpdf->Output('', 'S');
62
    }
63
64
    /**
65
     * Return the url of the item or experiment
66
     */
67
    private function getIdUrl(string $id): string
68
    {
69
        $Request = Request::createFromGlobals();
70
        $url = Tools::getUrl($Request) . '/' . $this->Entity->page . '.php';
71
72
        return $url . '?mode=view&id=' . $id;
73
    }
74
75
    /**
76
     * Get all the entity data from the id array
77
     */
78
    private function readAll(): array
79
    {
80
        $DisplayParams = new DisplayParams();
81
        $DisplayParams->limit = 9001;
82
        $this->Entity->idFilter = Tools::getIdFilterSql($this->idArr);
83
        $entityArr = $this->Entity->readShow($DisplayParams, true);
84
        foreach ($entityArr as &$entity) {
85
            $entity['url'] = $this->getIdUrl($entity['id']);
86
        }
87
        return $entityArr;
88
    }
89
}
90