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\SmartPlaylist;
26
27use Ampache\Repository\Model\ModelFactoryInterface;
28use Ampache\Module\Application\ApplicationActionInterface;
29use Ampache\Module\Application\Exception\AccessDeniedException;
30use Ampache\Module\Authorization\AccessLevelEnum;
31use Ampache\Module\Authorization\GuiGatekeeperInterface;
32use Ampache\Module\System\Dba;
33use Ampache\Module\Util\UiInterface;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36
37final class CreatePlaylistAction implements ApplicationActionInterface
38{
39    public const REQUEST_KEY = 'create_playlist';
40
41    private UiInterface $ui;
42
43    private ModelFactoryInterface $modelFactory;
44
45    public function __construct(
46        UiInterface $ui,
47        ModelFactoryInterface $modelFactory
48    ) {
49        $this->ui           = $ui;
50        $this->modelFactory = $modelFactory;
51    }
52
53    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
54    {
55        if ($gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER) === false) {
56            throw new AccessDeniedException();
57        }
58
59        $this->ui->showHeader();
60
61        foreach ($_REQUEST as $key => $value) {
62            $prefix = substr($key, 0, 4);
63            $value  = trim($value);
64
65            if ($prefix == 'rule' && strlen($value)) {
66                $rules[$key] = Dba::escape($value);
67            }
68        }
69
70        switch ($_REQUEST['operator']) {
71            case 'or':
72                $operator = 'OR';
73                break;
74            default:
75                $operator = 'AND';
76                break;
77        } // end switch on operator
78
79        $playlist_name    = (string) scrub_in($_REQUEST['playlist_name']);
80
81        $playlist                 = $this->modelFactory->createSearch(null);
82        $playlist->logic_operator = $operator;
83        $playlist->name           = $playlist_name;
84        $playlist->save();
85
86        $this->ui->showQueryStats();
87        $this->ui->showFooter();
88
89        return null;
90    }
91}
92