-improved code for shows grouping in now playing view

This commit is contained in:
martin 2011-03-05 12:28:48 -05:00
parent cb119802df
commit 93874af2b8
3 changed files with 100 additions and 131 deletions

View File

@ -2,92 +2,90 @@
class Application_Model_Nowplaying class Application_Model_Nowplaying
{ {
/*
public static function InsertBlankRow($i, $rows){
$startDateFull = $rows[$i-1][3];
$endDateFull = $rows[$i][2];
$startDate = explode(".", $startDateFull); public static function FindBeginningOfShow($rows){
$endDate = explode(".", $endDateFull); $numRows = count($rows);
$epochStartMS = strtotime($startDate[0])*1000; $newCopy = array();
$epochEndMS = strtotime($endDate[0])*1000;
if (count($startDate) > 1) for ($i=0; $i<$numRows; $i++){
$epochStartMS += $startDate[1]; $currentRow = $rows[$i];
if (count($endDate) > 1) if ($i == 0 || ($i != 0 && $currentRow['instance_id'] != $rows[$i-1]['instance_id'])){
$epochEndMS += $endDate[1]; //$currentRow is first instance of a show.
$group = $currentRow;
$group['group'] = 'x';
$group['item_starts'] = $group['show_starts'];
$group['item_ends'] = $group['show_ends'];
$blankRow = array(array("b", $startDateFull, $startDateFull, $endDate, Application_Model_DateHelper::ConvertMSToHHMMSSmm($epochEndMS - $epochStartMS), "-", "-", "-", "-" , "-", "", "")); array_push($newCopy, $group);
array_splice($rows, $i, 0, $blankRow); }
return $rows; array_push($newCopy, $currentRow);
}
public static function FindGaps($rows){
$n = count($rows);
$blankRowIndices = array();
$arrayIndexOffset = 0;
if ($n < 2)
return $rows;
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++){ return $newCopy;
$rows = Application_Model_Nowplaying::InsertBlankRow($blankRowIndices[$i]+$arrayIndexOffset, $rows);
$arrayIndexOffset++;
}
return $rows;
} }
*/
public static function FindGapAtEndOfShow($rows){
/* $numRows = count($rows);
public static function FindGapsBetweenShows($showsMap){
$previousShow = null; $newCopy = array();
foreach($showsMap as $k => $show){
$currentShow = $showsMap[$k];
if (!is_null($previousShow)){ for ($i=0; $i<$numRows; $i++){
$diff = strtotime($currentShow['starts']) - strtotime($previousShow['ends']) $currentRow = $rows[$i];
if ($$diff != 0){ array_push($newCopy, $currentRow);
//array_splice($showsMap, $i, 0, $blankRow); if ($i+1 == $numRows || ($i+1 !=$numRows && $currentRow['instance_id'] != $rows[$i+1]['instance_id'])){
//$row is the last instance in the show.
if ($currentRow['item_ends'] == ""){
//show is empty and has no scheduled items in it. Therefore
//the gap is the entire length of the show.
$currentRow['item_ends'] = $currentRow['show_starts'];
}
$diff = strtotime($currentRow['show_ends']) - strtotime($currentRow['item_ends']);
if ($diff > 0){
//gap at the end of show. Lets create a "gap" row
$gap = $currentRow;
$gap['gap'] = '';
$gap['item_starts'] = $diff;
array_push($newCopy, $gap);
} }
} }
$previousShow = $showsMap[$k];
} }
return $newCopy;
return $showsMap;
} }
*/
public static function FindGapAtEndOfShow($show, $rows){
$showStartTime = $show['starts'];
$showEndTime = $show['ends'];
if (count($rows) > 1){ public static function FilterRowsByDate($rows, $date, $startCutoff, $endCutoff){
$lastItem = $rows[count($rows)-1]; $dateNow = new Application_Model_DateHelper;
$lastItemEndTime = $lastItem['ends']; $timeNow = $dateNow->getDate();
} else {
$lastItemEndTime = $showStartTime; $data = array();
//iterate over each show, and calculate information for it.
$numItems = count($rows);
for ($i=0; $i<$numItems; $i++){
$item = $rows[$i];
if ((strtotime($item['item_ends']) > $date->getEpochTime() - $startCutoff
&& strtotime($item['item_starts']) < $date->getEpochTime() + $endCutoff) || array_key_exists("group", $item) || array_key_exists("gap", $item)){
if (array_key_exists("group", $item)){
$type = "g";
} else if (array_key_exists("gap", $item)){
$type = "b";
} else if (strtotime($item['item_ends']) < strtotime($timeNow)){
$type = "p";
} else if (strtotime($item['item_starts']) < strtotime($timeNow) && strtotime($timeNow) < strtotime($item['item_ends'])){
$type = "c";
} else {
$type = "n";
}
array_push($data, array($type, $item["item_starts"], $item["item_starts"], $item["item_ends"], $item["clip_length"], $item["track_title"], $item["artist_name"], $item["album_title"], $item["playlist_name"], $item["show_name"], $item["instance_id"]));
}
} }
$diff = Application_Model_DateHelper::TimeDiff($lastItemEndTime, $showEndTime); return $data;
if ($diff <= 0){
//ok!
return null;
} else {
//There is a gap at the end of the show. Return blank row
return array("b", $diff, "-", "-", "-", "-", "-", "-", "-", "-", "-", "-");
}
} }
public static function GetDataGridData($viewType, $dateString){ public static function GetDataGridData($viewType, $dateString){
@ -105,44 +103,19 @@ class Application_Model_Nowplaying
$timeNow = $date->getDate(); $timeNow = $date->getDate();
$startCutoff = $date->getNowDayStartDiff(); $startCutoff = $date->getNowDayStartDiff();
$endCutoff = $date->getNowDayEndDiff(); $endCutoff = $date->getNowDayEndDiff();
} }
$showsMap = Show_DAL::GetShowsInRange($timeNow, $startCutoff, $endCutoff); $rows = Show_DAL::GetShowsInRange($timeNow, $startCutoff, $endCutoff);
$rows = Application_Model_Nowplaying::FindBeginningOfShow($rows);
$rows = Application_Model_Nowplaying::FindGapAtEndOfShow($rows);
//$rows = FindGapsBetweenShows
$data = Application_Model_Nowplaying::FilterRowsByDate($rows, $date, $startCutoff, $endCutoff);
//iterate over each show, and calculate information for it.
foreach($showsMap as $k => $show){
$rows = Schedule::GetShowInstanceItems($k);
$gapRow = Application_Model_Nowplaying::FindGapAtEndOfShow($showsMap[$k], $rows);
foreach ($rows as $item){
//check if this item is in todays date range
if (strtotime($item['ends']) > $date->getEpochTime() - $startCutoff
&& strtotime($item['starts']) < $date->getEpochTime() + $endCutoff){
if ($item['ends'] < $timeNow){
$type = "p";
} else if ($item['starts'] < $timeNow && $timeNow < $item['ends']){
$type = "c";
} else {
$type = "n";
}
array_push($showsMap[$k]['items'], array($type, $item["starts"], $item["starts"], $item["ends"], $item["clip_length"], $item["track_title"], $item["artist_name"],
$item["album_title"], $item["name"], $item["show_name"], $item["instance_id"], $item["group_id"]));
}
}
if (!is_null($gapRow))
array_push($showsMap[$k]['items'], $gapRow);
}
//$showsMap = Application_Model_Nowplaying::FindGapsBetweenShows($showsMap);
$date = new Application_Model_DateHelper; $date = new Application_Model_DateHelper;
$timeNow = $date->getDate(); $timeNow = $date->getDate();
return array("currentShow"=>Show_DAL::GetCurrentShow($timeNow), "rows"=>$data);
return array("currentShow"=>Show_DAL::GetCurrentShow($timeNow), "rows"=>$showsMap);
} }
} }

