From f55e8327d2a5ee70f8d760d3a084bc8a274dd981 Mon Sep 17 00:00:00 2001 From: Naomi Date: Mon, 4 Nov 2013 13:40:52 -0500 Subject: [PATCH] CC-5450 : Refactor Media Management redone webstreams, will come back to do finishing touches for error handling after a bit more generic work on media. --- .../controllers/WebstreamController.php | 134 +++--------------- .../application/models/airtime/MediaItem.php | 17 +++ .../application/services/WebstreamService.php | 6 + .../views/scripts/form/webstream.phtml | 2 +- airtime_mvc/public/js/airtime/library/spl.js | 8 +- 5 files changed, 48 insertions(+), 119 deletions(-) diff --git a/airtime_mvc/application/controllers/WebstreamController.php b/airtime_mvc/application/controllers/WebstreamController.php index 4ffb99e68..8a5bea571 100644 --- a/airtime_mvc/application/controllers/WebstreamController.php +++ b/airtime_mvc/application/controllers/WebstreamController.php @@ -31,105 +31,37 @@ class WebstreamController extends Zend_Controller_Action $this->view->action = "new"; $this->view->html = $form->render(); - - /* - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - if (!$this->isAuthorized(-1)) { - // TODO: this header call does not actually print any error message - header("Status: 401 Not Authorized"); - return; - } - - $webstream = new CcWebstream(); - - //we're not saving this primary key in the DB so it's OK to be -1 - $webstream->setDbId(-1); - $webstream->setDbName(_("Untitled Webstream")); - $webstream->setDbDescription(""); - $webstream->setDbUrl("http://"); - $webstream->setDbLength("00:30:00"); - $webstream->setDbName(_("Untitled Webstream")); - $webstream->setDbCreatorId($userInfo->id); - $webstream->setDbUtime(new DateTime("now", new DateTimeZone('UTC'))); - $webstream->setDbMtime(new DateTime("now", new DateTimeZone('UTC'))); - - //clear the session in case an old playlist was open: CC-4196 - Application_Model_Library::changePlaylist(null, null); - - $this->view->obj = new Application_Model_Webstream($webstream); - $this->view->action = "new"; - $this->view->html = $this->view->render('webstream/webstream.phtml'); - */ } public function editAction() { $request = $this->getRequest(); - $id = $request->getParam("id"); - if (is_null($id)) { - throw new Exception("Missing parameter 'id'"); - } + + $service = new Application_Service_WebstreamService(); + $form = $service->makeWebstreamForm($id, true); + + Application_Model_Library::changePlaylist($id, "stream"); - $webstream = CcWebstreamQuery::create()->findPK($id); - if ($webstream) { - Application_Model_Library::changePlaylist($id, "stream"); - } - $this->view->obj = new Application_Model_Webstream($webstream); $this->view->action = "edit"; - $this->view->html = $this->view->render('webstream/webstream.phtml'); + $this->view->html = $form->render(); } public function deleteAction() { $request = $this->getRequest(); - $id = $request->getParam("ids"); + $ids = $request->getParam("ids"); - if (!$this->isAuthorized($id)) { - header("Status: 401 Not Authorized"); + Application_Model_Library::changePlaylist(null, null); + + $service = new Application_Service_WebstreamService(); + $service->deleteWebstreams($ids); - return; - } - - $type = "stream"; - Application_Model_Library::changePlaylist(null, $type); - - $webstream = CcWebstreamQuery::create()->findPK($id)->delete(); - - $this->view->obj = null; $this->view->action = "delete"; - $this->view->html = $this->view->render('webstream/webstream.phtml'); + $this->view->html = $this->view->render('form/webstream.phtml'); } - /*TODO : make a user object be passed a parameter into this function so - that it does not have to be fetched multiple times.*/ - public function isAuthorized($webstream_id) - { - $user = Application_Model_User::getCurrentUser(); - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - return true; - } - - if ($user->isHost()) { - // not creating a webstream - if ($webstream_id != -1) { - $webstream = CcWebstreamQuery::create()->findPK($webstream_id); - /*we are updating a playlist. Ensure that if the user is a - host/dj, that he has the correct permission.*/ - $user = Application_Model_User::getCurrentUser(); - //only allow when webstream belongs to the DJ - return $webstream->getDbCreatorId() == $user->getId(); - } - /*we are creating a new stream. Don't need to check whether the - DJ/Host owns the stream*/ - return true; - } else { - Logging::info( $user ); - } - return false; - } - public function saveAction() { $request = $this->getRequest(); @@ -150,42 +82,18 @@ class WebstreamController extends Zend_Controller_Action $values = $form->getValues(); Logging::info($values); - $service->saveWebstream($values); + $ws = $service->saveWebstream($values); + + $this->view->statusMessage = "
"._("Webstream saved.")."
"; + $this->view->streamId = $ws->getId(); + $this->view->length = "00:05"; //$di->format("%Hh %Im"); } else { Logging::info("form is not valid"); + + $this->view->statusMessage = "
"._("Invalid form values.")."
"; + $this->view->streamId = -1; + $this->view->errors = $form->getMessages(); } - - - - /* - if (!$this->isAuthorized($id)) { - header("Status: 401 Not Authorized"); - return; - } - */ - - - /* - list($analysis, $mime, $mediaUrl, $di) = Application_Model_Webstream::analyzeFormData($parameters); - try { - if (Application_Model_Webstream::isValid($analysis)) { - $streamId = Application_Model_Webstream::save($parameters, $mime, $mediaUrl, $di); - - Application_Model_Library::changePlaylist($streamId, "stream"); - - $this->view->statusMessage = "
"._("Webstream saved.")."
"; - $this->view->streamId = $streamId; - $this->view->length = $di->format("%Hh %Im"); - } else { - throw new Exception("isValid returned false"); - } - } catch (Exception $e) { - Logging::debug($e->getMessage()); - $this->view->statusMessage = "
"._("Invalid form values.")."
"; - $this->view->streamId = -1; - $this->view->analysis = $analysis; - } - */ } } diff --git a/airtime_mvc/application/models/airtime/MediaItem.php b/airtime_mvc/application/models/airtime/MediaItem.php index 54a3e9c9c..2e6be4ed3 100644 --- a/airtime_mvc/application/models/airtime/MediaItem.php +++ b/airtime_mvc/application/models/airtime/MediaItem.php @@ -23,6 +23,7 @@ class MediaItem extends BaseMediaItem { public function preInsert(PropelPDO $con = null) { + //TODO, don't overwrite owner here if already set (from media monitor request) try { $service = new Application_Service_UserService(); $this->setCcSubjs($service->getCurrentUser()); @@ -34,4 +35,20 @@ class MediaItem extends BaseMediaItem //have to return true for the insert to work. return true; } + + public function preDelete(PropelPDO $con = null) + { + try { + $service = new Application_Service_UserService(); + $user = $service->getCurrentUser(); + + //only users who are admins, PMs or owners of the media can delete the item. + return $user->isAdminOrPM() || $user->getId() === $this->getOwnerId(); + } + catch(Exception $e) { + Logging::warn("Unable to get current user while trying to delete media item {$this->getId()}"); + } + + return false; + } } diff --git a/airtime_mvc/application/services/WebstreamService.php b/airtime_mvc/application/services/WebstreamService.php index 726e352a4..6d6f2d5e7 100644 --- a/airtime_mvc/application/services/WebstreamService.php +++ b/airtime_mvc/application/services/WebstreamService.php @@ -17,6 +17,7 @@ class Application_Service_WebstreamService $length = $webstream->getHoursMins(); $formValues = array( + "id" => $id, "name" => $webstream->getName(), "description" => $webstream->getDescription(), "url" => $webstream->getUrl(), @@ -84,4 +85,9 @@ class Application_Service_WebstreamService return $ws; } + + public function deleteWebstreams($ids) { + + WebstreamQuery::create()->findPks($ids)->delete(); + } } \ No newline at end of file diff --git a/airtime_mvc/application/views/scripts/form/webstream.phtml b/airtime_mvc/application/views/scripts/form/webstream.phtml index 114f2f0a5..8474a9b29 100644 --- a/airtime_mvc/application/views/scripts/form/webstream.phtml +++ b/airtime_mvc/application/views/scripts/form/webstream.phtml @@ -24,7 +24,7 @@ - getValue() ?>"> + getValue() ?>"> obj->getLastModified('U'); ?>"> diff --git a/airtime_mvc/public/js/airtime/library/spl.js b/airtime_mvc/public/js/airtime/library/spl.js index f2f9ddbc5..5dcda0faf 100644 --- a/airtime_mvc/public/js/airtime/library/spl.js +++ b/airtime_mvc/public/js/airtime/library/spl.js @@ -784,7 +784,7 @@ var AIRTIME = (function(AIRTIME){ $pl.on("click", "#webstream_save", function() { //get all fields and POST to server - var id = $pl.find("#ws_id").attr("value"); + var id = $pl.find("#obj_id").attr("value"); var description = $pl.find("#ws_description").val(); var streamurl = $pl.find("#ws_url").val(); var hours = $pl.find("#ws_hours").val(); @@ -1084,16 +1084,14 @@ var AIRTIME = (function(AIRTIME){ }; mod.fnWsDelete = function(wsid) { - var url, id, lastMod; + var url, id; stopAudioPreview(); id = (wsid === undefined) ? getId() : wsid; - lastMod = getModified(); - type = $('#obj_type').val(); url = baseUrl+'Webstream/delete'; $.post(url, - {format: "json", ids: id, modified: lastMod, type: type}, + {format: "json", ids: id}, function(json){ openPlaylist(json); redrawLib();