~cjsmo/ampache/ampache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright 2001 - 2015 Ampache.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

/**
 * TempPlaylist Class
 *
 * This class handles the temporary playlists in Ampache. It handles the
 * tmp_playlist and tmp_playlist_data tables, and sneaks out at night to
 * visit user_vote from time to time.
 *
 */
class Tmp_Playlist extends database_object
{
    /* Variables from the Datbase */
    public $id;
    public $session;
    public $type;
    public $object_type;
    public $base_playlist;

    /* Generated Elements */
    public $items = array();

    /**
     * Constructor
     * This takes a playlist_id as an optional argument and gathers the
     * information.  If no playlist_id is passed or the requested one isn't
     * found, return false.
     */
    public function __construct($playlist_id='')
    {
        if (!$playlist_id) {
            return false;
        }

        $this->id     = intval($playlist_id);
        $info         = $this->_get_info();

        foreach ($info as $key=>$value) {
            $this->$key = $value;
        }

        return true;
    } // __construct

    /**
     * _get_info
     * This is an internal (private) function that gathers the information
     * for this object from the playlist_id that was passed in.
     */
    private function _get_info()
    {
        $sql        = "SELECT * FROM `tmp_playlist` WHERE `id`='" . Dba::escape($this->id) . "'";
        $db_results = Dba::read($sql);

        $results = Dba::fetch_assoc($db_results);

        return $results;
    } // _get_info

    /**
     * get_from_session
     * This returns a playlist object based on the session that is passed to
     * us.  This is used by the load_playlist on user for the most part.
     */
    public static function get_from_session($session_id)
    {
        $session_id = Dba::escape($session_id);

        $sql        = "SELECT `id` FROM `tmp_playlist` WHERE `session`='$session_id'";
        $db_results = Dba::read($sql);

        $results = Dba::fetch_row($db_results);

        if (!$results['0']) {
            $results['0'] = Tmp_Playlist::create(array(
                'session_id'  => $session_id,
                'type'        => 'user',
                'object_type' => 'song'
            ));
        }

        $playlist = new Tmp_Playlist($results['0']);

        return $playlist;
    } // get_from_session

    /**
     * get_from_userid
     * This returns a tmp playlist object based on a userid passed
     * this is used for the user profiles page
     */
    public static function get_from_userid($user_id)
    {
        // This is a little stupid, but because we don't have the
        // user_id in the session or in the tmp_playlist table we have
        // to do it this way.
        $client   = new User($user_id);
        $username = Dba::escape($client->username);

        $sql = "SELECT `tmp_playlist`.`id` FROM `tmp_playlist` " .
            "LEFT JOIN `session` ON " .
            "`session`.`id`=`tmp_playlist`.`session` " .
            "WHERE `session`.`username`='$username' " .
            "ORDER BY `session`.`expire` DESC";
        $db_results = Dba::read($sql);

        $data = Dba::fetch_assoc($db_results);

        return $data['id'];
    } // get_from_userid

    /**
     * get_items
     * Returns an array of all object_ids currently in this Tmp_Playlist.
     */
    public function get_items()
    {
        /* Select all objects from this playlist */
        $sql = "SELECT `object_type`, `id`, `object_id` " .
            "FROM `tmp_playlist_data` " .
            "WHERE `tmp_playlist` = ? ORDER BY `id` ASC";
        $db_results = Dba::read($sql, array($this->id));

        /* Define the array */
        $items = array();

        $i = 1;
        while ($results = Dba::fetch_assoc($db_results)) {
            $items[]     = array(
                'object_type' => $results['object_type'],
                'object_id' => $results['object_id'],
                'track_id' => $results['id'],
                'track' => $i++,
            );
        }

        return $items;
    } // get_items

    /**
     * get_next_object
     * This returns the next object in the tmp_playlist.
     */
    public function get_next_object()
    {
        $id = Dba::escape($this->id);

        $sql = "SELECT `object_id` FROM `tmp_playlist_data` " .
            "WHERE `tmp_playlist`='$id' ORDER BY `id` LIMIT 1";
        $db_results = Dba::read($sql);

        $results = Dba::fetch_assoc($db_results);

        return $results['object_id'];
    } // get_next_object

    /**
     * count_items
     * This returns a count of the total number of tracks that are in this
     * tmp playlist
     */
    public function count_items()
    {
        $id = Dba::escape($this->id);

        $sql = "SELECT COUNT(`id`) FROM `tmp_playlist_data` WHERE " .
            "`tmp_playlist`='$id'";
        $db_results = Dba::read($sql);

        $results = Dba::fetch_row($db_results);

        return $results['0'];
    } // count_items

