setDbUrl($podcastArray["url"]); $podcast->setDbTitle((string)$rss->title); $podcast->setDbCreator((string)$rss->author); $podcast->setDbDescription((string)$rss->description); $podcast->setDbOwner(self::getOwnerId()); $podcast->setDbType(IMPORTED_PODCAST); $podcast->save(); // $podcastArray = array(); // array_push($podcastArray, $podcast->toArray(BasePeer::TYPE_FIELDNAME)); $podcastArray = $podcast->toArray(BasePeer::TYPE_FIELDNAME); $podcastArray["episodes"] = array(); foreach ($rss->item as $item) { // Same as above, we need to explicitly cast the SimpleXMLElement 'array' into an actual array foreach($item as $k => $v) { $array[$k] = (string)$v; } array_push($podcastArray["episodes"], $item); } return $podcastArray; } catch(Exception $e) { $podcast->delete(); throw $e; } } /** * Fetches a Podcast's rss feed and returns all its episodes with * the Podcast object * * @param $podcastId * * @throws PodcastNotFoundException * @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(); } $rss = Application_Service_PodcastService::getPodcastFeed($podcast->getDbUrl()); if (!$rss) { throw new PodcastNotFoundException(); } // FIXME: Get rid of this duplication and move into a new function (serializer/deserializer) $podcastArray = $podcast->toArray(BasePeer::TYPE_FIELDNAME); $podcastArray["episodes"] = array(); foreach ($rss->item as $item) { // Same as above, we need to explicitly cast the SimpleXMLElement 'array' into an actual array foreach($item as $k => $v) { $array[$k] = (string)$v; } array_push($podcastArray["episodes"], $item); } return $podcastArray; } /** * Updates a Podcast object with the given metadata * * @param $podcastId * @param $data * @return array * @throws Exception * @throws PodcastNotFoundException */ public static function updateFromArray($podcastId, $data) { $podcast = PodcastQuery::create()->findPk($podcastId); if (!$podcast) { throw new PodcastNotFoundException(); } $data = self::removePrivateFields($data); $podcast->fromArray($data, BasePeer::TYPE_FIELDNAME); $podcast->save(); return $podcast->toArray(BasePeer::TYPE_FIELDNAME); } /** * Deletes a Podcast and its podcast episodes * * @param $podcastId * @throws Exception * @throws PodcastNotFoundException */ public static function deleteById($podcastId) { $podcast = PodcastQuery::create()->findPk($podcastId); if ($podcast) { $podcast->delete(); } else { throw new PodcastNotFoundException(); } } private static function removePrivateFields($data) { foreach (self::$privateFields as $key) { unset($data[$key]); } return $data; } //TODO move this somewhere where it makes sense private static function getOwnerId() { try { if (Zend_Auth::getInstance()->hasIdentity()) { $service_user = new Application_Service_UserService(); return $service_user->getCurrentUser()->getDbId(); } else { $defaultOwner = CcSubjsQuery::create() ->filterByDbType('A') ->orderByDbId() ->findOne(); if (!$defaultOwner) { // what to do if there is no admin user? // should we handle this case? return null; } return $defaultOwner->getDbId(); } } catch(Exception $e) { Logging::info($e->getMessage()); } } }