From a5ad30356a09992b6410afb11bdb95b9867b8059 Mon Sep 17 00:00:00 2001 From: mkonecny Date: Wed, 2 Feb 2011 18:25:42 -0500 Subject: [PATCH] -refactoring -added gaps in Now PLaying DataTable -show information and song information now sent as separate data in order. --- application/models/Nowplaying.php | 77 ++++++++++++++++++++++++-- application/models/Schedule.php | 89 ++++++++++++++++++++++++++----- public/js/playlist/playlist.js | 72 ++++++++++++++++--------- 3 files changed, 196 insertions(+), 42 deletions(-) diff --git a/application/models/Nowplaying.php b/application/models/Nowplaying.php index c5cd4ba9a..dc4c4e7aa 100644 --- a/application/models/Nowplaying.php +++ b/application/models/Nowplaying.php @@ -3,6 +3,45 @@ class Application_Model_Nowplaying { + public static function InsertBlankRow($i, $rows){ + $startDate = explode(".", $rows[$i-1][3]); + $endDate = explode(".", $rows[$i][2]); + + $epochStartMS = strtotime($startDate[0])*1000; + $epochEndMS = strtotime($endDate[0])*1000; + + if (count($startDate) > 1) + $epochStartMS += $startDate[1]; + if (count($endDate) > 1) + $epochEndMS += $endDate[1]; + + $blankRow = array(array("b", "-", "-", "-", TimeDateHelper::ConvertMSToHHMMSSmm($epochEndMS - $epochStartMS), "-", "-", "-", "-" , "-", "", "")); + array_splice($rows, $i, 0, $blankRow); + return $rows; + } + + public static function FindGaps($rows){ + $n = count($rows); + + $blankRowIndices = array(); + $arrayIndexOffset = 0; + + if ($n < 2) + return; + + for ($i=1; $i<$n; $i++){ + if ($rows[$i-1][3] != $rows[$i][2]) + array_push($blankRowIndices, $i); + } + + for ($i=0, $n=count($blankRowIndices); $i<$n; $i++){ + $rows = Application_Model_Nowplaying::InsertBlankRow($blankRowIndices[$i]+$arrayIndexOffset, $rows); + $arrayIndexOffset++; + } + + return $rows; + } + public static function GetDataGridData(){ $columnHeaders = array(array("sTitle"=>"type", "bVisible"=>false), @@ -30,12 +69,12 @@ class Application_Model_Nowplaying } } - $previous = Schedule::GetPreviousItems($timeNow, 1); - $current = Schedule::GetCurrentlyPlaying($timeNow); - $next = Schedule::GetNextItems($timeNow, 10); + $previous = Schedule::Get_Scheduled_Item_Data($timeNow, -1, 1, "60 seconds"); + $current = Schedule::Get_Scheduled_Item_Data($timeNow, 0); + $next = Schedule::Get_Scheduled_Item_Data($timeNow, 1, 10, "48 hours"); $rows = array(); - + foreach ($previous as $item){ $color = (count($currentShow) > 0) && in_array($item["group_id"], $groupIDs) ? "x" : ""; array_push($rows, array("p", $item["starts"], $item["starts"], $item["ends"], $item["clip_length"], $item["track_title"], $item["artist_name"], @@ -52,10 +91,38 @@ class Application_Model_Nowplaying array_push($rows, array("n", $item["starts"], $item["starts"], $item["ends"], $item["clip_length"], $item["track_title"], $item["artist_name"], $item["album_title"], "x" , $item["name"], $color, $item["group_id"])); } + + $rows = Application_Model_Nowplaying::FindGaps($rows); $data = array("columnHeaders"=>$columnHeaders, "rows"=>$rows); return $data; } - } +class TimeDateHelper +{ + + public static function ConvertMSToHHMMSSmm($time){ + $hours = floor($time / 3600000); + $time -= 3600000*$hours; + + $minutes = floor($time / 60000); + $time -= 60000*$minutes; + + $seconds = floor($time / 1000); + $time -= 1000*$seconds; + + $ms = $time; + + if (strlen($hours) == 1) + $hours = "0".$hours; + if (strlen($minutes) == 1) + $minutes = "0".$minutes; + if (strlen($seconds) == 1) + $seconds = "0".$seconds; + + return $hours.":".$minutes.":".$seconds.".".$ms; + } +} + + diff --git a/application/models/Schedule.php b/application/models/Schedule.php index c519d74b3..b8be30c16 100644 --- a/application/models/Schedule.php +++ b/application/models/Schedule.php @@ -457,9 +457,7 @@ class Schedule { } /** - * Returns current playlist. - * - * Note: Total playlist length is prev + next + 1 + * Returns data related to the scheduled items. * * @param int $prev * @param int $next @@ -473,20 +471,71 @@ class Schedule { $timeNow = Schedule::GetSchedulerTime(); return array("schedulerTime"=>gmdate("Y-m-d H:i:s"), - "previous"=>Schedule::GetPreviousItems($timeNow), - "current"=>Schedule::GetCurrentlyPlaying($timeNow), - "next"=>Schedule::GetNextItems($timeNow), - "showStartEndTime"=>Schedule::GetCurrentShow($timeNow), + "previous"=>Schedule::Get_Scheduled_Item_Data($timeNow, -1, $prev, "24 hours"), + "current"=>Schedule::Get_Scheduled_Item_Data($timeNow, 0), + "next"=>Schedule::Get_Scheduled_Item_Data($timeNow, 1, $next, "48 hours"), + "currentShow"=>Schedule::GetCurrentShow($timeNow), + "nextShow"=>Schedule::GetNextShow($timeNow), "timezone"=> date("T"), "timezoneOffset"=> date("Z")); } + + /** + * Builds an SQL Query for accessing scheduled item information from + * the database. + * + * @param int $timeNow + * @param int $timePeriod + * @param int $count + * @param String $interval + * @return date + * + * $timeNow is the the currentTime in the format "Y-m-d H:i:s". + * For example: 2011-02-02 22:00:54 + * + * $timePeriod can be either negative, zero or positive. This is used + * to indicate whether we want items from the past, present or future. + * + * $count indicates how many results we want to limit ourselves to. + * + * $interval is used to indicate how far into the past or future we + * want to search the database. For example "5 days", "18 hours", "60 minutes", + * "30 seconds" etc. + */ + public static function Get_Scheduled_Item_Data($timeNow, $timePeriod=0, $count = 0, $interval="0 hours"){ + global $CC_CONFIG, $CC_DBC; + $sql = "SELECT pt.name, ft.track_title, ft.artist_name, ft.album_title, st.starts, st.ends, st.clip_length, st.group_id" + ." FROM $CC_CONFIG[scheduleTable] st, $CC_CONFIG[filesTable] ft, $CC_CONFIG[playListTable] pt" + ." WHERE st.playlist_id = pt.id" + ." AND st.file_id = ft.id"; + + if ($timePeriod < 0){ + $sql .= " AND st.ends < TIMESTAMP '$timeNow'" + ." AND st.ends > (TIMESTAMP '$timeNow' - INTERVAL '$interval')" + ." ORDER BY st.starts DESC" + ." LIMIT $count"; + } else if ($timePeriod == 0){ + $sql .= " AND st.starts < TIMESTAMP '$timeNow'" + ." AND st.ends > TIMESTAMP '$timeNow'"; + } else if ($timePeriod > 0){ + $sql .= " AND st.starts > TIMESTAMP '$timeNow'" + ." AND st.starts < (TIMESTAMP '$timeNow' + INTERVAL '$interval')" + ." ORDER BY st.starts" + ." LIMIT $count"; + } + + $rows = $CC_DBC->GetAll($sql); + return $rows; + } + + /* - public static function GetPreviousItems($timeNow, $prevCount = 1){ + public static function GetPreviousItems($timeNow, $prevCount = 1, $prevInterval="24 hours"){ global $CC_CONFIG, $CC_DBC; $sql = "SELECT pt.name, ft.track_title, ft.artist_name, ft.album_title, st.starts, st.ends, st.clip_length, st.group_id" ." FROM $CC_CONFIG[scheduleTable] st, $CC_CONFIG[filesTable] ft, $CC_CONFIG[playListTable] pt" ." WHERE st.ends < TIMESTAMP '$timeNow'" - ." AND st.ends > (TIMESTAMP '$timeNow' - INTERVAL '24 hours')" + ." AND st.ends > (TIMESTAMP '$timeNow' - INTERVAL '$prevInterval')" ." AND st.playlist_id = pt.id" ." AND st.file_id = ft.id" ." ORDER BY st.starts DESC" @@ -514,7 +563,7 @@ class Schedule { $sql = "SELECT pt.name, ft.track_title, ft.artist_name, ft.album_title, st.starts, st.ends, st.clip_length, st.group_id" ." FROM $CC_CONFIG[scheduleTable] st, $CC_CONFIG[filesTable] ft, $CC_CONFIG[playListTable] pt" ." WHERE st.starts > TIMESTAMP '$timeNow'" - ." AND st.ends < (TIMESTAMP '$timeNow' + INTERVAL '24 hours')" + ." AND st.ends < (TIMESTAMP '$timeNow' + INTERVAL '7 days')" ." AND st.playlist_id = pt.id" ." AND st.file_id = ft.id" ." ORDER BY st.starts" @@ -522,11 +571,11 @@ class Schedule { $rows = $CC_DBC->GetAll($sql); return $rows; } - +*/ public static function GetCurrentShow($timeNow) { global $CC_CONFIG, $CC_DBC; - $timestamp = preg_split("/ /", $timeNow); + $timestamp = explode(" ", $timeNow); $date = $timestamp[0]; $time = $timestamp[1]; @@ -542,6 +591,22 @@ class Schedule { return $rows; } + public static function GetNextShow($timeNow) { + global $CC_CONFIG, $CC_DBC; + + $sql = "SELECT current_date + sd.start_time as start_timestamp, current_date + sd.end_time as end_timestamp, s.name, s.id" + ." FROM $CC_CONFIG[showDays] sd, $CC_CONFIG[showTable] s" + ." WHERE sd.show_id = s.id" + ." AND (sd.first_show + sd.start_time) >= TIMESTAMP '$timeNow'" + ." ORDER BY (sd.first_show + sd.start_time)" + ." LIMIT 1"; + + $rows = $CC_DBC->GetAll($sql); + return $rows; + } + + + public static function GetCurrentShowGroupIDs($showID){ global $CC_CONFIG, $CC_DBC; diff --git a/public/js/playlist/playlist.js b/public/js/playlist/playlist.js index fe64673b1..84e18524b 100644 --- a/public/js/playlist/playlist.js +++ b/public/js/playlist/playlist.js @@ -5,6 +5,9 @@ var previousSongs = new Array(); var currentSong = new Array(); var nextSongs = new Array(); +var currentShow = new Array(); +var nextShow = new Array(); + var currentElem; var serverUpdateInterval = 5000; @@ -12,15 +15,16 @@ var uiUpdateInterval = 200; var songEndFunc; -var showStartPosixTime = 0; -var showEndPosixTime = 0; -var showLengthMs = 1; -var currentShowName = ""; +//var showStartPosixTime = 0; +//var showEndPosixTime = 0; +//var showLengthMs = 1; +//var currentShowName = ""; /* boolean flag to let us know if we should prepare to execute a function * that flips the playlist to the next song. This flags purpose is to * make sure the function is only executed once*/ var nextSongPrepare = true; +var nextShowPrepare = true; /* Another script can register its function here * when it wishes to know when a song ends. */ @@ -61,21 +65,26 @@ function secondsTimer(){ function newSongStart(){ nextSongPrepare = true; currentSong[0] = nextSongs.shift(); - //updateGlobalValues(currentSong[0]); updatePlaybar(); notifySongEndListener(); } +function nextShowStart(){ + nextShowPrepare = true; + currentShow[0] = nextShow.shift(); + updatePlaybar(); + + //notifySongEndListener(); +} + /* Called every "uiUpdateInterval" mseconds. */ function updateProgressBarValue(){ - if (showStartPosixTime != 0){ - var showPercentDone = (estimatedSchedulePosixTime - showStartPosixTime)/showLengthMs*100; + if (currentShow.length > 0){ + var showPercentDone = (estimatedSchedulePosixTime - currentShow[0].showStartPosixTime)/currentShow[0].showLengthMs*100; if (showPercentDone < 0 || showPercentDone > 100){ showPercentDone = 0; - $('#on-air-info').attr("class", "on-air-info off"); - } else { - $('#on-air-info').attr("class", "on-air-info on"); + currentShow = new Array(); } $('#progress-show').attr("style", "width:"+showPercentDone+"%"); } @@ -86,8 +95,11 @@ function updateProgressBarValue(){ if (songPercentDone < 0 || songPercentDone > 100){ songPercentDone = 0; currentSong = new Array(); + } else { + $('#on-air-info').attr("class", "on-air-info on"); } - } + } else + $('#on-air-info').attr("class", "on-air-info off"); $('#progress-bar').attr("style", "width:"+songPercentDone+"%"); //calculate how much time left to next song if there is any @@ -97,6 +109,14 @@ function updateProgressBarValue(){ setTimeout(newSongStart, nextSongs[0].songStartPosixTime - estimatedSchedulePosixTime); } } + + //calculate how much time left to next show if there is any + if (nextShow.length > 0 && nextShowPrepare){ + if (nextShow[0].showStartPosixTime - estimatedSchedulePosixTime < serverUpdateInterval){ + nextShowPrepare = false; + setTimeout(nextShowStart, nextShow[0].showStartPosixTime - estimatedSchedulePosixTime); + } + } updatePlaybar(); } @@ -141,33 +161,31 @@ function updatePlaybar(){ /* Column 1 update */ $('#playlist').text("Current Show:"); - $('#playlist').text(currentShowName); + if (currentShow.length > 0) + $('#playlist').text(currentShow[0].name); $('#show-length').empty(); - if (estimatedSchedulePosixTime < showEndPosixTime){ - $('#show-length').text(convertDateToHHMMSS(showStartPosixTime) + " - " + convertDateToHHMMSS(showEndPosixTime)); + if (currentShow.length > 0){ + $('#show-length').text(convertDateToHHMMSS(currentShow[0].showStartPosixTime) + " - " + convertDateToHHMMSS(currentShow[0].showEndPosixTime)); } /* Column 2 update */ $('#time').text(convertDateToHHMMSS(estimatedSchedulePosixTime)); } -function calcAdditionalData(currentItem, bUpdateGlobalValues){ +function calcAdditionalData(currentItem){ for (var i=0; i 0){ - showStartPosixTime = convertDateToPosixTime(obj.showStartEndTime[0].start_timestamp); - showEndPosixTime = convertDateToPosixTime(obj.showStartEndTime[0].end_timestamp); - showLengthMs = showEndPosixTime - showStartPosixTime; - currentShowName = obj.showStartEndTime[0].name; +function calcAdditionalShowData(show){ + if (show.length > 0){ + show[0].showStartPosixTime = convertDateToPosixTime(show[0].start_timestamp); + show[0].showEndPosixTime = convertDateToPosixTime(show[0].end_timestamp); + show[0].showLengthMs = show[0].showEndPosixTime - show[0].showStartPosixTime; } } @@ -181,11 +199,15 @@ function parseItems(obj){ currentSong = obj.current; nextSongs = obj.next; - updateGlobalValues(obj); - calcAdditionalData(previousSongs); calcAdditionalData(currentSong); calcAdditionalData(nextSongs); + + currentShow = obj.currentShow; + nextShow = obj.nextShow; + + calcAdditionalShowData(obj.currentShow); + calcAdditionalShowData(obj.nextShow); if (localRemoteTimeOffset == null){ var date = new Date();