215 lines
6.1 KiB
PHP
215 lines
6.1 KiB
PHP
<?php
|
|
|
|
class PodcastLimitReachedException extends Exception
|
|
{
|
|
}
|
|
|
|
class InvalidPodcastException extends Exception
|
|
{
|
|
}
|
|
|
|
class PodcastNotFoundException extends Exception
|
|
{
|
|
}
|
|
|
|
|
|
/**
|
|
* Skeleton subclass for representing a row from the 'podcast' table.
|
|
*
|
|
* @package propel.generator.airtime
|
|
*/
|
|
class Podcast extends BasePodcast
|
|
{
|
|
// These fields should never be modified with POST/PUT data
|
|
private static $privateFields = array(
|
|
"id",
|
|
"url",
|
|
"type",
|
|
"owner"
|
|
);
|
|
|
|
/** Creates a Podcast object from the given podcast URL.
|
|
* This is used by our Podcast REST API
|
|
*
|
|
* @param $podcastArray An array containing the URL for a Podcast object.
|
|
*
|
|
* @return array - Podcast Array with a full list of episodes
|
|
* @throws Exception
|
|
* @throws InvalidPodcastException
|
|
* @throws PodcastLimitReachedException
|
|
*/
|
|
|
|
public static function create($data)
|
|
{
|
|
if (Application_Service_PodcastService::podcastLimitReached()) {
|
|
throw new PodcastLimitReachedException();
|
|
}
|
|
|
|
$rss = Application_Service_PodcastService::getPodcastFeed($data["url"]);
|
|
if (!$rss) {
|
|
throw new InvalidPodcastException();
|
|
}
|
|
|
|
// Ensure we are only creating Podcast with the given URL, and excluding
|
|
// any extra data fields that may have been POSTED
|
|
$podcastArray = array();
|
|
$podcastArray["url"] = $data["url"];
|
|
|
|
// Kind of a pain; since the rss fields are SimpleXMLElements,
|
|
// we need to explicitly cast them to strings
|
|
$podcastArray["title"] = (string)$rss->title;
|
|
$podcastArray["creator"] = (string)$rss->author;
|
|
$podcastArray["description"] = (string)$rss->description;
|
|
self::validatePodcastMetadata($podcastArray);
|
|
|
|
try {
|
|
$podcast = new Podcast();
|
|
$podcast->fromArray($podcastArray, BasePeer::TYPE_FIELDNAME);
|
|
$podcast->setDbOwner(self::getOwnerId());
|
|
$podcast->setDbType(IMPORTED_PODCAST);
|
|
$podcast->save();
|
|
|
|
$podcastArray = $podcast->toArray(BasePeer::TYPE_FIELDNAME);
|
|
|
|
$podcastArray["episodes"] = array();
|
|
foreach ($rss->item as $item) {
|
|
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) {
|
|
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();
|
|
}
|
|
|
|
self::removePrivateFields($data);
|
|
self::validatePodcastMetadata($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();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Trims the podcast metadata to fit the table's column max size
|
|
*
|
|
* @param $podcastArray
|
|
* @throws PropelException
|
|
*/
|
|
private static function validatePodcastMetadata(&$podcastArray)
|
|
{
|
|
$podcastTable = PodcastPeer::getTableMap();
|
|
|
|
foreach ($podcastArray as $key => &$value) {
|
|
try {
|
|
// Make sure column exists in table
|
|
$columnMaxSize = $podcastTable->getColumn($key)->getSize();
|
|
} catch (PropelException $e) {
|
|
continue;
|
|
}
|
|
|
|
if (strlen($value) > $columnMaxSize) {
|
|
$value = substr($value, 0, $podcastTable->getColumn($key)->getSize());
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function removePrivateFields(&$data)
|
|
{
|
|
foreach (self::$privateFields as $key) {
|
|
unset($data[$key]);
|
|
}
|
|
}
|
|
|
|
//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());
|
|
}
|
|
}
|
|
}
|