static function sortAlphabetically(X_Page_Item_PItem $item1, X_Page_Item_PItem $item2)
 {
     // prevent warning for array modification
     $label1 = $item1->getLabel();
     $label2 = $item2->getLabel();
     return strcasecmp($label1, $label2);
 }
 /**
  * Convert an array of items in DIDL xml format
  * @param array $prmItems
  */
 static function createMetaDIDL(X_Page_Item_PItem $item, $parentUrl = '0', &$num, $defaultController = 'browse', $defaultAction = 'share', $defaultProvider = 'onlinelibrary')
 {
     # TODO: put object.container in container tags where they belong. But as long as the WDTVL doesn't mind... ;)
     # $prmItems is an array of arrays
     $xmlDoc = new DOMDocument('1.0', 'utf-8');
     $xmlDoc->formatOutput = true;
     # Create root element and add namespaces:
     $ndDIDL = $xmlDoc->createElementNS('urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/', 'DIDL-Lite');
     $ndDIDL->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
     $ndDIDL->setAttribute('xmlns:upnp', 'urn:schemas-upnp-org:metadata-1-0/upnp/');
     $ndDIDL->setAttribute('xmlns:dnla', 'urn:schemas-dlna-org:metadata-1-0/');
     $xmlDoc->appendChild($ndDIDL);
     //$ndRes = $xmlDoc->createElement('res');
     // container, request, element
     $ndItem = $xmlDoc->createElement('container');
     self::appendTag('upnp:class', 'object.container', $ndItem, $xmlDoc);
     $ndItem->setAttribute('childCount', $num);
     //{{{ MANDATORY PARAMS
     if ($item->isUrl()) {
         $ndItem->setAttribute('id', "{$parentUrl}/" . X_Env::_($item->getKey()));
     } else {
         $controller = $item->getLinkController() ? $item->getLinkController() : $defaultController;
         $action = $item->getLinkAction() ? $item->getLinkAction() : $defaultAction;
         $provider = $item->getLinkParam("p") ? $item->getLinkParam("p") : $defaultProvider;
         $location = $item->getLinkParam('l') ? $item->getLinkParam('l') : '';
         $params = $item->getLink();
         foreach (array('controller', 'action', 'p', 'l') as $key) {
             if (isset($params[$key])) {
                 unset($params[$key]);
             }
         }
         $customs = '';
         if (count($params)) {
             $customs .= '#';
             foreach ($params as $key => $value) {
                 $customs .= "/{$key}/{$value}";
             }
         }
         $ndItem->setAttribute('id', "{$controller}/{$action}/{$provider}/{$location}{$customs}");
     }
     $ndItem->setAttribute('parentID', $parentUrl);
     $ndItem->setAttribute('restricted', true);
     $ndItem->setAttribute('searchable', '0');
     //}}}
     self::appendTag('dc:title', $item->getLabel(), $ndItem, $xmlDoc);
     self::appendTag('dc:description', $item->getDescription(), $ndItem, $xmlDoc);
     if ($item->getThumbnail()) {
         self::appendTag('upnp:album_art', $item->getThumbnail(), $ndItem, $xmlDoc);
     }
     $ndDIDL->appendChild($ndItem);
     return $xmlDoc;
 }
 private function _fillPlaylist(X_Page_ItemList_PItem $items, $decoded, $lStack)
 {
     X_Debug::i("Decoded: " . print_r($decoded, true));
     // The format of $decoded (only notable parts):
     // {
     //		ok: true|false
     //		msg: $msg ; Setted only if ok = false
     //		folderId: $thisFolder
     //		folders: [
     //				{
     //					id: $folderID
     //					name: $folderName
     //				}, ...
     //			]
     //		objects: [
     //				{
     //					id: $objectID
     //					name: $objectName
     //					link: http://...?...&video=urlencoded(URL)&...
     //				},
     //				{
     //					id: $objectID
     //					name: $objectName
     //					link: http://...?...&host=megavideo&video=urlencoded(MEGAVIDEOURL)&...
     //				},
     //			]
     //		...
     // }
     foreach ($decoded->folders as $folder) {
         $_stack = $lStack;
         $_stack[] = $folder->id;
         $item = new X_Page_Item_PItem("opfitalia-folder-{$folder->id}", urldecode($folder->name));
         $item->setType(X_Page_Item_PItem::TYPE_CONTAINER)->setGenerator(__CLASS__)->setCustom(__CLASS__ . ':location', implode(':', $_stack))->setLink(array('l' => X_Env::encode(implode(':', $_stack))), 'default', false);
         $items->append($item);
     }
     foreach ($decoded->objects as $object) {
         $item = new X_Page_Item_PItem("opfitalia-video-{$object->id}", urldecode($object->name));
         $item->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setGenerator(__CLASS__);
         // time to decode the link arg
         // file: index.php?page=show_streaming&video=http:\/\/www.dbforever.net\/strm\/onepiece\/one_piece_459.mp4&width=704&height=430
         // megavideo: index.php?page=show_streaming&host=megavideo&video=http:\/\/www.megavideo.com\/v\/GIV2R76V038221ac298ceb332a9cad75288c318b&width=640&height=480
         $link = explode('&', $object->link);
         $type = 'file';
         $href = '';
         foreach ($link as $sublink) {
             list($arg, $value) = @explode('=', $sublink, 2);
             if ($arg == 'host') {
                 if ($value == 'megavideo') {
                     $type = 'megavideo';
                 }
             }
             if ($arg == 'video') {
                 $href = str_replace('\\/', '/', urldecode($value));
             }
         }
         if ($type == 'megavideo') {
             // i gave to split the /v/ param only
             $splitted = explode('/v/', $href, 2);
             if (count($splitted) == 2) {
                 $href = $splitted[1];
             } else {
                 // try to decode it
                 // as ?v=
                 preg_match('#\\?v=(.+?)$#', $href, $id);
                 if (count($id) >= 2) {
                     $href = $id[1];
                 } else {
                     // if even this fail
                     // i have to skip thi entry
                     continue;
                 }
             }
             $item->setLabel($item->getLabel() . " [Megavideo]");
         }
         $item->setCustom(__CLASS__ . ':location', implode(':', $lStack) . "/{$type}/{$href}")->setLink(array('action' => 'mode', 'l' => X_Env::encode(implode(':', $lStack) . "/{$type}/{$href}")), 'default', false);
         $items->append($item);
     }
 }