CC-1805: Ability to edit a show

-All cases for "When" tab should be covered for editing shows.
This commit is contained in:
martin 2011-04-12 11:54:24 -04:00
parent 64124175c5
commit ea11e0114a

View file

@ -2,11 +2,11 @@
class Show { class Show {
private $_showId; private $_showId;
public function __construct($showId=NULL) public function __construct($showId=NULL)
{ {
$this->_showId = $showId; $this->_showId = $showId;
} }
public function getName() public function getName()
@ -93,7 +93,7 @@ class Show {
$sql = "SELECT first_name, last_name $sql = "SELECT first_name, last_name
FROM cc_show_hosts LEFT JOIN cc_subjs ON cc_show_hosts.subjs_id = cc_subjs.id FROM cc_show_hosts LEFT JOIN cc_subjs ON cc_show_hosts.subjs_id = cc_subjs.id
WHERE show_id = {$this->_showId}"; WHERE show_id = {$this->_showId}";
$hosts = $CC_DBC->GetAll($sql); $hosts = $CC_DBC->GetAll($sql);
@ -116,7 +116,7 @@ class Show {
->update(array('DbLastShow' => $timeinfo[0])); ->update(array('DbLastShow' => $timeinfo[0]));
$sql = "DELETE FROM cc_show_instances $sql = "DELETE FROM cc_show_instances
WHERE starts >= '{$day_timestamp}' AND show_id = {$this->_showId}"; WHERE starts >= '{$day_timestamp}' AND show_id = {$this->_showId}";
$CC_DBC->query($sql); $CC_DBC->query($sql);
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
@ -138,6 +138,7 @@ class Show {
$showId = $this->getId(); $showId = $this->getId();
$sql = "DELETE FROM cc_show_instances" $sql = "DELETE FROM cc_show_instances"
." WHERE EXTRACT(DOW FROM starts) IN ($uncheckedDaysImploded)" ." WHERE EXTRACT(DOW FROM starts) IN ($uncheckedDaysImploded)"
." AND starts > current_timestamp "
." AND show_id = $showId"; ." AND show_id = $showId";
$CC_DBC->query($sql); $CC_DBC->query($sql);
@ -251,11 +252,24 @@ class Show {
$showId = $this->getId(); $showId = $this->getId();
$sql = "DELETE FROM cc_show_instances " $sql = "DELETE FROM cc_show_instances "
."WHERE date(starts) > DATE '$p_date' " ."WHERE date(starts) > DATE '$p_date' "
."AND starts > current_timestamp "
."AND show_id = $showId"; ."AND show_id = $showId";
$CC_DBC->query($sql); $CC_DBC->query($sql);
} }
public function removeAllInstancesBeforeDate($p_date){
global $CC_DBC;
$showId = $this->getId();
$sql = "DELETE FROM cc_show_instances "
."WHERE date(starts) < DATE '$p_date' "
."AND starts > current_timestamp "
."AND show_id = $showId";
$CC_DBC->query($sql);
}
public function getStartDate(){ public function getStartDate(){
global $CC_DBC; global $CC_DBC;
@ -288,12 +302,13 @@ class Show {
} }
} }
public function getAllInstanceIds(){ public function getAllFutureInstanceIds(){
global $CC_DBC; global $CC_DBC;
$showId = $this->getId(); $showId = $this->getId();
$sql = "SELECT id from cc_show_instances " $sql = "SELECT id from cc_show_instances "
." WHERE show_id = $showId"; ." WHERE show_id = $showId"
." AND starts > current_timestamp";
$rows = $CC_DBC->GetAll($sql); $rows = $CC_DBC->GetAll($sql);
@ -316,7 +331,8 @@ class Show {
$sql = "UPDATE cc_show_instances " $sql = "UPDATE cc_show_instances "
."SET ends = starts + INTERVAL '$p_data[add_show_duration]' " ."SET ends = starts + INTERVAL '$p_data[add_show_duration]' "
."WHERE show_id = $p_data[add_show_id]"; ."WHERE show_id = $p_data[add_show_id] "
."AND starts > current_timestamp";
$CC_DBC->query($sql); $CC_DBC->query($sql);
} }
@ -344,10 +360,11 @@ class Show {
$sql = "UPDATE cc_show_instances " $sql = "UPDATE cc_show_instances "
."SET starts = starts + INTERVAL '$diff sec', " ."SET starts = starts + INTERVAL '$diff sec', "
."ends = ends + INTERVAL '$diff sec' " ."ends = ends + INTERVAL '$diff sec' "
."WHERE show_id = $p_data[add_show_id]"; ."WHERE show_id = $p_data[add_show_id] "
."AND starts > current_timestamp";
$CC_DBC->query($sql); $CC_DBC->query($sql);
$showInstanceIds = $this->getAllInstanceIds(); $showInstanceIds = $this->getAllFutureInstanceIds();
if (count($showInstanceIds) > 0 && $diff != 0){ if (count($showInstanceIds) > 0 && $diff != 0){
$showIdsImploded = implode(",", $showInstanceIds); $showIdsImploded = implode(",", $showInstanceIds);
$sql = "UPDATE cc_schedule " $sql = "UPDATE cc_schedule "
@ -409,6 +426,12 @@ class Show {
if ($p_data['add_show_start_date'] != $p_show->getStartDate() if ($p_data['add_show_start_date'] != $p_show->getStartDate()
|| $p_data['add_show_start_time'] != $p_show->getStartTime()){ || $p_data['add_show_start_time'] != $p_show->getStartTime()){
//start date/time has changed //start date/time has changed
$newDate = strtotime($p_data['add_show_start_date']);
$oldDate = strtotime($p_show->getStartDate());
if ($newDate > $oldDate){
$p_show->removeAllInstancesBeforeDate($p_data['add_show_start_date']);
}
$p_show->updateStartDateTime($p_data, $p_endDate); $p_show->updateStartDateTime($p_data, $p_endDate);
} }
@ -475,32 +498,32 @@ class Show {
* Show ID * Show ID
*/ */
public static function create($data) public static function create($data)
{ {
$con = Propel::getConnection(CcShowPeer::DATABASE_NAME); $con = Propel::getConnection(CcShowPeer::DATABASE_NAME);
$sql = "SELECT EXTRACT(DOW FROM TIMESTAMP '{$data['add_show_start_date']} {$data['add_show_start_time']}')"; $sql = "SELECT EXTRACT(DOW FROM TIMESTAMP '{$data['add_show_start_date']} {$data['add_show_start_time']}')";
$r = $con->query($sql); $r = $con->query($sql);
$startDow = $r->fetchColumn(0); $startDow = $r->fetchColumn(0);
if ($data['add_show_no_end']) { if ($data['add_show_no_end']) {
$endDate = NULL; $endDate = NULL;
$data['add_show_repeats'] = 1; $data['add_show_repeats'] = 1;
} }
else if ($data['add_show_repeats']) { else if ($data['add_show_repeats']) {
$sql = "SELECT date '{$data['add_show_end_date']}' + INTERVAL '1 day' "; $sql = "SELECT date '{$data['add_show_end_date']}' + INTERVAL '1 day' ";
$r = $con->query($sql); $r = $con->query($sql);
$endDate = $r->fetchColumn(0); $endDate = $r->fetchColumn(0);
} }
else { else {
$sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '1 day' "; $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '1 day' ";
$r = $con->query($sql); $r = $con->query($sql);
$endDate = $r->fetchColumn(0); $endDate = $r->fetchColumn(0);
} }
//only want the day of the week from the start date. //only want the day of the week from the start date.
if (!$data['add_show_repeats']) { if (!$data['add_show_repeats']) {
$data['add_show_day_check'] = array($startDow); $data['add_show_day_check'] = array($startDow);
} }
else if ($data['add_show_repeats'] && $data['add_show_day_check'] == "") { else if ($data['add_show_repeats'] && $data['add_show_day_check'] == "") {
$data['add_show_day_check'] = array($startDow); $data['add_show_day_check'] = array($startDow);
} }
@ -518,13 +541,13 @@ class Show {
} else { } else {
$ccShow = CcShowQuery::create()->findPK($data['add_show_id']); $ccShow = CcShowQuery::create()->findPK($data['add_show_id']);
} }
$ccShow->setDbName($data['add_show_name']); $ccShow->setDbName($data['add_show_name']);
$ccShow->setDbDescription($data['add_show_description']); $ccShow->setDbDescription($data['add_show_description']);
$ccShow->setDbUrl($data['add_show_url']); $ccShow->setDbUrl($data['add_show_url']);
$ccShow->setDbGenre($data['add_show_genre']); $ccShow->setDbGenre($data['add_show_genre']);
$ccShow->setDbColor($data['add_show_color']); $ccShow->setDbColor($data['add_show_color']);
$ccShow->setDbBackgroundColor($data['add_show_background_color']); $ccShow->setDbBackgroundColor($data['add_show_background_color']);
$ccShow->save(); $ccShow->save();
$isRecorded = ($data['add_show_record']) ? 1 : 0; $isRecorded = ($data['add_show_record']) ? 1 : 0;
@ -544,45 +567,45 @@ class Show {
//don't set day for monthly repeat type, it's invalid. //don't set day for monthly repeat type, it's invalid.
if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) { if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) {
$showDay = new CcShowDays(); $showDay = new CcShowDays();
$showDay->setDbFirstShow($data['add_show_start_date']); $showDay->setDbFirstShow($data['add_show_start_date']);
$showDay->setDbLastShow($endDate); $showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($data['add_show_start_time']); $showDay->setDbStartTime($data['add_show_start_time']);
$showDay->setDbDuration($data['add_show_duration']); $showDay->setDbDuration($data['add_show_duration']);
$showDay->setDbRepeatType($repeat_type); $showDay->setDbRepeatType($repeat_type);
$showDay->setDbShowId($showId); $showDay->setDbShowId($showId);
$showDay->setDbRecord($isRecorded); $showDay->setDbRecord($isRecorded);
$showDay->save(); $showDay->save();
} }
else { else {
foreach ($data['add_show_day_check'] as $day) { foreach ($data['add_show_day_check'] as $day) {
if ($startDow !== $day){ if ($startDow !== $day){
if ($startDow > $day) if ($startDow > $day)
$daysAdd = 6 - $startDow + 1 + $day; $daysAdd = 6 - $startDow + 1 + $day;
else else
$daysAdd = $day - $startDow; $daysAdd = $day - $startDow;
$sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '{$daysAdd} day' "; $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '{$daysAdd} day' ";
$r = $con->query($sql); $r = $con->query($sql);
$start = $r->fetchColumn(0); $start = $r->fetchColumn(0);
} }
else { else {
$start = $data['add_show_start_date']; $start = $data['add_show_start_date'];
} }
if (strtotime($start) < strtotime($endDate) || is_null($endDate)) { if (strtotime($start) < strtotime($endDate) || is_null($endDate)) {
$showDay = new CcShowDays(); $showDay = new CcShowDays();
$showDay->setDbFirstShow($start); $showDay->setDbFirstShow($start);
$showDay->setDbLastShow($endDate); $showDay->setDbLastShow($endDate);
$showDay->setDbStartTime($data['add_show_start_time']); $showDay->setDbStartTime($data['add_show_start_time']);
$showDay->setDbDuration($data['add_show_duration']); $showDay->setDbDuration($data['add_show_duration']);
$showDay->setDbDay($day); $showDay->setDbDay($day);
$showDay->setDbRepeatType($repeat_type); $showDay->setDbRepeatType($repeat_type);
$showDay->setDbShowId($showId); $showDay->setDbShowId($showId);
$showDay->setDbRecord($isRecorded); $showDay->setDbRecord($isRecorded);
$showDay->save(); $showDay->save();
} }
} }
} }
//check if we are adding or updating a show, and if updating //check if we are adding or updating a show, and if updating
@ -610,8 +633,8 @@ class Show {
if ($data['add_show_rebroadcast_absolute_date_'.$i]) { if ($data['add_show_rebroadcast_absolute_date_'.$i]) {
$sql = "SELECT date '{$data['add_show_rebroadcast_absolute_date_'.$i]}' - date '{$data['add_show_start_date']}' "; $sql = "SELECT date '{$data['add_show_rebroadcast_absolute_date_'.$i]}' - date '{$data['add_show_start_date']}' ";
$r = $con->query($sql); $r = $con->query($sql);
$offset_days = $r->fetchColumn(0); $offset_days = $r->fetchColumn(0);
$showRebroad = new CcShowRebroadcast(); $showRebroad = new CcShowRebroadcast();
$showRebroad->setDbDayOffset($offset_days." days"); $showRebroad->setDbDayOffset($offset_days." days");
@ -629,18 +652,18 @@ class Show {
} }
if (is_array($data['add_show_hosts'])) { if (is_array($data['add_show_hosts'])) {
//add selected hosts to cc_show_hosts table. //add selected hosts to cc_show_hosts table.
foreach ($data['add_show_hosts'] as $host) { foreach ($data['add_show_hosts'] as $host) {
$showHost = new CcShowHosts(); $showHost = new CcShowHosts();
$showHost->setDbShow($showId); $showHost->setDbShow($showId);
$showHost->setDbHost($host); $showHost->setDbHost($host);
$showHost->save(); $showHost->save();
} }
} }
Show::populateShowUntil($showId); Show::populateShowUntil($showId);
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
return $showId; return $showId;
} }
/** /**
* Get all the show instances in the given time range. * Get all the show instances in the given time range.
@ -694,7 +717,7 @@ class Show {
$sql = $sql." AND ({$exclude})"; $sql = $sql." AND ({$exclude})";
} }
return $CC_DBC->GetAll($sql); return $CC_DBC->GetAll($sql);
} }
private static function setNextPop($next_date, $show_id, $day) private static function setNextPop($next_date, $show_id, $day)
@ -722,7 +745,7 @@ class Show {
$start = $next_date; $start = $next_date;
$sql = "SELECT timestamp '{$start}' + interval '{$duration}'"; $sql = "SELECT timestamp '{$start}' + interval '{$duration}'";
$end = $CC_DBC->GetOne($sql); $end = $CC_DBC->GetOne($sql);
$show = new Show($show_id); $show = new Show($show_id);
if ($show->hasInstance()){ if ($show->hasInstance()){
@ -742,17 +765,17 @@ class Show {
$show_instance_id = $showInstance->getDbId(); $show_instance_id = $showInstance->getDbId();
$sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}"; $sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}";
$rebroadcasts = $CC_DBC->GetAll($sql); $rebroadcasts = $CC_DBC->GetAll($sql);
foreach($rebroadcasts as $rebroadcast) { foreach($rebroadcasts as $rebroadcast) {
$timeinfo = explode(" ", $start); $timeinfo = explode(" ", $start);
$sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'"; $sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'";
$rebroadcast_start_time = $CC_DBC->GetOne($sql); $rebroadcast_start_time = $CC_DBC->GetOne($sql);
$sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'"; $sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'";
$rebroadcast_end_time = $CC_DBC->GetOne($sql); $rebroadcast_end_time = $CC_DBC->GetOne($sql);
$newRebroadcastInstance = new CcShowInstances(); $newRebroadcastInstance = new CcShowInstances();
$newRebroadcastInstance->setDbShowId($show_id); $newRebroadcastInstance->setDbShowId($show_id);
@ -761,7 +784,7 @@ class Show {
$newRebroadcastInstance->setDbRecord(0); $newRebroadcastInstance->setDbRecord(0);
$newRebroadcastInstance->setDbRebroadcast(1); $newRebroadcastInstance->setDbRebroadcast(1);
$newRebroadcastInstance->setDbOriginalShow($show_instance_id); $newRebroadcastInstance->setDbOriginalShow($show_instance_id);
$newRebroadcastInstance->save(); $newRebroadcastInstance->save();
} }
} }
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
@ -781,7 +804,7 @@ class Show {
} }
$sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}"; $sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}";
$rebroadcasts = $CC_DBC->GetAll($sql); $rebroadcasts = $CC_DBC->GetAll($sql);
$show = new Show($show_id); $show = new Show($show_id);
while(strtotime($next_date) < strtotime($end_timestamp) && (strtotime($last_show) > strtotime($next_date) || is_null($last_show))) { while(strtotime($next_date) < strtotime($end_timestamp) && (strtotime($last_show) > strtotime($next_date) || is_null($last_show))) {
@ -789,7 +812,7 @@ class Show {
$start = $next_date; $start = $next_date;
$sql = "SELECT timestamp '{$start}' + interval '{$duration}'"; $sql = "SELECT timestamp '{$start}' + interval '{$duration}'";
$end = $CC_DBC->GetOne($sql); $end = $CC_DBC->GetOne($sql);
if ($show->hasInstanceOnDate($start)){ if ($show->hasInstanceOnDate($start)){
$showInstance = $show->getInstanceOnDate($start); $showInstance = $show->getInstanceOnDate($start);
@ -811,10 +834,10 @@ class Show {
$timeinfo = explode(" ", $next_date); $timeinfo = explode(" ", $next_date);
$sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'"; $sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'";
$rebroadcast_start_time = $CC_DBC->GetOne($sql); $rebroadcast_start_time = $CC_DBC->GetOne($sql);
$sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'"; $sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'";
$rebroadcast_end_time = $CC_DBC->GetOne($sql); $rebroadcast_end_time = $CC_DBC->GetOne($sql);
$newRebroadcastInstance = new CcShowInstances(); $newRebroadcastInstance = new CcShowInstances();
$newRebroadcastInstance->setDbShowId($show_id); $newRebroadcastInstance->setDbShowId($show_id);
@ -823,11 +846,11 @@ class Show {
$newRebroadcastInstance->setDbRecord(0); $newRebroadcastInstance->setDbRecord(0);
$newRebroadcastInstance->setDbRebroadcast(1); $newRebroadcastInstance->setDbRebroadcast(1);
$newRebroadcastInstance->setDbOriginalShow($show_instance_id); $newRebroadcastInstance->setDbOriginalShow($show_instance_id);
$newRebroadcastInstance->save(); $newRebroadcastInstance->save();
} }
$sql = "SELECT timestamp '{$start}' + interval '{$interval}'"; $sql = "SELECT timestamp '{$start}' + interval '{$interval}'";
$next_date = $CC_DBC->GetOne($sql); $next_date = $CC_DBC->GetOne($sql);
} }
Show::setNextPop($next_date, $show_id, $day); Show::setNextPop($next_date, $show_id, $day);
@ -863,7 +886,7 @@ class Show {
* *
* @param int $p_showId * @param int $p_showId
* @param string $p_date * @param string $p_date
* In the format "YYYY-MM-DD HH:mm:ss" * In the format "YYYY-MM-DD HH:mm:ss"
*/ */
public static function populateShowUntil($p_showId, $p_date = NULL) public static function populateShowUntil($p_showId, $p_date = NULL)
{ {
@ -878,7 +901,7 @@ class Show {
} }
$sql = "SELECT * FROM cc_show_days WHERE show_id = $p_showId"; $sql = "SELECT * FROM cc_show_days WHERE show_id = $p_showId";
$res = $CC_DBC->GetAll($sql); $res = $CC_DBC->GetAll($sql);
foreach ($res as $row) { foreach ($res as $row) {
Show::populateShow($row["repeat_type"], $row["show_id"], $row["next_pop_date"], $row["first_show"], Show::populateShow($row["repeat_type"], $row["show_id"], $row["next_pop_date"], $row["first_show"],
@ -890,9 +913,9 @@ class Show {
* Generate all the repeating shows in the given range. * Generate all the repeating shows in the given range.
* *
* @param string $p_startTimestamp * @param string $p_startTimestamp
* In the format "YYYY-MM-DD HH:mm:ss" * In the format "YYYY-MM-DD HH:mm:ss"
* @param string $p_endTimestamp * @param string $p_endTimestamp
* In the format "YYYY-MM-DD HH:mm:ss" * In the format "YYYY-MM-DD HH:mm:ss"
*/ */
public static function populateAllShowsInRange($p_startTimestamp, $p_endTimestamp) public static function populateAllShowsInRange($p_startTimestamp, $p_endTimestamp)
{ {
@ -900,17 +923,17 @@ class Show {
if ($p_startTimestamp != "") { if ($p_startTimestamp != "") {
$sql = "SELECT * FROM cc_show_days $sql = "SELECT * FROM cc_show_days
WHERE last_show IS NULL WHERE last_show IS NULL
OR first_show < '{$p_endTimestamp}' AND last_show > '{$p_startTimestamp}'"; OR first_show < '{$p_endTimestamp}' AND last_show > '{$p_startTimestamp}'";
} }
else { else {
$today_timestamp = date("Y-m-d"); $today_timestamp = date("Y-m-d");
$sql = "SELECT * FROM cc_show_days $sql = "SELECT * FROM cc_show_days
WHERE last_show IS NULL WHERE last_show IS NULL
OR first_show < '{$p_endTimestamp}' AND last_show > '{$today_timestamp}'"; OR first_show < '{$p_endTimestamp}' AND last_show > '{$today_timestamp}'";
} }
$res = $CC_DBC->GetAll($sql); $res = $CC_DBC->GetAll($sql);
foreach ($res as $row) { foreach ($res as $row) {
Show::populateShow($row["repeat_type"], $row["show_id"], $row["next_pop_date"], $row["first_show"], Show::populateShow($row["repeat_type"], $row["show_id"], $row["next_pop_date"], $row["first_show"],
@ -923,7 +946,7 @@ class Show {
* @param string $start * @param string $start
* In the format "YYYY-MM-DD HH:mm:ss" * In the format "YYYY-MM-DD HH:mm:ss"
* @param string $end * @param string $end
* In the format "YYYY-MM-DD HH:mm:ss" * In the format "YYYY-MM-DD HH:mm:ss"
* @param boolean $editable * @param boolean $editable
*/ */
public static function getFullCalendarEvents($start, $end, $editable=false) public static function getFullCalendarEvents($start, $end, $editable=false)
@ -948,7 +971,7 @@ class Show {
if ($editable && strtotime($today_timestamp) < strtotime($show["starts"])) { if ($editable && strtotime($today_timestamp) < strtotime($show["starts"])) {
$options["editable"] = true; $options["editable"] = true;
$events[] = Show::makeFullCalendarEvent($show, $options); $events[] = Show::makeFullCalendarEvent($show, $options);
} }
else { else {
$events[] = Show::makeFullCalendarEvent($show, $options); $events[] = Show::makeFullCalendarEvent($show, $options);
@ -958,7 +981,7 @@ class Show {
return $events; return $events;
} }
private static function makeFullCalendarEvent($show, $options=array()) private static function makeFullCalendarEvent($show, $options=array())
{ {
$event = array(); $event = array();
@ -966,12 +989,12 @@ class Show {
$event["disableResizing"] = true; $event["disableResizing"] = true;
} }
$event["id"] = $show["instance_id"]; $event["id"] = $show["instance_id"];
$event["title"] = $show["name"]; $event["title"] = $show["name"];
$event["start"] = $show["starts"]; $event["start"] = $show["starts"];
$event["end"] = $show["ends"]; $event["end"] = $show["ends"];
$event["allDay"] = false; $event["allDay"] = false;
$event["description"] = $show["description"]; $event["description"] = $show["description"];
$event["showId"] = $show["show_id"]; $event["showId"] = $show["show_id"];
$event["record"] = intval($show["record"]); $event["record"] = intval($show["record"]);
$event["rebroadcast"] = intval($show["rebroadcast"]); $event["rebroadcast"] = intval($show["rebroadcast"]);
@ -983,14 +1006,14 @@ class Show {
} }
if($show["background_color"] != "") { if($show["background_color"] != "") {
$event["color"] = "#".$show["background_color"]; $event["color"] = "#".$show["background_color"];
} }
foreach($options as $key=>$value) { foreach($options as $key=>$value) {
$event[$key] = $value; $event[$key] = $value;
} }
return $event; return $event;
} }
public static function getDateFromTimestamp($p_timestamp){ public static function getDateFromTimestamp($p_timestamp){
$explode = explode(" ", $p_timestamp); $explode = explode(" ", $p_timestamp);
@ -1030,11 +1053,11 @@ class Show {
class ShowInstance { class ShowInstance {
private $_instanceId; private $_instanceId;
public function __construct($instanceId) public function __construct($instanceId)
{ {
$this->_instanceId = $instanceId; $this->_instanceId = $instanceId;
} }
public function getShowId() public function getShowId()
@ -1170,40 +1193,40 @@ class ShowInstance {
public function moveShow($deltaDay, $deltaMin) public function moveShow($deltaDay, $deltaMin)
{ {
global $CC_DBC; global $CC_DBC;
$hours = $deltaMin/60; $hours = $deltaMin/60;
if($hours > 0) if($hours > 0)
$hours = floor($hours); $hours = floor($hours);
else else
$hours = ceil($hours); $hours = ceil($hours);
$mins = abs($deltaMin%60); $mins = abs($deltaMin%60);
$starts = $this->getShowStart(); $starts = $this->getShowStart();
$ends = $this->getShowEnd(); $ends = $this->getShowEnd();
$sql = "SELECT timestamp '{$starts}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'"; $sql = "SELECT timestamp '{$starts}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'";
$new_starts = $CC_DBC->GetOne($sql); $new_starts = $CC_DBC->GetOne($sql);
$sql = "SELECT timestamp '{$ends}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'"; $sql = "SELECT timestamp '{$ends}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'";
$new_ends = $CC_DBC->GetOne($sql); $new_ends = $CC_DBC->GetOne($sql);
$today_timestamp = date("Y-m-d H:i:s"); $today_timestamp = date("Y-m-d H:i:s");
if(strtotime($today_timestamp) > strtotime($new_starts)) { if(strtotime($today_timestamp) > strtotime($new_starts)) {
return "can't move show into past"; return "can't move show into past";
} }
$overlap = Show::getShows($new_starts, $new_ends, array($this->_instanceId)); $overlap = Show::getShows($new_starts, $new_ends, array($this->_instanceId));
if(count($overlap) > 0) { if(count($overlap) > 0) {
return "Should not overlap shows"; return "Should not overlap shows";
} }
$rebroadcast = $this->isRebroadcast(); $rebroadcast = $this->isRebroadcast();
if($rebroadcast) { if($rebroadcast) {
$sql = "SELECT timestamp '{$new_starts}' < (SELECT starts FROM cc_show_instances WHERE id = {$rebroadcast})"; $sql = "SELECT timestamp '{$new_starts}' < (SELECT starts FROM cc_show_instances WHERE id = {$rebroadcast})";
$isBeforeRecordedOriginal = $CC_DBC->GetOne($sql); $isBeforeRecordedOriginal = $CC_DBC->GetOne($sql);
if($isBeforeRecordedOriginal === 't'){ if($isBeforeRecordedOriginal === 't'){
return "Cannot move a rebroadcast show before its original"; return "Cannot move a rebroadcast show before its original";
@ -1214,33 +1237,33 @@ class ShowInstance {
$this->setShowStart($new_starts); $this->setShowStart($new_starts);
$this->setShowEnd($new_ends); $this->setShowEnd($new_ends);
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
} }
public function resizeShow($deltaDay, $deltaMin) public function resizeShow($deltaDay, $deltaMin)
{ {
global $CC_DBC; global $CC_DBC;
$hours = $deltaMin/60; $hours = $deltaMin/60;
if($hours > 0) if($hours > 0)
$hours = floor($hours); $hours = floor($hours);
else else
$hours = ceil($hours); $hours = ceil($hours);
$mins = abs($deltaMin%60); $mins = abs($deltaMin%60);
$starts = $this->getShowStart(); $starts = $this->getShowStart();
$ends = $this->getShowEnd(); $ends = $this->getShowEnd();
$sql = "SELECT timestamp '{$ends}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'"; $sql = "SELECT timestamp '{$ends}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'";
$new_ends = $CC_DBC->GetOne($sql); $new_ends = $CC_DBC->GetOne($sql);
//only need to check overlap if show increased in size. //only need to check overlap if show increased in size.
if(strtotime($new_ends) > strtotime($ends)) { if(strtotime($new_ends) > strtotime($ends)) {
$overlap = Show::getShows($ends, $new_ends); $overlap = Show::getShows($ends, $new_ends);
if(count($overlap) > 0) { if(count($overlap) > 0) {
return "Should not overlap shows"; return "Should not overlap shows";
} }
} }
//with overbooking no longer need to check already scheduled content still fits. //with overbooking no longer need to check already scheduled content still fits.
@ -1248,45 +1271,45 @@ class ShowInstance {
if($this->isRecorded()) { if($this->isRecorded()) {
$sql = "UPDATE cc_show_instances SET ends = (ends + interval '{$deltaDay} days' + interval '{$hours}:{$mins}') $sql = "UPDATE cc_show_instances SET ends = (ends + interval '{$deltaDay} days' + interval '{$hours}:{$mins}')
WHERE rebroadcast = 1 AND instance_id = {$this->_instanceId}"; WHERE rebroadcast = 1 AND instance_id = {$this->_instanceId}";
$CC_DBC->query($sql); $CC_DBC->query($sql);
} }
$this->setShowEnd($new_ends); $this->setShowEnd($new_ends);
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
} }
/** /**
* Get the group ID for this show. * Get the group ID for this show.
* *
*/ */
private function getLastGroupId() private function getLastGroupId()
{
global $CC_DBC;
$sql = "SELECT group_id FROM cc_schedule WHERE instance_id = '{$this->_instanceId}' ORDER BY ends DESC LIMIT 1";
$res = $CC_DBC->GetOne($sql);
return $res;
}
/**
* Add a playlist as the last item of the current show.
*
* @param int $plId
* Playlist ID.
*/
public function addPlaylistToShow($plId)
{ {
$sched = new ScheduleGroup(); global $CC_DBC;
$lastGroupId = $this->getLastGroupId(); $sql = "SELECT group_id FROM cc_schedule WHERE instance_id = '{$this->_instanceId}' ORDER BY ends DESC LIMIT 1";
$res = $CC_DBC->GetOne($sql);
return $res;
}
if (is_null($lastGroupId)) { /**
$groupId = $sched->add($this->_instanceId, $this->getShowStart(), null, $plId); * Add a playlist as the last item of the current show.
} *
else { * @param int $plId
$groupId = $sched->addPlaylistAfter($this->_instanceId, $lastGroupId, $plId); * Playlist ID.
} */
RabbitMq::PushSchedule(); public function addPlaylistToShow($plId)
{
$sched = new ScheduleGroup();
$lastGroupId = $this->getLastGroupId();
if (is_null($lastGroupId)) {
$groupId = $sched->add($this->_instanceId, $this->getShowStart(), null, $plId);
}
else {
$groupId = $sched->addPlaylistAfter($this->_instanceId, $lastGroupId, $plId);
}
RabbitMq::PushSchedule();
$this->updateScheduledTime(); $this->updateScheduledTime();
} }
/** /**
* Add a media file as the last item in the show. * Add a media file as the last item in the show.
@ -1296,14 +1319,14 @@ class ShowInstance {
public function addFileToShow($file_id) public function addFileToShow($file_id)
{ {
$sched = new ScheduleGroup(); $sched = new ScheduleGroup();
$lastGroupId = $this->getLastGroupId(); $lastGroupId = $this->getLastGroupId();
if (is_null($lastGroupId)) { if (is_null($lastGroupId)) {
$groupId = $sched->add($this->_instanceId, $this->getShowStart(), $file_id); $groupId = $sched->add($this->_instanceId, $this->getShowStart(), $file_id);
} }
else { else {
$groupId = $sched->addFileAfter($this->_instanceId, $lastGroupId, $file_id); $groupId = $sched->addFileAfter($this->_instanceId, $lastGroupId, $file_id);
} }
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
$this->updateScheduledTime(); $this->updateScheduledTime();
} }
@ -1312,7 +1335,7 @@ class ShowInstance {
* Add the given playlists to the show. * Add the given playlists to the show.
* *
* @param array $plIds * @param array $plIds
* An array of playlist IDs. * An array of playlist IDs.
*/ */
public function scheduleShow($plIds) public function scheduleShow($plIds)
{ {
@ -1321,37 +1344,37 @@ class ShowInstance {
} }
} }
public function removeGroupFromShow($group_id) public function removeGroupFromShow($group_id)
{ {
global $CC_DBC; global $CC_DBC;
$sql = "SELECT MAX(ends) as end_timestamp, (MAX(ends) - MIN(starts)) as length $sql = "SELECT MAX(ends) as end_timestamp, (MAX(ends) - MIN(starts)) as length
FROM cc_schedule FROM cc_schedule
WHERE group_id = '{$group_id}'"; WHERE group_id = '{$group_id}'";
$groupBoundry = $CC_DBC->GetRow($sql); $groupBoundry = $CC_DBC->GetRow($sql);
$group = CcScheduleQuery::create() $group = CcScheduleQuery::create()
->filterByDbGroupId($group_id) ->filterByDbGroupId($group_id)
->delete(); ->delete();
$sql = "UPDATE cc_schedule $sql = "UPDATE cc_schedule
SET starts = (starts - INTERVAL '{$groupBoundry["length"]}'), ends = (ends - INTERVAL '{$groupBoundry["length"]}') SET starts = (starts - INTERVAL '{$groupBoundry["length"]}'), ends = (ends - INTERVAL '{$groupBoundry["length"]}')
WHERE starts >= '{$groupBoundry["end_timestamp"]}' AND instance_id = {$this->_instanceId}"; WHERE starts >= '{$groupBoundry["end_timestamp"]}' AND instance_id = {$this->_instanceId}";
$CC_DBC->query($sql); $CC_DBC->query($sql);
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
$this->updateScheduledTime(); $this->updateScheduledTime();
} }
public function clearShow() public function clearShow()
{ {
CcScheduleQuery::create() CcScheduleQuery::create()
->filterByDbInstanceId($this->_instanceId) ->filterByDbInstanceId($this->_instanceId)
->delete(); ->delete();
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
$this->updateScheduledTime(); $this->updateScheduledTime();
} }
public function deleteShow() public function deleteShow()
{ {
@ -1359,7 +1382,7 @@ class ShowInstance {
->findPK($this->_instanceId) ->findPK($this->_instanceId)
->delete(); ->delete();
RabbitMq::PushSchedule(); RabbitMq::PushSchedule();
} }
public function setRecordedFile($file_id) public function setRecordedFile($file_id)
{ {
@ -1388,7 +1411,7 @@ class ShowInstance {
$time = "00:00:00"; $time = "00:00:00";
} }
return $time; return $time;
} }
public function getPercentScheduled() public function getPercentScheduled()
{ {
@ -1410,74 +1433,74 @@ class ShowInstance {
public function getShowLength() public function getShowLength()
{ {
global $CC_DBC; global $CC_DBC;
$start_timestamp = $this->getShowStart(); $start_timestamp = $this->getShowStart();
$end_timestamp = $this->getShowEnd(); $end_timestamp = $this->getShowEnd();
$sql = "SELECT TIMESTAMP '{$end_timestamp}' - TIMESTAMP '{$start_timestamp}' "; $sql = "SELECT TIMESTAMP '{$end_timestamp}' - TIMESTAMP '{$start_timestamp}' ";
$length = $CC_DBC->GetOne($sql); $length = $CC_DBC->GetOne($sql);
return $length; return $length;
} }
public function searchPlaylistsForShow($datatables) public function searchPlaylistsForShow($datatables)
{ {
return StoredFile::searchPlaylistsForSchedule($datatables); return StoredFile::searchPlaylistsForSchedule($datatables);
} }
public function getShowListContent() public function getShowListContent()
{ {
global $CC_DBC; global $CC_DBC;
$sql = "SELECT * $sql = "SELECT *
FROM (cc_schedule AS s LEFT JOIN cc_files AS f ON f.id = s.file_id FROM (cc_schedule AS s LEFT JOIN cc_files AS f ON f.id = s.file_id
LEFT JOIN cc_playlist AS p ON p.id = s.playlist_id ) LEFT JOIN cc_playlist AS p ON p.id = s.playlist_id )
WHERE s.instance_id = '{$this->_instanceId}' ORDER BY starts"; WHERE s.instance_id = '{$this->_instanceId}' ORDER BY starts";
return $CC_DBC->GetAll($sql); return $CC_DBC->GetAll($sql);
} }
public function getShowContent() public function getShowContent()
{ {
global $CC_DBC; global $CC_DBC;
$res = $this->getShowListContent(); $res = $this->getShowListContent();
if(count($res) <= 0) { if(count($res) <= 0) {
return $res; return $res;
} }
$items = array(); $items = array();
$currGroupId = -1; $currGroupId = -1;
$pl_counter = -1; $pl_counter = -1;
$f_counter = -1; $f_counter = -1;
foreach ($res as $row) { foreach ($res as $row) {
if($currGroupId != $row["group_id"]){ if($currGroupId != $row["group_id"]){
$currGroupId = $row["group_id"]; $currGroupId = $row["group_id"];
$pl_counter = $pl_counter + 1; $pl_counter = $pl_counter + 1;
$f_counter = -1; $f_counter = -1;
$items[$pl_counter]["pl_name"] = $row["name"]; $items[$pl_counter]["pl_name"] = $row["name"];
$items[$pl_counter]["pl_creator"] = $row["creator"]; $items[$pl_counter]["pl_creator"] = $row["creator"];
$items[$pl_counter]["pl_description"] = $row["description"]; $items[$pl_counter]["pl_description"] = $row["description"];
$items[$pl_counter]["pl_group"] = $row["group_id"]; $items[$pl_counter]["pl_group"] = $row["group_id"];
$sql = "SELECT SUM(clip_length) FROM cc_schedule WHERE group_id = '{$currGroupId}'"; $sql = "SELECT SUM(clip_length) FROM cc_schedule WHERE group_id = '{$currGroupId}'";
$length = $CC_DBC->GetOne($sql); $length = $CC_DBC->GetOne($sql);
$items[$pl_counter]["pl_length"] = $length; $items[$pl_counter]["pl_length"] = $length;
} }
$f_counter = $f_counter + 1; $f_counter = $f_counter + 1;
$items[$pl_counter]["pl_content"][$f_counter]["f_name"] = $row["track_title"]; $items[$pl_counter]["pl_content"][$f_counter]["f_name"] = $row["track_title"];
$items[$pl_counter]["pl_content"][$f_counter]["f_artist"] = $row["artist_name"]; $items[$pl_counter]["pl_content"][$f_counter]["f_artist"] = $row["artist_name"];
$items[$pl_counter]["pl_content"][$f_counter]["f_length"] = $row["length"]; $items[$pl_counter]["pl_content"][$f_counter]["f_length"] = $row["length"];
} }
return $items; return $items;
} }
} }
/* Show Data Access Layer */ /* Show Data Access Layer */
@ -1501,11 +1524,11 @@ class Show_DAL {
{ {
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
$sql = "SELECT *, si.starts as start_timestamp, si.ends as end_timestamp FROM " $sql = "SELECT *, si.starts as start_timestamp, si.ends as end_timestamp FROM "
." $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" ." $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s"
." WHERE si.show_id = s.id" ." WHERE si.show_id = s.id"
." AND si.starts >= TIMESTAMP '$timeNow'" ." AND si.starts >= TIMESTAMP '$timeNow'"
." AND si.starts < TIMESTAMP '$timeNow' + INTERVAL '48 hours'" ." AND si.starts < TIMESTAMP '$timeNow' + INTERVAL '48 hours'"
." ORDER BY si.starts" ." ORDER BY si.starts"
." LIMIT $limit"; ." LIMIT $limit";
@ -1516,7 +1539,7 @@ 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 show_starts," ." si.starts as show_starts,"
." si.ends as show_ends," ." si.ends as show_ends,"
." si.rebroadcast as rebroadcast," ." si.rebroadcast as rebroadcast,"
@ -1537,8 +1560,8 @@ class Show_DAL {
." LEFT JOIN $CC_CONFIG[filesTable] ft" ." LEFT JOIN $CC_CONFIG[filesTable] ft"
." ON st.file_id = ft.id" ." ON st.file_id = ft.id"
." LEFT JOIN $CC_CONFIG[showTable] s" ." LEFT JOIN $CC_CONFIG[showTable] s"
." ON si.show_id = s.id" ." ON si.show_id = s.id"
." WHERE ((si.starts < TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends > TIMESTAMP '$timeNow' - INTERVAL '$start seconds')" ." 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'))"
//checking for st.starts IS NULL so that the query also returns shows that do not have any items scheduled. //checking for st.starts IS NULL so that the query also returns shows that do not have any items scheduled.
@ -1556,14 +1579,14 @@ class Show_DAL {
//Result: 5 //Result: 5
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
$sql = "SELECT" $sql = "SELECT"
." si.starts as show_starts," ." si.starts as show_starts,"
." si.ends as show_ends," ." si.ends as show_ends,"
." s.name as show_name," ." s.name as show_name,"
." s.url as url" ." s.url as url"
." FROM $CC_CONFIG[showInstances] si" ." FROM $CC_CONFIG[showInstances] si"
." LEFT JOIN $CC_CONFIG[showTable] s" ." LEFT JOIN $CC_CONFIG[showTable] s"
." ON si.show_id = s.id" ." ON si.show_id = s.id"
." WHERE EXTRACT(DOW FROM si.starts) = $day" ." WHERE EXTRACT(DOW FROM si.starts) = $day"
." AND EXTRACT(WEEK FROM si.starts) = EXTRACT(WEEK FROM localtimestamp)" ." AND EXTRACT(WEEK FROM si.starts) = EXTRACT(WEEK FROM localtimestamp)"
." ORDER BY si.starts"; ." ORDER BY si.starts";