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
23namespace Ampache\Repository\Model;
24
25use Ampache\Module\Authorization\Access;
26use Ampache\Module\Util\InterfaceImplementationChecker;
27use Ampache\Module\Util\ObjectTypeToClassNameMapper;
28use Ampache\Config\AmpConfig;
29use Ampache\Module\System\Core;
30use Ampache\Module\Util\Ui;
31
32/**
33 * playlist_object
34 * Abstracting out functionality needed by both normal and smart playlists
35 */
36abstract class playlist_object extends database_object implements library_item
37{
38    // Database variables
39    /**
40     * @var integer $id
41     */
42    public $id;
43    /**
44     * @var string $name
45     */
46    public $name;
47    /**
48     * @var integer $user
49     */
50    public $user;
51    /**
52     * @var string $user
53     */
54    public $username;
55    /**
56     * @var string $type
57     */
58    public $type;
59    /**
60     * @var string $link
61     */
62    public $link;
63    /**
64     * @var string $f_type
65     */
66    public $f_type;
67    /**
68     * @var string $f_name
69     */
70    public $f_name;
71
72    /**
73     * @return array
74     */
75    abstract public function get_items();
76
77    /**
78     * format
79     * This takes the current playlist object and gussies it up a little bit so it is presentable to the users
80     * @param boolean $details
81     */
82    public function format($details = true)
83    {
84        // format shared lists using the username
85        $this->f_name = (($this->user == Core::get_global('user')->id))
86            ? filter_var($this->name, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES)
87            : filter_var($this->name . " (" . $this->username . ")", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
88        $this->f_type = ($this->type == 'private') ? Ui::get_icon('lock', T_('Private')) : '';
89    } // format
90
91    /**
92     * has_access
93     * This function returns true or false if the current user
94     * has access to this playlist
95     * @param integer $user_id
96     * @return boolean
97     */
98    public function has_access($user_id = null)
99    {
100        if (!Access::check('interface', 25)) {
101            return false;
102        }
103        if (Access::check('interface', 100)) {
104            return true;
105        }
106        // allow the owner
107        if (($this->user == Core::get_global('user')->id) || ($this->user == $user_id)) {
108            return true;
109        }
110
111        return false;
112    } // has_access
113
114    /**
115     * @param string $filter_type
116     * @return array
117     */
118    public function get_medias($filter_type = null)
119    {
120        $medias = $this->get_items();
121        if ($filter_type) {
122            $nmedias = array();
123            foreach ($medias as $media) {
124                if ($media['object_type'] == $filter_type) {
125                    $nmedias[] = $media;
126                }
127            }
128            $medias = $nmedias;
129        }
130
131        return $medias;
132    }
133
134    /**
135     * @return array|mixed
136     */
137    public function get_keywords()
138    {
139        return array();
140    }
141
142    /**
143     * @return string
144     */
145    public function get_fullname()
146    {
147        if (!isset($this->f_name)) {
148            $this->f_name = ($this->user == Core::get_global('user')->id)
149                ? filter_var($this->name, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES)
150                : filter_var($this->name . " (" . $this->username . ")", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
151        }
152
153        return $this->f_name;
154    }
155
156    /**
157     * @return null
158     */
159    public function get_parent()
160    {
161        return null;
162    }
163
164    /**
165     * @return mixed
166     */
167    public function get_childrens()
168    {
169        $childrens = array();
170        $items     = $this->get_items();
171        foreach ($items as $item) {
172            if (!in_array($item['object_type'], $childrens)) {
173                $childrens[$item['object_type']] = array();
174            }
175            $childrens[$item['object_type']][] = $item['object_id'];
176        }
177
178        return $this->get_items();
179    }
180
181    /**
182     * @param string $name
183     * @return array
184     */
185    public function search_childrens($name)
186    {
187        debug_event('playlist_object.abstract', 'search_childrens ' . $name, 5);
188
189        return array();
190    }
191
192    /**
193     * @return integer
194     */
195    public function get_user_owner()
196    {
197        return $this->user;
198    }
199
200    /**
201     * @return string
202     */
203    public function get_default_art_kind()
204    {
205        return 'default';
206    }
207
208    /**
209     * @return mixed|null
210     */
211    public function get_description()
212    {
213        return null;
214    }
215
216    /**
217     * display_art
218     * @param integer $thumb
219     * @param boolean $force
220     */
221    public function display_art($thumb = 2, $force = false)
222    {
223        if (AmpConfig::get('playlist_art') || $force) {
224            $medias     = $this->get_medias();
225            shuffle($medias);
226            foreach ($medias as $media) {
227                if (InterfaceImplementationChecker::is_library_item($media['object_type'])) {
228                    if (!Art::has_db($media['object_id'], $media['object_type'])) {
229                        $class_name = ObjectTypeToClassNameMapper::map($media['object_type']);
230                        $libitem    = new $class_name($media['object_id']);
231                        $parent     = $libitem->get_parent();
232                        if ($parent !== null) {
233                            $media = $parent;
234                        } elseif (!$force) {
235                            $media = null;
236                        }
237                    }
238
239                    if ($media !== null) {
240                        Art::display($media['object_type'], $media['object_id'], $this->get_fullname(), $thumb, $this->link);
241
242                        return;
243                    }
244                }
245            }
246        }
247    }
248
249    /**
250     * get_catalogs
251     *
252     * Get all catalog ids related to this item.
253     * @return integer[]
254     */
255    public function get_catalogs()
256    {
257        return array();
258    }
259} // end playlist_object.class
260