1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 *  LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25namespace Ampache\Module\Application\Label;
26
27use Ampache\Config\ConfigContainerInterface;
28use Ampache\Config\ConfigurationKeyEnum;
29use Ampache\Repository\Model\Label;
30use Ampache\Repository\Model\ModelFactoryInterface;
31use Ampache\Module\Application\ApplicationActionInterface;
32use Ampache\Module\Authorization\AccessLevelEnum;
33use Ampache\Module\Authorization\Check\PrivilegeCheckerInterface;
34use Ampache\Module\Authorization\GuiGatekeeperInterface;
35use Ampache\Module\Util\UiInterface;
36use Ampache\Repository\LabelRepositoryInterface;
37use Psr\Http\Message\ResponseInterface;
38use Psr\Http\Message\ServerRequestInterface;
39
40final class ShowAction implements ApplicationActionInterface
41{
42    public const REQUEST_KEY = 'show';
43
44    private ConfigContainerInterface $configContainer;
45
46    private UiInterface $ui;
47
48    private PrivilegeCheckerInterface $privilegeChecker;
49
50    private LabelRepositoryInterface $labelRepository;
51
52    private ModelFactoryInterface $modelFactory;
53
54    public function __construct(
55        ConfigContainerInterface $configContainer,
56        UiInterface $ui,
57        PrivilegeCheckerInterface $privilegeChecker,
58        LabelRepositoryInterface $labelRepository,
59        ModelFactoryInterface $modelFactory
60    ) {
61        $this->configContainer  = $configContainer;
62        $this->ui               = $ui;
63        $this->privilegeChecker = $privilegeChecker;
64        $this->labelRepository  = $labelRepository;
65        $this->modelFactory     = $modelFactory;
66    }
67
68    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
69    {
70        $this->ui->showHeader();
71
72        $label_id = (int) $request->getQueryParams()['label'] ?? 0;
73        if (!$label_id) {
74            $name = $_REQUEST['name'] ?? null;
75            if ($name !== null) {
76                $label_id = $this->labelRepository->lookup((string) $name);
77            }
78        }
79        if ($label_id > 0) {
80            $label = $this->modelFactory->createLabel($label_id);
81            $label->format();
82
83            $this->ui->show(
84                'show_label.inc.php',
85                [
86                    'object_ids' => $label->get_artists(),
87                    'object_type' => 'artist',
88                    'label' => $label,
89                    'isLabelEditable' => $this->isEditable(
90                        $gatekeeper->getUserId(),
91                        $label
92                    )
93                ]
94            );
95
96            $this->ui->showFooter();
97
98            return null;
99        }
100        if (
101            $gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_CONTENT_MANAGER) ||
102            $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::UPLOAD_ALLOW_EDIT) === true
103        ) {
104            $this->ui->show(
105                'show_add_label.inc.php',
106                []
107            );
108        } else {
109            echo T_('The Label cannot be found');
110        }
111
112        $this->ui->showQueryStats();
113        $this->ui->showFooter();
114
115        return null;
116    }
117
118    private function isEditable(
119        int $userId,
120        Label $label
121    ): bool {
122        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::UPLOAD_ALLOW_EDIT) === true) {
123            if ($label->user !== null && $userId == $label->user) {
124                return true;
125            }
126        }
127
128        return $this->privilegeChecker->check(
129            AccessLevelEnum::TYPE_INTERFACE,
130            AccessLevelEnum::LEVEL_CONTENT_MANAGER
131        );
132    }
133}
134