From 81dbb17922e75526bb367f19bbb64f907d4648e4 Mon Sep 17 00:00:00 2001 From: denise Date: Wed, 3 Apr 2013 11:46:46 -0400 Subject: [PATCH] CC-4961: Show linking -moved show deletion into show service --- .../controllers/ScheduleController.php | 52 +++----- .../application/services/CalendarService.php | 42 +++++- .../application/services/ShowFormService.php | 1 + .../application/services/ShowService.php | 123 +++++++++++++++++- 4 files changed, 179 insertions(+), 39 deletions(-) diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index 3a30319e7..02ed93dfa 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -16,14 +16,14 @@ class ScheduleController extends Zend_Controller_Action ->addActionContext('edit-show', 'json') ->addActionContext('move-show', 'json') ->addActionContext('resize-show', 'json') - ->addActionContext('delete-show', 'json') + ->addActionContext('delete-show-instance', '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-repeating-show-instance-form', 'json') - ->addActionContext('cancel-show', 'json') + ->addActionContext('delete-show', 'json') ->addActionContext('cancel-current-show', 'json') ->addActionContext('get-form', 'json') ->addActionContext('upload-to-sound-cloud', 'json') @@ -209,28 +209,17 @@ class ScheduleController extends Zend_Controller_Action } } - public function deleteShowAction() + public function deleteShowInstanceAction() { - $showInstanceId = $this->_getParam('id'); + $instanceId = $this->_getParam('id'); - $userInfo = Zend_Auth::getInstance()->getStorage()->read(); - $user = new Application_Model_User($userInfo->id); + $service_show = new Application_Service_ShowService(); + $showId = $service_show->deleteShow($instanceId, true); - 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(); + if (!$showId) { + $this->view->show_error = true; } + $this->view->show_id = $showId; } public function uploadToSoundCloudAction() @@ -254,7 +243,6 @@ class ScheduleController extends Zend_Controller_Action public function makeContextMenuAction() { $instanceId = $this->_getParam('instanceId'); - $showId = $this->_getParam('showId'); $service_calendar = new Application_Service_CalendarService($instanceId); @@ -598,25 +586,17 @@ class ScheduleController extends Zend_Controller_Action return $forms; } - public function cancelShowAction() + public function deleteShowAction() { - $user = Application_Model_User::getCurrentUser(); + $instanceId = $this->_getParam('id'); - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - $showInstanceId = $this->_getParam('id'); + $service_show = new Application_Service_ShowService(); + $showId = $service_show->deleteShow($instanceId); - 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(); + if (!$showId) { + $this->view->show_error = true; } + $this->view->show_id = $showId; } public function cancelCurrentShowAction() diff --git a/airtime_mvc/application/services/CalendarService.php b/airtime_mvc/application/services/CalendarService.php index 0d60e8a1f..876b3566a 100644 --- a/airtime_mvc/application/services/CalendarService.php +++ b/airtime_mvc/application/services/CalendarService.php @@ -147,12 +147,12 @@ class Application_Service_CalendarService $menu["del"]["items"]["single"] = array( "name"=> _("Delete This Instance"), "icon" => "delete", - "url" => $baseUrl."schedule/delete-show"); + "url" => $baseUrl."schedule/delete-show-instance"); $menu["del"]["items"]["following"] = array( "name"=> _("Delete This Instance and All Following"), "icon" => "delete", - "url" => $baseUrl."schedule/cancel-show"); + "url" => $baseUrl."schedule/delete-show"); } else { $menu["del"] = array( "name"=> _("Delete"), @@ -164,4 +164,42 @@ class Application_Service_CalendarService return $menu; } + /* + * @param $dateTime + * php Datetime object to add deltas to + * + * @param $deltaDay + * php int, delta days show moved + * + * @param $deltaMin + * php int, delta mins show moved + * + * @return $newDateTime + * php DateTime, $dateTime with the added time deltas. + */ + public static function addDeltas($dateTime, $deltaDay, $deltaMin) + { + $newDateTime = clone $dateTime; + + $days = abs($deltaDay); + $mins = abs($deltaMin); + + $dayInterval = new DateInterval("P{$days}D"); + $minInterval = new DateInterval("PT{$mins}M"); + + if ($deltaDay > 0) { + $newDateTime->add($dayInterval); + } elseif ($deltaDay < 0) { + $newDateTime->sub($dayInterval); + } + + if ($deltaMin > 0) { + $newDateTime->add($minInterval); + } elseif ($deltaMin < 0) { + $newDateTime->sub($minInterval); + } + + return $newDateTime; + } + } \ No newline at end of file diff --git a/airtime_mvc/application/services/ShowFormService.php b/airtime_mvc/application/services/ShowFormService.php index eaaf4da1d..8240c9488 100644 --- a/airtime_mvc/application/services/ShowFormService.php +++ b/airtime_mvc/application/services/ShowFormService.php @@ -342,6 +342,7 @@ class Application_Service_ShowFormService SELECT starts, ends FROM cc_show_instances WHERE ends > now() at time zone 'UTC' AND show_id = :showId +AND modified_instance = FALSE ORDER BY starts LIMIT 1 SQL; diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index 614e28d37..0d99854c2 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -73,7 +73,10 @@ class Application_Service_ShowService } //delete the edited instance from the repeating sequence - CcShowInstancesQuery::create()->findPk($showData["add_show_instance_id"])->delete(); + CcShowInstancesQuery::create() + ->findPk($showData["add_show_instance_id"]) + ->setDbModifiedInstance(true) + ->save(); $con->commit(); Application_Model_RabbitMq::PushSchedule(); @@ -484,6 +487,124 @@ SQL; } + public function deleteShow($instanceId, $singleInstance=false) + { + $service_user = new Application_Service_UserService(); + $currentUser = $service_user->getCurrentUser(); + + $con = Propel::getConnection(); + $con->beginTransaction(); + try { + if (!$currentUser->isAdminOrPM()) { + throw new Exception("Permission denied"); + } + + $ccShowInstance = CcShowInstancesQuery::create() + ->findPk($instanceId); + if (!$ccShowInstance) { + throw new Exception("Could not find show instance"); + } + + $showId = $ccShowInstance->getDbShowId(); + if ($singleInstance) { + $ccShowInstances = CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbStarts($ccShowInstance->getDbStarts(), Criteria::GREATER_EQUAL) + ->filterByDbEnds($ccShowInstance->getDbEnds(), Criteria::LESS_EQUAL) + ->find(); + } else { + $ccShowInstances = CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbStarts($ccShowInstance->getDbStarts(), Criteria::GREATER_EQUAL) + ->find(); + } + + if (gmdate("Y-m-d H:i:s") <= $ccShowInstance->getDbEnds()) { + $this->deleteShowInstances($ccShowInstances, $ccShowInstance->getDbShowId()); + } + + Application_Model_RabbitMq::PushSchedule(); + + $con->commit(); + return $showId; + } catch (Exception $e) { + $con->rollback(); + Logging::info("Delete show instance failed"); + Logging::info($e->getMessage()); + return false; + } + } + + public function deleteShowInstances($ccShowInstances, $showId) + { + foreach ($ccShowInstances as $ccShowInstance) { + $instanceId = $ccShowInstance->getDbId(); + + $ccShowInstance + ->setDbModifiedInstance(true) + ->save(); + + //delete the rebroadcasts of the removed recorded show + if ($ccShowInstance->isRecorded()) { + CcShowInstancesQuery::create() + ->filterByDbOriginalShow($instanceId) + ->delete(); + } + + //delete all files scheduled in cc_schedules table + CcScheduleQuery::create() + ->filterByDbInstanceId($instanceId) + ->delete(); + } + + if ($this->checkToDeleteCcShow($showId)) { + CcShowQuery::create() + ->filterByDbId($showId) + ->delete(); + } + } + + private function checkToDeleteCcShow($showId) + { + // check if there are any non deleted show instances remaining. + $ccShowInstances = CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbModifiedInstance(false) + ->filterByDbRebroadcast(0) + ->find(); + + if ($ccShowInstances->isEmpty()) { + return true; + } + //only 1 show instance left of the show, make it non repeating. + else if (count($ccShowInstances) === 1) { + $ccShowInstance = $ccShowInstances[0]; + + $ccShowDay = CcShowDaysQuery::create() + ->filterByDbShowId($showId) + ->findOne(); + $tz = $ccShowDay->getDbTimezone(); + + $startDate = new DateTime($ccShowInstance->getDbStarts(), new DateTimeZone("UTC")); + $startDate->setTimeZone(new DateTimeZone($tz)); + $endDate = Application_Service_CalendarService::addDeltas($startDate, 1, 0); + + $ccShowDay->setDbFirstShow($startDate->format("Y-m-d")); + $ccShowDay->setDbLastShow($endDate->format("Y-m-d")); + $ccShowDay->setDbStartTime($startDate->format("H:i:s")); + $ccShowDay->setDbRepeatType(-1); + $ccShowDay->save(); + + //remove the old repeating deleted instances. + CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbModifiedInstance(true) + ->delete(); + } + + return false; + } + private function deleteAllInstances($showId) { $sql = <<