1<?php
2
3/*
4 * vim:set softtabstop=4 shiftwidth=4 expandtab:
5 *
6 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
7 * Copyright 2001 - 2020 Ampache.org
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=0);
25
26namespace Ampache\Module\Api\Method;
27
28use Ampache\Repository\Model\Preference;
29use Ampache\Repository\Model\User;
30use Ampache\Module\Api\Api;
31use Ampache\Module\Api\Xml_Data;
32use Ampache\Module\System\Session;
33
34/**
35 * Class PreferenceEditMethod
36 * @package Lib\ApiMethods
37 */
38final class PreferenceEditMethod
39{
40    private const ACTION = 'preference_edit';
41
42    /**
43     * preference_edit
44     * MINIMUM_API_VERSION=5.0.0
45     *
46     * Edit a preference value and apply to all users if allowed
47     *
48     * @param array $input
49     * filter = (string) Preference name e.g ('notify_email', 'ajax_load')
50     * value  = (string|integer) Preference value
51     * all    = (boolean) apply to all users //optional
52     * @return boolean
53     */
54    public static function preference_edit(array $input)
55    {
56        if (!Api::check_parameter($input, array('filter', 'value'), self::ACTION)) {
57            return false;
58        }
59        $user = User::get_from_username(Session::username($input['auth']));
60        $all  = (int) $input['all'] == 1;
61        // don't apply to all when you aren't an admin
62        if ($all && !Api::check_access('interface', 100, $user->id, self::ACTION, $input['api_format'])) {
63            return false;
64        }
65        // fix preferences that are missing for user
66        User::fix_preferences($user->id);
67
68        $pref_name  = (string) $input['filter'];
69        $preference = Preference::get($pref_name, $user->id);
70        if (empty($preference)) {
71            /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
72            Api::error(sprintf(T_('Not Found: %s'), $pref_name), '4704', self::ACTION, 'filter', $input['api_format']);
73
74            return false;
75        }
76        $value = $input['value'];
77        if (!Preference::update($pref_name, $value, $all)) {
78            Api::error(T_('Bad Request'), '4710', self::ACTION, 'system', $input['api_format']);
79
80            return false;
81        }
82        $preference   = Preference::get($pref_name, $user->id);
83        $output_array = array('preference' => $preference);
84        switch ($input['api_format']) {
85            case 'json':
86                echo json_encode($output_array, JSON_PRETTY_PRINT);
87                break;
88            default:
89                echo Xml_Data::object_array($output_array['preference'], 'preference');
90        }
91        Session::extend($input['auth']);
92
93        return true;
94    }
95}
96