Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzDownloads
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 1
 getHtml
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3namespace Olz\Service\Components\OlzDownloads;
4
5use Doctrine\Common\Collections\Criteria;
6use Doctrine\Common\Collections\Order;
7use Olz\Components\Common\OlzComponent;
8use Olz\Entity\Service\Download;
9
10/** @extends OlzComponent<array<string, mixed>> */
11class OlzDownloads extends OlzComponent {
12    public function getHtml(mixed $args): string {
13        $code_href = $this->envUtils()->getCodeHref();
14        $has_permission = $this->authUtils()->hasPermission('downloads');
15
16        $out = '';
17        $out .= "<h2>Downloads</h2>";
18
19        if ($has_permission) {
20            $out .= <<<ZZZZZZZZZZ
21                <button
22                    id='create-download-button'
23                    class='btn btn-secondary'
24                    onclick='return olz.initOlzEditDownloadModal()'
25                >
26                    <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' />
27                    Neuer Download
28                </button>
29                ZZZZZZZZZZ;
30        }
31
32        $download_repo = $this->entityManager()->getRepository(Download::class);
33        $criteria = Criteria::create()->where(Criteria::expr()->andX(
34            Criteria::expr()->eq('on_off', 1)
35        ));
36        $criteria = $criteria
37            ->orderBy(['position' => Order::Ascending])
38            ->setFirstResult(0)
39            ->setMaxResults(100)
40        ;
41        $downloads = $download_repo->matching($criteria);
42
43        $out .= "<ul class='no-style olz-downloads'>";
44        foreach ($downloads as $download) {
45            $id = $download->getId();
46            $name = $download->getName();
47            $on_off = $download->getOnOff();
48            $file_id = $download->getStoredFileUploadIds()[0] ?? null;
49
50            $user = $this->authUtils()->getCurrentUser();
51            $owner_user = $download->getOwnerUser();
52            $is_owner = $user && $owner_user && intval($owner_user->getId() ?? 0) === intval($user->getId());
53            $can_edit = $is_owner || $has_permission;
54            $edit_admin = '';
55            if ($can_edit) {
56                $json_id = json_encode($id);
57                $edit_admin = <<<ZZZZZZZZZZ
58                    <button
59                        id='edit-download-{$id}-button'
60                        class='btn btn-secondary-outline btn-sm edit-download-list-button'
61                        onclick='return olz.olzDownloadsEditDownload({$json_id})'
62                    >
63                        <img src='{$code_href}assets/icns/edit_16.svg' class='noborder' />
64                    </button>
65                    ZZZZZZZZZZ;
66            }
67
68            $class = '';
69            if ($on_off == 0) {
70                $class .= " error";
71            }
72            if ($can_edit) {
73                $class .= " edit-flex";
74            }
75
76            if ($name === '---') {
77                $out .= "{$edit_admin}<br />";
78            } else {
79                if ($file_id) {
80                    $file_href = $download->getFileHref($file_id);
81                    $out .= <<<ZZZZZZZZZZ
82                        <li class='{$class} pretty'>
83                            {$edit_admin}
84                            <a href='{$file_href}'>{$name}</a>
85                        </li>
86                        ZZZZZZZZZZ;
87                } else {
88                    $this->log()->error("No file ID for download (ID:{$id})");
89                    $out .= "<li class='{$class} pretty'>{$edit_admin}{$name}</li>";
90                }
91            }
92        }
93        $out .= "</ul>";
94
95        return $out;
96    }
97}