From de8f7b0ff7952cc5538c39648d21466e2eea8417 Mon Sep 17 00:00:00 2001 From: denise Date: Mon, 29 Apr 2013 15:27:14 -0400 Subject: [PATCH] CC-4961: Show linking --- .../controllers/_old_ScheduleController.php | 1002 -------- airtime_mvc/application/models/Scheduler.php | 50 +- airtime_mvc/application/models/Show.php | 1 - .../application/models/_old_Schedule.php | 1329 ---------- airtime_mvc/application/models/_old_Show.php | 2241 ----------------- .../application/models/airtime/CcShow.php | 1 + .../application/services/ShowService.php | 3 + 7 files changed, 24 insertions(+), 4603 deletions(-) delete mode 100644 airtime_mvc/application/controllers/_old_ScheduleController.php delete mode 100644 airtime_mvc/application/models/_old_Schedule.php delete mode 100644 airtime_mvc/application/models/_old_Show.php diff --git a/airtime_mvc/application/controllers/_old_ScheduleController.php b/airtime_mvc/application/controllers/_old_ScheduleController.php deleted file mode 100644 index 08fde37dc..000000000 --- a/airtime_mvc/application/controllers/_old_ScheduleController.php +++ /dev/null @@ -1,1002 +0,0 @@ -_helper->getHelper('AjaxContext'); - $ajaxContext->addActionContext('event-feed', 'json') - ->addActionContext('event-feed-preload', 'json') - ->addActionContext('make-context-menu', 'json') - ->addActionContext('add-show-dialog', 'json') - ->addActionContext('add-show', 'json') - ->addActionContext('edit-show', 'json') - ->addActionContext('move-show', 'json') - ->addActionContext('resize-show', 'json') - ->addActionContext('delete-show', 'json') - ->addActionContext('show-content-dialog', 'json') - ->addActionContext('clear-show', 'json') - ->addActionContext('get-current-playlist', 'json') - ->addActionContext('remove-group', 'json') - ->addActionContext('populate-show-form', 'json') - ->addActionContext('populate-show-instance-form', 'json') - ->addActionContext('cancel-show', 'json') - ->addActionContext('cancel-current-show', 'json') - ->addActionContext('get-form', 'json') - ->addActionContext('upload-to-sound-cloud', 'json') - ->addActionContext('content-context-menu', 'json') - ->addActionContext('set-time-scale', 'json') - ->addActionContext('set-time-interval', 'json') - ->addActionContext('edit-show-instance', 'json') - ->addActionContext('dj-edit-show', 'json') - ->addActionContext('calculate-duration', 'json') - ->addActionContext('get-current-show', 'json') - ->addActionContext('update-future-is-scheduled', 'json') - ->initContext(); - - $this->sched_sess = new Zend_Session_Namespace("schedule"); - } - - public function indexAction() - { - $CC_CONFIG = Config::getConfig(); - - $baseUrl = Application_Common_OsPath::getBaseDir(); - - $this->view->headScript()->appendFile($baseUrl.'js/contextmenu/jquery.contextMenu.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - - //full-calendar-functions.js requires this variable, so that datePicker widget can be offset to server time instead of client time - $this->view->headScript()->appendScript("var timezoneOffset = ".date("Z")."; //in seconds"); - $this->view->headScript()->appendFile($baseUrl.'js/airtime/schedule/full-calendar-functions.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - - $this->view->headScript()->appendFile($baseUrl.'js/fullcalendar/fullcalendar.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/timepicker/jquery.ui.timepicker.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/colorpicker/js/colorpicker.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - - $this->view->headScript()->appendFile($baseUrl.'js/airtime/schedule/add-show.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/airtime/schedule/schedule.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/blockui/jquery.blockUI.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - - $this->view->headLink()->appendStylesheet($baseUrl.'css/jquery.ui.timepicker.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/fullcalendar.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/colorpicker/css/colorpicker.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/add-show.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/jquery.contextMenu.css?'.$CC_CONFIG['airtime_version']); - - //Start Show builder JS/CSS requirements - $this->view->headScript()->appendFile($baseUrl.'js/contextmenu/jquery.contextMenu.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/js/jquery.dataTables.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.pluginAPI.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.fnSetFilteringDelay.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.ColVis.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.ColReorder.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.FixedColumns.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.TableTools.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/datatables/plugin/dataTables.columnFilter.js?'.$CC_CONFIG['airtime_version'], 'text/javascript'); - - $this->view->headScript()->appendFile($baseUrl.'js/airtime/buttons/buttons.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/airtime/library/events/library_showbuilder.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/airtime/library/library.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - $this->view->headScript()->appendFile($baseUrl.'js/airtime/showbuilder/builder.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - - $this->view->headLink()->appendStylesheet($baseUrl.'css/media_library.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/jquery.contextMenu.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/datatables/css/ColVis.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/datatables/css/ColReorder.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/TableTools.css?'.$CC_CONFIG['airtime_version']); - $this->view->headLink()->appendStylesheet($baseUrl.'css/showbuilder.css?'.$CC_CONFIG['airtime_version']); - //End Show builder JS/CSS requirements - - - Application_Model_Schedule::createNewFormSections($this->view); - $user = Application_Model_User::getCurrentUser(); - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - $this->view->preloadShowForm = true; - } - - $this->view->headScript()->appendScript( - "var calendarPref = {};\n". - "calendarPref.weekStart = ".Application_Model_Preference::GetWeekStartDay().";\n". - "calendarPref.timestamp = ".time().";\n". - "calendarPref.timezoneOffset = ".date("Z").";\n". - "calendarPref.timeScale = '".Application_Model_Preference::GetCalendarTimeScale()."';\n". - "calendarPref.timeInterval = ".Application_Model_Preference::GetCalendarTimeInterval().";\n". - "calendarPref.weekStartDay = ".Application_Model_Preference::GetWeekStartDay().";\n". - "var calendarEvents = null;" - ); - } - - public function eventFeedAction() - { - $start = new DateTime($this->_getParam('start', null)); - $start->setTimezone(new DateTimeZone("UTC")); - $end = new DateTime($this->_getParam('end', null)); - $end->setTimezone(new DateTimeZone("UTC")); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - $editable = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - - $events = &Application_Model_Show::getFullCalendarEvents($start, $end, $editable); - $this->view->events = $events; - } - - public function eventFeedPreloadAction() - { - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - $editable = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - - $calendar_interval = Application_Model_Preference::GetCalendarTimeScale(); - Logging::info($calendar_interval); - if ($calendar_interval == "agendaDay") { - list($start, $end) = Application_Model_Show::getStartEndCurrentDayView(); - } else if ($calendar_interval == "agendaWeek") { - list($start, $end) = Application_Model_Show::getStartEndCurrentWeekView(); - } else if ($calendar_interval == "month") { - list($start, $end) = Application_Model_Show::getStartEndCurrentMonthView(); - } else { - Logging::error("Invalid Calendar Interval '$calendar_interval'"); - } - - $events = &Application_Model_Show::getFullCalendarEvents($start, $end, $editable); - $this->view->events = $events; - } - - public function getCurrentShowAction() - { - $currentShow = Application_Model_Show::getCurrentShow(); - if (!empty($currentShow)) { - $this->view->si_id = $currentShow[0]["instance_id"]; - $this->view->current_show = true; - } else { - $this->view->current_show = false; - } - } - - public function moveShowAction() - { - $deltaDay = $this->_getParam('day'); - $deltaMin = $this->_getParam('min'); - $showInstanceId = $this->_getParam('showInstanceId'); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - try { - $showInstance = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - $error = $showInstance->moveShow($deltaDay, $deltaMin); - } - - if (isset($error)) { - $this->view->error = $error; - } - } - - public function resizeShowAction() - { - $deltaDay = $this->_getParam('day'); - $deltaMin = $this->_getParam('min'); - $showId = $this->_getParam('showId'); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - try { - $show = new Application_Model_Show($showId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - $error = $show->resizeShow($deltaDay, $deltaMin); - } - - if (isset($error)) { - $this->view->error = $error; - } - } - - public function deleteShowAction() - { - $showInstanceId = $this->_getParam('id'); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - - try { - $showInstance = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - Logging::info($e->getMessage()); - $this->view->show_error = true; - - return false; - } - - $showInstance->delete(); - - $this->view->show_id = $showInstance->getShowId(); - } - } - - public function uploadToSoundCloudAction() - { - $show_instance = $this->_getParam('id'); - try { - $show_inst = new Application_Model_ShowInstance($show_instance); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - $file = $show_inst->getRecordedFile(); - $id = $file->getId(); - Application_Model_Soundcloud::uploadSoundcloud($id); - // we should die with ui info - $this->_helper->json->sendJson(null); - } - - public function makeContextMenuAction() - { - $id = $this->_getParam('id'); - $menu = array(); - $epochNow = time(); - $baseUrl = Application_Common_OsPath::getBaseDir(); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - try { - $instance = new Application_Model_ShowInstance($id); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - $isDJ = $user->isHostOfShow($instance->getShowId()); - - $showStartLocalDT = Application_Common_DateHelper::ConvertToLocalDateTime($instance->getShowInstanceStart()); - $showEndLocalDT = Application_Common_DateHelper::ConvertToLocalDateTime($instance->getShowInstanceEnd()); - - if ($instance->isRecorded() && $epochNow > $showEndLocalDT->getTimestamp()) { - - $file = $instance->getRecordedFile(); - $fileId = $file->getId(); - - $menu["view_recorded"] = array("name" => _("View Recorded File Metadata"), "icon" => "overview", - "url" => $baseUrl."library/edit-file-md/id/".$fileId); - } - - if ($epochNow < $showStartLocalDT->getTimestamp()) { - if ( ($isAdminOrPM || $isDJ) - && !$instance->isRecorded() - && !$instance->isRebroadcast()) { - - $menu["schedule"] = array("name"=> _("Add / Remove Content"), "icon" => "add-remove-content", - "url" => $baseUrl."showbuilder/builder-dialog/"); - - $menu["clear"] = array("name"=> _("Remove All Content"), "icon" => "remove-all-content", - "url" => $baseUrl."schedule/clear-show"); - } - } - - if (!$instance->isRecorded()) { - - $menu["content"] = array("name"=> _("Show Content"), "icon" => "overview", "url" => $baseUrl."schedule/show-content-dialog"); - } - - if ($showEndLocalDT->getTimestamp() <= $epochNow - && $instance->isRecorded() - && Application_Model_Preference::GetUploadToSoundcloudOption()) { - - $file = $instance->getRecordedFile(); - $fileId = $file->getId(); - $scid = $instance->getSoundCloudFileId(); - - if ($scid > 0) { - $url = $file->getSoundCloudLinkToFile(); - $menu["soundcloud_view"] = array("name" => _("View on Soundcloud"), "icon" => "soundcloud", "url" => $url); - } - - $text = is_null($scid) ? _('Upload to SoundCloud') : _('Re-upload to SoundCloud'); - $menu["soundcloud_upload"] = array("name"=> $text, "icon" => "soundcloud"); - } - - if ($showStartLocalDT->getTimestamp() <= $epochNow && - $epochNow < $showEndLocalDT->getTimestamp() && $isAdminOrPM) { - - if ($instance->isRecorded()) { - $menu["cancel_recorded"] = array("name"=> _("Cancel Current Show"), "icon" => "delete"); - } else { - - if (!$instance->isRebroadcast()) { - $menu["edit"] = array("name"=> _("Edit Show"), "icon" => "edit", "_type"=>"all", "url" => $baseUrl."Schedule/populate-show-form"); - } - - $menu["cancel"] = array("name"=> _("Cancel Current Show"), "icon" => "delete"); - } - } - - if ($epochNow < $showStartLocalDT->getTimestamp()) { - - if (!$instance->isRebroadcast() && $isAdminOrPM) { - $menu["edit"] = array("name"=> _("Edit Show"), "icon" => "edit", "_type"=>"all", "url" => $baseUrl."Schedule/populate-show-form"); - } - - if ($instance->getShow()->isRepeating() && $isAdminOrPM) { - - //create delete sub menu. - $menu["del"] = array("name"=> _("Delete"), "icon" => "delete", "items" => array()); - - $menu["del"]["items"]["single"] = array("name"=> _("Delete This Instance"), "icon" => "delete", "url" => $baseUrl."schedule/delete-show"); - - $menu["del"]["items"]["following"] = array("name"=> _("Delete This Instance and All Following"), "icon" => "delete", "url" => $baseUrl."schedule/cancel-show"); - } elseif ($isAdminOrPM) { - - $menu["del"] = array("name"=> _("Delete"), "icon" => "delete", "url" => $baseUrl."schedule/delete-show"); - } - } - - $this->view->items = $menu; - } - - public function clearShowAction() - { - $showInstanceId = $this->_getParam('id'); - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - try { - $show = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - if($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)) || $user->isHostOfShow($show->getShowId())) - $show->clearShow(); - } - - public function getCurrentPlaylistAction() - { - $range = Application_Model_Schedule::GetPlayOrderRange(); - $show = Application_Model_Show::getCurrentShow(); - - /* Convert all UTC times to localtime before sending back to user. */ - if (isset($range["previous"])) { - $range["previous"]["starts"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["previous"]["starts"]); - $range["previous"]["ends"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["previous"]["ends"]); - } - if (isset($range["current"])) { - $range["current"]["starts"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["current"]["starts"]); - $range["current"]["ends"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["current"]["ends"]); - } - if (isset($range["next"])) { - $range["next"]["starts"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["next"]["starts"]); - $range["next"]["ends"] = Application_Common_DateHelper::ConvertToLocalDateTimeString($range["next"]["ends"]); - } - - Application_Model_Show::convertToLocalTimeZone($range["currentShow"], array("starts", "ends", "start_timestamp", "end_timestamp")); - Application_Model_Show::convertToLocalTimeZone($range["nextShow"], array("starts", "ends", "start_timestamp", "end_timestamp")); - - $source_status = array(); - $switch_status = array(); - $live_dj = Application_Model_Preference::GetSourceStatus("live_dj"); - $master_dj = Application_Model_Preference::GetSourceStatus("master_dj"); - - $scheduled_play_switch = Application_Model_Preference::GetSourceSwitchStatus("scheduled_play"); - $live_dj_switch = Application_Model_Preference::GetSourceSwitchStatus("live_dj"); - $master_dj_switch = Application_Model_Preference::GetSourceSwitchStatus("master_dj"); - - //might not be the correct place to implement this but for now let's just do it here - $source_status['live_dj_source'] = $live_dj; - $source_status['master_dj_source'] = $master_dj; - $this->view->source_status = $source_status; - - $switch_status['live_dj_source'] = $live_dj_switch; - $switch_status['master_dj_source'] = $master_dj_switch; - $switch_status['scheduled_play'] = $scheduled_play_switch; - $this->view->switch_status = $switch_status; - - $this->view->entries = $range; - $this->view->show_name = isset($show[0])?$show[0]["name"]:""; - } - - public function removeGroupAction() - { - $showInstanceId = $this->sched_sess->showInstanceId; - $group_id = $this->_getParam('groupId'); - - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - try { - $show = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)) || $user->isHostOfShow($show->getShowId())) { - $show->removeGroupFromShow($group_id); - } - - $this->view->showContent = $show->getShowContent(); - $this->view->timeFilled = $show->getTimeScheduled(); - $this->view->percentFilled = $show->getPercentScheduled(); - $this->view->chosen = $this->view->render('schedule/scheduled-content.phtml'); - unset($this->view->showContent); - } - - public function showContentDialogAction() - { - $showInstanceId = $this->_getParam('id'); - try { - $show = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - $originalShowId = $show->isRebroadcast(); - if (!is_null($originalShowId)) { - try { - $originalShow = new Application_Model_ShowInstance($originalShowId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - $originalShowName = $originalShow->getName(); - $originalShowStart = $originalShow->getShowInstanceStart(); - - //convert from UTC to user's timezone for display. - $originalDateTime = new DateTime($originalShowStart, new DateTimeZone("UTC")); - $originalDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - //$timestamp = Application_Common_DateHelper::ConvertToLocalDateTimeString($originalDateTime->format("Y-m-d H:i:s")); - $this->view->additionalShowInfo = - sprintf(_("Rebroadcast of show %s from %s at %s"), - $originalShowName, - $originalDateTime->format("l, F jS"), - $originalDateTime->format("G:i")); - } - $this->view->showLength = $show->getShowLength(); - $this->view->timeFilled = $show->getTimeScheduled(); - $this->view->percentFilled = $show->getPercentScheduled(); - $this->view->showContent = $show->getShowListContent(); - $this->view->dialog = $this->view->render('schedule/show-content-dialog.phtml'); - $this->view->showTitle = htmlspecialchars($show->getName()); - unset($this->view->showContent); - } - - // we removed edit show instance option in menu item - // this feature is disabled in 2.1 and should be back in 2.2 - /*public function populateShowInstanceFormAction(){ - $formWhat = new Application_Form_AddShowWhat(); - $formWho = new Application_Form_AddShowWho(); - $formWhen = new Application_Form_AddShowWhen(); - $formRepeats = new Application_Form_AddShowRepeats(); - $formStyle = new Application_Form_AddShowStyle(); - $formLive = new Application_Form_AddShowLiveStream(); - - $formWhat->removeDecorator('DtDdWrapper'); - $formWho->removeDecorator('DtDdWrapper'); - $formWhen->removeDecorator('DtDdWrapper'); - $formRepeats->removeDecorator('DtDdWrapper'); - $formStyle->removeDecorator('DtDdWrapper'); - - $this->view->what = $formWhat; - $this->view->when = $formWhen; - $this->view->repeats = $formRepeats; - $this->view->who = $formWho; - $this->view->style = $formStyle; - $this->view->live = $formLive; - $this->view->addNewShow = false; - - $showInstanceId = $this->_getParam('id'); - - $show_instance = CcShowInstancesQuery::create()->findPK($showInstanceId); - $show = new Application_Model_Show($show_instance->getDbShowId()); - - $starts_string = $show_instance->getDbStarts(); - $ends_string = $show_instance->getDbEnds(); - - $starts_datetime = new DateTime($starts_string, new DateTimeZone("UTC")); - $ends_datetime = new DateTime($ends_string, new DateTimeZone("UTC")); - - $starts_datetime->setTimezone(new DateTimeZone(date_default_timezone_get())); - $ends_datetime->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $instance_duration = $starts_datetime->diff($ends_datetime); - - $formWhat->populate(array('add_show_id' => $show->getId(), - 'add_show_instance_id' => $showInstanceId, - 'add_show_name' => $show->getName(), - 'add_show_url' => $show->getUrl(), - 'add_show_genre' => $show->getGenre(), - 'add_show_description' => $show->getDescription())); - - $formWhen->populate(array('add_show_start_date' => $starts_datetime->format("Y-m-d"), - 'add_show_start_time' => $starts_datetime->format("H:i"), - 'add_show_end_date_no_repeat' => $ends_datetime->format("Y-m-d"), - 'add_show_end_time' => $ends_datetime->format("H:i"), - 'add_show_duration' => $instance_duration->format("%h"))); - - $formWhat->disable(); - $formWho->disable(); - $formWhen->disableRepeatCheckbox(); - $formRepeats->disable(); - $formStyle->disable(); - - //$formRecord->disable(); - //$formAbsoluteRebroadcast->disable(); - //$formRebroadcast->disable(); - - $this->view->action = "edit-show-instance"; - $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); - }*/ - - public function populateShowFormAction() - { - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - - $showInstanceId = $this->_getParam('id'); - - $this->view->action = "edit-show"; - try { - $showInstance = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - - $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - $isDJ = $user->isHostOfShow($showInstance->getShowId()); - - if (!($isAdminOrPM || $isDJ)) { - return; - } - - // in case a user was once a dj and had been assigned to a show - // but was then changed to an admin user we need to allow - // the user to edit the show as an admin (CC-4925) - if ($isDJ && !$isAdminOrPM) { - $this->view->action = "dj-edit-show"; - } - - $formWhat = new Application_Form_AddShowWhat(); - $formWho = new Application_Form_AddShowWho(); - $formWhen = new Application_Form_AddShowWhen(); - $formRepeats = new Application_Form_AddShowRepeats(); - $formStyle = new Application_Form_AddShowStyle(); - $formLive = new Application_Form_AddShowLiveStream(); - - $formWhat->removeDecorator('DtDdWrapper'); - $formWho->removeDecorator('DtDdWrapper'); - $formWhen->removeDecorator('DtDdWrapper'); - $formRepeats->removeDecorator('DtDdWrapper'); - $formStyle->removeDecorator('DtDdWrapper'); - - $this->view->what = $formWhat; - $this->view->when = $formWhen; - $this->view->repeats = $formRepeats; - $this->view->who = $formWho; - $this->view->style = $formStyle; - $this->view->live = $formLive; - $this->view->addNewShow = false; - - $show = new Application_Model_Show($showInstance->getShowId()); - - $formWhat->populate(array('add_show_id' => $show->getId(), - 'add_show_instance_id' => $showInstanceId, - 'add_show_name' => $show->getName(), - 'add_show_url' => $show->getUrl(), - 'add_show_genre' => $show->getGenre(), - 'add_show_description' => $show->getDescription())); - - $startsDateTime = new DateTime($show->getStartDate()." ".$show->getStartTime(), new DateTimeZone("UTC")); - $endsDateTime = new DateTime($show->getEndDate()." ".$show->getEndTime(), new DateTimeZone("UTC")); - - $startsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - $endsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $formWhen->populate(array('add_show_start_date' => $startsDateTime->format("Y-m-d"), - 'add_show_start_time' => $startsDateTime->format("H:i"), - 'add_show_end_date_no_repeat' => $endsDateTime->format("Y-m-d"), - 'add_show_end_time' => $endsDateTime->format("H:i"), - 'add_show_duration' => $show->getDuration(true), - 'add_show_repeats' => $show->isRepeating() ? 1 : 0)); - - if ($show->isStartDateTimeInPast()) { - // for a non-repeating show, we should never allow user to change the start time. - // for the repeating show, we should allow because the form works as repeating template form - if (!$showInstance->getShow()->isRepeating()) { - $formWhen->disableStartDateAndTime(); - } else { - $nextFutureRepeatShow = $show->getNextFutureRepeatShowTime(); - $formWhen->getElement('add_show_start_date')->setValue($nextFutureRepeatShow["starts"]->format("Y-m-d")); - $formWhen->getElement('add_show_start_time')->setValue($nextFutureRepeatShow["starts"]->format("H:i")); - $formWhen->getElement('add_show_end_date_no_repeat')->setValue($nextFutureRepeatShow["ends"]->format("Y-m-d")); - $formWhen->getElement('add_show_end_time')->setValue($nextFutureRepeatShow["ends"]->format("H:i")); - } - } - - //need to get the days of the week in the php timezone (for the front end). - $days = array(); - $showDays = CcShowDaysQuery::create()->filterByDbShowId($showInstance->getShowId())->find(); - foreach ($showDays as $showDay) { - $showStartDay = new DateTime($showDay->getDbFirstShow(), new DateTimeZone($showDay->getDbTimezone())); - $showStartDay->setTimezone(new DateTimeZone(date_default_timezone_get())); - array_push($days, $showStartDay->format('w')); - } - - $displayedEndDate = new DateTime($show->getRepeatingEndDate(), new DateTimeZone($showDays[0]->getDbTimezone())); - $displayedEndDate->sub(new DateInterval("P1D"));//end dates are stored non-inclusively. - $displayedEndDate->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $formRepeats->populate(array('add_show_repeat_type' => $show->getRepeatType(), - 'add_show_day_check' => $days, - 'add_show_end_date' => $displayedEndDate->format("Y-m-d"), - 'add_show_no_end' => ($show->getRepeatingEndDate() == ''))); - - $hosts = array(); - $showHosts = CcShowHostsQuery::create()->filterByDbShow($showInstance->getShowId())->find(); - foreach ($showHosts as $showHost) { - array_push($hosts, $showHost->getDbHost()); - } - $formWho->populate(array('add_show_hosts' => $hosts)); - $formStyle->populate(array('add_show_background_color' => $show->getBackgroundColor(), - 'add_show_color' => $show->getColor())); - - $formLive->populate($show->getLiveStreamInfo()); - - $formRecord = new Application_Form_AddShowRR(); - $formAbsoluteRebroadcast = new Application_Form_AddShowAbsoluteRebroadcastDates(); - $formRebroadcast = new Application_Form_AddShowRebroadcastDates(); - - $formRecord->removeDecorator('DtDdWrapper'); - $formAbsoluteRebroadcast->removeDecorator('DtDdWrapper'); - $formRebroadcast->removeDecorator('DtDdWrapper'); - - $this->view->rr = $formRecord; - $this->view->absoluteRebroadcast = $formAbsoluteRebroadcast; - $this->view->rebroadcast = $formRebroadcast; - - $formRecord->populate(array('add_show_record' => $show->isRecorded(), - 'add_show_rebroadcast' => $show->isRebroadcast())); - - $formRecord->getElement('add_show_record')->setOptions(array('disabled' => true)); - - - - $rebroadcastsRelative = $show->getRebroadcastsRelative(); - $rebroadcastFormValues = array(); - $i = 1; - foreach ($rebroadcastsRelative as $rebroadcast) { - $rebroadcastFormValues["add_show_rebroadcast_date_$i"] = $rebroadcast['day_offset']; - $rebroadcastFormValues["add_show_rebroadcast_time_$i"] = Application_Common_DateHelper::removeSecondsFromTime($rebroadcast['start_time']); - $i++; - } - $formRebroadcast->populate($rebroadcastFormValues); - - $rebroadcastsAbsolute = $show->getRebroadcastsAbsolute(); - $rebroadcastAbsoluteFormValues = array(); - $i = 1; - foreach ($rebroadcastsAbsolute as $rebroadcast) { - $rebroadcastAbsoluteFormValues["add_show_rebroadcast_date_absolute_$i"] = $rebroadcast['start_date']; - $rebroadcastAbsoluteFormValues["add_show_rebroadcast_time_absolute_$i"] = $rebroadcast['start_time']; - $i++; - } - $formAbsoluteRebroadcast->populate($rebroadcastAbsoluteFormValues); - if (!$isAdminOrPM) { - $formRecord->disable(); - $formAbsoluteRebroadcast->disable(); - $formRebroadcast->disable(); - } - - if (!$isAdminOrPM) { - $formWhat->disable(); - $formWho->disable(); - $formWhen->disable(); - $formRepeats->disable(); - $formStyle->disable(); - } - - $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); - $this->view->entries = 5; - } - - public function getFormAction() - { - $user = Application_Model_User::getCurrentUser(); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - Application_Model_Schedule::createNewFormSections($this->view); - $this->view->form = $this->view->render('schedule/add-show-form.phtml'); - } - } - - public function djEditShowAction() - { - $js = $this->_getParam('data'); - $data = array(); - - //need to convert from serialized jQuery array. - foreach ($js as $j) { - $data[$j["name"]] = $j["value"]; - } - - //update cc_show - $show = new Application_Model_Show($data["add_show_id"]); - $show->setAirtimeAuthFlag($data["cb_airtime_auth"]); - $show->setCustomAuthFlag($data["cb_custom_auth"]); - $show->setCustomUsername($data["custom_username"]); - $show->setCustomPassword($data["custom_password"]); - - $this->view->edit = true; - } - - /*public function editShowInstanceAction(){ - $js = $this->_getParam('data'); - $data = array(); - - //need to convert from serialized jQuery array. - foreach ($js as $j) { - $data[$j["name"]] = $j["value"]; - } - - $success = Application_Model_Schedule::updateShowInstance($data, $this); - if ($success) { - $this->view->addNewShow = true; - $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); - } else { - $this->view->addNewShow = false; - $this->view->form = $this->view->render('schedule/add-show-form.phtml'); - } - }*/ - - public function editShowAction() - { - //1) Get add_show_start_date since it might not have been sent - $js = $this->_getParam('data'); - $data = array(); - - //need to convert from serialized jQuery array. - foreach ($js as $j) { - $data[$j["name"]] = $j["value"]; - } - - $data['add_show_hosts'] = $this->_getParam('hosts'); - $data['add_show_day_check'] = $this->_getParam('days'); - - if ($data['add_show_day_check'] == "") { - $data['add_show_day_check'] = null; - } - - $show = new Application_Model_Show($data['add_show_id']); - - $validateStartDate = true; - $validateStartTime = true; - if (!array_key_exists('add_show_start_date', $data)) { - //Changing the start date was disabled, since the - //array key does not exist. We need to repopulate this entry from the db. - //The start date will be returned in UTC time, so lets convert it to local time. - $dt = Application_Common_DateHelper::ConvertToLocalDateTime($show->getStartDateAndTime()); - $data['add_show_start_date'] = $dt->format("Y-m-d"); - - if (!array_key_exists('add_show_start_time', $data)) { - $data['add_show_start_time'] = $dt->format("H:i"); - $validateStartTime = false; - } - $validateStartDate = false; - } - $data['add_show_record'] = $show->isRecorded(); - - if ($show->isRepeating()) { - $nextFutureRepeatShow = $show->getNextFutureRepeatShowTime(); - $originalShowStartDateTime = $nextFutureRepeatShow["starts"]; - } else { - $originalShowStartDateTime = Application_Common_DateHelper::ConvertToLocalDateTime( - $show->getStartDateAndTime()); - } - - $success = Application_Model_Schedule::addUpdateShow($data, $this, - $validateStartDate, $originalShowStartDateTime, true, - $data['add_show_instance_id']); - - if ($success) { - $scheduler = new Application_Model_Scheduler(); - $showInstances = CcShowInstancesQuery::create()->filterByDbShowId($data['add_show_id'])->find(); - foreach ($showInstances as $si) { - $scheduler->removeGaps($si->getDbId()); - } - $this->view->addNewShow = true; - $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); - } else { - if (!$validateStartDate) { - $this->view->when->getElement('add_show_start_date')->setOptions(array('disabled' => true)); - } - if (!$validateStartTime) { - $this->view->when->getElement('add_show_start_time')->setOptions(array('disabled' => true)); - } - $this->view->rr->getElement('add_show_record')->setOptions(array('disabled' => true)); - $this->view->addNewShow = false; - $this->view->action = "edit-show"; - $this->view->form = $this->view->render('schedule/add-show-form.phtml'); - } - } - - public function addShowAction() - { - $js = $this->_getParam('data'); - $data = array(); - - //need to convert from serialized jQuery array. - foreach ($js as $j) { - $data[$j["name"]] = $j["value"]; - } - - $data['add_show_hosts'] = $this->_getParam('hosts'); - $data['add_show_day_check'] = $this->_getParam('days'); - - if ($data['add_show_day_check'] == "") { - $data['add_show_day_check'] = null; - } - - $validateStartDate = true; - $success = Application_Model_Schedule::addUpdateShow($data, $this, - $validateStartDate); - - if ($success) { - $this->view->addNewShow = true; - $this->view->newForm = $this->view->render( - 'schedule/add-show-form.phtml'); - Logging::debug("Show creation succeeded"); - } else { - $this->view->addNewShow = true; - $this->view->form = $this->view->render( - 'schedule/add-show-form.phtml'); - Logging::debug("Show creation failed"); - } - } - - public function cancelShowAction() - { - $user = Application_Model_User::getCurrentUser(); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - $showInstanceId = $this->_getParam('id'); - - try { - $showInstance = new Application_Model_ShowInstance($showInstanceId); - } catch (Exception $e) { - $this->view->show_error = true; - - return false; - } - $show = new Application_Model_Show($showInstance->getShowId()); - - $show->cancelShow($showInstance->getShowInstanceStart()); - $this->view->show_id = $showInstance->getShowId(); - } - } - - public function cancelCurrentShowAction() - { - $user = Application_Model_User::getCurrentUser(); - - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - $id = $this->_getParam('id'); - - try { - $scheduler = new Application_Model_Scheduler(); - $scheduler->cancelShow($id); - Application_Model_StoredFile::updatePastFilesIsScheduled(); - // send kick out source stream signal to pypo - $data = array("sourcename"=>"live_dj"); - Application_Model_RabbitMq::SendMessageToPypo("disconnect_source", $data); - } catch (Exception $e) { - $this->view->error = $e->getMessage(); - Logging::info($e->getMessage()); - } - } - } - - public function contentContextMenuAction() - { - $id = $this->_getParam('id'); - - $params = '/format/json/id/#id#/'; - - $paramsPop = str_replace('#id#', $id, $params); - - // added for downlaod - $id = $this->_getParam('id'); - - $file_id = $this->_getParam('id', null); - $file = Application_Model_StoredFile::Recall($file_id); - - $baseUrl = $this->getRequest()->getBaseUrl(); - $url = $file->getRelativeFileUrl($baseUrl).'download/true'; - $menu[] = array('action' => array('type' => 'gourl', 'url' => $url), - 'title' => _('Download')); - - //returns format jjmenu is looking for. - $this->_helper->json->sendJson($menu); - } - - /** - * Sets the user specific preference for which time scale to use in Calendar. - * This is only being used by schedule.js at the moment. - */ - public function setTimeScaleAction() - { - Application_Model_Preference::SetCalendarTimeScale($this->_getParam('timeScale')); - } - -/** - * Sets the user specific preference for which time interval to use in Calendar. - * This is only being used by schedule.js at the moment. - */ - public function setTimeIntervalAction() - { - Application_Model_Preference::SetCalendarTimeInterval($this->_getParam('timeInterval')); - } - - public function calculateDurationAction() - { - $startParam = $this->_getParam('startTime'); - $endParam = $this->_getParam('endTime'); - - try { - $startDateTime = new DateTime($startParam); - $endDateTime = new DateTime($endParam); - - $UTCStartDateTime = $startDateTime->setTimezone(new DateTimeZone('UTC')); - $UTCEndDateTime = $endDateTime->setTimezone(new DateTimeZone('UTC')); - - $duration = $UTCEndDateTime->diff($UTCStartDateTime); - - $day = intval($duration->format('%d')); - if ($day > 0) { - $hour = intval($duration->format('%h')); - $min = intval($duration->format('%i')); - $hour += $day * 24; - $hour = min($hour, 99); - $sign = $duration->format('%r'); - $result = sprintf('%s%02dh %02dm', $sign, $hour, $min); - } else { - $result = $duration->format('%r%Hh %Im'); - } - } catch (Exception $e) { - $result = "Invalid Date"; - } - - echo Zend_Json::encode($result); - exit(); - } - - public function updateFutureIsScheduledAction() - { - $schedId = $this->_getParam('schedId'); - $redrawLibTable = Application_Model_StoredFile::setIsScheduled($schedId, false); - $this->_helper->json->sendJson(array("redrawLibTable" => $redrawLibTable)); - } -} diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index dd05c19c7..a6aece8c8 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -53,17 +53,20 @@ class Application_Model_Scheduler $isDestinationLinked = $destinationCcShowInstance->getCcShow()->isLinked(); foreach ($itemsToMove as $itemToMove) { + $sourceInstanceId = $itemToMove["instance"]; $ccShowInstance = CcShowInstancesQuery::create() - ->findPk($itemToMove["instance"]); + ->findPk($sourceInstanceId); //does the item being moved belong to a linked show $isSourceLinked = $ccShowInstance->getCcShow()->isLinked(); - if ($isSourceLinked && $itemToMove["instance"] != $destinationInstanceId) { - throw new Exception(_("Linked items can only be moved within its own show")); - } /*elseif ($isDestinationLinked && $isSourceLinked) { - throw new Exception(_("Items can only be moved to and from unlinked shows")); - }*/ + if ($isDestinationLinked && !$isSourceLinked) { + throw new Exception("Cannot move items into linked shows"); + } elseif (!$isDestinationLinked && $isSourceLinked) { + throw new Exception("Cannot move items out of linked shows"); + } elseif ($isSourceLinked && $sourceInstanceId != $destinationInstanceId) { + throw new Exception(_("Cannot move items out of linked shows")); + } } } @@ -496,7 +499,6 @@ class Application_Model_Scheduler */ $instances = $this->getInstances($schedule["instance"]); foreach($instances as $instance) { - if ($id !== 0) { $schedItem = CcScheduleQuery::create()->findPK($id, $this->con); /* We use the selected cursor's position to find the same @@ -537,12 +539,6 @@ class Application_Model_Scheduler $pstart = microtime(true); $initalStartDT = clone $nextStartDT; - /*$followingSchedItems = CcScheduleQuery::create() - ->filterByDBStarts($nextStartDT->format("Y-m-d H:i:s.u"), Criteria::GREATER_EQUAL) - ->filterByDbInstanceId($instance->getDbId()) - ->filterByDbId($excludeIds, Criteria::NOT_IN) - ->orderByDbStarts() - ->find($this->con);*/ $pend = microtime(true); Logging::debug("finding all following items."); @@ -559,11 +555,6 @@ class Application_Model_Scheduler //item existed previously and is being moved. //need to keep same id for resources if we want REST. if (isset($file['sched_id'])) { - - /*$sched = CcScheduleQuery::create() - ->filterByDbInstanceId($instance->getDbId()) - ->filterByDbFileId($file["id"]) - ->findOne();*/ $sched = CcScheduleQuery::create()->findPk($file["sched_id"]); $excludeIds[] = intval($sched->getDbId()); @@ -590,8 +581,11 @@ class Application_Model_Scheduler ->setDbFadeIn($file['fadein']) ->setDbFadeOut($file['fadeout']) ->setDbClipLength($file['cliplength']) - ->setDbPosition($pos) - ->setDbInstanceId($instance->getDbId()); + ->setDbPosition($pos); + //->setDbInstanceId($instance->getDbId()); + if (!$moveAction) { + $sched->setDbInstanceId($instance->getDbId()); + } switch ($file["type"]) { case 0: @@ -640,7 +634,10 @@ class Application_Model_Scheduler $pstart = microtime(true); //recalculate the start/end times after the inserted items. + Logging::info($excludeIds); + Logging::info($initalStartDT); foreach ($followingSchedItems as $item) { + Logging::info($item); $endTimeDT = $this->findEndTime($nextStartDT, $item->getDbClipLength()); $item->setDbStarts($nextStartDT); @@ -674,13 +671,6 @@ class Application_Model_Scheduler $instance->updateScheduleStatus($this->con); } - // update is_scheduled flag for each cc_file - /*foreach ($filesToInsert as $file) { - $db_file = CcFilesQuery::create()->findPk($file['id'], $this->con); - $db_file->setDbIsScheduled(true); - $db_file->save($this->con); - }*/ - $endProfile = microtime(true); Logging::debug("updating show instances status."); Logging::debug(floatval($endProfile) - floatval($startProfile)); @@ -849,7 +839,7 @@ class Application_Model_Scheduler } } - public function removeItems($scheduledItems, $adjustSched = true) + public function removeItems($scheduledItems, $adjustSched = true, $cancelShow=false) { $showInstances = array(); $this->con->beginTransaction(); @@ -872,7 +862,7 @@ class Application_Model_Scheduler //check if instance is linked and if so get the schedule items //for all linked instances so we can delete them too - if ($instance->getCcShow()->isLinked()) { + if (!$cancelShow && $instance->getCcShow()->isLinked()) { //returns all linked instances if linked $ccShowInstances = $this->getInstances($instance->getDbId()); $instanceIds = array(); @@ -998,7 +988,7 @@ class Application_Model_Scheduler $remove[$i]["id"] = $items[$i]->getDbId(); } - $this->removeItems($remove, false); + $this->removeItems($remove, false, true); } } else { $rebroadcasts = $instance->getCcShowInstancessRelatedByDbId(null, $this->con); diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 121e0a3e1..186603f97 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1664,7 +1664,6 @@ SQL; $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil(); //if application is requesting shows past our previous populated until date, generate shows up until this point. if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $end_timestamp->getTimestamp()) { - //Application_Model_Show::populateAllShowsInRange($showsPopUntil, $end_timestamp); $service_show = new Application_Service_ShowService(); $ccShow = $service_show->delegateInstanceCreation(null, $end_timestamp, true); Application_Model_Preference::SetShowsPopulatedUntil($end_timestamp); diff --git a/airtime_mvc/application/models/_old_Schedule.php b/airtime_mvc/application/models/_old_Schedule.php deleted file mode 100644 index 1aac2566e..000000000 --- a/airtime_mvc/application/models/_old_Schedule.php +++ /dev/null @@ -1,1329 +0,0 @@ - NOW() AT TIME ZONE 'UTC' -SQL; - $count = Application_Common_Database::prepareAndExecute( $sql, array( - ':file_id'=>$p_fileId), 'column'); - return (is_numeric($count) && ($count != '0')); - } - - public static function getAllFutureScheduledFiles() - { - $con = Propel::getConnection(); - $sql = << now() AT TIME ZONE 'UTC' -AND file_id is not null -SQL; - $files = $con->query($sql)->fetchAll(); - $real_files = array(); - foreach ($files as $f) { - $real_files[] = $f['file_id']; - } - - return $real_files; - } - - public static function getAllFutureScheduledWebstreams() - { - $con = Propel::getConnection(); - $sql = << now() AT TIME ZONE 'UTC' -AND stream_id is not null -SQL; - $streams = $con->query($sql)->fetchAll(); - $real_streams = array(); - foreach ($streams as $s) { - $real_streams[] = $s['stream_id']; - } - - return $real_streams; - } - /** - * Returns data related to the scheduled items. - * - * @param int $p_prev - * @param int $p_next - * @return date - */ - public static function GetPlayOrderRange($p_prev = 1, $p_next = 1) - { - if (!is_int($p_prev) || !is_int($p_next)) { - //must enter integers to specify ranges - Logging::info("Invalid range parameters: $p_prev or $p_next"); - - return array(); - } - - $date = new Application_Common_DateHelper; - $timeNow = $date->getTimestamp(); - $utcTimeNow = $date->getUtcTimestamp(); - - $shows = Application_Model_Show::getPrevCurrentNext($utcTimeNow); - $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null; - $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null; - $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; - $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcTimeNow); - - $range = array("env"=>APPLICATION_ENV, - "schedulerTime"=>$timeNow, - "previous"=>$results['previous'] !=null?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null), - "current"=>$results['current'] !=null?$results['current']:((count($shows['currentShow'])>0 && $shows['currentShow'][0]['record'] == 1)?$shows['currentShow'][0]:null), - "next"=> $results['next'] !=null?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null), - "currentShow"=>$shows['currentShow'], - "nextShow"=>$shows['nextShow'], - "timezone"=> date("T"), - "timezoneOffset"=> date("Z") - ); - - return $range; - } - - /** - * Queries the database for the set of schedules one hour before - * and after the given time. If a show starts and ends within that - * time that is considered the current show. Then the scheduled item - * before it is the previous show, and the scheduled item after it - * is the next show. This way the dashboard getCurrentPlaylist is - * very fast. But if any one of the three show types are not found - * through this mechanism a call is made to the old way of querying - * the database to find the track info. - **/ - public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $p_timeNow) - { - if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null) { - return; - } - - $sql = "SELECT %%columns%% st.starts as starts, st.ends as ends, - st.media_item_played as media_item_played, si.ends as show_ends - %%tables%% WHERE "; - - $fileColumns = "ft.artist_name, ft.track_title, "; - $fileJoin = "FROM cc_schedule st JOIN cc_files ft ON st.file_id = ft.id - LEFT JOIN cc_show_instances si ON st.instance_id = si.id"; - - $streamColumns = "ws.name AS artist_name, wm.liquidsoap_data AS track_title, "; - $streamJoin = << $rows[$i]["show_ends"]) { - $rows[$i]['ends'] = $rows[$i]["show_ends"]; - } - - if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)) { - if ($i - 1 >= 0) { - $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], - "starts"=>$rows[$i-1]["starts"], - "ends"=>$rows[$i-1]["ends"], - "type"=>'track'); - } - $results['current'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], - "starts"=>$rows[$i]["starts"], - "ends"=> (($rows[$i]["ends"] > $rows[$i]["show_ends"]) ? $rows[$i]["show_ends"]: $rows[$i]["ends"]), - "media_item_played"=>$rows[$i]["media_item_played"], - "record"=>0, - "type"=>'track'); - if (isset($rows[$i+1])) { - $results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], - "starts"=>$rows[$i+1]["starts"], - "ends"=>$rows[$i+1]["ends"], - "type"=>'track'); - } - break; - } - if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { - $previousIndex = $i; - } - if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { - $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], - "starts"=>$rows[$i]["starts"], - "ends"=>$rows[$i]["ends"], - "type"=>'track'); - break; - } - } - //If we didn't find a a current show because the time didn't fit we may still have - //found a previous show so use it. - if ($results['previous'] === null && isset($previousIndex)) { - $results['previous'] = array("name"=>$rows[$previousIndex]["artist_name"]." - ".$rows[$previousIndex]["track_title"], - "starts"=>$rows[$previousIndex]["starts"], - "ends"=>$rows[$previousIndex]["ends"]);; - } - - return $results; - } - - public static function GetLastScheduleItem($p_timeNow) - { - $sql = <<= sit.starts - AND st.starts < sit.ends -ORDER BY st.ends DESC LIMIT 1; -SQL; - $row = Application_Common_Database::prepareAndExecute($sql, array(':timeNow'=>$p_timeNow)); - - return $row; - } - - public static function GetCurrentScheduleItem($p_timeNow, $p_instanceId) - { - /* Note that usually there will be one result returned. In some - * rare cases two songs are returned. This happens when a track - * that was overbooked from a previous show appears as if it - * hasnt ended yet (track end time hasn't been reached yet). For - * this reason, we need to get the track that starts later, as - * this is the *real* track that is currently playing. So this - * is why we are ordering by track start time. */ - $sql = "SELECT *" - ." FROM cc_schedule st" - ." LEFT JOIN cc_files ft" - ." ON st.file_id = ft.id" - ." WHERE st.starts <= TIMESTAMP :timeNow1" - ." AND st.instance_id = :instanceId" - ." AND st.ends > TIMESTAMP :timeNow2" - ." ORDER BY st.starts DESC" - ." LIMIT 1"; - - $row = Application_Common_Database::prepareAndExecute($sql, array(':timeNow1'=>$p_timeNow, ':instanceId'=>$p_instanceId, ':timeNow2'=>$p_timeNow,)); - - return $row; - } - - public static function GetNextScheduleItem($p_timeNow) - { - $sql = "SELECT" - ." ft.artist_name, ft.track_title," - ." st.starts as starts, st.ends as ends" - ." FROM cc_schedule st" - ." LEFT JOIN cc_files ft" - ." ON st.file_id = ft.id" - ." LEFT JOIN cc_show_instances sit" - ." ON st.instance_id = sit.id" - ." WHERE st.starts > TIMESTAMP :timeNow" - ." AND st.starts >= sit.starts" //this and the next line are necessary since we can overbook shows. - ." AND st.starts < sit.ends" - ." ORDER BY st.starts" - ." LIMIT 1"; - - $row = Application_Common_Database::prepareAndExecute($sql, array(':timeNow'=>$p_timeNow)); - - return $row; - } - - /* - * - * @param DateTime $p_startDateTime - * - * @param DateTime $p_endDateTime - * - * @return array $scheduledItems - * - */ - public static function GetScheduleDetailItems($p_start, $p_end, $p_shows) - { - $con = Propel::getConnection(); - - $p_start_str = $p_start->format("Y-m-d H:i:s"); - $p_end_str = $p_end->format("Y-m-d H:i:s"); - - - //We need to search 24 hours before and after the show times so that that we - //capture all of the show's contents. - $p_track_start= $p_start->sub(new DateInterval("PT24H"))->format("Y-m-d H:i:s"); - $p_track_end = $p_end->add(new DateInterval("PT24H"))->format("Y-m-d H:i:s"); - - $templateSql = <<= '{$p_track_start}' - AND sched.starts < '{$p_track_end}') - OR (sched.ends > '{$p_track_start}' - AND sched.ends <= '{$p_track_end}') - OR (sched.starts <= '{$p_track_start}' - AND sched.ends >= '{$p_track_end}')) - ) -SQL; - - - $filesSql = str_replace("%%columns%%", - $filesColumns, - $templateSql); - $filesSql= str_replace("%%join%%", - $filesJoin, - $filesSql); - - $streamColumns = <<= '{$p_track_start}' - AND sched.starts < '{$p_track_end}') - OR (sched.ends > '{$p_track_start}' - AND sched.ends <= '{$p_track_end}') - OR (sched.starts <= '{$p_track_start}' - AND sched.ends >= '{$p_track_end}')) - ) - LEFT JOIN cc_subjs AS sub ON (ws.creator_id = sub.id) -SQL; - - $streamSql = str_replace("%%columns%%", - $streamColumns, - $templateSql); - $streamSql = str_replace("%%join%%", - $streamJoin, - $streamSql); - - - $showPredicate = ""; - if (count($p_shows) > 0) { - $showPredicate = " AND show_id IN (".implode(",", $p_shows).")"; - } - - $sql = <<= '{$p_start_str}' - AND si.starts < '{$p_end_str}') - OR (si.ends > '{$p_start_str}' - AND si.ends <= '{$p_end_str}') - OR (si.starts <= '{$p_start_str}' - AND si.ends >= '{$p_end_str}')) -ORDER BY si_starts, - sched_starts; -SQL; - - $rows = $con->query($sql)->fetchAll(PDO::FETCH_ASSOC); - - return $rows; - } - - public static function UpdateMediaPlayedStatus($p_id) - { - $con = Propel::getConnection(); - $sql = "UPDATE cc_schedule" - ." SET media_item_played=TRUE"; - // we need to update 'broadcasted' column as well - // check the current switch status - $live_dj = Application_Model_Preference::GetSourceSwitchStatus('live_dj') == 'on'; - $master_dj = Application_Model_Preference::GetSourceSwitchStatus('master_dj') == 'on'; - $scheduled_play = Application_Model_Preference::GetSourceSwitchStatus('scheduled_play') == 'on'; - - if (!$live_dj && !$master_dj && $scheduled_play) { - $sql .= ", broadcasted=1"; - } - - $sql .= " WHERE id=$p_id"; - - $retVal = $con->exec($sql); - - return $retVal; - } - - public static function UpdateBrodcastedStatus($dateTime, $value) - { - $now = $dateTime->format("Y-m-d H:i:s"); - - $sql = <<= :ends::TIMESTAMP -SQL; - - $retVal = Application_Common_Database::prepareAndExecute($sql, array( - ':broadcastedValue' => $value, - ':starts' => $now, - ':ends' => $now), 'execute'); - return $retVal; - } - - public static function getSchduledPlaylistCount() - { - $con = Propel::getConnection(); - $sql = "SELECT count(*) as cnt FROM cc_schedule"; - - return $con->query($sql)->fetchColumn(0); - } - - /** - * Convert a time string in the format "YYYY-MM-DD HH:mm:SS" - * to "YYYY-MM-DD-HH-mm-SS". - * - * @param string $p_time - * @return string - */ - private static function AirtimeTimeToPypoTime($p_time) - { - $p_time = substr($p_time, 0, 19); - $p_time = str_replace(" ", "-", $p_time); - $p_time = str_replace(":", "-", $p_time); - - return $p_time; - } - - /** - * Convert a time string in the format "YYYY-MM-DD-HH-mm-SS" to - * "YYYY-MM-DD HH:mm:SS". - * - * @param string $p_time - * @return string - */ - private static function PypoTimeToAirtimeTime($p_time) - { - $t = explode("-", $p_time); - - return $t[0]."-".$t[1]."-".$t[2]." ".$t[3].":".$t[4].":00"; - } - - /** - * Return true if the input string is in the format YYYY-MM-DD-HH-mm - * - * @param string $p_time - * @return boolean - */ - public static function ValidPypoTimeFormat($p_time) - { - $t = explode("-", $p_time); - if (count($t) != 5) { - return false; - } - foreach ($t as $part) { - if (!is_numeric($part)) { - return false; - } - } - - return true; - } - - /** - * Converts a time value as a string (with format HH:MM:SS.mmmmmm) to - * millisecs. - * - * @param string $p_time - * @return int - */ - public static function WallTimeToMillisecs($p_time) - { - $t = explode(":", $p_time); - $millisecs = 0; - if (strpos($t[2], ".")) { - $secParts = explode(".", $t[2]); - $millisecs = $secParts[1]; - $millisecs = str_pad(substr($millisecs, 0, 3),3, '0'); - $millisecs = intval($millisecs); - $seconds = intval($secParts[0]); - } else { - $seconds = intval($t[2]); - } - $ret = $millisecs + ($seconds * 1000) + ($t[1] * 60 * 1000) + ($t[0] * 60 * 60 * 1000); - - return $ret; - } - - /** - * Compute the difference between two times in the format . - * "HH:MM:SS.mmmmmm" Note: currently only supports calculating . - * millisec differences . - * - * @param string $p_time1 - * @param string $p_time2 - * @return double - */ - private static function TimeDiff($p_time1, $p_time2) - { - $parts1 = explode(".", $p_time1); - $parts2 = explode(".", $p_time2); - $diff = 0; - if ( (count($parts1) > 1) && (count($parts2) > 1) ) { - $millisec1 = substr($parts1[1], 0, 3); - $millisec1 = str_pad($millisec1, 3, "0"); - $millisec1 = intval($millisec1); - $millisec2 = substr($parts2[1], 0, 3); - $millisec2 = str_pad($millisec2, 3, "0"); - $millisec2 = intval($millisec2); - $diff = abs($millisec1 - $millisec2)/1000; - } - - return $diff; - } - - /** - * Returns an array of schedule items from cc_schedule table. Tries - * to return at least 3 items (if they are available). The parameters - * $p_startTime and $p_endTime specify the range. Schedule items returned - * do not have to be entirely within this range. It is enough that the end - * or beginning of the scheduled item is in the range. - * - * - * @param string $p_startTime - * In the format YYYY-MM-DD HH:MM:SS.nnnnnn - * @param string $p_endTime - * In the format YYYY-MM-DD HH:MM:SS.nnnnnn - * @return array - * Returns null if nothing found, else an array of associative - * arrays representing each row. - */ - public static function getItems($p_startTime, $p_endTime) - { - $baseQuery = << :startTime1 - AND st.starts < :endTime - AND st.playout_status > 0 - AND si.ends > :startTime2 - AND si.modified_instance = 'f' -ORDER BY st.starts -SQL; - - $sql = $baseQuery." ".$predicates; - - $rows = Application_Common_Database::prepareAndExecute($sql, array( - ':startTime1' => $p_startTime, - ':endTime' => $p_endTime, - ':startTime2' => $p_startTime)); - - if (count($rows) < 3) { - $dt = new DateTime("@".time()); - $dt->add(new DateInterval("PT24H")); - $range_end = $dt->format("Y-m-d H:i:s"); - - $predicates = << :startTime1 - AND st.starts < :rangeEnd - AND st.playout_status > 0 - AND si.ends > :startTime2 - AND si.modified_instance = 'f' -ORDER BY st.starts LIMIT 3 -SQL; - - $sql = $baseQuery." ".$predicates." "; - $rows = Application_Common_Database::prepareAndExecute($sql, - array( - ':startTime1' => $p_startTime, - ':rangeEnd' => $range_end, - ':startTime2' => $p_startTime)); - } - - return $rows; - } - - /** - * This function will ensure that an existing index in the - * associative array is never overwritten, instead appending - * _0, _1, _2, ... to the end of the key to make sure it is unique - */ - private static function appendScheduleItem(&$data, $time, $item) - { - $key = $time; - $i = 0; - - while (array_key_exists($key, $data["media"])) { - $key = "{$time}_{$i}"; - $i++; - } - - $data["media"][$key] = $item; - } - - private static function createInputHarborKickTimes(&$data, $range_start, $range_end) - { - $utcTimeZone = new DateTimeZone("UTC"); - $kick_times = Application_Model_ShowInstance::GetEndTimeOfNextShowWithLiveDJ($range_start, $range_end); - foreach ($kick_times as $kick_time_info) { - $kick_time = $kick_time_info['ends']; - $temp = explode('.', Application_Model_Preference::GetDefaultTransitionFade()); - // we round down transition time since PHP cannot handle millisecond. We need to - // handle this better in the future - $transition_time = intval($temp[0]); - $switchOffDataTime = new DateTime($kick_time, $utcTimeZone); - $switch_off_time = $switchOffDataTime->sub(new DateInterval('PT'.$transition_time.'S')); - $switch_off_time = $switch_off_time->format("Y-m-d H:i:s"); - - $kick_start = self::AirtimeTimeToPypoTime($kick_time); - $data["media"][$kick_start]['start'] = $kick_start; - $data["media"][$kick_start]['end'] = $kick_start; - $data["media"][$kick_start]['event_type'] = "kick_out"; - $data["media"][$kick_start]['type'] = "event"; - $data["media"][$kick_start]['independent_event'] = true; - - if ($kick_time !== $switch_off_time) { - $switch_start = self::AirtimeTimeToPypoTime($switch_off_time); - $data["media"][$switch_start]['start'] = $switch_start; - $data["media"][$switch_start]['end'] = $switch_start; - $data["media"][$switch_start]['event_type'] = "switch_off"; - $data["media"][$switch_start]['type'] = "event"; - $data["media"][$switch_start]['independent_event'] = true; - } - } - } - - private static function createFileScheduleEvent(&$data, $item, $media_id, $uri) - { - $start = self::AirtimeTimeToPypoTime($item["start"]); - $end = self::AirtimeTimeToPypoTime($item["end"]); - - list(,,,$start_hour,,) = explode("-", $start); - list(,,,$end_hour,,) = explode("-", $end); - - $same_hour = $start_hour == $end_hour; - $independent_event = !$same_hour; - - $replay_gain = is_null($item["replay_gain"]) ? "0": $item["replay_gain"]; - $replay_gain += Application_Model_Preference::getReplayGainModifier(); - - if ( !Application_Model_Preference::GetEnableReplayGain() ) { - $replay_gain = 0; - } - - $schedule_item = array( - 'id' => $media_id, - 'type' => 'file', - 'row_id' => $item["id"], - 'uri' => $uri, - 'fade_in' => Application_Model_Schedule::WallTimeToMillisecs($item["fade_in"]), - 'fade_out' => Application_Model_Schedule::WallTimeToMillisecs($item["fade_out"]), - 'cue_in' => Application_Common_DateHelper::CalculateLengthInSeconds($item["cue_in"]), - 'cue_out' => Application_Common_DateHelper::CalculateLengthInSeconds($item["cue_out"]), - 'start' => $start, - 'end' => $end, - 'show_name' => $item["show_name"], - 'replay_gain' => $replay_gain, - 'independent_event' => $independent_event, - ); - - if ($schedule_item['cue_in'] > $schedule_item['cue_out']) { - $schedule_item['cue_in'] = $schedule_item['cue_out']; - } - self::appendScheduleItem($data, $start, $schedule_item); - } - - private static function createStreamScheduleEvent(&$data, $item, $media_id, $uri) - { - $start = self::AirtimeTimeToPypoTime($item["start"]); - $end = self::AirtimeTimeToPypoTime($item["end"]); - - //create an event to start stream buffering 5 seconds ahead of the streams actual time. - $buffer_start = new DateTime($item["start"], new DateTimeZone('UTC')); - $buffer_start->sub(new DateInterval("PT5S")); - - $stream_buffer_start = self::AirtimeTimeToPypoTime($buffer_start->format("Y-m-d H:i:s")); - - $schedule_item = array( - 'start' => $stream_buffer_start, - 'end' => $stream_buffer_start, - 'uri' => $uri, - 'row_id' => $item["id"], - 'type' => 'stream_buffer_start', - 'independent_event' => true - ); - - self::appendScheduleItem($data, $start, $schedule_item); - - $schedule_item = array( - 'id' => $media_id, - 'type' => 'stream_output_start', - 'row_id' => $item["id"], - 'uri' => $uri, - 'start' => $start, - 'end' => $end, - 'show_name' => $item["show_name"], - 'row_id' => $item["id"], - 'independent_event' => true - ); - self::appendScheduleItem($data, $start, $schedule_item); - - //since a stream never ends we have to insert an additional "kick stream" event. The "start" - //time of this event is the "end" time of the stream minus 1 second. - $dt = new DateTime($item["end"], new DateTimeZone('UTC')); - $dt->sub(new DateInterval("PT1S")); - - $stream_end = self::AirtimeTimeToPypoTime($dt->format("Y-m-d H:i:s")); - - $schedule_item = array( - 'start' => $stream_end, - 'end' => $stream_end, - 'uri' => $uri, - 'type' => 'stream_buffer_end', - 'row_id' => $item["id"], - 'independent_event' => true - ); - self::appendScheduleItem($data, $stream_end, $schedule_item); - - $schedule_item = array( - 'start' => $stream_end, - 'end' => $stream_end, - 'uri' => $uri, - 'type' => 'stream_output_end', - 'independent_event' => true - ); - self::appendScheduleItem($data, $stream_end, $schedule_item); - } - - private static function getRangeStartAndEnd($p_fromDateTime, $p_toDateTime) - { - $CC_CONFIG = Config::getConfig(); - - /* if $p_fromDateTime and $p_toDateTime function parameters are null, - then set range * from "now" to "now + 24 hours". */ - if (is_null($p_fromDateTime)) { - $t1 = new DateTime("@".time()); - $range_start = $t1->format("Y-m-d H:i:s"); - } else { - $range_start = Application_Model_Schedule::PypoTimeToAirtimeTime($p_fromDateTime); - } - if (is_null($p_fromDateTime)) { - $t2 = new DateTime("@".time()); - - $cache_ahead_hours = $CC_CONFIG["cache_ahead_hours"]; - - if (is_numeric($cache_ahead_hours)) { - //make sure we are not dealing with a float - $cache_ahead_hours = intval($cache_ahead_hours); - } else { - $cache_ahead_hours = 1; - } - - $t2->add(new DateInterval("PT".$cache_ahead_hours."H")); - $range_end = $t2->format("Y-m-d H:i:s"); - } else { - $range_end = Application_Model_Schedule::PypoTimeToAirtimeTime($p_toDateTime); - } - - return array($range_start, $range_end); - } - - - private static function createScheduledEvents(&$data, $range_start, $range_end) - { - $utcTimeZone = new DateTimeZone("UTC"); - $items = self::getItems($range_start, $range_end); - foreach ($items as $item) { - $showEndDateTime = new DateTime($item["show_end"], $utcTimeZone); - - $trackStartDateTime = new DateTime($item["start"], $utcTimeZone); - $trackEndDateTime = new DateTime($item["end"], $utcTimeZone); - - if ($trackStartDateTime->getTimestamp() > $showEndDateTime->getTimestamp()) { - //do not send any tracks that start past their show's end time - continue; - } - - if ($trackEndDateTime->getTimestamp() > $showEndDateTime->getTimestamp()) { - $di = $trackStartDateTime->diff($showEndDateTime); - - $item["cue_out"] = $di->format("%H:%i:%s").".000"; - $item["end"] = $showEndDateTime->format("Y-m-d H:i:s"); - } - - if (!is_null($item['file_id'])) { - //row is from "file" - $media_id = $item['file_id']; - $storedFile = Application_Model_StoredFile::Recall($media_id); - $uri = $storedFile->getFilePath(); - self::createFileScheduleEvent($data, $item, $media_id, $uri); - } elseif (!is_null($item['stream_id'])) { - //row is type "webstream" - $media_id = $item['stream_id']; - $uri = $item['url']; - self::createStreamScheduleEvent($data, $item, $media_id, $uri); - } else { - throw new Exception("Unknown schedule type: ".print_r($item, true)); - } - - } - } - - /* Check if two events are less than or equal to 1 second apart - */ - public static function areEventsLinked($event1, $event2) { - $dt1 = DateTime::createFromFormat("Y-m-d-H-i-s", $event1['start']); - $dt2 = DateTime::createFromFormat("Y-m-d-H-i-s", $event2['start']); - - $seconds = $dt2->getTimestamp() - $dt1->getTimestamp(); - return $seconds <= 1; - } - - /** - * Streams are a 4 stage process. - * 1) start buffering stream 5 seconds ahead of its start time - * 2) at the start time tell liquidsoap to switch to this source - * 3) at the end time, tell liquidsoap to stop reading this stream - * 4) at the end time, tell liquidsoap to switch away from input.http source. - * - * When we have two streams back-to-back, some of these steps are unnecessary - * for the second stream. Instead of sending commands 1,2,3,4,1,2,3,4 we should - * send 1,2,1,2,3,4 - We don't need to tell liquidsoap to stop reading (#3), because #1 - * of the next stream implies this when we pass in a new url. We also don't need #4. - * - * There's a special case here is well. When the back-to-back streams are the same, we - * can collapse the instructions 1,2,(3,4,1,2),3,4 to 1,2,3,4. We basically cut out the - * middle part. This function handles this. - */ - private static function foldData(&$data) - { - $previous_key = null; - $previous_val = null; - $previous_previous_key = null; - $previous_previous_val = null; - $previous_previous_previous_key = null; - $previous_previous_previous_val = null; - foreach ($data as $k => $v) { - - if ($v["type"] == "stream_output_start" - && !is_null($previous_previous_val) - && $previous_previous_val["type"] == "stream_output_end" - && self::areEventsLinked($previous_previous_val, $v)) { - - unset($data[$previous_previous_previous_key]); - unset($data[$previous_previous_key]); - unset($data[$previous_key]); - if ($previous_previous_val['uri'] == $v['uri']) { - unset($data[$k]); - } - } - - $previous_previous_previous_key = $previous_previous_key; - $previous_previous_previous_val = $previous_previous_val; - $previous_previous_key = $previous_key; - $previous_previous_val = $previous_val; - $previous_key = $k; - $previous_val = $v; - } - } - - public static function getSchedule($p_fromDateTime = null, $p_toDateTime = null) - { - list($range_start, $range_end) = self::getRangeStartAndEnd($p_fromDateTime, $p_toDateTime); - - $data = array(); - $data["media"] = array(); - - //Harbor kick times *MUST* be ahead of schedule events, so that pypo - //executes them first. - self::createInputHarborKickTimes($data, $range_start, $range_end); - self::createScheduledEvents($data, $range_start, $range_end); - - self::foldData($data["media"]); - return $data; - } - - public static function deleteAll() - { - $con = Propel::getConnection(); - $con->exec("TRUNCATE TABLE cc_schedule"); - } - - public static function deleteWithFileId($fileId) - { - $sql = "DELETE FROM cc_schedule WHERE file_id=:file_id"; - Application_Common_Database::prepareAndExecute($sql, array(':file_id'=>$fileId), 'execute'); - } - - public static function createNewFormSections($p_view) - { - $formWhat = new Application_Form_AddShowWhat(); - $formWho = new Application_Form_AddShowWho(); - $formWhen = new Application_Form_AddShowWhen(); - $formRepeats = new Application_Form_AddShowRepeats(); - $formStyle = new Application_Form_AddShowStyle(); - $formLive = new Application_Form_AddShowLiveStream(); - - $formWhat->removeDecorator('DtDdWrapper'); - $formWho->removeDecorator('DtDdWrapper'); - $formWhen->removeDecorator('DtDdWrapper'); - $formRepeats->removeDecorator('DtDdWrapper'); - $formStyle->removeDecorator('DtDdWrapper'); - $formLive->removeDecorator('DtDdWrapper'); - - $p_view->what = $formWhat; - $p_view->when = $formWhen; - $p_view->repeats = $formRepeats; - $p_view->who = $formWho; - $p_view->style = $formStyle; - $p_view->live = $formLive; - - $formWhat->populate(array('add_show_id' => '-1', - 'add_show_instance_id' => '-1')); - $formWhen->populate(array('add_show_start_date' => date("Y-m-d"), - 'add_show_start_time' => '00:00', - 'add_show_end_date_no_repeate' => date("Y-m-d"), - 'add_show_end_time' => '01:00', - 'add_show_duration' => '01h 00m')); - - $formRepeats->populate(array('add_show_end_date' => date("Y-m-d"))); - - $formRecord = new Application_Form_AddShowRR(); - $formAbsoluteRebroadcast = new Application_Form_AddShowAbsoluteRebroadcastDates(); - $formRebroadcast = new Application_Form_AddShowRebroadcastDates(); - - $formRecord->removeDecorator('DtDdWrapper'); - $formAbsoluteRebroadcast->removeDecorator('DtDdWrapper'); - $formRebroadcast->removeDecorator('DtDdWrapper'); - - $p_view->rr = $formRecord; - $p_view->absoluteRebroadcast = $formAbsoluteRebroadcast; - $p_view->rebroadcast = $formRebroadcast; - $p_view->addNewShow = true; - } - - /* This function is responsible for handling the case where an individual - * show instance in a repeating show was edited (via the context menu in the Calendar). - * There is still lots of clean-up to do. For example we shouldn't be passing $controller into - * this method to manipulate the view (this should be done inside the controller function). With - * 2.1 deadline looming, this is OK for now. -Martin */ - public static function updateShowInstance($data, $controller) - { - $formWhat = new Application_Form_AddShowWhat(); - $formWhen = new Application_Form_AddShowWhen(); - $formRepeats = new Application_Form_AddShowRepeats(); - $formWho = new Application_Form_AddShowWho(); - $formStyle = new Application_Form_AddShowStyle(); - $formLive = new Application_Form_AddShowLiveStream(); - - $formWhat->removeDecorator('DtDdWrapper'); - $formWhen->removeDecorator('DtDdWrapper'); - $formRepeats->removeDecorator('DtDdWrapper'); - $formWho->removeDecorator('DtDdWrapper'); - $formStyle->removeDecorator('DtDdWrapper'); - $formLive->removeDecorator('DtDdWrapper'); - - $formRecord = new Application_Form_AddShowRR(); - $formAbsoluteRebroadcast = new Application_Form_AddShowAbsoluteRebroadcastDates(); - $formRebroadcast = new Application_Form_AddShowRebroadcastDates(); - - $formRecord->removeDecorator('DtDdWrapper'); - $formAbsoluteRebroadcast->removeDecorator('DtDdWrapper'); - $formRebroadcast->removeDecorator('DtDdWrapper'); - $when = $formWhen->isValid($data); - - if ($when && $formWhen->checkReliantFields($data, true, null, true)) { - $start_dt = new DateTime($data['add_show_start_date']." ".$data['add_show_start_time'], - new DateTimeZone(date_default_timezone_get())); - $start_dt->setTimezone(new DateTimeZone('UTC')); - - $end_dt = new DateTime($data['add_show_end_date_no_repeat']." ".$data['add_show_end_time'], - new DateTimeZone(date_default_timezone_get())); - $end_dt->setTimezone(new DateTimeZone('UTC')); - - $ccShowInstance = CcShowInstancesQuery::create()->findPK($data["add_show_instance_id"]); - $ccShowInstance->setDbStarts($start_dt); - $ccShowInstance->setDbEnds($end_dt); - $ccShowInstance->save(); - - Application_Model_Schedule::createNewFormSections($controller->view); - - return true; - } else { - $formWhat->disable(); - $formWhen->disableRepeatCheckbox(); - $formRepeats->disable(); - $formWho->disable(); - $formStyle->disable(); - //$formLive->disable(); - - $controller->view->what = $formWhat; - $controller->view->when = $formWhen; - $controller->view->repeats = $formRepeats; - $controller->view->who = $formWho; - $controller->view->style = $formStyle; - $controller->view->live = $formLive; - $controller->view->rr = $formRecord; - $controller->view->absoluteRebroadcast = $formAbsoluteRebroadcast; - $controller->view->rebroadcast = $formRebroadcast; - - //$formRecord->disable(); - //$formAbsoluteRebroadcast->disable(); - //$formRebroadcast->disable(); - - return false; - } - } - - /* This function is responsible for handling the case where the entire show (not a single show instance) - * was edited (via the context menu in the Calendar). - * There is still lots of clean-up to do. For example we shouldn't be passing $controller into - * this method to manipulate the view (this should be done inside the controller function). With - * 2.1 deadline looming, this is OK for now. - * Another clean-up is to move all the form manipulation to the proper form class..... - * -Martin - */ - public static function addUpdateShow($data, $controller, $validateStartDate, - $originalStartDate=null, $update=false, $instanceId=null) - { - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); - $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - - $record = false; - - $formWhat = new Application_Form_AddShowWhat(); - $formWho = new Application_Form_AddShowWho(); - $formWhen = new Application_Form_AddShowWhen(); - $formRepeats = new Application_Form_AddShowRepeats(); - $formStyle = new Application_Form_AddShowStyle(); - $formLive = new Application_Form_AddShowLiveStream(); - - $formWhat->removeDecorator('DtDdWrapper'); - $formWho->removeDecorator('DtDdWrapper'); - $formWhen->removeDecorator('DtDdWrapper'); - $formRepeats->removeDecorator('DtDdWrapper'); - $formStyle->removeDecorator('DtDdWrapper'); - $formLive->removeDecorator('DtDdWrapper'); - - $what = $formWhat->isValid($data); - $when = $formWhen->isValid($data); - $live = $formLive->isValid($data); - if ($when) { - $when = $formWhen->checkReliantFields($data, $validateStartDate, $originalStartDate, $update, $instanceId); - } - - //The way the following code works is that is parses the hour and - //minute from a string with the format "1h 20m" or "2h" or "36m". - //So we are detecting whether an hour or minute value exists via strpos - //and then parse appropriately. A better way to do this in the future is - //actually pass the format from javascript in the format hh:mm so we don't - //have to do this extra String parsing. - $hPos = strpos($data["add_show_duration"], 'h'); - $mPos = strpos($data["add_show_duration"], 'm'); - - $hValue = 0; - $mValue = 0; - - if ($hPos !== false) { - $hValue = trim(substr($data["add_show_duration"], 0, $hPos)); - } - if ($mPos !== false) { - $hPos = $hPos === false ? 0 : $hPos+1; - $mValue = trim(substr($data["add_show_duration"], $hPos, -1 )); - } - - $data["add_show_duration"] = $hValue.":".$mValue; - - $formRecord = new Application_Form_AddShowRR(); - $formAbsoluteRebroadcast = new Application_Form_AddShowAbsoluteRebroadcastDates(); - $formRebroadcast = new Application_Form_AddShowRebroadcastDates(); - - $formRecord->removeDecorator('DtDdWrapper'); - $formAbsoluteRebroadcast->removeDecorator('DtDdWrapper'); - $formRebroadcast->removeDecorator('DtDdWrapper'); - - - $record = $formRecord->isValid($data); - - if ($data["add_show_repeats"]) { - $repeats = $formRepeats->isValid($data); - if ($repeats) { - $repeats = $formRepeats->checkReliantFields($data); - } - $formAbsoluteRebroadcast->reset(); - //make it valid, results don't matter anyways. - $rebroadAb = 1; - - if ($data["add_show_rebroadcast"]) { - $rebroad = $formRebroadcast->isValid($data); - if ($rebroad) { - $rebroad = $formRebroadcast->checkReliantFields($data); - } - } else { - $rebroad = 1; - } - } else { - $repeats = 1; - $formRebroadcast->reset(); - //make it valid, results don't matter anyways. - $rebroad = 1; - - if ($data["add_show_rebroadcast"]) { - $rebroadAb = $formAbsoluteRebroadcast->isValid($data); - if ($rebroadAb) { - $rebroadAb = $formAbsoluteRebroadcast->checkReliantFields($data); - } - } else { - $rebroadAb = 1; - } - } - - $who = $formWho->isValid($data); - $style = $formStyle->isValid($data); - if ($what && $when && $repeats && $who && $style && $live) { - if ($record && $rebroadAb && $rebroad) { - if ($isAdminOrPM) { - Application_Model_Show::create($data); - } - - //send back a new form for the user. - Application_Model_Schedule::createNewFormSections($controller->view); - - //$controller->view->newForm = $controller->view->render('schedule/add-show-form.phtml'); - return true; - } else { - $controller->view->what = $formWhat; - $controller->view->when = $formWhen; - $controller->view->repeats = $formRepeats; - $controller->view->who = $formWho; - $controller->view->style = $formStyle; - $controller->view->rr = $formRecord; - $controller->view->absoluteRebroadcast = $formAbsoluteRebroadcast; - $controller->view->rebroadcast = $formRebroadcast; - $controller->view->live = $formLive; - //$controller->view->addNewShow = !$editShow; - - //$controller->view->form = $controller->view->render('schedule/add-show-form.phtml'); - return false; - - } - } else { - $controller->view->what = $formWhat; - $controller->view->when = $formWhen; - $controller->view->repeats = $formRepeats; - $controller->view->who = $formWho; - $controller->view->style = $formStyle; - $controller->view->live = $formLive; - - $controller->view->rr = $formRecord; - $controller->view->absoluteRebroadcast = $formAbsoluteRebroadcast; - $controller->view->rebroadcast = $formRebroadcast; - //$controller->view->addNewShow = !$editShow; - //$controller->view->form = $controller->view->render('schedule/add-show-form.phtml'); - return false; - } - } - - public static function checkOverlappingShows($show_start, $show_end, - $update=false, $instanceId=null, $showId=null) - { - $overlapping = false; - - $params = array( - ':show_end1' => $show_end->format('Y-m-d H:i:s'), - ':show_end2' => $show_end->format('Y-m-d H:i:s'), - ':show_end3' => $show_end->format('Y-m-d H:i:s') - ); - - - /* If a show is being edited, exclude it from the query - * In both cases (new and edit) we only grab shows that - * are scheduled 2 days prior - */ - if ($update) { - $sql = <<= (date(:show_end3) - INTERVAL '2 days') - AND modified_instance = FALSE -SQL; - if (is_null($showId)) { - $sql .= <<= (date(:show_end3) - INTERVAL '2 days') - AND modified_instance = FALSE -ORDER BY ends -SQL; - - $rows = Application_Common_Database::prepareAndExecute($sql, array( - ':show_end1' => $show_end->format('Y-m-d H:i:s'), - ':show_end2' => $show_end->format('Y-m-d H:i:s'), - ':show_end3' => $show_end->format('Y-m-d H:i:s')), 'all'); - } - - foreach ($rows as $row) { - $start = new DateTime($row["starts"], new DateTimeZone('UTC')); - $end = new DateTime($row["ends"], new DateTimeZone('UTC')); - - if ($show_start->getTimestamp() < $end->getTimestamp() && - $show_end->getTimestamp() > $start->getTimestamp()) { - $overlapping = true; - break; - } - } - - return $overlapping; - } - - public static function GetType($p_scheduleId){ - $scheduledItem = CcScheduleQuery::create()->findPK($p_scheduleId); - if ($scheduledItem->getDbFileId() == null) { - return 'webstream'; - } else { - return 'file'; - } - } - - public static function GetFileId($p_scheduleId) - { - $scheduledItem = CcScheduleQuery::create()->findPK($p_scheduleId); - - return $scheduledItem->getDbFileId(); - } - - public static function GetStreamId($p_scheduleId) - { - $scheduledItem = CcScheduleQuery::create()->findPK($p_scheduleId); - - return $scheduledItem->getDbStreamId(); - } -} diff --git a/airtime_mvc/application/models/_old_Show.php b/airtime_mvc/application/models/_old_Show.php deleted file mode 100644 index f485e41bc..000000000 --- a/airtime_mvc/application/models/_old_Show.php +++ /dev/null @@ -1,2241 +0,0 @@ -_showId = $showId; - } - - public function getName() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbName(); - } - - public function setName($name) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbName($name); - Application_Model_RabbitMq::PushSchedule(); - } - - public function setAirtimeAuthFlag($flag) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbLiveStreamUsingAirtimeAuth($flag); - $show->save(); - } - - public function setCustomAuthFlag($flag) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbLiveStreamUsingCustomAuth($flag); - $show->save(); - } - - public function setCustomUsername($username) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbLiveStreamUser($username); - $show->save(); - } - - public function setCustomPassword($password) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbLiveStreamPass($password); - $show->save(); - } - - public function getDescription() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbDescription(); - } - - public function setDescription($description) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbDescription($description); - } - - public function getColor() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbColor(); - } - - public function setColor($color) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbColor($color); - } - - public function getUrl() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbUrl(); - } - - /*TODO : This method is not actually used anywhere as far as I can tell. We - can safely remove it and probably many other superfluous methods. - --RG*/ - - public function setUrl($p_url) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbUrl($p_url); - } - - public function getGenre() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbGenre(); - } - - public function setGenre($p_genre) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbGenre($p_genre); - } - - public function getBackgroundColor() - { - $show = CcShowQuery::create()->findPK($this->_showId); - - return $show->getDbBackgroundColor(); - } - - public function setBackgroundColor($backgroundColor) - { - $show = CcShowQuery::create()->findPK($this->_showId); - $show->setDbBackgroundColor($backgroundColor); - } - - public function getId() - { - return $this->_showId; - } - - public function getHosts() - { - $sql = << $this->getId() ), 'all'); - - $res = array(); - foreach ($hosts as $host) { - $res[] = $host['first_name']." ".$host['last_name']; - } - return $res; - } - - public function getHostsIds() - { - $sql = << $this->getId() ), 'all'); - - return $hosts; - } - - /** - * remove everything about this show. - */ - public function delete() - { - //usually we hide the show-instance, but in this case we are deleting the show template - //so delete all show-instances as well. - CcShowInstancesQuery::create()->filterByDbOriginalShow($this->_showId)->delete(); - - $show = CcShowQuery::create()->findPK($this->_showId); - $show->delete(); - } - - public function resizeShow($deltaDay, $deltaMin) - { - $con = Propel::getConnection(); - - if ($deltaDay > 0) { - return _("Shows can have a max length of 24 hours."); - } - - $utc = new DateTimeZone("UTC"); - - $nowDateTime = new DateTime("now", $utc); - - $showInstances = CcShowInstancesQuery::create() - ->filterByDbShowId($this->_showId) - ->find($con); - - /* Check two things: - 1. If the show being resized and any of its repeats end in the past - 2. If the show being resized and any of its repeats overlap - with other scheduled shows */ - - foreach ($showInstances as $si) { - $startsDateTime = new DateTime($si->getDbStarts(), new DateTimeZone("UTC")); - $endsDateTime = new DateTime($si->getDbEnds(), new DateTimeZone("UTC")); - - /* The user is moving the show on the calendar from the perspective - of local time. * incase a show is moved across a time change - border offsets should be added to the local * timestamp and - then converted back to UTC to avoid show time changes */ - $startsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - $endsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $newStartsDateTime = Application_Model_ShowInstance::addDeltas($startsDateTime, $deltaDay, $deltaMin); - $newEndsDateTime = Application_Model_ShowInstance::addDeltas($endsDateTime, $deltaDay, $deltaMin); - - if ($newEndsDateTime->getTimestamp() < $nowDateTime->getTimestamp()) { - return _("End date/time cannot be in the past"); - } - - //convert our new starts/ends to UTC. - $newStartsDateTime->setTimezone($utc); - $newEndsDateTime->setTimezone($utc); - - $overlapping = Application_Model_Schedule::checkOverlappingShows( - $newStartsDateTime, $newEndsDateTime, true, $si->getDbId()); - - if ($overlapping) { - return _("Cannot schedule overlapping shows.\nNote: Resizing a repeating show ". - "affects all of its repeats."); - } - } - - $hours = $deltaMin/60; - $hours = ($hours > 0) ? floor($hours) : ceil($hours); - $mins = abs($deltaMin % 60); - - //current timesamp in UTC. - $current_timestamp = gmdate("Y-m-d H:i:s"); - - $sql_gen = << :current_timestamp1) - AND ((ends + :deltaDay2::INTERVAL + :interval2::INTERVAL - starts) <= interval '24:00') -SQL; - - Application_Common_Database::prepareAndExecute($sql_gen, - array( - ':deltaDay1' => "$deltaDay days", - ':interval1' => "$hours:$mins", - ':show_id1' => $this->_showId, - ':current_timestamp1' => $current_timestamp, - ':deltaDay2' => "$deltaDay days", - ':interval2' => "$hours:$mins" - ), "execute"); - - $sql_gen = << "$deltaDay days", - ':interval3' => "$hours:$mins", - ':show_id2' => $this->_showId, - ':deltaDay4' => "$deltaDay days", - ':interval4' => "$hours:$mins" - ), "execute"); - - $con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); - $con->beginTransaction(); - - try { - //update the status flag in cc_schedule. - - /* Since we didn't use a propel object when updating - * cc_show_instances table we need to clear the instances - * so the correct information is retrieved from the db - */ - CcShowInstancesPeer::clearInstancePool(); - - $instances = CcShowInstancesQuery::create() - ->filterByDbEnds($current_timestamp, Criteria::GREATER_THAN) - ->filterByDbShowId($this->_showId) - ->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_RabbitMq::PushSchedule(); - } - - public function cancelShow($day_timestamp) - { - $timeinfo = explode(" ", $day_timestamp); - - CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->update(array('DbLastShow' => $timeinfo[0])); - - $sql = <<= :dayTimestamp::TIMESTAMP - AND show_id = :showId -SQL; - - $rows = Application_Common_Database::prepareAndExecute( $sql, array( - ':dayTimestamp' => $day_timestamp, - ':showId' => $this->getId()), 'all'); - - foreach ($rows as $row) { - try { - $showInstance = new Application_Model_ShowInstance($row["id"]); - $showInstance->delete($rabbitmqPush = false); - } catch (Exception $e) { - Logging::info($e->getMessage()); - } - } - - Application_Model_RabbitMq::PushSchedule(); - } - - /** - * This function is called when a repeating show is edited and the - * days that is repeats on have changed. More specifically, a day - * that the show originally repeated on has been "unchecked". - * - * Removes Show Instances that occur on days of the week specified - * by input array. For example, if array contains one value of "0", - * (0 = Sunday, 1=Monday) then all show instances that occur on - * Sunday are removed. - * - * @param array p_uncheckedDays - * An array specifying which days should be removed. (in the local timezone) - */ - public function removeUncheckedDaysInstances($p_uncheckedDays) - { - //need to convert local doftw to UTC doftw (change made for 2.0 since shows are stored in UTC) - $daysRemovedUTC = array(); - - $showDays = CcShowDaysQuery::create() - ->filterByDbShowId($this->getId()) - ->find(); - - Logging::info("Unchecked days:"); - foreach ($p_uncheckedDays as $day) { - Logging::info($day); - } - - foreach ($showDays as $showDay) { - //Logging::info("Local show day is: {$showDay->getDbDay()}"); - //Logging::info("First show day is: {$showDay->getDbFirstShow()}"); - //Logging::info("Id show days is: {$showDay->getDbId()}"); - - if (in_array($showDay->getDbDay(), $p_uncheckedDays)) { - $showDay->reload(); - //Logging::info("Local show day is: {$showDay->getDbDay()}"); - //Logging::info("First show day is: {$showDay->getDbFirstShow()}"); - //Logging::info("Id show days is: {$showDay->getDbId()}"); - $startDay = new DateTime("{$showDay->getDbFirstShow()} {$showDay->getDbStartTime()}", new DateTimeZone($showDay->getDbTimezone())); - //Logging::info("Show start day: {$startDay->format('Y-m-d H:i:s')}"); - $startDay->setTimezone(new DateTimeZone("UTC")); - //Logging::info("Show start day UTC: {$startDay->format('Y-m-d H:i:s')}"); - $daysRemovedUTC[] = $startDay->format('w'); - //Logging::info("UTC show day is: {$startDay->format('w')}"); - } - } - - $uncheckedDaysImploded = implode(",", $daysRemovedUTC); - $showId = $this->getId(); - - $esc_uncheckedDays = pg_escape_string($uncheckedDaysImploded); - $timestamp = gmdate("Y-m-d H:i:s"); - - $sql = << :timestamp::TIMESTAMP - AND show_id = :showId -SQL; - - Application_Common_Database::prepareAndExecute( $sql, - array( - ":timestamp" => $timestamp, - ":showId" => $showId, - ), "execute"); - } - - /** - * Check whether the current show originated - * from a recording. - * - * @return boolean - * true if originated from recording, otherwise false. - */ - public function isRecorded() - { - $showInstancesRow = CcShowInstancesQuery::create() - ->filterByDbShowId($this->getId()) - ->filterByDbRecord(1) - ->filterByDbModifiedInstance(false) - ->findOne(); - - return !is_null($showInstancesRow); - } - - /** - * Check whether the current show has rebroadcasts of a recorded - * show. Should be used in conjunction with isRecorded(). - * - * @return boolean - * true if show has rebroadcasts, otherwise false. - */ - public function isRebroadcast() - { - $showInstancesRow = CcShowInstancesQuery::create() - ->filterByDbShowId($this->_showId) - ->filterByDbRebroadcast(1) - ->filterByDbModifiedInstance(false) - ->findOne(); - - return !is_null($showInstancesRow); - } - - /** - * Get start time and absolute start date for a recorded - * shows rebroadcasts. For example start date format would be - * YYYY-MM-DD and time would HH:MM - * - * @return array - * array of associate arrays containing "start_date" and "start_time" - */ - public function getRebroadcastsAbsolute() - { - $sql = << $this->getId() ), 'all' ); - - $rebroadcastsLocal = array(); - //get each rebroadcast show in cc_show_instances, convert to current timezone to get start date/time. - /*TODO: refactor the following code to get rid of the $i temporary - variable. -- RG*/ - $i = 0; - - $utc = new DateTimeZone("UTC"); - $dtz = new DateTimeZone( date_default_timezone_get() ); - - foreach ($rebroadcasts as $show) { - $startDateTime = new DateTime($show["starts"], $utc); - $startDateTime->setTimezone($dtz); - - $rebroadcastsLocal[$i]["start_date"] = - $startDateTime->format("Y-m-d"); - $rebroadcastsLocal[$i]["start_time"] = - $startDateTime->format("H:i"); - - $i = $i + 1; - } - - return $rebroadcastsLocal; - } - - /** - * Get start time and relative start date for a recorded - * shows rebroadcasts. For example start date format would be - * "x days" and time would HH:MM:SS - * - * @return array - * array of associate arrays containing "day_offset" and "start_time" - */ - public function getRebroadcastsRelative() - { - $sql = << $this->getId() ), 'all' ); - } - - /** - * Check whether the current show is set to repeat - * repeating shows. - * - * @return boolean - * true if repeating shows, otherwise false. - */ - public function isRepeating() - { - $showDaysRow = CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->findOne(); - - if (!is_null($showDaysRow)) { - return ($showDaysRow->getDbRepeatType() != -1); - } else { - return false; - } - } - - /** - * Get the repeat type of the show. Show can have repeat - * type of "weekly", "bi-weekly" and "monthly". These values - * are represented by 0, 1, and 2 respectively. - * - * @return int - * Return the integer corresponding to the repeat type. - */ - public function getRepeatType() - { - $showDaysRow = CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->findOne(); - - if (!is_null($showDaysRow)) - return $showDaysRow->getDbRepeatType(); - else - return -1; - } - - /** - * Get the end date for a repeating show in the format yyyy-mm-dd - * - * @return string - * Return the end date for the repeating show or the empty - * string if there is no end. - */ - public function getRepeatingEndDate() - { - $sql = << $this->getId() ), 'column' ); - - /* TODO: Why return empty string instead of false? very confusing --RG - */ - return ($query !== false) ? $query : ""; - } - - /** - * Deletes all future instances of the current show object - * from the show_instances table. This function is used when - * a show is being edited - in some cases, when a show is edited - * we just destroy all future show instances, and let another function - * regenerate them later on. Note that this isn't always the most - * desirable thing to do. Deleting a show instance and regenerating - * it cause any scheduled playlists within those show instances to - * be gone for good. - */ - public function deleteAllInstances() - { - $sql = << :timestamp::TIMESTAMP - AND show_id = :showId -SQL; - Application_Common_Database::prepareAndExecute( $sql, - array( ':timestamp' => gmdate("Y-m-d H:i:s"), - ':showId' => $this->getId()), 'execute'); - } - - /** - * Deletes all future rebroadcast instances of the current - * show object from the show_instances table. - */ - public function deleteAllRebroadcasts() - { - $sql = << :timestamp::TIMESTAMP - AND show_id :showId - AND rebroadcast 1 -SQL; - Application_Common_Database::prepareAndExecute( $sql, - array( ':showId' => $this->getId(), - ':timestamp' => gmdate("Y-m-d H:i:s")), 'execute'); - - $con->exec($sql); - } - - /** - * Deletes all show instances of current show after a - * certain date. Note that although not enforced, $p_date - * should never be in the past, as we never want to allow - * deletion of shows that have already occured. - * - * @param string $p_date - * The date which to delete after, if null deletes from the current timestamp. - */ - public function removeAllInstancesFromDate($p_date=null) - { - $con = Propel::getConnection(); - - $timestamp = gmdate("Y-m-d H:i:s"); - - if (is_null($p_date)) { - $date = new Application_Common_DateHelper; - $p_date = $date->getDate(); - } - - $showId = $this->getId(); - $sql = "DELETE FROM cc_show_instances " - ." WHERE date(starts) >= DATE '$p_date'" - ." AND starts > TIMESTAMP '$timestamp'" - ." AND show_id = $showId"; - - $con->exec($sql); - - } - - /** - * Deletes all show instances of current show before a - * certain date. - * - * This function is used in the case where a repeating show is being - * edited and the start date of the first show has been changed more - * into the future. In this case, delete any show instances that - * exist before the new start date. - * - * @param string $p_date - * The date which to delete before - */ - public function removeAllInstancesBeforeDate($p_date) - { - $con = Propel::getConnection(); - - $timestamp = gmdate("Y-m-d H:i:s"); - - $showId = $this->getId(); - $sql = "DELETE FROM cc_show_instances " - ." WHERE date(starts) < DATE '$p_date'" - ." AND starts > TIMESTAMP '$timestamp'" - ." AND show_id = $showId"; - - $con->exec($sql); - } - - public function getNextFutureRepeatShowTime() - { - $sql = << now() at time zone 'UTC' -AND show_id = :showId -ORDER BY starts -LIMIT 1 -SQL; - $result = Application_Common_Database::prepareAndExecute( $sql, - array( 'showId' => $this->getId() ), 'all' ); - - foreach ($result as $r) { - $show["starts"] = new DateTime($r["starts"], new DateTimeZone('UTC')); - $show["ends"] = new DateTime($r["ends"], new DateTimeZone('UTC')); - } - $currentUser = Application_Model_User::getCurrentUser(); - $currentUserId = $currentUser->getId(); - $userTimezone = Application_Model_Preference::GetUserTimezone($currentUserId); - $show["starts"]->setTimezone(new DateTimeZone($userTimezone)); - $show["ends"]->setTimezone(new DateTimeZone($userTimezone)); - return $show; - } - - /** - * Get the start date of the current show in UTC timezone. - * - * @return string - * The start date in the format YYYY-MM-DD or empty string in case - * start date could not be found - */ - public function getStartDateAndTime() - { - $con = Propel::getConnection(); - - $showId = $this->getId(); - $stmt = $con->prepare( - "SELECT first_show, start_time, timezone FROM cc_show_days" - ." WHERE show_id = :showId" - ." ORDER BY first_show" - ." LIMIT 1"); - - $stmt->bindParam(':showId', $showId); - $stmt->execute(); - - if (!$stmt) { - return ""; - } - - $rows = $stmt->fetchAll(); - $row = $rows[0]; - - $dt = new DateTime($row["first_show"]." ".$row["start_time"], new DateTimeZone($row["timezone"])); - $dt->setTimezone(new DateTimeZone("UTC")); - - return $dt->format("Y-m-d H:i"); - } - - /** - * Get the start date of the current show in UTC timezone. - * - * @return string - * The start date in the format YYYY-MM-DD - */ - public function getStartDate() - { - list($date,) = explode(" ", $this->getStartDateAndTime()); - - return $date; - } - - /** - * Get the start time of the current show in UTC timezone. - * - * @return string - * The start time in the format HH:MM - */ - - public function getStartTime() - { - list(,$time) = explode(" ", $this->getStartDateAndTime()); - - return $time; - } - - /** - * Get the end date of the current show. - * Note that this is not the end date of repeated show - * - * @return string - * The end date in the format YYYY-MM-DD - */ - public function getEndDate() - { - $startDate = $this->getStartDate(); - $startTime = $this->getStartTime(); - $duration = $this->getDuration(); - - $startDateTime = new DateTime($startDate.' '.$startTime); - $duration = explode(":", $duration); - - $endDate = $startDateTime->add(new DateInterval('PT'.$duration[0].'H'.$duration[1].'M')); - - return $endDate->format('Y-m-d'); - } - - /** - * Get the end time of the current show. - * - * @return string - * The start time in the format HH:MM:SS - */ - public function getEndTime() - { - $startDate = $this->getStartDate(); - $startTime = $this->getStartTime(); - $duration = $this->getDuration(); - - $startDateTime = new DateTime($startDate.' '.$startTime); - $duration = explode(":", $duration); - - $endDate = $startDateTime->add(new DateInterval('PT'.$duration[0].'H'.$duration[1].'M')); - - return $endDate->format('H:i:s'); - } - - /** - * Indicate whether the starting point of the show is in the - * past. - * - * @return boolean - * true if the StartDate is in the past, false otherwise - */ - public function isStartDateTimeInPast() - { - $date = new Application_Common_DateHelper; - $current_timestamp = $date->getUtcTimestamp(); - - return ($current_timestamp > ($this->getStartDate()." ".$this->getStartTime())); - } - - /** - * Get the ID's of future instance of the current show. - * - * @return array - * A simple array containing all ID's of show instance - * scheduled in the future. - */ - public function getAllFutureInstanceIds() - { - $sql = << :timestamp::TIMESTAMP - AND modified_instance != TRUE -SQL; - $rows = Application_Common_Database::prepareAndExecute($sql, - array( ':showId' => $this->getId(), - ':timestamp' => gmdate("Y-m-d H:i:s")), "all"); - - $res = array(); - foreach ($rows as $r) { - $res[] = $r['id']; - } - return $res; - } - - /* Called when a show's duration is changed (edited). - * - * @param array $p_data - * array containing the POST data about the show from the - * browser. - * - */ - private function updateDurationTime($p_data) - { - //need to update cc_show_instances, cc_show_days - $con = Propel::getConnection(); - - $date = new Application_Common_DateHelper; - $timestamp = $date->getUtcTimestamp(); - - $stmt = $con->prepare("UPDATE cc_show_days " - ."SET duration = :add_show_duration " - ."WHERE show_id = :add_show_id" ); - $stmt->execute( array( - ':add_show_duration' => $p_data['add_show_duration'], - ':add_show_id' => $p_data['add_show_id'] - )); - - - $sql = << :timestamp::TIMESTAMP -SQL; - - Application_Common_Database::prepareAndExecute( $sql, array( - ':add_show_duration' => $p_data['add_show_duration'], - ':show_id' => $p_data['add_show_id'], - ':timestamp' => $timestamp), "execute"); - } - - private function updateStartDateTime($p_data, $p_endDate) - { - //need to update cc_schedule, cc_show_instances, cc_show_days - $con = Propel::getConnection(); - - $date = new Application_Common_DateHelper; - $timestamp = $date->getTimestamp(); - - //TODO fix this from overwriting info. - $sql = "UPDATE cc_show_days " - ."SET start_time = TIME '$p_data[add_show_start_time]', " - ."first_show = DATE '$p_data[add_show_start_date]', "; - if (strlen ($p_endDate) == 0) { - $sql .= "last_show = NULL "; - } else { - $sql .= "last_show = DATE '$p_endDate' "; - } - $sql .= "WHERE show_id = $p_data[add_show_id]"; - $con->exec($sql); - - $dtOld = new DateTime($this->getStartDate()." ".$this->getStartTime(), new DateTimeZone("UTC")); - $dtNew = new DateTime($p_data['add_show_start_date']." ".$p_data['add_show_start_time'], new DateTimeZone(date_default_timezone_get())); - $diff = $dtOld->getTimestamp() - $dtNew->getTimestamp(); - - $sql = "UPDATE cc_show_instances " - ."SET starts = starts + INTERVAL '$diff sec', " - ."ends = ends + INTERVAL '$diff sec' " - ."WHERE show_id = $p_data[add_show_id] " - ."AND starts > TIMESTAMP '$timestamp'"; - $con->exec($sql); - - $showInstanceIds = $this->getAllFutureInstanceIds(); - if (count($showInstanceIds) > 0 && $diff != 0) { - $showIdsImploded = implode(",", $showInstanceIds); - $sql = "UPDATE cc_schedule " - ."SET starts = starts + INTERVAL '$diff sec', " - ."ends = ends + INTERVAL '$diff sec' " - ."WHERE instance_id IN ($showIdsImploded)"; - $con->exec($sql); - } - } - - public function getDuration($format=false) - { - $showDay = CcShowDaysQuery::create()->filterByDbShowId($this->getId())->findOne(); - if (!$format) { - return $showDay->getDbDuration(); - } else { - $info = explode(':',$showDay->getDbDuration()); - - return str_pad(intval($info[0]),2,'0',STR_PAD_LEFT).'h '.str_pad(intval($info[1]),2,'0',STR_PAD_LEFT).'m'; - } - } - - public function getShowDays() - { - $showDays = CcShowDaysQuery::create()->filterByDbShowId( - $this->getId())->find(); - $res = array(); - foreach ($showDays as $showDay) { - $res[] = $showDay->getDbDay(); - } - return $res; - } - - /* Only used for shows that aren't repeating. - * - * @return Boolean: true if show has an instance, otherwise false. */ - public function hasInstance() - { - return (!is_null($this->getInstance())); - } - - /* Only used for shows that aren't repeating. - * - * @return CcShowInstancesQuery: An propel object representing a - * row in the cc_show_instances table. */ - public function getInstance() - { - $showInstance = CcShowInstancesQuery::create() - ->filterByDbShowId($this->getId()) - ->findOne(); - - return $showInstance; - } - - /** - * returns info about live stream override info - */ - public function getLiveStreamInfo() - { - $info = array(); - if ($this->getId() == null) { - return $info; - } else { - $ccShow = CcShowQuery::create()->findPK($this->_showId); - $info['custom_username'] = $ccShow->getDbLiveStreamUser(); - $info['cb_airtime_auth'] = $ccShow->getDbLiveStreamUsingAirtimeAuth(); - $info['cb_custom_auth'] = $ccShow->getDbLiveStreamUsingCustomAuth(); - $info['custom_username'] = $ccShow->getDbLiveStreamUser(); - $info['custom_password'] = $ccShow->getDbLiveStreamPass(); - return $info; - } - } - - /* Only used for shows that are repeating. Note that this will return - * true even for dates that only have a "modified" show instance (does not - * check if the "modified_instance" column is set to true). This is intended - * behaviour. - * - * @param $p_dateTime: Date for which we are checking if instance - * exists. - * - * @return Boolean: true if show has an instance on $p_dateTime, - * otherwise false. */ - public function hasInstanceOnDate($p_dateTime) - { - return (!is_null($this->getInstanceOnDate($p_dateTime))); - } - - /* Only used for shows that are repeating. Note that this will return - * shows that have been "modified" (does not check if the "modified_instance" - * column is set to true). This is intended behaviour. - * - * @param $p_dateTime: Date for which we are getting an instance. - * - * @return CcShowInstancesQuery: An propel object representing a - * row in the cc_show_instances table. */ - public function getInstanceOnDate($p_dateTime) - { - $timestamp = $p_dateTime->format("Y-m-d H:i:s"); - $sql = << $this->getId(), - ':timestamp' => $timestamp ), 'column'); - return CcShowInstancesQuery::create() - ->findPk($row); - } catch (Exception $e) { - return null; - } - - } - - public function deletePossiblyInvalidInstances($p_data, $p_endDate, $isRecorded, $repeatType) - { - if ($p_data['add_show_repeats'] != $this->isRepeating()) { - //repeat option was toggled - $this->deleteAllInstances(); - } - - if ($p_data['add_show_duration'] != $this->getDuration()) { - //duration has changed - $this->updateDurationTime($p_data); - } - - if ($p_data['add_show_repeats']) { - if (($repeatType == 1 || $repeatType == 2) && - $p_data['add_show_start_date'] != $this->getStartDate()){ - - //start date has changed when repeat type is bi-weekly or monthly. - //This screws up the repeating positions of show instances, so lets - //just delete them for now. (CC-2351) - - $this->deleteAllInstances(); - } - - if ($repeatType != $this->getRepeatType()) { - //repeat type changed. - $this->deleteAllInstances(); - } else { - //repeat type is the same, check if the days of the week are the same - $repeatingDaysChanged = false; - $showDaysArray = $this->getShowDays(); - if (count($p_data['add_show_day_check']) == count($showDaysArray)) { - //same number of days checked, lets see if they are the same numbers - $intersect = array_intersect($p_data['add_show_day_check'], $showDaysArray); - if (count($intersect) != count($p_data['add_show_day_check'])) { - $repeatingDaysChanged = true; - } - } else { - $repeatingDaysChanged = true; - } - - if ($repeatingDaysChanged) { - $daysRemoved = array_diff($showDaysArray, $p_data['add_show_day_check']); - - if (count($daysRemoved) > 0) { - - $this->removeUncheckedDaysInstances($daysRemoved); - } - } - - if ($p_data['add_show_start_date'] != $this->getStartDate() - || $p_data['add_show_start_time'] != $this->getStartTime()){ - //start date/time has changed - - $newDate = strtotime($p_data['add_show_start_date']); - $oldDate = strtotime($this->getStartDate()); - if ($newDate > $oldDate) { - $this->removeAllInstancesBeforeDate($p_data['add_show_start_date']); - } - - $this->updateStartDateTime($p_data, $p_endDate); - } - } - - //Check if end date for the repeat option has changed. If so, need to take care - //of deleting possible invalid Show Instances. - if ((strlen($this->getRepeatingEndDate()) == 0) == $p_data['add_show_no_end']) { - //show "Never Ends" option was toggled. - if ($p_data['add_show_no_end']) { - } else { - $this->removeAllInstancesFromDate($p_endDate); - } - } - if ($this->getRepeatingEndDate() != $p_data['add_show_end_date']) { - //end date was changed. - - $newDate = strtotime($p_data['add_show_end_date']); - $oldDate = strtotime($this->getRepeatingEndDate()); - if ($newDate < $oldDate) { - $this->removeAllInstancesFromDate($p_endDate); - } - } - } - } - - /** - * 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 = << - $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; - } - - /** - * Generate repeating show instances for a single show up to the given date. - * It will always try to use enddate from DB but if that's empty, it will use - * time now. - * - * @param int $p_showId - */ - public static function populateShowUntil($p_showId) - { - $con = Propel::getConnection(); - $date = Application_Model_Preference::GetShowsPopulatedUntil(); - - if (is_null($date)) { - $p_populateUntilDateTime = new DateTime("now", new DateTimeZone('UTC')); - Application_Model_Preference::SetShowsPopulatedUntil($p_populateUntilDateTime); - } else { - $p_populateUntilDateTime = $date; - } - - $stmt = $con->prepare("SELECT * FROM cc_show_days WHERE show_id = :show_id"); - $stmt->bindParam(':show_id', $p_showId); - $stmt->execute(); - - $res = $stmt->fetchAll(); - - foreach ($res as $showDaysRow) { - Application_Model_Show::populateShow($showDaysRow, $p_populateUntilDateTime); - } - } - - /** - * We are going to use cc_show_days as a template, to generate Show Instances. This function - * is basically a dispatcher that looks at the show template, and sends it to the correct function - * so that Show Instance generation can begin. After the all show instances have been created, pushes - * the schedule to Pypo. - * - * @param array $p_showRow - * A row from cc_show_days table - * @param DateTime $p_populateUntilDateTime - * DateTime object in UTC time. - */ - private static function populateShow($p_showDaysRow, $p_populateUntilDateTime) - { - // TODO : use constants instead of int values here? or maybe php will - // get enum types by the time somebody gets around to fix this. -- RG - if ($p_showDaysRow["repeat_type"] == -1) { - Application_Model_Show::populateNonRepeatingShow($p_showDaysRow, $p_populateUntilDateTime); - } elseif ($p_showDaysRow["repeat_type"] == 0) { - Application_Model_Show::populateRepeatingShow($p_showDaysRow, $p_populateUntilDateTime, 'P7D'); - } elseif ($p_showDaysRow["repeat_type"] == 1) { - Application_Model_Show::populateRepeatingShow($p_showDaysRow, $p_populateUntilDateTime, 'P14D'); - } elseif ($p_showDaysRow["repeat_type"] == 2) { - Application_Model_Show::populateRepeatingShow($p_showDaysRow, $p_populateUntilDateTime, 'P1M'); - } - Application_Model_RabbitMq::PushSchedule(); - } - - /** - * Creates a single show instance. If the show is recorded, it may have multiple - * rebroadcast dates, and so this function will create those as well. - * - * @param array $p_showRow - * A row from cc_show_days table - * @param DateTime $p_populateUntilDateTime - * DateTime object in UTC time. - */ - private static function populateNonRepeatingShow($p_showRow, $p_populateUntilDateTime) - { - $show_id = $p_showRow["show_id"]; - $first_show = $p_showRow["first_show"]; //non-UTC - $start_time = $p_showRow["start_time"]; //non-UTC - $duration = $p_showRow["duration"]; - $record = $p_showRow["record"]; - $timezone = $p_showRow["timezone"]; - $start = $first_show." ".$start_time; - - //start & end UTC DateTimes for the show. - list($utcStartDateTime, $utcEndDateTime) = Application_Model_Show::createUTCStartEndDateTime($start, $duration, $timezone); - if ($utcStartDateTime->getTimestamp() < $p_populateUntilDateTime->getTimestamp()) { - $currentUtcTimestamp = gmdate("Y-m-d H:i:s"); - - $show = new Application_Model_Show($show_id); - if ($show->hasInstance()) { - $ccShowInstance = $show->getInstance(); - $newInstance = false; - } else { - $ccShowInstance = new CcShowInstances(); - $newInstance = true; - } - - if ($newInstance || $ccShowInstance->getDbStarts() > $currentUtcTimestamp) { - $ccShowInstance->setDbShowId($show_id); - $ccShowInstance->setDbStarts($utcStartDateTime); - $ccShowInstance->setDbEnds($utcEndDateTime); - $ccShowInstance->setDbRecord($record); - $ccShowInstance->save(); - } - - $show_instance_id = $ccShowInstance->getDbId(); - $showInstance = new Application_Model_ShowInstance($show_instance_id); - - if (!$newInstance) { - $showInstance->correctScheduleStartTimes(); - } - - $sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id=:show_id"; - $rebroadcasts = Application_Common_Database::prepareAndExecute($sql, - array( ':show_id' => $show_id ), 'all'); - - if ($showInstance->isRecorded()) { - $showInstance->deleteRebroadcasts(); - self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); - } - } - } - - /** - * Creates a 1 or more than 1 show instances (user has stated this show repeats). If the show - * is recorded, it may have multiple rebroadcast dates, and so this function will create - * those as well. - * - * @param array $p_showRow - * A row from cc_show_days table - * @param DateTime $p_populateUntilDateTime - * DateTime object in UTC time. "shows_populated_until" date YY-mm-dd in cc_pref - * @param string $p_interval - * Period of time between repeating shows (in php DateInterval notation 'P7D') - */ - private static function populateRepeatingShow($p_showDaysRow, $p_populateUntilDateTime, $p_interval) - { - $show_id = $p_showDaysRow["show_id"]; - $next_pop_date = $p_showDaysRow["next_pop_date"]; - $first_show = $p_showDaysRow["first_show"]; //non-UTC - $last_show = $p_showDaysRow["last_show"]; //non-UTC - $start_time = $p_showDaysRow["start_time"]; //non-UTC - $duration = $p_showDaysRow["duration"]; - $day = $p_showDaysRow["day"]; - $record = $p_showDaysRow["record"]; - $timezone = $p_showDaysRow["timezone"]; - - $currentUtcTimestamp = gmdate("Y-m-d H:i:s"); - - if (isset($next_pop_date)) { - $start = $next_pop_date." ".$start_time; - } else { - $start = $first_show." ".$start_time; - } - - $utcStartDateTime = Application_Common_DateHelper::ConvertToUtcDateTime($start, $timezone); - //convert $last_show into a UTC DateTime object, or null if there is no last show. - $utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; - - $sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id=:show_id"; - - $rebroadcasts = Application_Common_Database::prepareAndExecute( $sql, - array( ':show_id' => $show_id ), 'all'); - - $show = new Application_Model_Show($show_id); - - while ($utcStartDateTime->getTimestamp() <= $p_populateUntilDateTime->getTimestamp() - && (is_null($utcLastShowDateTime) || $utcStartDateTime->getTimestamp() < $utcLastShowDateTime->getTimestamp())){ - - list($utcStartDateTime, $utcEndDateTime) = self::createUTCStartEndDateTime($start, $duration, $timezone); - - if ($show->hasInstanceOnDate($utcStartDateTime)) { - $ccShowInstance = $show->getInstanceOnDate($utcStartDateTime); - - if ($ccShowInstance->getDbModifiedInstance()) { - //show instance on this date has been deleted. - list($start, $utcStartDateTime) = self::advanceRepeatingDate($p_interval, $start, $timezone); - continue; - } - - $newInstance = false; - } else { - $ccShowInstance = new CcShowInstances(); - $newInstance = true; - } - - /* When editing the start/end time of a repeating show, we don't want to - * change shows that started in the past. So check the start time. - */ - if ($newInstance || $ccShowInstance->getDbStarts() > $currentUtcTimestamp) { - $ccShowInstance->setDbShowId($show_id); - $ccShowInstance->setDbStarts($utcStartDateTime); - $ccShowInstance->setDbEnds($utcEndDateTime); - $ccShowInstance->setDbRecord($record); - $ccShowInstance->save(); - } - - - $show_instance_id = $ccShowInstance->getDbId(); - $showInstance = new Application_Model_ShowInstance($show_instance_id); - - /* If we are updating a show then make sure that the scheduled content within - * the show is updated to the correct time. */ - if (!$newInstance) { - $showInstance->correctScheduleStartTimes(); - } - - $showInstance->deleteRebroadcasts(); - self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); - list($start, $utcStartDateTime) = self::advanceRepeatingDate($p_interval, $start, $timezone); - - } - - Application_Model_Show::setNextPop($start, $show_id, $day); - } - - private static function advanceRepeatingDate($p_interval, $start, $timezone) - { - $startDt = new DateTime($start, new DateTimeZone($timezone)); - if ($p_interval == 'P1M') { - /* When adding months, there is a problem if we are on January 31st and add one month with PHP. - * What ends up happening is that since February 31st doesn't exist, the date returned is - * March 3rd. For now let's ignore the day and assume we are always working with the - * first of each month, and use PHP to add 1 month to this (this will take care of rolling - * over the years 2011->2012, etc.). Then let's append the actual day, and use the php - * checkdate() function, to see if it is valid. If not, then we'll just skip this month. */ - - /* pass in only the year and month (not the day) */ - $dt = new DateTime($startDt->format("Y-m"), new DateTimeZone($timezone)); - - - /* Keep adding 1 month, until we find the next month that contains the day - * we are looking for (31st day for example) */ - do { - $dt->add(new DateInterval($p_interval)); - } while (!checkdate($dt->format("m"), $startDt->format("d"), $dt->format("Y"))); - - $dt->setDate($dt->format("Y"), $dt->format("m"), $startDt->format("d")); - - } else { - $dt = new DateTime($start, new DateTimeZone($timezone)); - $dt->add(new DateInterval($p_interval)); - } - - $start = $dt->format("Y-m-d H:i:s"); - - $dt->setTimezone(new DateTimeZone('UTC')); - $utcStartDateTime = $dt; - - return array($start, $utcStartDateTime); - } - - /* - * @param $p_start - * timestring format "Y-m-d H:i:s" (not UTC) - * @param $p_duration - * string time interval (h)h:(m)m(:ss) - * @param $p_timezone - * string "Europe/Prague" - * @param $p_offset - * array (days, hours, mins) used for rebroadcast shows. - * - * @return - * array of 2 DateTime objects, start/end time of the show in UTC. - */ - private static function createUTCStartEndDateTime($p_start, $p_duration, $p_timezone=null, $p_offset=null) - { - $timezone = $p_timezone ? $p_timezone : date_default_timezone_get(); - - $startDateTime = new DateTime($p_start, new DateTimeZone($timezone)); - if (isset($p_offset)) { - $startDateTime->add(new DateInterval("P{$p_offset["days"]}DT{$p_offset["hours"]}H{$p_offset["mins"]}M")); - } - //convert time to UTC - $startDateTime->setTimezone(new DateTimeZone('UTC')); - - $endDateTime = clone $startDateTime; - $duration = explode(":", $p_duration); - list($hours, $mins) = array_slice($duration, 0, 2); - $endDateTime->add(new DateInterval("PT{$hours}H{$mins}M")); - - return array($startDateTime, $endDateTime); - } - - /* Create rebroadcast instances for a created show marked for recording - * - * @param $p_rebroadcasts - * rows gotten from the db table cc_show_rebroadcasts, tells airtime when to schedule the rebroadcasts. - * @param $p_currentUtcTimestamp - * a timestring in format "Y-m-d H:i:s", current UTC time. - * @param $p_showId - * int of the show it belongs to (from cc_show) - * @param $p_showInstanceId - * the instance id of the created recorded show instance - * (from cc_show_instances), used to associate rebroadcasts to this show. - * @param $p_startTime - * a timestring in format "Y-m-d H:i:s" in the timezone, not UTC of the rebroadcasts' parent recorded show. - * @param $p_duration - * string time interval (h)h:(m)m:(ss) length of the show. - * @param $p_timezone - * string of user's timezone "Europe/Prague" - * - */ - private static function createRebroadcastInstances($p_rebroadcasts, $p_currentUtcTimestamp, $p_showId, $p_showInstanceId, $p_startTime, $p_duration, $p_timezone=null) - { - //Y-m-d - //use only the date part of the show start time stamp for the offsets to work properly. - $date = explode(" ", $p_startTime); - $start_date = $date[0]; - - foreach ($p_rebroadcasts as $rebroadcast) { - - $days = explode(" ", $rebroadcast["day_offset"]); - $time = explode(":", $rebroadcast["start_time"]); - $offset = array("days"=>$days[0], "hours"=>$time[0], "mins"=>$time[1]); - - list($utcStartDateTime, $utcEndDateTime) = Application_Model_Show::createUTCStartEndDateTime($start_date, $p_duration, $p_timezone, $offset); - - if ($utcStartDateTime->format("Y-m-d H:i:s") > $p_currentUtcTimestamp) { - - $newRebroadcastInstance = new CcShowInstances(); - $newRebroadcastInstance->setDbShowId($p_showId); - $newRebroadcastInstance->setDbStarts($utcStartDateTime); - $newRebroadcastInstance->setDbEnds($utcEndDateTime); - $newRebroadcastInstance->setDbRecord(0); - $newRebroadcastInstance->setDbRebroadcast(1); - $newRebroadcastInstance->setDbOriginalShow($p_showInstanceId); - $newRebroadcastInstance->save(); - } - } - } - - /** - * Get all the show instances in the given time range (inclusive). - * - * @param DateTime $start_timestamp - * In UTC time. - * @param DateTime $end_timestamp - * In UTC time. - * @param unknown_type $excludeInstance - * @param boolean $onlyRecord - * @return array - */ - public static function getShows($start_timestamp, $end_timestamp, $onlyRecord=FALSE) - { - //UTC DateTime object - $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil(); - //if application is requesting shows past our previous populated until date, generate shows up until this point. - if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $end_timestamp->getTimestamp()) { - Application_Model_Show::populateAllShowsInRange($showsPopUntil, $end_timestamp); - Application_Model_Preference::SetShowsPopulatedUntil($end_timestamp); - } - - $sql = <<format("Y-m-d H:i:s"); - $end_string = $end_timestamp->format("Y-m-d H:i:s"); - if ($onlyRecord) { - $sql .= " AND (si1.starts >= :start::TIMESTAMP AND si1.starts < :end::TIMESTAMP)"; - $sql .= " AND (si1.record = 1)"; - - return Application_Common_Database::prepareAndExecute( $sql, - array( ':start' => $start_string, - ':end' => $end_string ), 'all'); - - } else { - $sql .= " ". <<= :start1::TIMESTAMP AND si1.starts < :end1::TIMESTAMP) - OR (si1.ends > :start2::TIMESTAMP AND si1.ends <= :end2::TIMESTAMP) - OR (si1.starts <= :start3::TIMESTAMP AND si1.ends >= :end3::TIMESTAMP)) -SQL; - return Application_Common_Database::prepareAndExecute( $sql, - array( - 'start1' => $start_string, - 'start2' => $start_string, - 'start3' => $start_string, - 'end1' => $end_string, - 'end2' => $end_string, - 'end3' => $end_string - ), 'all'); - } - } - - private static function setNextPop($next_date, $show_id, $day) - { - $nextInfo = explode(" ", $next_date); - - $repeatInfo = CcShowDaysQuery::create() - ->filterByDbShowId($show_id) - ->filterByDbDay($day) - ->findOne(); - - $repeatInfo->setDbNextPopDate($nextInfo[0]) - ->save(); - } - - /** - * Generate all the repeating shows in the given range. - * - * @param DateTime $p_startTimestamp - * In UTC format. - * @param DateTime $p_endTimestamp - * In UTC format. - */ - public static function populateAllShowsInRange($p_startTimestamp, $p_endTimestamp) - { - $con = Propel::getConnection(); - - $endTimeString = $p_endTimestamp->format("Y-m-d H:i:s"); - if (!is_null($p_startTimestamp)) { - $startTimeString = $p_startTimestamp->format("Y-m-d H:i:s"); - } else { - $today_timestamp = new DateTime("now", new DateTimeZone("UTC")); - $startTimeString = $today_timestamp->format("Y-m-d H:i:s"); - } - - $stmt = $con->prepare(" - SELECT * FROM cc_show_days - WHERE last_show IS NULL - OR first_show < :endTimeString AND last_show > :startTimeString"); - - $stmt->bindParam(':endTimeString', $endTimeString); - $stmt->bindParam(':startTimeString', $startTimeString); - $stmt->execute(); - - $res = $stmt->fetchAll(); - foreach ($res as $row) { - Application_Model_Show::populateShow($row, $p_endTimestamp); - } - } - - /** - * - * @param DateTime $start - * -in UTC time - * @param DateTime $end - * -in UTC time - * @param boolean $editable - */ - public static function &getFullCalendarEvents($p_start, $p_end, $p_editable=false) - { - $events = array(); - $interval = $p_start->diff($p_end); - $days = $interval->format('%a'); - $shows = Application_Model_Show::getShows($p_start, $p_end); - $content_count = Application_Model_ShowInstance::getContentCount( - $p_start, $p_end); - $isFull = Application_Model_ShowInstance::getIsFull($p_start, $p_end); - $timezone = date_default_timezone_get(); - $current_timezone = new DateTimeZone($timezone); - $utc = new DateTimeZone("UTC"); - $now = new DateTime("now", $utc); - - foreach ($shows as &$show) { - $options = array(); - - //only bother calculating percent for week or day view. - if (intval($days) <= 7) { - $options["percent"] = Application_Model_Show::getPercentScheduled($show["starts"], $show["ends"], $show["time_filled"]); - } - - if (isset($show["parent_starts"])) { - $parentStartsDT = new DateTime($show["parent_starts"], $utc); - } - - $startsDT = DateTime::createFromFormat("Y-m-d G:i:s", - $show["starts"],$utc); - $endsDT = DateTime::createFromFormat("Y-m-d G:i:s", - $show["ends"], $utc); - - if( $p_editable ) { - if ($show["record"] && $now > $startsDT) { - $options["editable"] = false; - } elseif ($show["rebroadcast"] && - $now > $parentStartsDT) { - $options["editable"] = false; - } elseif ($now < $endsDT) { - $options["editable"] = true; - } - } - - $startsDT->setTimezone($current_timezone); - $endsDT->setTimezone($current_timezone); - - $options["show_empty"] = (array_key_exists($show['instance_id'], - $content_count)) ? 0 : 1; - - $options["show_partial_filled"] = !$isFull[$show['instance_id']]; - - $event = array(); - - $event["id"] = intval($show["instance_id"]); - $event["title"] = $show["name"]; - $event["start"] = $startsDT->format("Y-m-d H:i:s"); - $event["end"] = $endsDT->format("Y-m-d H:i:s"); - $event["allDay"] = false; - $event["showId"] = intval($show["show_id"]); - $event["record"] = intval($show["record"]); - $event["rebroadcast"] = intval($show["rebroadcast"]); - $event["soundcloud_id"] = is_null($show["soundcloud_id"]) - ? -1 : $show["soundcloud_id"]; - - //event colouring - if ($show["color"] != "") { - $event["textColor"] = "#".$show["color"]; - } - - if ($show["background_color"] != "") { - $event["color"] = "#".$show["background_color"]; - } - - foreach ($options as $key => $value) { - $event[$key] = $value; - } - - $events[] = $event; - } - return $events; - } - - /** - * Calculates the percentage of a show scheduled given the start and end times in date/time format - * and the time_filled as the total time the schow is scheduled for in time format. - **/ - private static function getPercentScheduled($p_starts, $p_ends, $p_time_filled) - { - $durationSeconds = (strtotime($p_ends) - strtotime($p_starts)); - $time_filled = Application_Model_Schedule::WallTimeToMillisecs($p_time_filled) / 1000; - $percent = ceil(( $time_filled / $durationSeconds) * 100); - - return $percent; - } - -/* private static function &makeFullCalendarEvent(&$show, $options=array(), $startDateTime, $endDateTime, $startsEpoch, $endsEpoch) - { - $event = array(); - - $event["id"] = intval($show["instance_id"]); - $event["title"] = $show["name"]; - $event["start"] = $startDateTime->format("Y-m-d H:i:s"); - $event["startUnix"] = $startsEpoch; - $event["end"] = $endDateTime->format("Y-m-d H:i:s"); - $event["endUnix"] = $endsEpoch; - $event["allDay"] = false; - $event["showId"] = intval($show["show_id"]); - $event["record"] = intval($show["record"]); - $event["rebroadcast"] = intval($show["rebroadcast"]); - $event["soundcloud_id"] = is_null($show["soundcloud_id"]) - ? -1 : $show["soundcloud_id"]; - - //event colouring - if ($show["color"] != "") { - $event["textColor"] = "#".$show["color"]; - } - - if ($show["background_color"] != "") { - $event["color"] = "#".$show["background_color"]; - } - - foreach ($options as $key => $value) { - $event[$key] = $value; - } - - return $event; - }*/ - - /* Takes in a UTC DateTime object. - * Converts this to local time, since cc_show days - * requires local time. */ - public function setShowFirstShow($p_dt) - { - //clone object since we are modifying it and it was passed by reference. - $dt = clone $p_dt; - - $dt->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $showDay = CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->findOne(); - - $showDay->setDbFirstShow($dt)->setDbStartTime($dt) - ->save(); - - //Logging::info("setting show's first show."); - } - - /* Takes in a UTC DateTime object - * Converts this to local time, since cc_show days - * requires local time. */ - public function setShowLastShow($p_dt) - { - //clone object since we are modifying it and it was passed by reference. - $dt = clone $p_dt; - - $dt->setTimezone(new DateTimeZone(date_default_timezone_get())); - - //add one day since the Last Show date in CcShowDays is non-inclusive. - $dt->add(new DateInterval("P1D")); - - $showDay = CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->findOne(); - - $showDay->setDbLastShow($dt) - ->save(); - } - - /** - * Given time $timeNow, returns the show being played right now. - * Times are all in UTC time. - * - * @param String $timeNow - current time (in UTC) - * @return array - show being played right now - */ - public static function getCurrentShow($timeNow=null) - { - $CC_CONFIG = Config::getConfig(); - $con = Propel::getConnection(); - if ($timeNow == null) { - $date = new Application_Common_DateHelper; - $timeNow = $date->getUtcTimestamp(); - } - //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin - $sql = << :timeNow2::timestamp - AND modified_instance != TRUE -SQL; - - $stmt = $con->prepare($sql); - $stmt->bindParam(':timeNow1', $timeNow); - $stmt->bindParam(':timeNow2', $timeNow); - - if ($stmt->execute()) { - $rows = $stmt->fetchAll(); - } else { - $msg = implode(',', $stmt->errorInfo()); - throw new Exception("Error: $msg"); - } - - return $rows; - } - - /** - * Gets the current show, previous and next with an 2day window from - * the given timeNow, so timeNow-2days and timeNow+2days. - */ - public static function getPrevCurrentNext($p_timeNow) - { - $CC_CONFIG = Config::getConfig(); - $con = Propel::getConnection(); - // - //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin - $sql = << :timeNow1::timestamp - INTERVAL '2 days' - AND si.ends < :timeNow2::timestamp + INTERVAL '2 days' - AND modified_instance != TRUE -ORDER BY si.starts -SQL; - - $stmt = $con->prepare($sql); - - $stmt->bindValue(':timeNow1', $p_timeNow); - $stmt->bindValue(':timeNow2', $p_timeNow); - - if ($stmt->execute()) { - $rows = $stmt->fetchAll(); - } else { - $msg = implode(',', $stmt->errorInfo()); - throw new Exception("Error: $msg"); - } - - $numberOfRows = count($rows); - - $results['previousShow'] = array(); - $results['currentShow'] = array(); - $results['nextShow'] = array(); - - $timeNowAsMillis = strtotime($p_timeNow); - - for ($i = 0; $i < $numberOfRows; ++$i) { - //Find the show that is within the current time. - if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) - && (strtotime($rows[$i]['ends']) > $timeNowAsMillis)) { - if ($i-1 >= 0) { - $results['previousShow'][0] = array( - "id" => $rows[$i-1]['id'], - "instance_id" => $rows[$i-1]['instance_id'], - "name" => $rows[$i-1]['name'], - "url" => $rows[$i-1]['url'], - "start_timestamp" => $rows[$i-1]['start_timestamp'], - "end_timestamp" => $rows[$i-1]['end_timestamp'], - "starts" => $rows[$i-1]['starts'], - "ends" => $rows[$i-1]['ends'], - "record" => $rows[$i-1]['record'], - "type" => "show"); - } - - $results['currentShow'][0] = $rows[$i]; - - if (isset($rows[$i+1])) { - $results['nextShow'][0] = array( - "id" => $rows[$i+1]['id'], - "instance_id" => $rows[$i+1]['instance_id'], - "name" => $rows[$i+1]['name'], - "url" => $rows[$i+1]['url'], - "start_timestamp" => $rows[$i+1]['start_timestamp'], - "end_timestamp" => $rows[$i+1]['end_timestamp'], - "starts" => $rows[$i+1]['starts'], - "ends" => $rows[$i+1]['ends'], - "record" => $rows[$i+1]['record'], - "type" => "show"); - } - break; - } - //Previous is any row that ends after time now capture it in case we need it later. - if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { - $previousShowIndex = $i; - } - //if we hit this we know we've gone to far and can stop looping. - if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { - $results['nextShow'][0] = array( - "id" => $rows[$i]['id'], - "instance_id" => $rows[$i]['instance_id'], - "name" => $rows[$i]['name'], - "url" => $rows[$i]['url'], - "start_timestamp" => $rows[$i]['start_timestamp'], - "end_timestamp" => $rows[$i]['end_timestamp'], - "starts" => $rows[$i]['starts'], - "ends" => $rows[$i]['ends'], - "record" => $rows[$i]['record'], - "type" => "show"); - break; - } - } - //If we didn't find a a current show because the time didn't fit we may still have - //found a previous show so use it. - if (count($results['previousShow']) == 0 && isset($previousShowIndex)) { - $results['previousShow'][0] = array( - "id" => $rows[$previousShowIndex]['id'], - "instance_id" => $rows[$previousShowIndex]['instance_id'], - "name" => $rows[$previousShowIndex]['name'], - "start_timestamp" => $rows[$previousShowIndex]['start_timestamp'], - "end_timestamp" => $rows[$previousShowIndex]['end_timestamp'], - "starts" => $rows[$previousShowIndex]['starts'], - "ends" => $rows[$previousShowIndex]['ends'], - "record" => $rows[$previousShowIndex]['record'], - "type" => "show"); - } - - return $results; - } - - /** - * Given a start time $timeStart and end time $timeEnd, returns the next $limit - * number of shows within the time interval - * If $timeEnd not given, shows within next 48 hours from $timeStart are returned - * If $limit not given, all shows within the intervals are returned - * Times are all in UTC time. - * - * @param String $timeStart - interval start time (in UTC) - * @param int $limit - number of shows to return - * @param String $timeEnd - interval end time (in UTC) - * @return array - the next $limit number of shows within the time interval - */ - public static function getNextShows($timeStart, $limit = "ALL", $timeEnd = "") - { - // defaults to retrieving shows from next 2 days if no end time has - // been specified - if ($timeEnd == "") { - $timeEnd = "'$timeStart' + INTERVAL '2 days'"; - } - - //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin - $sql = <<= :timeStart::timestamp - AND si.starts < :timeEnd::timestamp - AND modified_instance != TRUE -ORDER BY si.starts -SQL; - - //PDO won't accept "ALL" as a limit value (complains it is not an - //integer, and so we must completely remove the limit clause if we - //want to show all results - MK - if ($limit != "ALL") { - $sql .= PHP_EOL."LIMIT :lim"; - $params = array( - ':timeStart' => $timeStart, - ':timeEnd' => $timeEnd, - ':lim' => $limit); - } else { - $params = array( - ':timeStart' => $timeStart, - ':timeEnd' => $timeEnd); - } - - return Application_Common_Database::prepareAndExecute( $sql, $params, 'all'); - } - - /** - * Convert the columns given in the array $columnsToConvert in the - * database result $rows to local timezone. - * - * @param type $rows arrays of arrays containing database query result - * @param type $columnsToConvert array of column names to convert - */ - public static function convertToLocalTimeZone(&$rows, $columnsToConvert) - { - if (!is_array($rows)) { - return; - } - foreach ($rows as &$row) { - foreach ($columnsToConvert as $column) { - $row[$column] = Application_Common_DateHelper::ConvertToLocalDateTimeString($row[$column]); - } - } - } - - public static function getMaxLengths() - { - $con = Propel::getConnection(); - $sql = << 0 -SQL; - $result = $con->query($sql)->fetchAll(); - $assocArray = array(); - foreach ($result as $row) { - $assocArray[$row['column_name']] = $row['character_maximum_length']; - } - return $assocArray; - } - - public static function getStartEndCurrentMonthView() { - $first_day_of_calendar_month_view = mktime(0, 0, 0, date("n"), 1); - $weekStart = Application_Model_Preference::GetWeekStartDay(); - while (date('w', $first_day_of_calendar_month_view) != $weekStart) { - $first_day_of_calendar_month_view -= 60*60*24; - } - $last_day_of_calendar_view = $first_day_of_calendar_month_view + 3600*24*42; - - $start = new DateTime("@".$first_day_of_calendar_month_view); - $end = new DateTime("@".$last_day_of_calendar_view); - - return array($start, $end); - } - - public static function getStartEndCurrentWeekView() { - $first_day_of_calendar_week_view = mktime(0, 0, 0, date("n"), date("j")); - $weekStart = Application_Model_Preference::GetWeekStartDay(); - while (date('w', $first_day_of_calendar_week_view) != $weekStart) { - $first_day_of_calendar_week_view -= 60*60*24; - } - $last_day_of_calendar_view = $first_day_of_calendar_week_view + 3600*24*7; - - $start = new DateTime("@".$first_day_of_calendar_week_view); - $end = new DateTime("@".$last_day_of_calendar_view); - - return array($start, $end); - } - - public static function getStartEndCurrentDayView() { - $today = mktime(0, 0, 0, date("n"), date("j")); - $tomorrow = $today + 3600*24; - - $start = new DateTime("@".$today); - $end = new DateTime("@".$tomorrow); - - return array($start, $end); - } -} diff --git a/airtime_mvc/application/models/airtime/CcShow.php b/airtime_mvc/application/models/airtime/CcShow.php index 12139a3ff..5e300f00c 100644 --- a/airtime_mvc/application/models/airtime/CcShow.php +++ b/airtime_mvc/application/models/airtime/CcShow.php @@ -163,6 +163,7 @@ class CcShow extends BaseCcShow { $collCcShowInstancess = CcShowInstancesQuery::create(null, $criteria) ->filterByCcShow($this) ->filterByDbModifiedInstance(false) + ->filterByDbStarts(gmdate("Y-m-d H:i:s"), criteria::GREATER_THAN) ->orderByDbId() ->find($con); if (null !== $criteria) { diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index c5e205e48..50707b3f8 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -836,7 +836,10 @@ SQL; $utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; + $utcStartDateTime = new DateTime("now"); foreach ($datePeriod as $date) { + Logging::info($date); + Logging::info($duration); list($utcStartDateTime, $utcEndDateTime) = $this->createUTCStartEndDateTime( $date, $duration); /*