public function addAction()
 {
     $request = $this->getRequest();
     $form = new Application_Form_Video();
     $form->setAction($this->_helper->url('add'));
     $rtmp = $this->getRequest()->getParam('rtmp', false);
     $category = $this->getRequest()->getParam('category', false);
     if ($category) {
         $category = X_Env::decode($category);
     }
     try {
         $_hosters = X_VlcShares_Plugins::helpers()->hoster()->getHosters();
         if ($rtmp === false) {
             $hosters = array('auto' => X_Env::_('p_onlinelibrary_hosterauto'), 'direct-url' => 'direct-url');
             foreach ($_hosters as $idHoster => $pattern) {
                 $hosters[$idHoster] = $idHoster;
             }
         } else {
             $hosters = array('direct-url' => 'direct-url (rtmp)');
             $form->idVideo->setValue($rtmp);
         }
         $form->hoster->setMultiOptions($hosters);
         if ($category) {
             $form->setDefault('category', $category);
         }
     } catch (Exception $e) {
     }
     $isAjax = $request->getParam('isAjax', false);
     if ($rtmp === false && $this->getRequest()->isPost()) {
         if ($form->isValid($request->getPost())) {
             $video = new Application_Model_Video();
             $id = $request->getParam('id', false);
             if ($id !== false && !is_null($id)) {
                 Application_Model_VideosMapper::i()->find($id, $video);
                 if ($video->getId() != $id) {
                     throw new Exception(X_Env::_('p_onlinelibrary_err_invalidid'));
                 }
             }
             $idVideo = $form->getValue('idVideo');
             $check = $form->getValue('check');
             $hoster = $form->getValue('hoster');
             $title = $form->getValue('title');
             $description = $form->getValue('description');
             $thumbnail = $form->getValue('thumbnail');
             $category = $form->getValue('category');
             // direct url is a special hoster
             // it's not inside the hoster helper and can be handler only
             // with manual insertion
             if ($hoster !== 'direct-url') {
                 try {
                     if ($hoster == 'auto') {
                         $hosterObj = X_VlcShares_Plugins::helpers()->hoster()->findHoster($idVideo);
                         $hoster = $hosterObj->getId();
                     } else {
                         $hosterObj = X_VlcShares_Plugins::helpers()->hoster()->getHoster($hoster);
                     }
                 } catch (Exception $e) {
                     $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_onlinelibrary_invalidhoster', $hoster)));
                     $this->_helper->redirector('index', 'onlinelibrary');
                     return;
                 }
                 try {
                     $resourceId = $hosterObj->getResourceId($idVideo);
                 } catch (Exception $e) {
                     // if hoster can't strip video id from $idVideo, it means that $idVideo is
                     // an id already
                     $resourceId = $idVideo;
                 }
                 // check will be performed if title == null
                 if ($check || trim($title) == '') {
                     try {
                         $infos = $hosterObj->getPlayableInfos($resourceId);
                     } catch (Exception $e) {
                         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_onlinelibrary_invalidvideo', $e->getMessage())));
                         $this->_helper->redirector('index', 'onlinelibrary');
                         return;
                     }
                     if (trim($thumbnail) == "" && array_key_exists('thumbnail', $infos)) {
                         $thumbnail = $infos['thumbnail'];
                     }
                     if (trim($description) == "" && array_key_exists('description', $infos)) {
                         $description = $infos['description'];
                     }
                     if (trim($title) == "" && array_key_exists('title', $infos)) {
                         $title = $infos['title'];
                     }
                 }
             } else {
                 $resourceId = $idVideo;
             }
             // title must be setted or fetched from internet
             if (trim($title) == '') {
                 $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_onlinelibrary_invalidtitle')));
                 $this->_helper->redirector('index', 'onlinelibrary');
                 return;
             }
             $video->setIdVideo($resourceId)->setCategory($category)->setTitle($title)->setHoster($hoster)->setThumbnail($thumbnail)->setDescription($description);
             Application_Model_VideosMapper::i()->save($video);
             if ($isAjax) {
                 header('Content-Type:text/plain');
                 echo '1';
                 $this->_helper->viewRenderer->setNoRender(true);
                 $this->_helper->layout->disableLayout();
                 return;
             } else {
                 //return $this->_helper->redirector('index');
                 $this->_helper->redirector('category', 'onlinelibrary', 'default', array('id' => X_Env::encode($video->getCategory())));
             }
         }
     }
     if ($isAjax) {
         header('Content-Type:text/plain');
         echo '0';
         $this->_helper->viewRenderer->setNoRender(true);
         $this->_helper->layout->disableLayout();
     } else {
         $this->view->form = $form;
     }
 }
 public function editVideoAction()
 {
     $idEvento = $this->_request->getParam("idEvento");
     $form = new Application_Form_Video();
     $form->submit->setLabel('Salvar video');
     $this->view->form = $form;
     if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
             $id = (int) $form->getValue('id');
             $nome = $form->getValue('nome');
             $url = $form->getValue('url');
             $fk_evento = $form->getValue('fk_evento');
             //$form->getValue('interior_capital');
             $videos = new Application_Model_DbTable_Video();
             $videos->updateVideo($id, $nome, $url, $fk_evento);
             $this->_redirect('/upload/lista-videos/idEvento/' . $idEvento);
         } else {
             $form->populate($formData);
         }
     } else {
         $id = $this->_getParam('id', 0);
         if ($id > 0) {
             $videos = new Application_Model_DbTable_Video();
             $form->populate($videos->getVideo($id));
         }
     }
 }