    /**
      * clear
     * This clears all the objects out of a single playlist
     */
    public function clear()
    {
        $sql = "DELETE FROM `tmp_playlist_data` WHERE `tmp_playlist` = ?";
        Dba::write($sql, array($this->id));

        return true;
    } // clear

    /**
     * create
     * This function initializes a new Tmp_Playlist. It is associated with
     * the current session rather than a user, as you could have the same
     * user logged in from multiple locations.
     */
    public static function create($data)
    {
        $sql = "INSERT INTO `tmp_playlist` " .
            "(`session`,`type`,`object_type`) " .
            " VALUES (?, ?, ?)";
        Dba::write($sql, array($data['session_id'], $data['type'], $data['object_type']));

        $id = Dba::insert_id();

        /* Clean any other playlists associated with this session */
        self::session_clean($data['session_id'], $id);

        return $id;
    } // create

    /**
     * update_playlist
     * This updates the base_playlist on this tmp_playlist
     */
    public function update_playlist($playlist_id)
    {
        $sql = "UPDATE `tmp_playlist` SET " .
            "`base_playlist`= ? WHERE `id`= ?";
        Dba::write($sql, array($playlist_id, $this->id));

        return true;
    } // update_playlist

    /**
     * session_clean
     * This deletes any other tmp_playlists associated with this
     * session
     */
    public static function session_clean($sessid, $id)
    {
        $sql = "DELETE FROM `tmp_playlist` WHERE `session`= ? AND `id` != ?";
        Dba::write($sql, array($sessid, $id));

        /* Remove associated tracks */
        self::prune_tracks();

        return true;
    } // session_clean

    /**
     * gc
     * This cleans up old data
     */
    public static function gc()
    {
        self::prune_playlists();
        self::prune_tracks();
        //Dba::write("DELETE FROM `tmp_playlist_data` USING `tmp_playlist_data` LEFT JOIN `song` ON `tmp_playlist_data`.`object_id` = `song`.`id` WHERE `song`.`id` IS NULL");
    }

    /**
     * prune_playlists
     * This deletes any playlists that don't have an associated session
     */
    public static function prune_playlists()
    {
        /* Just delete if no matching session row */
        $sql = "DELETE FROM `tmp_playlist` USING `tmp_playlist` " .
            "LEFT JOIN `session` " .
            "ON `session`.`id`=`tmp_playlist`.`session` " .
            "WHERE `session`.`id` IS NULL " .
            "AND `tmp_playlist`.`type` != 'vote'";
        Dba::write($sql);

        return true;
    } // prune_playlists

    /**
     * prune_tracks
     * This prunes tracks that don't have playlists or don't have votes
     */
    public static function prune_tracks()
    {
        // This prune is always run and clears data for playlists that
        // don't exist anymore
        $sql = "DELETE FROM `tmp_playlist_data` USING " .
            "`tmp_playlist_data` LEFT JOIN `tmp_playlist` ON " .
            "`tmp_playlist_data`.`tmp_playlist`=`tmp_playlist`.`id` " .
            "WHERE `tmp_playlist`.`id` IS NULL";
        Dba::write($sql);
    } // prune_tracks

    /**
     * add_object
     * This adds the object of $this->object_type to this tmp playlist
     * it takes an optional type, default is song
     */
    public function add_object($object_id,$object_type)
    {
        $sql = "INSERT INTO `tmp_playlist_data` " .
            "(`object_id`,`tmp_playlist`,`object_type`) " .
            " VALUES (?, ?, ?)";
        Dba::write($sql, array($object_id, $this->id, $object_type));

        return true;
    } // add_object

    public function add_medias($medias)
    {
        foreach ($medias as $media) {
            $this->add_object($media['object_id'], $media['object_type']);
        }
    }

    /**
     * vote_active
     * This checks to see if this playlist is a voting playlist
     * and if it is active
     */
    public function vote_active()
    {
        /* Going to do a little more here later */
        if ($this->type == 'vote') {
            return true;
        }

        return false;
    } // vote_active

    /**
     * delete_track
     * This deletes a track from the tmpplaylist
     */
    public function delete_track($id)
    {
        /* delete the track its self */
        $sql = "DELETE FROM `tmp_playlist_data` WHERE `id` = ?";
        Dba::write($sql, array($id));

        return true;
    } // delete_track
} // class Tmp_Playlist