2015-09-14 23:00:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Rest_PodcastController extends Zend_Rest_Controller
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->view->layout()->disableLayout();
|
|
|
|
|
|
|
|
// Remove reliance on .phtml files to render requests
|
|
|
|
$this->_helper->viewRenderer->setNoRender(true);
|
2022-02-04 11:00:41 +01:00
|
|
|
$this->view->setScriptPath(APPLICATION_PATH . '/views/scripts/');
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:47:53 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* headAction is needed as it is defined as an abstract function in the base controller.
|
2017-02-20 21:47:53 +01:00
|
|
|
*/
|
|
|
|
public function headAction()
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::info('HEAD action received');
|
2017-02-20 21:47:53 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Sorting parameters
|
2015-09-16 20:22:13 +02:00
|
|
|
$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
|
2019-05-01 23:31:58 +02:00
|
|
|
->filterByDbId($stationPodcastId, Criteria::NOT_EQUAL)
|
|
|
|
->leftJoinImportedPodcast()
|
2022-01-23 19:15:55 +01:00
|
|
|
->withColumn('auto_ingest_timestamp');
|
2019-05-01 22:00:49 +02:00
|
|
|
$total = $result->count();
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($limit > 0) {
|
|
|
|
$result->setLimit($limit);
|
|
|
|
}
|
2015-12-02 18:25:04 +01:00
|
|
|
$result->setOffset($offset)
|
2022-01-23 19:15:55 +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)
|
2019-05-01 22:00:49 +02:00
|
|
|
->setHeader('X-TOTAL-COUNT', $total)
|
2022-01-23 19:15:55 +01: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)
|
2022-01-23 19:15:55 +01: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()
|
|
|
|
{
|
2022-03-14 11:15:04 +01:00
|
|
|
// If we do get an ID on a POST, then that doesn't make any sense
|
|
|
|
// since POST is only for creating.
|
2015-09-16 20:22:13 +02:00
|
|
|
if ($id = $this->_getParam('id', false)) {
|
|
|
|
$resp = $this->getResponse();
|
|
|
|
$resp->setHttpResponseCode(400);
|
2021-10-11 16:10:47 +02:00
|
|
|
$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');
|
|
|
|
|
2015-09-16 20:22:13 +02:00
|
|
|
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();
|
2021-10-11 16:10:47 +02: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;
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->_helper->json->sendJson([
|
|
|
|
'podcast' => json_encode($podcast),
|
|
|
|
'html' => $this->view->render($path),
|
|
|
|
]);
|
|
|
|
} catch (InvalidPodcastException $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->getResponse()
|
|
|
|
->setHttpResponseCode(400)
|
2022-01-23 19:15:55 +01:00
|
|
|
->appendBody('Invalid podcast!');
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2015-09-16 20:22:13 +02:00
|
|
|
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)
|
2022-01-23 19:15:55 +01:00
|
|
|
->appendBody(json_encode($podcast));
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (PodcastNotFoundException $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->podcastNotFoundResponse();
|
|
|
|
Logging::error($e->getMessage());
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$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()
|
2022-01-23 19:15:55 +01:00
|
|
|
->setHttpResponseCode(204);
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (PodcastNotFoundException $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->podcastNotFoundResponse();
|
|
|
|
Logging::error($e->getMessage());
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2015-09-18 14:15:48 +02:00
|
|
|
$this->unknownErrorResponse();
|
|
|
|
Logging::error($e->getMessage());
|
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 21:39:12 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Endpoint for performing bulk actions (deleting multiple podcasts, opening multiple editors).
|
2015-09-18 21:39:12 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
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)
|
2022-01-23 19:15:55 +01:00
|
|
|
->appendBody('ERROR: Method not accepted');
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-09-18 21:39:12 +02:00
|
|
|
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
|
2021-10-11 16:10:47 +02:00
|
|
|
switch ($method) {
|
2015-10-22 18:12:41 +02:00
|
|
|
case HttpRequestType::DELETE:
|
2021-10-11 16:10:47 +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
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-09-12 13:16:14 +02:00
|
|
|
break;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-10-22 18:12:41 +02:00
|
|
|
case HttpRequestType::GET:
|
2015-11-17 23:34:13 +01:00
|
|
|
$path = 'podcast/podcast.phtml';
|
2021-10-11 16:10:47 +02:00
|
|
|
foreach ($ids as $id) {
|
|
|
|
$responseBody[] = [
|
|
|
|
'podcast' => json_encode(Application_Service_PodcastService::getPodcastById($id)),
|
|
|
|
'html' => $this->view->render($path),
|
|
|
|
];
|
2015-09-18 21:39:12 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-09-12 13:16:14 +02:00
|
|
|
break;
|
2015-09-18 21:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_helper->json->sendJson($responseBody);
|
|
|
|
}
|
|
|
|
|
2018-12-13 04:53:28 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Endpoint for triggering the generation of a smartblock and playlist to match the podcast name.
|
2018-12-13 04:53:28 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function smartblockAction()
|
|
|
|
{
|
2018-12-14 22:24:28 +01:00
|
|
|
$title = $this->_getParam('title', []);
|
2018-12-13 04:53:28 +01:00
|
|
|
$id = $this->_getParam('id', []);
|
|
|
|
if (!$id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$podcast = Application_Service_PodcastService::getPodcastById($id);
|
|
|
|
|
2018-12-14 22:24:28 +01:00
|
|
|
// logging::info($podcast);
|
|
|
|
Application_Service_PodcastService::createPodcastSmartblockAndPlaylist($podcast, $title);
|
2018-12-13 04:53:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-13 20:57:32 +01:00
|
|
|
/**
|
|
|
|
* @throws PodcastNotFoundException
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function stationAction()
|
|
|
|
{
|
2015-10-29 17:17:52 +01:00
|
|
|
$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;
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->_helper->json->sendJson([
|
|
|
|
'podcast' => json_encode($podcast),
|
|
|
|
'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);
|
2021-10-11 16:10:47 +02:00
|
|
|
$resp->appendBody('ERROR: No podcast ID specified.');
|
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-09-14 23:00:54 +02:00
|
|
|
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);
|
2021-10-11 16:10:47 +02:00
|
|
|
$resp->appendBody('An unknown error occurred.');
|
2015-09-16 20:22:13 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 14:15:48 +02:00
|
|
|
private function podcastNotFoundResponse()
|
|
|
|
{
|
|
|
|
$resp = $this->getResponse();
|
|
|
|
$resp->setHttpResponseCode(404);
|
2021-10-11 16:10:47 +02:00
|
|
|
$resp->appendBody('ERROR: Podcast not found.');
|
2015-09-18 14:15:48 +02:00
|
|
|
}
|
2015-09-14 23:00:54 +02:00
|
|
|
}
|