/** * Create a show. * * Note: end dates are non inclusive. * * @param array $data * @return int * Show ID */ public static function create($data) { $startDateTime = new DateTime($data['add_show_start_date'] . " " . $data['add_show_start_time']); $utcStartDateTime = clone $startDateTime; $utcStartDateTime->setTimezone(new DateTimeZone('UTC')); if ($data['add_show_no_end']) { $endDate = NULL; } elseif ($data['add_show_repeats']) { $endDateTime = new DateTime($data['add_show_end_date']); //$endDateTime->setTimezone(new DateTimeZone('UTC')); $endDateTime->add(new DateInterval("P1D")); $endDate = $endDateTime->format("Y-m-d"); } else { $endDateTime = new DateTime($data['add_show_start_date']); //$endDateTime->setTimezone(new DateTimeZone('UTC')); $endDateTime->add(new DateInterval("P1D")); $endDate = $endDateTime->format("Y-m-d"); } //What we are doing here is checking if the show repeats or if //any repeating days have been checked. If not, then by default //the "selected" DOW is the initial day. //DOW in local time. $startDow = date("w", $startDateTime->getTimestamp()); if (!$data['add_show_repeats']) { $data['add_show_day_check'] = array($startDow); } elseif ($data['add_show_repeats'] && $data['add_show_day_check'] == "") { $data['add_show_day_check'] = array($startDow); } //find repeat type or set to a non repeating show. $repeatType = $data['add_show_repeats'] ? $data['add_show_repeat_type'] : -1; if ($data['add_show_id'] == -1) { $ccShow = new CcShow(); } else { $ccShow = CcShowQuery::create()->findPK($data['add_show_id']); } $ccShow->setDbName($data['add_show_name']); $ccShow->setDbDescription($data['add_show_description']); $ccShow->setDbUrl($data['add_show_url']); $ccShow->setDbGenre($data['add_show_genre']); $ccShow->setDbColor($data['add_show_color']); $ccShow->setDbBackgroundColor($data['add_show_background_color']); $ccShow->setDbLiveStreamUsingAirtimeAuth($data['cb_airtime_auth'] == 1); $ccShow->setDbLiveStreamUsingCustomAuth($data['cb_custom_auth'] == 1); $ccShow->setDbLiveStreamUser($data['custom_username']); $ccShow->setDbLiveStreamPass($data['custom_password']); $ccShow->save(); $showId = $ccShow->getDbId(); $isRecorded = isset($data['add_show_record']) && $data['add_show_record'] ? 1 : 0; if ($data['add_show_id'] != -1) { $show = new Application_Model_Show($showId); $show->deletePossiblyInvalidInstances($data, $endDate, $isRecorded, $repeatType); } //check if we are adding or updating a show, and if updating //erase all the show's show_days information first. if ($data['add_show_id'] != -1) { CcShowDaysQuery::create()->filterByDbShowId($data['add_show_id'])->delete(); } //don't set day for monthly repeat type, it's invalid. if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) { $showDay = new CcShowDays(); $showDay->setDbFirstShow($startDateTime->format("Y-m-d")); $showDay->setDbLastShow($endDate); $showDay->setDbStartTime($startDateTime->format("H:i:s")); $showDay->setDbTimezone(date_default_timezone_get()); $showDay->setDbDuration($data['add_show_duration']); $showDay->setDbRepeatType($repeatType); $showDay->setDbShowId($showId); $showDay->setDbRecord($isRecorded); $showDay->save(); } else { foreach ($data['add_show_day_check'] as $day) { $daysAdd = 0; $startDateTimeClone = clone $startDateTime; if ($startDow !== $day) { if ($startDow > $day) { $daysAdd = 6 - $startDow + 1 + $day; } else { $daysAdd = $day - $startDow; } $startDateTimeClone->add(new DateInterval("P" . $daysAdd . "D")); } if (is_null($endDate) || $startDateTimeClone->getTimestamp() <= $endDateTime->getTimestamp()) { $showDay = new CcShowDays(); $showDay->setDbFirstShow($startDateTimeClone->format("Y-m-d")); $showDay->setDbLastShow($endDate); $showDay->setDbStartTime($startDateTimeClone->format("H:i")); $showDay->setDbTimezone(date_default_timezone_get()); $showDay->setDbDuration($data['add_show_duration']); $showDay->setDbDay($day); $showDay->setDbRepeatType($repeatType); $showDay->setDbShowId($showId); $showDay->setDbRecord($isRecorded); $showDay->save(); } } } //check if we are adding or updating a show, and if updating //erase all the show's future show_rebroadcast information first. if ($data['add_show_id'] != -1 && isset($data['add_show_rebroadcast']) && $data['add_show_rebroadcast']) { CcShowRebroadcastQuery::create()->filterByDbShowId($data['add_show_id'])->delete(); } //adding rows to cc_show_rebroadcast /* TODO: Document magic constant 10 and define it properly somewhere --RG */ if ($isRecorded && $data['add_show_rebroadcast'] && $repeatType != -1) { for ($i = 1; $i <= 10; $i++) { if ($data['add_show_rebroadcast_date_' . $i]) { $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($data['add_show_rebroadcast_date_' . $i]); $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } elseif ($isRecorded && $data['add_show_rebroadcast'] && $repeatType == -1) { for ($i = 1; $i <= 10; $i++) { if ($data['add_show_rebroadcast_date_absolute_' . $i]) { //$con = Propel::getConnection(CcShowPeer::DATABASE_NAME); //$sql = "SELECT date '{$data['add_show_rebroadcast_date_absolute_'.$i]}' - date '{$data['add_show_start_date']}' "; $sql = <<<SQL SELECT :rebroadcast::date - :start::date SQL; $offset_days = Application_Common_Database::prepareAndExecute($sql, array('rebroadcast' => $data["add_show_rebroadcast_date_absolute_{$i}"], 'start' => $data['add_show_start_date']), "column"); //$r = $con->query($sql); //$offset_days = $r->fetchColumn(0); $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($offset_days . " days"); $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_absolute_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } //check if we are adding or updating a show, and if updating //erase all the show's show_rebroadcast information first. if ($data['add_show_id'] != -1) { CcShowHostsQuery::create()->filterByDbShow($data['add_show_id'])->delete(); } if (is_array($data['add_show_hosts'])) { //add selected hosts to cc_show_hosts table. foreach ($data['add_show_hosts'] as $host) { $showHost = new CcShowHosts(); $showHost->setDbShow($showId); $showHost->setDbHost($host); $showHost->save(); } } if ($data['add_show_id'] != -1) { $con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); $con->beginTransaction(); //current timesamp in UTC. $current_timestamp = gmdate("Y-m-d H:i:s"); try { //update the status flag in cc_schedule. $instances = CcShowInstancesQuery::create()->filterByDbEnds($current_timestamp, Criteria::GREATER_THAN)->filterByDbShowId($data['add_show_id'])->find($con); foreach ($instances as $instance) { $instance->updateScheduleStatus($con); } $con->commit(); } catch (Exception $e) { $con->rollback(); Logging::info("Couldn't update schedule status."); Logging::info($e->getMessage()); } } Application_Model_Show::populateShowUntil($showId); Application_Model_RabbitMq::PushSchedule(); return $showId; }
/** * Create a show. * * Note: end dates are non inclusive. * * @param array $data * @return int * Show ID */ public static function create($data) { $con = Propel::getConnection(CcShowPeer::DATABASE_NAME); $sql = "SELECT EXTRACT(DOW FROM TIMESTAMP '{$data['add_show_start_date']} {$data['add_show_start_time']}')"; $r = $con->query($sql); $startDow = $r->fetchColumn(0); if ($data['add_show_no_end']) { $endDate = NULL; } else { if ($data['add_show_repeats']) { $sql = "SELECT date '{$data['add_show_end_date']}' + INTERVAL '1 day' "; $r = $con->query($sql); $endDate = $r->fetchColumn(0); } else { $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '1 day' "; $r = $con->query($sql); $endDate = $r->fetchColumn(0); } } //only want the day of the week from the start date. if (!$data['add_show_repeats']) { $data['add_show_day_check'] = array($startDow); } else { if ($data['add_show_repeats'] && $data['add_show_day_check'] == "") { $data['add_show_day_check'] = array($startDow); } } //find repeat type or set to a non repeating show. $repeatType = $data['add_show_repeats'] ? $data['add_show_repeat_type'] : -1; if ($data['add_show_id'] == -1) { $ccShow = new CcShow(); } else { $ccShow = CcShowQuery::create()->findPK($data['add_show_id']); } $ccShow->setDbName($data['add_show_name']); $ccShow->setDbDescription($data['add_show_description']); $ccShow->setDbUrl($data['add_show_url']); $ccShow->setDbGenre($data['add_show_genre']); $ccShow->setDbColor($data['add_show_color']); $ccShow->setDbBackgroundColor($data['add_show_background_color']); $ccShow->save(); $showId = $ccShow->getDbId(); $show = new Show($showId); $isRecorded = $data['add_show_record'] ? 1 : 0; if ($data['add_show_id'] != -1) { $show->deletePossiblyInvalidInstances($data, $endDate, $isRecorded, $repeatType); } //check if we are adding or updating a show, and if updating //erase all the show's show_days information first. if ($data['add_show_id'] != -1) { CcShowDaysQuery::create()->filterByDbShowId($data['add_show_id'])->delete(); } //don't set day for monthly repeat type, it's invalid. if ($data['add_show_repeats'] && $data['add_show_repeat_type'] == 2) { $showDay = new CcShowDays(); $showDay->setDbFirstShow($data['add_show_start_date']); $showDay->setDbLastShow($endDate); $showDay->setDbStartTime($data['add_show_start_time']); $showDay->setDbDuration($data['add_show_duration']); $showDay->setDbRepeatType($repeatType); $showDay->setDbShowId($showId); $showDay->setDbRecord($isRecorded); $showDay->save(); } else { foreach ($data['add_show_day_check'] as $day) { if ($startDow !== $day) { if ($startDow > $day) { $daysAdd = 6 - $startDow + 1 + $day; } else { $daysAdd = $day - $startDow; } $sql = "SELECT date '{$data['add_show_start_date']}' + INTERVAL '{$daysAdd} day' "; $r = $con->query($sql); $start = $r->fetchColumn(0); } else { $start = $data['add_show_start_date']; } if (strtotime($start) <= strtotime($endDate) || is_null($endDate)) { $showDay = new CcShowDays(); $showDay->setDbFirstShow($start); $showDay->setDbLastShow($endDate); $showDay->setDbStartTime($data['add_show_start_time']); $showDay->setDbDuration($data['add_show_duration']); $showDay->setDbDay($day); $showDay->setDbRepeatType($repeatType); $showDay->setDbShowId($showId); $showDay->setDbRecord($isRecorded); $showDay->save(); } } } $date = new DateHelper(); $currentTimestamp = $date->getTimestamp(); //check if we are adding or updating a show, and if updating //erase all the show's future show_rebroadcast information first. if ($data['add_show_id'] != -1 && $data['add_show_rebroadcast']) { CcShowRebroadcastQuery::create()->filterByDbShowId($data['add_show_id'])->delete(); } //adding rows to cc_show_rebroadcast if ($isRecorded && $data['add_show_rebroadcast'] && $repeatType != -1) { for ($i = 1; $i <= 10; $i++) { if ($data['add_show_rebroadcast_date_' . $i]) { $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($data['add_show_rebroadcast_date_' . $i]); $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } else { if ($isRecorded && $data['add_show_rebroadcast'] && $repeatType == -1) { for ($i = 1; $i <= 10; $i++) { if ($data['add_show_rebroadcast_date_absolute_' . $i]) { $sql = "SELECT date '{$data['add_show_rebroadcast_date_absolute_' . $i]}' - date '{$data['add_show_start_date']}' "; $r = $con->query($sql); $offset_days = $r->fetchColumn(0); $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($offset_days . " days"); $showRebroad->setDbStartTime($data['add_show_rebroadcast_time_absolute_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } } //check if we are adding or updating a show, and if updating //erase all the show's show_rebroadcast information first. if ($data['add_show_id'] != -1) { CcShowHostsQuery::create()->filterByDbShow($data['add_show_id'])->delete(); } if (is_array($data['add_show_hosts'])) { //add selected hosts to cc_show_hosts table. foreach ($data['add_show_hosts'] as $host) { $showHost = new CcShowHosts(); $showHost->setDbShow($showId); $showHost->setDbHost($host); $showHost->save(); } } Show::populateShowUntil($showId); RabbitMq::PushSchedule(); return $showId; }
/** * * Sets the fields for a cc_show_rebroadcast table row * @param $showData * @param $showId * @param $repeatType * @param $isRecorded */ private function setCcShowRebroadcasts($showData) { $showId = $this->ccShow->getDbId(); if ($this->isRecorded && $showData['add_show_rebroadcast'] && $this->repeatType != -1) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) { if ($showData['add_show_rebroadcast_date_' . $i]) { $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($showData['add_show_rebroadcast_date_' . $i]); $showRebroad->setDbStartTime($showData['add_show_rebroadcast_time_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } elseif ($this->isRecorded && $showData['add_show_rebroadcast'] && $this->repeatType == -1) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) { if ($showData['add_show_rebroadcast_date_absolute_' . $i]) { $rebroadcastDate = new DateTime($showData["add_show_rebroadcast_date_absolute_{$i}"]); $startDate = new DateTime($showData['add_show_start_date']); $offsetDays = $startDate->diff($rebroadcastDate); $showRebroad = new CcShowRebroadcast(); $showRebroad->setDbDayOffset($offsetDays->format("%a days")); $showRebroad->setDbStartTime($showData['add_show_rebroadcast_time_absolute_' . $i]); $showRebroad->setDbShowId($showId); $showRebroad->save(); } } } }