/**
  * Singleton
  * @return Application_Model_VideosMapper
  */
 public static function i()
 {
     if (self::$_instance == null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 public function deleteAction()
 {
     $request = $this->getRequest();
     $id = $request->getParam('id', null);
     $type = $request->getParam('type', 'video');
     if (!is_null($id)) {
         if ($type == 'video') {
             $video = new Application_Model_Video();
             Application_Model_VideosMapper::i()->find($id, $video);
             if ($video->getId()) {
                 $category = $video->getCategory();
                 Application_Model_VideosMapper::i()->delete($video);
                 $this->_helper->redirector('category', 'onlinelibrary', 'default', array('id' => X_Env::encode($category)));
             }
         } elseif ($type == 'category') {
             Application_Model_VideosMapper::i()->deleteCategory(X_Env::decode($id));
         }
     }
     $this->_helper->redirector('index', 'onlinelibrary');
 }
 public function addAction()
 {
     $csrf = new Zend_Form_Element_Hash('csrf', array('salt' => __CLASS__));
     $validCheck = $csrf->isValid($this->getRequest()->getParam('csrf', false));
     $csrf->initCsrfToken();
     $hash = $csrf->getHash();
     $return = array('success' => true, 'api' => array('resolver' => $this->_helper->url->url(array('controller' => 'bookmarklets', 'action' => 'resolver', 'csrf' => $hash)), 'adder' => $this->_helper->url->url(array('controller' => 'bookmarklets', 'action' => 'add', 'csrf' => $hash)), 'bookmark' => $this->_helper->url->url(array('controller' => 'bookmarklets', 'action' => 'bookmark', 'csrf' => $hash))), 'links' => array());
     if ($validCheck) {
         $links = $this->getRequest()->getParam("links", array());
         $category = $this->getRequest()->getParam("category", false);
         $added = 0;
         $ignored = 0;
         $total = count($links);
         if ($category) {
             $hosterHelper = X_VlcShares_Plugins::helpers()->hoster();
             foreach ($links as $link) {
                 $href = isset($link['href']) ? $link['href'] : false;
                 $title = isset($link['title']) ? $link['title'] : false;
                 $description = isset($link['description']) ? $link['description'] : false;
                 $thumbnail = isset($link['thumbnail']) ? $link['thumbnail'] : false;
                 if (!$href || !$title) {
                     $ignored++;
                     continue;
                 }
                 try {
                     $hoster = $hosterHelper->findHoster($href);
                     $id = $hoster->getResourceId($href);
                     $video = new Application_Model_Video();
                     $video->setHoster($hoster->getId())->setIdVideo($id)->setTitle($title)->setCategory($category);
                     if ($thumbnail) {
                         $video->setThumbnail($thumbnail);
                     }
                     if ($description) {
                         $video->setDescription($description);
                     }
                     try {
                         Application_Model_VideosMapper::i()->save($video);
                         $added++;
                     } catch (Exception $e) {
                         $ignored++;
                     }
                 } catch (Exception $e) {
                     X_Debug::w("No hoster found: {{$e->getMessage()}}");
                     $ignored++;
                 }
             }
             $return['links'] = array('total' => $total, 'ignored' => $ignored, 'added' => $added);
         } else {
             X_Debug::e("No category selected");
             $return['success'] = false;
         }
     } else {
         X_Debug::e("Invalid CSRF");
         $return['success'] = false;
     }
     $this->_helper->json($return, true, false);
 }
 /**
  * Restore backupped videos 
  * This is not a trigger of plugin API. It's called by Backupper plugin
  */
 function restoreItems($items)
 {
     //return parent::restoreItems($items);
     $models = Application_Model_VideosMapper::i()->fetchAll();
     // cleaning up all shares
     foreach ($models as $model) {
         Application_Model_VideosMapper::i()->delete($model);
     }
     foreach (@$items['videos'] as $modelInfo) {
         $model = new Application_Model_Video();
         $model->setIdVideo(@$modelInfo['idVideo'])->setDescription(@$modelInfo['description'])->setCategory(@$modelInfo['category'])->setTitle(@$modelInfo['title'])->setHoster(@$modelInfo['hoster'])->setThumbnail(@$modelInfo['thumbnail']);
         // i don't set id, or db adapter will try to update old data that i cleaned
         Application_Model_VideosMapper::i()->save($model);
     }
     return X_Env::_('p_onlinelibrary_backupper_restoreditems', count($items['videos']));
 }
 /**
  * Add megavideo id to jdownloader download queue
  * @param string $provider
  * @param string $location
  * @param string $pid
  * @param Zend_Controller_Action $controller
  * @return X_Page_ItemList_PItem
  */
 public function getSelectionItems($provider, $location, $pid, Zend_Controller_Action $controller)
 {
     // we want to expose items only if pid is this plugin
     if ($this->getId() != $pid) {
         return;
     }
     X_Debug::i('Plugin triggered');
     $action = $controller->getRequest()->getParam("{$this->getId()}:action", '');
     /* @var $jdownloader X_VlcShares_Plugins_Helper_JDownloader */
     $jdownloader = $this->helpers('jdownloader');
     // IF action = 'start, stop or pause'
     // execute the action and return confirm, then exit
     if ($action != '') {
         try {
             if ($action == "start") {
                 $jdownloader->sendRawCommand(X_VlcShares_Plugins_Helper_JDownloader::CMD_ACTION_START);
                 $link = new X_Page_Item_PItem($this->getId() . '-started', X_Env::_('p_jdownloader_selection_started'));
                 $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setLink(array('action' => 'share', 'pid' => null, "{$this->getId()}:action" => null), 'default', false);
                 return new X_Page_ItemList_PItem(array($link));
             }
             if ($action == "pause") {
                 $jdownloader->sendRawCommand(X_VlcShares_Plugins_Helper_JDownloader::CMD_ACTION_PAUSE);
                 $link = new X_Page_Item_PItem($this->getId() . '-started', X_Env::_('p_jdownloader_selection_paused'));
                 $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setLink(array('action' => 'share', 'pid' => null, "{$this->getId()}:action" => null), 'default', false);
                 return new X_Page_ItemList_PItem(array($link));
             }
             if ($action == "stop") {
                 $jdownloader->sendRawCommand(X_VlcShares_Plugins_Helper_JDownloader::CMD_ACTION_STOP);
                 $link = new X_Page_Item_PItem($this->getId() . '-started', X_Env::_('p_jdownloader_selection_stopped'));
                 $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setLink(array('action' => 'share', 'pid' => null, "{$this->getId()}:action" => null), 'default', false);
                 return new X_Page_ItemList_PItem(array($link));
             }
         } catch (Exception $e) {
             // connection problems or timeout
             $link = new X_Page_Item_PItem($this->getId() . '-error', X_Env::_('p_jdownloader_selection_error_network'));
             $link->setDescription($e->getMessage());
             $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setLink(array('action' => 'share', 'pid' => null, "{$this->getId()}:action" => null), 'default', false);
             return new X_Page_ItemList_PItem(array($link));
         }
     } else {
         // else only confirm the addict
         /* @var $megavideo X_VlcShares_Plugins_Helper_Megavideo */
         $megavideo = $this->helpers('megavideo');
         try {
             $megavideo->getServer();
         } catch (Exception $e) {
             // not loaded location yet
             X_Debug::i("Force location loading");
             $providerObj = X_VlcShares_Plugins::broker()->getPlugins($provider);
             if ($providerObj instanceof X_VlcShares_Plugins_ResolverInterface) {
                 $providerObj->resolveLocation($location);
             }
         }
         $url = null;
         try {
             $url = "http://www.megavideo.com/?v=" . $megavideo->getId();
         } catch (Exception $e) {
             // TODO please, rewrite this piece of code
             // it's really a shame
             // let's try to get the url from the hoster
             try {
                 switch ($provider) {
                     // special cases
                     case 'onlinelibrary':
                         // hoster/id is stored inside db
                         $exploded = explode('/', $location);
                         $exploded = array_pop($exploded);
                         $video = new Application_Model_Video();
                         Application_Model_VideosMapper::i()->find($exploded, $video);
                         $hoster = $video->getHoster();
                         $videoId = $video->getIdVideo();
                         break;
                     default:
                         // we try to get information by location decoding
                         // many plugins use a /-divided location with last param in the format of HOSTER:VIDEOID
                         $exploded = explode('/', $location);
                         $exploded = array_pop($exploded);
                         $exploded = explode(':', $exploded, 2);
                         @(list($hoster, $videoId) = $exploded);
                         break;
                 }
                 // lets search a valid hoster
                 $url = $this->helpers()->hoster()->getHoster($hoster)->getHosterUrl($videoId);
             } catch (Exception $e) {
                 // simply: provider isn't compatible
                 // trying direct download
                 $pObj = X_VlcShares_Plugins::broker()->getPlugins($provider);
                 if ($pObj instanceof X_VlcShares_Plugins_ResolverInterface) {
                     $url = $pObj->resolveLocation($location);
                     if (!X_Env::startWith($url, "http://") && !X_Env::startWith($url, "https://")) {
                         // not valid, revert changes
                         $url = null;
                     }
                 }
             }
         }
         try {
             if ($url === null) {
                 throw new Exception();
             }
             X_Debug::i("Appending {{$url}} to the queue");
             $jdownloader->addLink($url);
             $link = new X_Page_Item_PItem($this->getId() . '-added', X_Env::_('p_jdownloader_selection_added'));
         } catch (Zend_Http_Client_Adapter_Exception $e) {
             // connection problems or timeout
             $link = new X_Page_Item_PItem($this->getId() . '-error', X_Env::_('p_jdownloader_selection_error_network'));
             $link->setDescription($e->getMessage());
         } catch (Exception $e) {
             // wrapper/other error
             X_Debug::e("Trying to add a new download to JDowloader, but location isn't a megavideo url or there is an error");
             $link = new X_Page_Item_PItem($this->getId() . '-error', X_Env::_('p_jdownloader_selection_error_invalid'));
             $link->setDescription($e->getMessage());
         }
         $link->setType(X_Page_Item_PItem::TYPE_ELEMENT)->setLink(array('action' => 'mode', $this->getId() => null, 'pid' => null), 'default', false);
         return new X_Page_ItemList_PItem(array($link));
     }
 }