From a4ba776b0f3d23b264939dc2952420e4b9c77f70 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 17 Jul 2012 11:07:16 -0400 Subject: [PATCH] CC-84: Smart Playlists - save playlist type to db - on edit type is loaded --- .../forms/SmartPlaylistCriteria.php | 25 ++++++--- airtime_mvc/application/models/Playlist.php | 37 ++++++++++++-- airtime_mvc/application/models/Scheduler.php | 51 ++++++++++++------- .../airtime/playlist/smart_playlistbuilder.js | 8 +-- 4 files changed, 89 insertions(+), 32 deletions(-) diff --git a/airtime_mvc/application/forms/SmartPlaylistCriteria.php b/airtime_mvc/application/forms/SmartPlaylistCriteria.php index 5a480fafa..26a6356ed 100644 --- a/airtime_mvc/application/forms/SmartPlaylistCriteria.php +++ b/airtime_mvc/application/forms/SmartPlaylistCriteria.php @@ -96,14 +96,25 @@ class Application_Form_SmartPlaylistCriteria extends Zend_Form_SubForm 'criteriasLength' => count($criteriaOptions))) )); + // load type + $c = new Criteria(); + $c->add(CcPlaylistPeer::ID, $p_playlistId); + $out = CcPlaylistPeer::doSelect($c); + + if ($out[0]->getDbType() == "static") { + $playlistType = 0; + } else { + $playlistType = 1; + } + $spType = new Zend_Form_Element_Radio('sp_type'); - $spType->setLabel('Set smart playlist type:'); - $spType->setDecorators(array('viewHelper')); - $spType->setMultiOptions(array( - 'static' => 'Static', - 'dynamic' => 'Dynamic' - )); - $spType->setValue('Static'); + $spType->setLabel('Set smart playlist type:') + ->setDecorators(array('viewHelper')) + ->setMultiOptions(array( + 'static' => 'Static', + 'dynamic' => 'Dynamic' + )) + ->setValue($playlistType); $this->addElement($spType); // load criteria from db diff --git a/airtime_mvc/application/models/Playlist.php b/airtime_mvc/application/models/Playlist.php index 99ab93839..d84f0a50d 100644 --- a/airtime_mvc/application/models/Playlist.php +++ b/airtime_mvc/application/models/Playlist.php @@ -874,6 +874,14 @@ class Application_Model_Playlist { $result = 0; $errors = array(); $error = array(); + + // saving dynamic/static flag + $playlistType = $data['etc']['sp_type'] == 0 ? 'static':'dynamic'; + $playlistCrit = new Criteria(); + $playlistCrit->add(CcPlaylistPeer::ID, $p_playlistId); + $playlistCrit->add(CcPlaylistPeer::TYPE, $playlistType); + CcPlaylistPeer::doUpdate($playlistCrit); + if ($data['etc']['sp_limit_options'] == 'hours') { $multiplier = 60; } @@ -967,13 +975,13 @@ class Application_Model_Playlist { * tracks. * @param array $p_criteria */ - public static function generateSmartPlaylist($p_criteria, $p_playlistId) + public static function generateSmartPlaylist($p_criteria, $p_playlistId, $returnList=false) { $result = self::saveSmartPlaylistCriteria($p_criteria, $p_playlistId); if ($result['result'] != 0) { return $result; } else { - $info = self::getListofFilesMeetCriteria($p_playlistId); + /*$info = self::getListofFilesMeetCriteria($p_playlistId); $files = $info['files']; $limit = $info['limit']; // construct ids of track candidates @@ -989,12 +997,35 @@ class Application_Model_Playlist { if ( !is_null($limit['items']) && $limit['items'] == count($insertList)) { break; } - } + }*/ + $insertList = self::getListOfFilesUnderLimit($p_playlistId); + $playlist = new self($p_playlistId); $playlist->addAudioClips(array_keys($insertList)); return array("result"=>0, "ids"=>array_keys($insertList)); } } + public static function getListOfFilesUnderLimit($p_playlistId) + { + $info = self::getListofFilesMeetCriteria($p_playlistId); + $files = $info['files']; + $limit = $info['limit']; + // construct ids of track candidates + self::deleteAllFilesFromPlaylist($p_playlistId); + $insertList = array(); + $totalTime = 0; + while ($totalTime < $limit['time'] && !empty($files)) { + $key = array_rand($files); + $insertList[$key] = $files[$key]; + $totalTime += $files[$key]; + unset($files[$key]); + if ( !is_null($limit['items']) && $limit['items'] == count($insertList)) { + break; + } + } + return $insertList; + } + // this function return list of propel object private static function getListofFilesMeetCriteria($p_playlistId) { diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index 6e8e21278..27eb4285c 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -148,31 +148,46 @@ class Application_Model_Scheduler { } } else if ($type === "playlist") { - - $contents = CcPlaylistcontentsQuery::create() - ->orderByDbPosition() - ->filterByDbPlaylistId($id) - ->find($this->con); + + // find if the playslit is static or dynamic + $c = new Criteria(); + $c->add(CcPlaylistPeer::ID, $id); + $pl = CcPlaylistPeer::doSelect($c); + $playlistType = $pl->getDbType(); + + if ($playlistType == "static") { + $contents = CcPlaylistcontentsQuery::create() + ->orderByDbPosition() + ->filterByDbPlaylistId($id) + ->find($this->con); + } else { + $contents = Application_Model_Playlist::getListOfFilesUnderLimit($id); + } if (is_null($contents)) { throw new Exception("A selected Playlist does not exist!"); } foreach ($contents as $plItem) { - - $file = $plItem->getCcFiles($this->con); - if (isset($file) && $file->getDbFileExists()) { - - $data = $this->fileInfo; - $data["id"] = $plItem->getDbFileId(); - $data["cliplength"] = $plItem->getDbCliplength(); - $data["cuein"] = $plItem->getDbCuein(); - $data["cueout"] = $plItem->getDbCueout(); - $data["fadein"] = $plItem->getDbFadein(); - $data["fadeout"] = $plItem->getDbFadeout(); - - $files[] = $data; + $data = $this->fileInfo; + if ($playlistType == "static"){ + $file = $plItem->getCcFiles($this->con); + if (isset($file) && $file->getDbFileExists()) { + $data["id"] = $plItem->getDbFileId(); + $data["cliplength"] = $plItem->getDbCliplength(); + $data["cuein"] = $plItem->getDbCuein(); + $data["cueout"] = $plItem->getDbCueout(); + $data["fadein"] = $plItem->getDbFadein(); + $data["fadeout"] = $plItem->getDbFadeout(); + } + } else { + // on dynamic playslsit, $plItem is id of files + $file = Application_Model_StoredFile::Recall($plItem); + if (isset($file) && $file->getDbFileExists()) { + $data["id"] = $plItem; + } } + $files[] = $data; } } diff --git a/airtime_mvc/public/js/airtime/playlist/smart_playlistbuilder.js b/airtime_mvc/public/js/airtime/playlist/smart_playlistbuilder.js index fe4a0b89b..482c63568 100644 --- a/airtime_mvc/public/js/airtime/playlist/smart_playlistbuilder.js +++ b/airtime_mvc/public/js/airtime/playlist/smart_playlistbuilder.js @@ -1,7 +1,7 @@ $(document).ready(function() { setSmartPlaylistEvents(); - var form = $('#smart-playlist-form'); - appendAddButton(form); + /*var form = $('#smart-playlist-form'); + appendAddButton(form);*/ }); function setSmartPlaylistEvents() { @@ -158,7 +158,8 @@ function setSmartPlaylistEvents() { disableAndHideExtraField(criteria_value, index_num); } }); - + + appendAddButton(form); } function enableAndShowExtraField(valEle, index) { @@ -222,7 +223,6 @@ function staticCallback(data) { }); } else { AIRTIME.playlist.fnOpenPlaylist(json); - var form = $('#smart-playlist-form'); form.find('.success').text('Smart playlist generated'); form.find('.success').show(); form.find('#smart_playlist_options').removeClass("closed");