2015-09-14 23:00:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Rest_PodcastController extends Zend_Rest_Controller
|
|
|
|
{
|
2015-09-24 18:58:02 +02:00
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->view->layout()->disableLayout();
|
|
|
|
|
|
|
|
// Remove reliance on .phtml files to render requests
|
|
|
|
$this->_helper->viewRenderer->setNoRender(true);
|
2015-09-18 21:39:12 +02:00
|
|
|
$this->view->setScriptPath(APPLICATION_PATH . 'views/scripts/');
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:47:53 +01:00
|
|
|
/**
|
|
|
|
* headAction is needed as it is defined as an abstract function in the base controller
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function headAction()
|
|
|
|
{
|
|
|
|
Logging::info("HEAD action received");
|
|
|
|
}
|
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
public function indexAction()
|
|
|
|
{
|
2015-09-16 20:22:13 +02:00
|
|
|
// Check if offset and limit were sent with request.
|
|
|
|
// Default limit to zero and offset to $totalFileCount
|
|
|
|
$offset = $this->_getParam('offset', 0);
|
2015-12-02 18:25:04 +01:00
|
|
|
$limit = $this->_getParam('limit', 0);
|
2015-09-16 20:22:13 +02:00
|
|
|
|
|
|
|
//Sorting parameters
|
|
|
|
$sortColumn = $this->_getParam('sort', PodcastPeer::ID);
|
|
|
|
$sortDir = $this->_getParam('sort_dir', Criteria::ASC);
|
|
|
|
|
2015-11-03 17:45:01 +01:00
|
|
|
$stationPodcastId = Application_Model_Preference::getStationPodcastId();
|
2015-12-02 18:25:04 +01:00
|
|
|
$result = PodcastQuery::create()
|
2015-11-03 22:23:17 +01:00
|
|
|
// Don't return the Station podcast - we fetch it separately
|
2015-12-02 18:25:04 +01:00
|
|
|
->filterByDbId($stationPodcastId, Criteria::NOT_EQUAL);
|
|
|
|
if ($limit > 0) { $result->setLimit($limit); }
|
|
|
|
$result->setOffset($offset)
|
2015-11-03 22:23:17 +01:00
|
|
|
->orderBy($sortColumn, $sortDir);
|
2015-12-02 18:25:04 +01:00
|
|
|
$result = $result->find();
|
2015-09-16 20:22:13 +02:00
|
|
|
|
2015-12-02 18:25:04 +01:00
|
|
|
$podcastArray = $result->toArray(null, false, BasePeer::TYPE_FIELDNAME);
|
2015-09-16 20:22:13 +02:00
|
|
|
|
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(200)
|
2015-12-02 18:25:04 +01:00
|
|
|
->setHeader('X-TOTAL-COUNT', $result->count())
|
2015-09-16 20:22:13 +02:00
|
|
|
->appendBody(json_encode($podcastArray));
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAction()
|
|
|
|
{
|
|
|
|
$id = $this->getId();
|
|
|
|
if (!$id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(200)
|
2015-10-14 16:58:13 +02:00
|
|
|
->appendBody(json_encode(Application_Service_PodcastService::getPodcastById($id)));
|
2015-09-18 14:15:48 +02:00
|
|
|
} catch (PodcastNotFoundException $e) {
|
|
|
|
$this->podcastNotFoundResponse();
|
|
|
|
Logging::error($e->getMessage());
|
2015-09-14 23:00:54 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postAction()
|
|
|
|
{
|
2015-09-16 20:22:13 +02:00
|
|
|
//If we do get an ID on a POST, then that doesn't make any sense
|
|
|
|
//since POST is only for creating.
|
|
|
|
if ($id = $this->_getParam('id', false)) {
|
|
|
|
$resp = $this->getResponse();
|
|
|
|
$resp->setHttpResponseCode(400);
|
|
|
|
$resp->appendBody("ERROR: ID should not be specified when using POST. POST is only used for podcast creation, and an ID will be chosen by Airtime");
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
|
2015-09-16 20:22:13 +02:00
|
|
|
try {
|
2015-09-18 21:39:12 +02:00
|
|
|
$requestData = $this->getRequest()->getPost();
|
2015-11-03 22:23:17 +01:00
|
|
|
$podcast = Application_Service_PodcastService::createFromFeedUrl($requestData["url"]);
|
2015-09-16 20:22:13 +02:00
|
|
|
|
2015-09-18 21:39:12 +02:00
|
|
|
$path = 'podcast/podcast.phtml';
|
2015-11-17 23:34:13 +01:00
|
|
|
|
2015-09-18 21:39:12 +02:00
|
|
|
$this->view->podcast = $podcast;
|
|
|
|
$this->_helper->json->sendJson(array(
|
|
|
|
"podcast"=>json_encode($podcast),
|
|
|
|
"html"=>$this->view->render($path),
|
|
|
|
));
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|
|
|
|
catch (PodcastLimitReachedException $e) {
|
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(400)
|
2015-11-11 01:26:25 +01:00
|
|
|
->appendBody("Podcast limit reached.");
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|
|
|
|
catch (InvalidPodcastException $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(400)
|
2015-11-11 01:26:25 +01:00
|
|
|
->appendBody("Invalid podcast!");
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
Logging::error($e->getMessage());
|
2015-11-11 01:26:25 +01:00
|
|
|
$this->unknownErrorResponse();
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function putAction()
|
|
|
|
{
|
2015-09-18 14:15:48 +02:00
|
|
|
$id = $this->getId();
|
|
|
|
if (!$id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$requestData = json_decode($this->getRequest()->getRawBody(), true);
|
2015-10-14 16:58:13 +02:00
|
|
|
$podcast = Application_Service_PodcastService::updatePodcastFromArray($id, $requestData);
|
2015-09-18 14:15:48 +02:00
|
|
|
|
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(201)
|
|
|
|
->appendBody(json_encode($podcast));
|
|
|
|
}
|
|
|
|
catch (PodcastNotFoundException $e) {
|
|
|
|
$this->podcastNotFoundResponse();
|
|
|
|
Logging::error($e->getMessage());
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->unknownErrorResponse();
|
|
|
|
Logging::error($e->getMessage());
|
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAction()
|
|
|
|
{
|
2015-09-18 14:15:48 +02:00
|
|
|
$id = $this->getId();
|
|
|
|
if (!$id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-10-14 16:58:13 +02:00
|
|
|
Application_Service_PodcastService::deletePodcastById($id);
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(204);
|
|
|
|
}
|
|
|
|
catch (PodcastNotFoundException $e) {
|
|
|
|
$this->podcastNotFoundResponse();
|
|
|
|
Logging::error($e->getMessage());
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->unknownErrorResponse();
|
|
|
|
Logging::error($e->getMessage());
|
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 21:39:12 +02:00
|
|
|
/**
|
|
|
|
* Endpoint for performing bulk actions (deleting multiple podcasts, opening multiple editors)
|
|
|
|
*/
|
|
|
|
public function bulkAction() {
|
2015-10-22 18:12:41 +02:00
|
|
|
if ($this->_request->getMethod() != HttpRequestType::POST) {
|
2015-09-18 21:39:12 +02:00
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(405)
|
|
|
|
->appendBody("ERROR: Method not accepted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $this->_getParam('ids', []);
|
2015-10-22 18:12:41 +02:00
|
|
|
$method = $this->_getParam('method', HttpRequestType::GET);
|
2015-09-18 21:39:12 +02:00
|
|
|
$responseBody = [];
|
|
|
|
|
2015-10-28 17:17:29 +01:00
|
|
|
// XXX: Should this be a map of HttpRequestType => function call instead? Would be a bit cleaner
|
2015-09-18 21:39:12 +02:00
|
|
|
switch($method) {
|
2015-10-22 18:12:41 +02:00
|
|
|
case HttpRequestType::DELETE:
|
2015-09-18 21:39:12 +02:00
|
|
|
foreach($ids as $id) {
|
2015-10-14 16:58:13 +02:00
|
|
|
Application_Service_PodcastService::deletePodcastById($id);
|
2015-09-18 21:39:12 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-10-22 18:12:41 +02:00
|
|
|
case HttpRequestType::GET:
|
2015-11-17 23:34:13 +01:00
|
|
|
$path = 'podcast/podcast.phtml';
|
2015-09-18 21:39:12 +02:00
|
|
|
foreach($ids as $id) {
|
2015-10-29 17:17:52 +01:00
|
|
|
$responseBody[] = array(
|
2015-10-29 22:53:45 +01:00
|
|
|
"podcast" => json_encode(Application_Service_PodcastService::getPodcastById($id)),
|
2015-11-17 23:34:13 +01:00
|
|
|
"html" => $this->view->render($path)
|
2015-10-29 17:17:52 +01:00
|
|
|
);
|
2015-09-18 21:39:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_helper->json->sendJson($responseBody);
|
|
|
|
}
|
|
|
|
|
2015-11-13 20:57:32 +01:00
|
|
|
/**
|
|
|
|
* @throws PodcastNotFoundException
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2015-10-29 17:17:52 +01:00
|
|
|
public function stationAction() {
|
|
|
|
$stationPodcastId = Application_Model_Preference::getStationPodcastId();
|
|
|
|
$podcast = Application_Service_PodcastService::getPodcastById($stationPodcastId);
|
2015-11-13 20:57:32 +01:00
|
|
|
$path = 'podcast/station.phtml';
|
2015-10-29 17:17:52 +01:00
|
|
|
$this->view->podcast = $podcast;
|
|
|
|
$this->_helper->json->sendJson(array(
|
|
|
|
"podcast" => json_encode($podcast),
|
2015-11-13 20:57:32 +01:00
|
|
|
"html" => $this->view->render($path)
|
2015-10-29 17:17:52 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
private function getId()
|
|
|
|
{
|
|
|
|
if (!$id = $this->_getParam('id', false)) {
|
|
|
|
$resp = $this->getResponse();
|
|
|
|
$resp->setHttpResponseCode(400);
|
|
|
|
$resp->appendBody("ERROR: No podcast ID specified.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:22:13 +02:00
|
|
|
private function unknownErrorResponse()
|
|
|
|
{
|
|
|
|
$resp = $this->getResponse();
|
2015-11-11 01:26:25 +01:00
|
|
|
$resp->setHttpResponseCode(500);
|
2015-09-16 20:22:13 +02:00
|
|
|
$resp->appendBody("An unknown error occurred.");
|
|
|
|
}
|
|
|
|
|
2015-09-18 14:15:48 +02:00
|
|
|
private function podcastNotFoundResponse()
|
|
|
|
{
|
|
|
|
$resp = $this->getResponse();
|
|
|
|
$resp->setHttpResponseCode(404);
|
|
|
|
$resp->appendBody("ERROR: Podcast not found.");
|
|
|
|
}
|
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|