diff --git a/schema/patches/13.sql b/schema/patches/13.sql new file mode 100644 index 000000000..2c67382c3 --- /dev/null +++ b/schema/patches/13.sql @@ -0,0 +1,26 @@ +create table uryplayer.podcast_category +( + podcast_category_id serial not null, + category_name text not null +); + +create unique index podcast_category_category_name_uindex + on uryplayer.podcast_category (category_name); + +create unique index podcast_category_podcast_category_id_uindex + on uryplayer.podcast_category (podcast_category_id); + +alter table uryplayer.podcast_category + add constraint podcast_category_pk + primary key (podcast_category_id); + +insert into uryplayer.podcast_category (category_name) values ('URY Podcast'), ('Music Team Interview'); + +alter table uryplayer.podcast + add category_id int; + +alter table uryplayer.podcast + add constraint podcast_category__fk + foreign key (category_id) references uryplayer.podcast_category; + +update uryplayer.podcast set category_id=1; diff --git a/src/Classes/ServiceAPI/MyRadio_Podcast.php b/src/Classes/ServiceAPI/MyRadio_Podcast.php index 0d7cdd845..0b5359b9a 100644 --- a/src/Classes/ServiceAPI/MyRadio_Podcast.php +++ b/src/Classes/ServiceAPI/MyRadio_Podcast.php @@ -80,6 +80,13 @@ class MyRadio_Podcast extends MyRadio_Metadata_Common */ private $show_id; + /** + * The category name of the podcast + * + * @var string + */ + private $category; + /** * Construct the API Key Object. * @@ -90,7 +97,7 @@ protected function __construct($podcast_id) $this->podcast_id = (int) $podcast_id; $result = self::$db->fetchOne( - 'SELECT file, memberid, approvedid, submitted, suspended, show_id, ( + 'SELECT file, memberid, approvedid, submitted, suspended, show_id, category_name ( SELECT array_to_json(array( SELECT metadata_key_id FROM uryplayer.podcast_metadata WHERE podcast_id=$1 AND effective_from <= NOW() @@ -127,6 +134,7 @@ protected function __construct($podcast_id) ) AS credits FROM uryplayer.podcast LEFT JOIN schedule.show_podcast_link USING (podcast_id) + INNER JOIN uryplayer.podcast_category ON podcast.category_id=podcast_category.podcast_category_id WHERE podcast_id=$1', [$podcast_id] ); @@ -141,6 +149,7 @@ protected function __construct($podcast_id) $this->submitted = strtotime($result['submitted']); $this->suspended = ($result['suspended'] === 't') ? true : false; $this->show_id = (int) $result['show_id']; + $this->category = $result['category_name']; //Deal with the Credits arrays $credit_types = json_decode($result['credit_types']); @@ -171,6 +180,15 @@ protected function __construct($podcast_id) } } + /** + * Get all podcast categories + */ + public static function getPodcastCategories() { + return self::$db->fetchAll( + 'SELECT podcast_category_id AS value, category_name AS text FROM uryplayer.podcast_category' + ); + } + /** * Get all the Podcasts that the User is Owner of Creditor of. * @@ -248,6 +266,15 @@ public static function getForm() MyRadioFormField::TYPE_BLOCKTEXT, ['label' => 'Description'] ) + )->addField( + new MyRadioFormField( + 'category', + MyRadioFormField::TYPE_SELECT, + [ + 'label' => 'Category', + 'options' => MyRadio_Podcast::getPodcastCategories() + ] + ) )->addField( new MyRadioFormField( 'tags', @@ -485,7 +512,8 @@ public static function create( $tags, $file, MyRadio_Show $show = null, - $credits = null + $credits = null, + $category_id ) { //Validate the tags $tags = CoreUtils::explodeTags($tags); @@ -495,9 +523,9 @@ public static function create( //Get an ID for the new Podcast $id = (int) self::$db->fetchColumn( 'INSERT INTO uryplayer.podcast ' - .'(memberid, approvedid, submitted) VALUES ($1, $1, NULL) ' + .'(memberid, approvedid, submitted, category_id) VALUES ($1, $1, NULL, $2) ' .'RETURNING podcast_id', - [MyRadio_User::getInstance()->getID()] + [MyRadio_User::getInstance()->getID(), $category_id] )[0]; // DANGER WILL ROBINSON DANGER @@ -707,6 +735,17 @@ public function getWebpage() return '/uryplayer/podcasts/'.$this->getID(); } + /** + * Get the podcast category + * + * @return string + */ + + public function getCategory() + { + return $this->category; + } + /** * Set the suspended status of this podcast. * @@ -818,6 +857,7 @@ public function toDataSource($mixins = []) 'status' => $this->getStatus(), 'time' => $this->getSubmitted(), 'uri' => $this->getURI(), + 'category' => $this->getCategory(), 'editlink' => [ 'display' => 'icon', 'value' => 'pencil', @@ -1027,27 +1067,36 @@ public function convert() * @param int $page The page required. * @param bool $include_suspended Whether to include suspended podcasts in the result * @param bool $include_pending Whether to include pending (future publish/processing) podcasts in the result - * + * @param int | null $category_id - the ID number of the category of podcast to filter by + * * @return Array[MyRadio_Podcast] */ public static function getAllPodcasts( $include_suspended = false, $include_pending = false, + $category_id = null, $num_results = 0, $page = 1 ) { $where = ''; - if (!$include_suspended || !$include_pending) { - $where = 'WHERE '; - if (!$include_suspended) { - $where .= 'suspended = false'; - } - if (!$include_suspended && !$include_pending) { - $where .= ' AND '; - } - if (!$include_pending) { - $where .= 'submitted IS NOT NULL'; - } + $filter_strings = []; + + if (!$include_suspended) { + array_push($filter_strings, 'suspended = false'); + } + + if (!$include_pending) { + array_push($filter_strings, 'submitted IS NOT NULL'); + } + + if (!isNull($category_id)) { + array_push($filter_strings, sprintf('category_id = %u', $category_id)); + } + + $where = join(" AND ", $filter_strings); + + if ($where != "") { + $where = 'WHERE ' . $where; } $filterLimit = $num_results == 0 ? 'ALL' : $num_results; diff --git a/src/Controllers/Podcast/editPodcast.php b/src/Controllers/Podcast/editPodcast.php index 9faf81a0e..215af5f0f 100644 --- a/src/Controllers/Podcast/editPodcast.php +++ b/src/Controllers/Podcast/editPodcast.php @@ -25,7 +25,8 @@ $data['tags'], $data['file']['tmp_name'], empty($data['show']) ? null : MyRadio_Show::getInstance($data['show']), - $data['credits'] + $data['credits'], + $data['category'] ); $return_message = "New Podcast Created"; } else {