diff --git a/airtime_mvc/application/models/airtime/Podcast.php b/airtime_mvc/application/models/airtime/Podcast.php index 88ad42ce3..d74690ece 100644 --- a/airtime_mvc/application/models/airtime/Podcast.php +++ b/airtime_mvc/application/models/airtime/Podcast.php @@ -15,22 +15,40 @@ */ class Podcast extends BasePodcast { - /*public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) + /** + * Override this function so it returns its child class fields as well. + * Child class will either be ImportedPodcast or StationPodcast + * + * @param string $keyType + * @param bool $includeLazyLoadColumns + * @param array $alreadyDumpedObjects + * @param bool $includeForeignObjects + * @return array + */ + public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { - $podcastArray = parent::toArray(BasePeer::TYPE_FIELDNAME); - $podcastArray["url"] = $this->getDbUrl(); - //$array = array_merge($podcastArray, $importedPodcastArray); + $importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($this->getDbId())->findOne(); + $stationPodcast = StationPodcastQuery::create()->filterByDbPodcastId($this->getDbId())->findOne(); + if (!is_null($importedPodcast)) { + $importedPodcastArray = $importedPodcast->toArray(BasePeer::TYPE_FIELDNAME); - //unset podcast_id because we already have that value in $importedPodcastArray - //unset($array["podcast_id"]); + //unset these values because we already have the podcast id in $podcastArray + //and we don't need the imported podcast ID + unset($importedPodcastArray["id"]); + unset($importedPodcastArray["podcast_id"]); + + return array_merge($podcastArray, $importedPodcastArray); + + } else if (!is_null($stationPodcast)) { + // For now just return $podcastArray because StationPodcast objects do not have any + // extra fields we want to return right now. This may change in the future. + return $podcastArray; + } else { + return $podcastArray; + } - return $podcastArray; } - public function getDbUrl() - { - return $this->getImportedPodcasts()->getFirst()->getDbUrl(); - }*/ } diff --git a/airtime_mvc/application/modules/rest/controllers/PodcastController.php b/airtime_mvc/application/modules/rest/controllers/PodcastController.php index 54e34f7ce..0147644b5 100644 --- a/airtime_mvc/application/modules/rest/controllers/PodcastController.php +++ b/airtime_mvc/application/modules/rest/controllers/PodcastController.php @@ -62,7 +62,7 @@ class Rest_PodcastController extends Zend_Rest_Controller try { $this->getResponse() ->setHttpResponseCode(200) - ->appendBody(json_encode(Podcast::getPodcastById($id))); + ->appendBody(json_encode(Application_Service_PodcastService::getPodcastById($id))); } catch (PodcastNotFoundException $e) { $this->podcastNotFoundResponse(); Logging::error($e->getMessage()); @@ -127,8 +127,8 @@ class Rest_PodcastController extends Zend_Rest_Controller // to prevent the user from trying to download them again while Celery is running $episodes = $this->_service->addPodcastEpisodePlaceholders($requestData["podcast"]["id"], $requestData["podcast"]["episodes"]); - $this->_service->downloadEpisodes($episodes); - $podcast = Podcast::updateFromArray($id, $requestData); + //$this->_service->downloadEpisodes($episodes); + $podcast = Application_Service_PodcastService::updatePodcastFromArray($id, $requestData); $this->getResponse() ->setHttpResponseCode(201) @@ -152,7 +152,7 @@ class Rest_PodcastController extends Zend_Rest_Controller } try { - Podcast::deleteById($id); + Application_Service_PodcastService::deletePodcastById($id); $this->getResponse() ->setHttpResponseCode(204); } @@ -185,13 +185,13 @@ class Rest_PodcastController extends Zend_Rest_Controller switch($method) { case "DELETE": foreach($ids as $id) { - Podcast::deleteById($id); + Application_Service_PodcastService::deletePodcastById($id); $responseBody = "Success"; // TODO: make this more descriptive } break; case "GET": foreach($ids as $id) { - $podcast = Podcast::getPodcastById($id); + $podcast = Application_Service_PodcastService::getPodcastById($id); $responseBody[] = array( "podcast"=>json_encode($podcast), "html"=>$this->view->render($path), diff --git a/airtime_mvc/application/services/PodcastFactory.php b/airtime_mvc/application/services/PodcastFactory.php index d3ad3f4dd..04fe28e3c 100644 --- a/airtime_mvc/application/services/PodcastFactory.php +++ b/airtime_mvc/application/services/PodcastFactory.php @@ -6,6 +6,6 @@ class PodcastFactory { // check if station podcast exists and if not, create one - return Application_Service_ImportedPodcastService::createFromFeedUrl($feedUrl); + return Application_Service_PodcastService::createFromFeedUrl($feedUrl); } } \ No newline at end of file diff --git a/airtime_mvc/application/services/ImportedPodcastService.php b/airtime_mvc/application/services/PodcastService.php similarity index 69% rename from airtime_mvc/application/services/ImportedPodcastService.php rename to airtime_mvc/application/services/PodcastService.php index f0e8532cb..7308fbb19 100644 --- a/airtime_mvc/application/services/ImportedPodcastService.php +++ b/airtime_mvc/application/services/PodcastService.php @@ -7,7 +7,7 @@ class InvalidPodcastException extends Exception {} class PodcastNotFoundException extends Exception {} -class Application_Service_ImportedPodcastService +class Application_Service_PodcastService { // These fields should never be modified with POST/PUT data @@ -25,9 +25,9 @@ class Application_Service_ImportedPodcastService * * @return bool */ - public static function importedPodcastLimitReached() + public static function PodcastLimitReached() { - if (ImportedPodcastQuery::create()->count() >= 50) { + if (PodcastQuery::create()->count() >= 50) { return true; } else { return false; @@ -66,7 +66,7 @@ class Application_Service_ImportedPodcastService */ public static function createFromFeedUrl($feedUrl) { - if (self::importedPodcastLimitReached()) { + if (self::PodcastLimitReached()) { throw new PodcastLimitReachedException(); } @@ -130,7 +130,7 @@ class Application_Service_ImportedPodcastService $importedPodcast->setPodcast($podcast); $importedPodcast->save(); - return self::_generatePodcastArray($importedPodcast, $rss); + return self::_generatePodcastArray($podcast, $rss); } catch(Exception $e) { $podcast->delete(); throw $e; @@ -188,13 +188,13 @@ class Application_Service_ImportedPodcastService * Given a podcast object and a SimplePie feed object, * generate a data array to pass back to the front-end * - * @param $importedPodcast ImportedPodcast model object + * @param $podcast Podcast model object * @param SimplePie $rss SimplePie feed object * * @return array */ - private static function _generatePodcastArray($importedPodcast, $rss) { - $podcastArray = $importedPodcast->toArray(BasePeer::TYPE_FIELDNAME); + private static function _generatePodcastArray($podcast, $rss) { + $podcastArray = $podcast->toArray(BasePeer::TYPE_FIELDNAME); $podcastArray["episodes"] = array(); foreach ($rss->get_items() as $item) { @@ -212,4 +212,93 @@ class Application_Service_ImportedPodcastService return $podcastArray; } + + /** + * Fetches a Podcast's rss feed and returns all its episodes with + * the Podcast object + * + * @param $podcastId + * + * @throws PodcastNotFoundException + * @throws InvalidPodcastException + * @return array - Podcast Array with a full list of episodes + */ + public static function getPodcastById($podcastId) + { + $podcast = PodcastQuery::create()->findPk($podcastId); + if (!$podcast) { + throw new PodcastNotFoundException(); + } + + // Is it an imported podcast? + $importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($podcast->getDbId())->findOne(); + if (!is_null($importedPodcast)) { + + $rss = self::getPodcastFeed($importedPodcast->getDbUrl()); + if (!$rss) { + throw new InvalidPodcastException(); + } + } else { + //TODO: get station podcast + } + + return self::_generatePodcastArray($podcast, $rss); + } + + /** + * Deletes a Podcast and its podcast episodes + * + * @param $podcastId + * @throws Exception + * @throws PodcastNotFoundException + */ + public static function deletePodcastById($podcastId) + { + $podcast = PodcastQuery::create()->findPk($podcastId); + if ($podcast) { + $podcast->delete(); + } else { + throw new PodcastNotFoundException(); + } + } + + /** + * Updates a Podcast object with the given metadata + * + * @param $podcastId + * @param $data + * @return array + * @throws Exception + * @throws PodcastNotFoundException + */ + public static function updatePodcastFromArray($podcastId, $data) + { + $podcast = PodcastQuery::create()->findPk($podcastId); + if (!$podcast) { + throw new PodcastNotFoundException(); + } + + self::removePrivateFields($data); + self::validatePodcastMetadata($data); + + $importedPodcast = ImportedPodcastQuery::create()->filterByDbPodcastId($podcast->getDbId())->findOne(); + if (!is_null($importedPodcast)) { + $importedPodcast->fromArray($data, BasePeer::TYPE_FIELDNAME); + $importedPodcast->save(); + } else { + //TODO: station podcast + } + + //$podcast->fromArray($data, BasePeer::TYPE_FIELDNAME); + //$podcast->save(); + + return $podcast->toArray(BasePeer::TYPE_FIELDNAME); + } + + private static function removePrivateFields(&$data) + { + foreach (self::$privateFields as $key) { + unset($data[$key]); + } + } } \ No newline at end of file