2010-12-07 20:19:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Show {
|
|
|
|
|
|
|
|
private $_userRole;
|
|
|
|
|
|
|
|
public function __construct($userType='G')
|
|
|
|
{
|
|
|
|
$this->_userRole = $userType;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function makeFullCalendarEvent($show, $date, $options=array()) {
|
|
|
|
|
|
|
|
$start = $date."T".$show["start_time"];
|
|
|
|
$end = $date."T".$show["end_time"];
|
|
|
|
|
|
|
|
$event = array(
|
|
|
|
"id" => $show["show_id"],
|
|
|
|
"title" => $show["name"],
|
|
|
|
"start" => $start,
|
|
|
|
"end" => $end,
|
|
|
|
"allDay" => false,
|
|
|
|
"description" => $show["description"]
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($options as $key=>$value) {
|
|
|
|
$event[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->_userRole === "A") {
|
|
|
|
$event["editable"] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2010-12-09 20:54:12 +01:00
|
|
|
//end dates are non inclusive.
|
2010-12-08 21:40:53 +01:00
|
|
|
public function addShow($data) {
|
|
|
|
|
2010-12-08 06:47:51 +01:00
|
|
|
$con = Propel::getConnection("campcaster");
|
|
|
|
|
2010-12-08 21:40:53 +01:00
|
|
|
$sql = "SELECT time '{$data['start_time']}' + INTERVAL '{$data['duration']} hour' ";
|
2010-12-08 06:47:51 +01:00
|
|
|
$r = $con->query($sql);
|
|
|
|
$endTime = $r->fetchColumn(0);
|
|
|
|
|
2010-12-08 21:40:53 +01:00
|
|
|
$sql = "SELECT nextval('show_group_id_seq')";
|
2010-12-08 06:47:51 +01:00
|
|
|
$r = $con->query($sql);
|
|
|
|
$showId = $r->fetchColumn(0);
|
|
|
|
|
2010-12-08 21:40:53 +01:00
|
|
|
$sql = "SELECT EXTRACT(DOW FROM TIMESTAMP '{$data['start_date']} {$data['start_time']}')";
|
2010-12-08 06:47:51 +01:00
|
|
|
$r = $con->query($sql);
|
2010-12-08 21:40:53 +01:00
|
|
|
$startDow = $r->fetchColumn(0);
|
|
|
|
|
|
|
|
if($data['no_end']) {
|
|
|
|
$endDate = NULL;
|
2010-12-09 20:33:34 +01:00
|
|
|
$data['repeats'] = 1;
|
2010-12-08 21:40:53 +01:00
|
|
|
}
|
|
|
|
else if($data['repeats']) {
|
2010-12-09 20:54:12 +01:00
|
|
|
$sql = "SELECT date '{$data['end_date']}' + INTERVAL '1 day' ";
|
|
|
|
$r = $con->query($sql);
|
|
|
|
$endDate = $r->fetchColumn(0);
|
2010-12-08 21:40:53 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-12-09 20:54:12 +01:00
|
|
|
$sql = "SELECT date '{$data['start_date']}' + INTERVAL '1 day' ";
|
|
|
|
$r = $con->query($sql);
|
|
|
|
$endDate = $r->fetchColumn(0);
|
2010-12-08 21:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($data['day_check'] === null) {
|
|
|
|
$data['day_check'] = array($startDow);
|
|
|
|
}
|
2010-12-08 06:47:51 +01:00
|
|
|
|
2010-12-08 21:40:53 +01:00
|
|
|
foreach ($data['day_check'] as $day) {
|
2010-12-08 06:47:51 +01:00
|
|
|
|
|
|
|
if($startDow !== $day){
|
|
|
|
|
|
|
|
if($startDow > $day)
|
|
|
|
$daysAdd = 6 - $startDow + 1 + $day;
|
|
|
|
else
|
|
|
|
$daysAdd = $day - $startDow;
|
|
|
|
|
2010-12-08 21:40:53 +01:00
|
|
|
$sql = "SELECT date '{$data['start_date']}' + INTERVAL '{$daysAdd} day' ";
|
2010-12-08 06:47:51 +01:00
|
|
|
$r = $con->query($sql);
|
|
|
|
$start = $r->fetchColumn(0);
|
|
|
|
}
|
|
|
|
else {
|
2010-12-08 21:40:53 +01:00
|
|
|
$start = $data['start_date'];
|
2010-12-08 06:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$show = new CcShow();
|
2010-12-08 21:40:53 +01:00
|
|
|
$show->setDbName($data['name']);
|
2010-12-08 06:47:51 +01:00
|
|
|
$show->setDbFirstShow($start);
|
|
|
|
$show->setDbLastShow($endDate);
|
2010-12-08 21:40:53 +01:00
|
|
|
$show->setDbStartTime($data['start_time']);
|
2010-12-08 06:47:51 +01:00
|
|
|
$show->setDbEndTime($endTime);
|
2010-12-08 21:40:53 +01:00
|
|
|
$show->setDbRepeats($data['repeats']);
|
2010-12-08 06:47:51 +01:00
|
|
|
$show->setDbDay($day);
|
2010-12-08 21:40:53 +01:00
|
|
|
$show->setDbDescription($data['description']);
|
2010-12-08 06:47:51 +01:00
|
|
|
$show->setDbShowId($showId);
|
|
|
|
$show->save();
|
|
|
|
}
|
2010-12-07 20:19:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-09 20:33:34 +01:00
|
|
|
public function moveShow($showId, $deltaDay, $deltaMin){
|
|
|
|
global $CC_DBC;
|
|
|
|
|
|
|
|
$sql = "SELECT * FROM cc_show WHERE show_id = '{$showId}'";
|
|
|
|
$res = $CC_DBC->GetAll($sql);
|
|
|
|
|
|
|
|
$show = $res[0];
|
|
|
|
$start = $show["first_show"];
|
|
|
|
$end = $show["last_show"];
|
|
|
|
$days = array();
|
2010-12-09 23:02:37 +01:00
|
|
|
|
|
|
|
$hours = $deltaMin/60;
|
|
|
|
if($hours > 0)
|
|
|
|
$hours = floor($hours);
|
|
|
|
else
|
|
|
|
$hours = ceil($hours);
|
|
|
|
|
|
|
|
$mins = abs($deltaMin%60);
|
|
|
|
|
|
|
|
$sql = "SELECT time '{$show["start_time"]}' + interval '{$hours}:{$mins}'";
|
|
|
|
//echo $sql;
|
|
|
|
$s_time = $CC_DBC->GetOne($sql);
|
|
|
|
|
|
|
|
$sql = "SELECT time '{$show["end_time"]}' + interval '{$hours}:{$mins}'";
|
|
|
|
//echo $sql;
|
|
|
|
$e_time = $CC_DBC->GetOne($sql);
|
2010-12-09 20:33:34 +01:00
|
|
|
|
|
|
|
foreach($res as $show) {
|
2010-12-09 20:54:12 +01:00
|
|
|
$days[] = $show["day"] + $deltaDay;
|
2010-12-09 20:33:34 +01:00
|
|
|
}
|
|
|
|
|
2010-12-09 23:02:37 +01:00
|
|
|
return $this->getShows($start, $end, $days, $s_time, $e_time, array($showId));
|
2010-12-09 20:33:34 +01:00
|
|
|
}
|
|
|
|
|
2010-12-09 23:02:37 +01:00
|
|
|
public function getShows($start=NULL, $end=NULL, $days=NULL, $s_time=NULL, $e_time=NULL, $exclude_shows=NULL) {
|
2010-12-07 20:19:27 +01:00
|
|
|
global $CC_DBC;
|
|
|
|
|
|
|
|
$sql;
|
|
|
|
|
|
|
|
$sql_gen = "SELECT * FROM cc_show";
|
|
|
|
$sql = $sql_gen;
|
|
|
|
|
|
|
|
if(!is_null($start) && !is_null($end)) {
|
|
|
|
$sql_range = "(first_show < '{$start}' AND last_show IS NULL)
|
|
|
|
OR (first_show >= '{$start}' AND first_show < '{$end}')
|
|
|
|
OR (last_show >= '{$start}' AND last_show < '{$end}')
|
|
|
|
OR (first_show < '{$start}' AND last_show >= '{$end}')";
|
|
|
|
|
|
|
|
$sql = $sql_gen ." WHERE ". $sql_range;
|
|
|
|
}
|
2010-12-09 20:33:34 +01:00
|
|
|
if(!is_null($days)){
|
|
|
|
|
|
|
|
$sql_opt = array();
|
|
|
|
foreach ($days as $day) {
|
|
|
|
$sql_opt[] = "day = {$day}";
|
|
|
|
}
|
|
|
|
$sql_day = join(" OR ", $sql_opt);
|
2010-12-07 20:19:27 +01:00
|
|
|
|
2010-12-09 23:02:37 +01:00
|
|
|
$sql = $sql_gen ." WHERE ((". $sql_day .") AND (". $sql_range ."))";
|
2010-12-07 20:19:27 +01:00
|
|
|
}
|
2010-12-09 20:33:34 +01:00
|
|
|
if(!is_null($s_time) && !is_null($e_time)) {
|
|
|
|
$sql_time = "(start_time <= '{$s_time}' AND end_time >= '{$e_time}')
|
|
|
|
OR (start_time >= '{$s_time}' AND end_time <= '{$e_time}')
|
|
|
|
OR (end_time > '{$s_time}' AND end_time <= '{$e_time}')
|
|
|
|
OR (start_time >= '{$s_time}' AND start_time < '{$e_time}')";
|
|
|
|
|
2010-12-09 23:02:37 +01:00
|
|
|
$sql = $sql_gen ." WHERE ((". $sql_day .") AND (". $sql_range .") AND (". $sql_time ."))";
|
|
|
|
}
|
|
|
|
if(!is_null($exclude_shows)){
|
|
|
|
|
|
|
|
$sql_opt = array();
|
|
|
|
foreach ($exclude_shows as $showid) {
|
|
|
|
$sql_opt[] = "show_id = {$showid}";
|
|
|
|
}
|
|
|
|
$sql_showid = join(" OR ", $sql_opt);
|
|
|
|
|
|
|
|
$sql = $sql_gen ." WHERE ((". $sql_day .") AND NOT (". $sql_showid .") AND (". $sql_range .") AND (". $sql_time ."))";
|
2010-12-09 20:33:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//echo $sql;
|
|
|
|
|
2010-12-09 23:02:37 +01:00
|
|
|
return $CC_DBC->GetAll($sql);
|
2010-12-07 20:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullCalendarEvents($start, $end, $weekday=NULL) {
|
|
|
|
global $CC_DBC;
|
|
|
|
$shows = array();
|
|
|
|
|
|
|
|
$res = $this->getShows($start, $end, $weekday);
|
|
|
|
|
|
|
|
foreach($res as $row) {
|
|
|
|
|
|
|
|
if(!is_null($start)) {
|
|
|
|
|
|
|
|
$timeDiff = "SELECT date '{$start}' - date '{$row["first_show"]}' as diff";
|
|
|
|
$diff = $CC_DBC->GetOne($timeDiff);
|
|
|
|
|
|
|
|
if($diff > 0) {
|
|
|
|
|
|
|
|
$add = ($diff % 7 === 0) ? $diff : $diff + (7 - $diff % 7);
|
|
|
|
|
|
|
|
$new = "SELECT date '{$row["first_show"]}' + integer '{$add}'";
|
|
|
|
$newDate = $CC_DBC->GetOne($new);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$newDate = $row["first_show"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$shows[] = $this->makeFullCalendarEvent($row, $newDate);
|
|
|
|
|
|
|
|
$end_epoch = strtotime($end);
|
|
|
|
|
2010-12-09 20:33:34 +01:00
|
|
|
//add repeating events until the show end is reached or fullcalendar's end date is reached.
|
2010-12-07 20:19:27 +01:00
|
|
|
if($row["repeats"]) {
|
|
|
|
|
2010-12-09 20:33:34 +01:00
|
|
|
if(!is_null($row["last_show"])) {
|
2010-12-09 20:54:12 +01:00
|
|
|
$show_end_epoch = strtotime($row["last_show"]);
|
2010-12-09 20:33:34 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 20:19:27 +01:00
|
|
|
while(true) {
|
|
|
|
|
|
|
|
$diff = "SELECT date '{$newDate}' + integer '7'";
|
|
|
|
$repeatDate = $CC_DBC->GetOne($diff);
|
|
|
|
$repeat_epoch = strtotime($repeatDate);
|
|
|
|
|
2010-12-09 20:33:34 +01:00
|
|
|
if (isset($show_end_epoch) && $repeat_epoch < $show_end_epoch && $repeat_epoch < $end_epoch) {
|
|
|
|
$shows[] = $this->makeFullCalendarEvent($row, $repeatDate);
|
|
|
|
}
|
2010-12-09 20:54:12 +01:00
|
|
|
//case for non-ending shows.
|
2010-12-09 20:33:34 +01:00
|
|
|
else if(!isset($show_end_epoch) && $repeat_epoch < $end_epoch) {
|
2010-12-07 20:19:27 +01:00
|
|
|
$shows[] = $this->makeFullCalendarEvent($row, $repeatDate);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newDate = $repeatDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shows;
|
|
|
|
}
|
|
|
|
}
|