View File

@ -760,32 +760,33 @@ class Show_DAL{
public static function GetShowsInRange($timeNow, $start, $end){ public static function GetShowsInRange($timeNow, $start, $end){
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
$sql = "SELECT *," $sql = "SELECT"
." si.starts as start_timestamp," ." si.starts as show_starts,"
." si.ends as end_timestamp," ." si.ends as show_ends,"
." si.id as instance_id" ." st.starts as item_starts,"
." FROM " ." st.ends as item_ends,"
." $CC_CONFIG[showInstances] si," ." st.clip_length as clip_length,"
." $CC_CONFIG[showTable] s" ." ft.track_title as track_title,"
." WHERE si.show_id = s.id" ." ft.artist_name as artist_name,"
." AND ((si.starts < TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends > TIMESTAMP '$timeNow' - INTERVAL '$start seconds')" ." ft.album_title as album_title,"
." s.name as show_name,"
." si.id as instance_id,"
." pt.name as playlist_name"
." FROM $CC_CONFIG[showInstances] si"
." LEFT JOIN $CC_CONFIG[scheduleTable] st"
." ON st.instance_id = si.id"
." LEFT JOIN $CC_CONFIG[playListTable] pt"
." ON st.playlist_id = pt.id"
." LEFT JOIN $CC_CONFIG[filesTable] ft"
." ON st.file_id = ft.id"
." LEFT JOIN $CC_CONFIG[showTable] s"
." ON si.show_id = s.id"
." WHERE ((si.starts < TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends > TIMESTAMP '$timeNow' - INTERVAL '$start seconds')"
." OR (si.starts > TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '$end seconds')" ." OR (si.starts > TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '$end seconds')"
." OR (si.starts < TIMESTAMP '$timeNow' + INTERVAL '$end seconds' AND si.ends > TIMESTAMP '$timeNow' + INTERVAL '$end seconds'))" ." OR (si.starts < TIMESTAMP '$timeNow' + INTERVAL '$end seconds' AND si.ends > TIMESTAMP '$timeNow' + INTERVAL '$end seconds'))"
." ORDER BY si.starts"; ." ORDER BY st.starts";
$rows = $CC_DBC->GetAll($sql); return $CC_DBC->GetAll($sql);
$showsMap = array();
$rowsCount = count($rows);
for ($i=0; $i<$rowsCount; $i++){
$rows[$i]['items'] = array();
array_push($rows[$i]['items'],
array("s", $rows[$i]["starts"], $rows[$i]["starts"], $rows[$i]["ends"], "", "", "", "", "", $rows[$i]["name"], $rows[$i]["instance_id"], ""));
$showsMap[$rows[$i]['instance_id']] = $rows[$i];
}
return $showsMap;
} }
} }

