WebstreamController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. class WebstreamController extends Zend_Controller_Action
  3. {
  4. public function init()
  5. {
  6. $ajaxContext = $this->_helper->getHelper('AjaxContext');
  7. $ajaxContext->addActionContext('new', 'json')
  8. ->addActionContext('save', 'json')
  9. ->addActionContext('edit', 'json')
  10. ->addActionContext('delete', 'json')
  11. ->initContext();
  12. }
  13. public function newAction()
  14. {
  15. $userInfo = Zend_Auth::getInstance()->getStorage()->read();
  16. if (!$this->isAuthorized(-1)) {
  17. // TODO: this header call does not actually print any error message
  18. header("Status: 401 Not Authorized");
  19. return;
  20. }
  21. $webstream = new CcWebstream();
  22. //we're not saving this primary key in the DB so it's OK to be -1
  23. $webstream->setDbId(-1);
  24. $webstream->setDbName(_("Untitled Webstream"));
  25. $webstream->setDbDescription("");
  26. $webstream->setDbUrl("http://");
  27. $webstream->setDbLength("00:30:00");
  28. $webstream->setDbName(_("Untitled Webstream"));
  29. $webstream->setDbCreatorId($userInfo->id);
  30. $webstream->setDbUtime(new DateTime("now", new DateTimeZone('UTC')));
  31. $webstream->setDbMtime(new DateTime("now", new DateTimeZone('UTC')));
  32. //clear the session in case an old playlist was open: CC-4196
  33. Application_Model_Library::changePlaylist(null, null);
  34. $this->view->obj = new Application_Model_Webstream($webstream);
  35. $this->view->action = "new";
  36. $this->view->html = $this->view->render('webstream/webstream.phtml');
  37. }
  38. public function editAction()
  39. {
  40. $request = $this->getRequest();
  41. $id = $request->getParam("id");
  42. if (is_null($id)) {
  43. throw new Exception("Missing parameter 'id'");
  44. }
  45. $webstream = CcWebstreamQuery::create()->findPK($id);
  46. if ($webstream) {
  47. Application_Model_Library::changePlaylist($id, "stream");
  48. }
  49. $this->view->obj = new Application_Model_Webstream($webstream);
  50. $this->view->action = "edit";
  51. $this->view->html = $this->view->render('webstream/webstream.phtml');
  52. }
  53. public function deleteAction()
  54. {
  55. $request = $this->getRequest();
  56. $id = $request->getParam("ids");
  57. if (!$this->isAuthorized($id)) {
  58. header("Status: 401 Not Authorized");
  59. return;
  60. }
  61. $type = "stream";
  62. Application_Model_Library::changePlaylist(null, $type);
  63. $webstream = CcWebstreamQuery::create()->findPK($id)->delete();
  64. $this->view->obj = null;
  65. $this->view->action = "delete";
  66. $this->view->html = $this->view->render('webstream/webstream.phtml');
  67. }
  68. /*TODO : make a user object be passed a parameter into this function so
  69. that it does not have to be fetched multiple times.*/
  70. public function isAuthorized($webstream_id)
  71. {
  72. $user = Application_Model_User::getCurrentUser();
  73. if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) {
  74. return true;
  75. }
  76. if ($user->isHost()) {
  77. // not creating a webstream
  78. if ($webstream_id != -1) {
  79. $webstream = CcWebstreamQuery::create()->findPK($webstream_id);
  80. /*we are updating a playlist. Ensure that if the user is a
  81. host/dj, that he has the correct permission.*/
  82. $user = Application_Model_User::getCurrentUser();
  83. //only allow when webstream belongs to the DJ
  84. return $webstream->getDbCreatorId() == $user->getId();
  85. }
  86. /*we are creating a new stream. Don't need to check whether the
  87. DJ/Host owns the stream*/
  88. return true;
  89. } else {
  90. Logging::info( $user );
  91. }
  92. return false;
  93. }
  94. public function saveAction()
  95. {
  96. $request = $this->getRequest();
  97. $id = $request->getParam("id");
  98. $parameters = array();
  99. foreach (array('id','length','name','description','url') as $p) {
  100. $parameters[$p] = trim($request->getParam($p));
  101. }
  102. if (!$this->isAuthorized($id)) {
  103. header("Status: 401 Not Authorized");
  104. return;
  105. }
  106. list($analysis, $mime, $mediaUrl, $di) = Application_Model_Webstream::analyzeFormData($parameters);
  107. try {
  108. if (Application_Model_Webstream::isValid($analysis)) {
  109. $streamId = Application_Model_Webstream::save($parameters, $mime, $mediaUrl, $di);
  110. Application_Model_Library::changePlaylist($streamId, "stream");
  111. $this->view->statusMessage = "<div class='success'>"._("Webstream saved.")."</div>";
  112. $this->view->streamId = $streamId;
  113. $this->view->length = $di->format("%Hh %Im");
  114. } else {
  115. throw new Exception("isValid returned false");
  116. }
  117. } catch (Exception $e) {
  118. Logging::debug($e->getMessage());
  119. $this->view->statusMessage = "<div class='errors'>"._("Invalid form values.")."</div>";
  120. $this->view->streamId = -1;
  121. $this->view->analysis = $analysis;
  122. }
  123. }
  124. }