Example #1
0
 public static function playlists($params)
 {
     $r = Plex_XML_Data::createContainer();
     $n = count($params);
     $createMode = $_SERVER['REQUEST_METHOD'] == 'POST';
     $editMode = $_SERVER['REQUEST_METHOD'] == 'PUT';
     $delMode = $_SERVER['REQUEST_METHOD'] == 'DELETE';
     if ($createMode || $editMode || $delMode) {
         self::check_access(50);
     }
     if ($n <= 1) {
         $plid = 0;
         if ($n == 0 && $createMode) {
             // Create a new playlist
             //$type = $_GET['type'];
             $title = $_GET['title'];
             //$smart = $_GET['smart'];
             //$summary = $_GET['summary'];
             $uri = $_GET['uri'];
             $plid = Playlist::create($title, 'public');
             $playlist = new Playlist($plid);
             $key = Plex_XML_Data::getKeyFromFullUri($uri);
             $id = Plex_XML_Data::getKeyFromMetadataUri($key);
             if ($id) {
                 $item = Plex_XML_Data::createLibraryItem($id);
                 $medias = $item->get_medias();
                 $playlist->add_medias($medias);
             }
             $plid = Plex_XML_Data::getPlaylistId($plid);
         } else {
             if ($n == 1 && $params[0] != "all") {
                 $plid = $params[0];
             }
         }
         if ($plid) {
             if (Plex_XML_Data::isPlaylist($plid)) {
                 $playlist = new Playlist(Plex_XML_Data::getAmpacheId($plid));
                 if ($playlist->id) {
                     if ($delMode) {
                         // Delete playlist
                         $playlist->delete();
                     } else {
                         // Display playlist information
                         Plex_XML_Data::addPlaylist($r, $playlist);
                     }
                 }
             }
         } else {
             // List all playlists
             Plex_XML_Data::setPlaylists($r);
         }
     } elseif ($n >= 2) {
         $plid = $params[0];
         if (Plex_XML_Data::isPlaylist($plid) && $params[1] == "items") {
             $playlist = new Playlist(Plex_XML_Data::getAmpacheId($plid));
             if ($playlist->id) {
                 if ($n == 2) {
                     if ($editMode) {
                         // Add a new item to playlist
                         $uri = $_GET['uri'];
                         $key = Plex_XML_Data::getKeyFromFullUri($uri);
                         $id = Plex_XML_Data::getKeyFromMetadataUri($key);
                         if ($id) {
                             $item = Plex_XML_Data::createLibraryItem($id);
                             $medias = $item->get_medias();
                             $playlist->add_medias($medias);
                             Plex_XML_Data::addPlaylist($r, $playlist);
                         }
                     } else {
                         Plex_XML_Data::setPlaylistItems($r, $playlist);
                     }
                 } elseif ($n == 3) {
                     $index = intval($params[2]);
                     if ($delMode) {
                         $playlist->delete_track_number($index);
                         $playlist->regenerate_track_numbers();
                         exit;
                     }
                 }
             }
         }
     }
     Plex_XML_Data::setContainerSize($r);
     self::apiOutputXml($r->asXML());
 }