View File

@ -60,8 +60,7 @@ var columns = [{"sTitle": "type", "bVisible":false},
{"sTitle":"Album"}, {"sTitle":"Album"},
{"sTitle":"Playlist"}, {"sTitle":"Playlist"},
{"sTitle":"Show"}, {"sTitle":"Show"},
{"sTitle":"instance_id", "bVisible":true}, {"sTitle":"instance_id", "bVisible":true}];
{"sTitle":"group_id", "bVisible":false}];
function getDateString(){ function getDateString(){
var date0 = $("#datepicker").datepicker("getDate"); var date0 = $("#datepicker").datepicker("getDate");
@ -85,9 +84,7 @@ function updateDataTable(){
//function can be called before ajax call has been returned. //function can be called before ajax call has been returned.
if (datagridData != null){ if (datagridData != null){
table.fnClearTable(false); table.fnClearTable(false);
for (var show in datagridData.rows){ table.fnAddData(datagridData.rows, false);
table.fnAddData(datagridData.rows[show].items, false);
}
table.fnDraw(true); table.fnDraw(true);
} }
} }
@ -131,8 +128,6 @@ function createDataGrid(){
$(nRow).addClass("playing-list"); $(nRow).addClass("playing-list");
if (aData[0] == "c") if (aData[0] == "c")
$(nRow).attr("class", "playing-song"); $(nRow).attr("class", "playing-song");
else if (aData[0] == "b")
$(nRow).attr("class", "gap");
return nRow; return nRow;
}, },
"fnDrawCallback": function(oSettings){ "fnDrawCallback": function(oSettings){
@ -148,7 +143,7 @@ function createDataGrid(){
var iDisplayIndex = oSettings._iDisplayStart + i; var iDisplayIndex = oSettings._iDisplayStart + i;
var sType = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[0]; var sType = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[0];
if ( sType == "s" ){ if ( sType == "g" ){
var showName = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[9]; var showName = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[9];
var startTime = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[2]; var startTime = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[2];
var endTime = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[3]; var endTime = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex]]._aData[3];