2013-03-15 21:56:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Application_Service_CalendarService
|
|
|
|
{
|
2013-03-26 21:03:53 +01:00
|
|
|
private $currentUser;
|
|
|
|
private $ccShowInstance;
|
2013-04-05 23:44:30 +02:00
|
|
|
private $ccShow;
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
public function __construct($instanceId = null)
|
2013-03-15 21:56:22 +01:00
|
|
|
{
|
2013-03-26 21:03:53 +01:00
|
|
|
if (!is_null($instanceId)) {
|
|
|
|
$this->ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
|
2013-04-05 23:44:30 +02:00
|
|
|
if (is_null($this->ccShowInstance)) {
|
|
|
|
throw new Exception("Instance does not exist");
|
|
|
|
}
|
|
|
|
$this->ccShow = $this->ccShowInstance->getCcShow();
|
2013-03-26 21:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$service_user = new Application_Service_UserService();
|
|
|
|
$this->currentUser = $service_user->getCurrentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Enter description here ...
|
|
|
|
*/
|
|
|
|
public function makeContextMenu()
|
|
|
|
{
|
|
|
|
$menu = array();
|
|
|
|
$now = time();
|
|
|
|
$baseUrl = Application_Common_OsPath::getBaseDir();
|
|
|
|
$isAdminOrPM = $this->currentUser->isAdminOrPM();
|
2013-04-05 23:44:30 +02:00
|
|
|
$isHostOfShow = $this->currentUser->isHostOfShow($this->ccShow->getDbId());
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
//DateTime objects in UTC
|
2013-03-27 21:25:39 +01:00
|
|
|
$startDT = $this->ccShowInstance->getDbStarts(null);
|
|
|
|
$endDT = $this->ccShowInstance->getDbEnds(null);
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
//timestamps
|
|
|
|
$start = $startDT->getTimestamp();
|
|
|
|
$end = $endDT->getTimestamp();
|
|
|
|
|
|
|
|
//show has ended
|
|
|
|
if ($now > $end) {
|
|
|
|
if ($this->ccShowInstance->isRecorded()) {
|
|
|
|
|
|
|
|
$ccFile = $this->ccShowInstance->getCcFiles();
|
2013-08-23 20:23:47 +02:00
|
|
|
if (!isset($ccFile)) {
|
|
|
|
$menu["error when recording"] = array (
|
|
|
|
"name" => _("Record file doesn't exist"),
|
|
|
|
"icon" => "error");
|
|
|
|
}else {
|
|
|
|
$menu["view_recorded"] = array(
|
|
|
|
"name" => _("View Recorded File Metadata"),
|
|
|
|
"icon" => "overview",
|
|
|
|
"url" => $baseUrl."library/edit-file-md/id/".$ccFile->getDbId());
|
|
|
|
}
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
//recorded show can be uploaded to soundcloud
|
|
|
|
if (Application_Model_Preference::GetUploadToSoundcloudOption()) {
|
|
|
|
$scid = $ccFile->getDbSoundcloudId();
|
|
|
|
|
|
|
|
if ($scid > 0) {
|
|
|
|
$menu["soundcloud_view"] = array(
|
|
|
|
"name" => _("View on Soundcloud"),
|
|
|
|
"icon" => "soundcloud",
|
|
|
|
"url" => $ccFile->getDbSoundcloudLinkToFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = is_null($scid) ? _('Upload to SoundCloud') : _('Re-upload to SoundCloud');
|
|
|
|
$menu["soundcloud_upload"] = array(
|
|
|
|
"name"=> $text,
|
|
|
|
"icon" => "soundcloud");
|
|
|
|
}
|
2013-05-08 19:43:27 +02:00
|
|
|
} else {
|
|
|
|
$menu["content"] = array(
|
|
|
|
"name"=> _("Show Content"),
|
|
|
|
"icon" => "overview",
|
|
|
|
"url" => $baseUrl."schedule/show-content-dialog");
|
2013-03-26 21:03:53 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Show content can be modified from the calendar if:
|
2013-06-20 21:21:33 +02:00
|
|
|
// the show has not started,
|
2013-03-26 21:03:53 +01:00
|
|
|
// the user is admin or hosting the show,
|
2013-05-31 17:42:35 +02:00
|
|
|
// the show is not recorded
|
2013-06-20 21:21:33 +02:00
|
|
|
if ($now < $start && ($isAdminOrPM || $isHostOfShow) &&
|
2013-06-05 18:05:41 +02:00
|
|
|
!$this->ccShowInstance->isRecorded() ) {
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
$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");
|
|
|
|
}
|
|
|
|
|
|
|
|
//"Show Content" should be a menu item at all times except when
|
|
|
|
//the show is recorded
|
|
|
|
if (!$this->ccShowInstance->isRecorded()) {
|
|
|
|
|
|
|
|
$menu["content"] = array(
|
|
|
|
"name"=> _("Show Content"),
|
|
|
|
"icon" => "overview",
|
|
|
|
"url" => $baseUrl."schedule/show-content-dialog");
|
|
|
|
}
|
|
|
|
|
|
|
|
//show is currently playing and user is admin
|
|
|
|
if ($start <= $now && $now < $end && $isAdminOrPM) {
|
|
|
|
|
|
|
|
if ($this->ccShowInstance->isRecorded()) {
|
|
|
|
$menu["cancel_recorded"] = array(
|
|
|
|
"name"=> _("Cancel Current Show"),
|
|
|
|
"icon" => "delete");
|
|
|
|
} else {
|
|
|
|
$menu["cancel"] = array(
|
|
|
|
"name"=> _("Cancel Current Show"),
|
|
|
|
"icon" => "delete");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:44:30 +02:00
|
|
|
$isRepeating = $this->ccShow->getFirstCcShowDay()->isRepeating();
|
2013-06-05 18:05:41 +02:00
|
|
|
if (!$this->ccShowInstance->isRebroadcast() && $isAdminOrPM) {
|
2013-03-26 21:03:53 +01:00
|
|
|
if ($isRepeating) {
|
|
|
|
$menu["edit"] = array(
|
|
|
|
"name" => _("Edit"),
|
|
|
|
"icon" => "edit",
|
|
|
|
"items" => array());
|
|
|
|
|
|
|
|
$menu["edit"]["items"]["all"] = array(
|
|
|
|
"name" => _("Edit Show"),
|
|
|
|
"icon" => "edit",
|
|
|
|
"url" => $baseUrl."Schedule/populate-show-form");
|
|
|
|
|
|
|
|
$menu["edit"]["items"]["instance"] = array(
|
|
|
|
"name" => _("Edit This Instance"),
|
|
|
|
"icon" => "edit",
|
2013-03-27 21:25:39 +01:00
|
|
|
"url" => $baseUrl."Schedule/populate-repeating-show-instance-form");
|
2013-03-26 21:03:53 +01:00
|
|
|
} else {
|
|
|
|
$menu["edit"] = array(
|
|
|
|
"name"=> _("Edit Show"),
|
|
|
|
"icon" => "edit",
|
|
|
|
"_type"=>"all",
|
|
|
|
"url" => $baseUrl."Schedule/populate-show-form");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//show hasn't started yet and user is admin
|
|
|
|
if ($now < $start && $isAdminOrPM) {
|
|
|
|
//show is repeating so give user the option to delete all
|
|
|
|
//repeating instances or just the one
|
|
|
|
if ($isRepeating) {
|
|
|
|
$menu["del"] = array(
|
|
|
|
"name"=> _("Delete"),
|
|
|
|
"icon" => "delete",
|
|
|
|
"items" => array());
|
|
|
|
|
|
|
|
$menu["del"]["items"]["single"] = array(
|
|
|
|
"name"=> _("Delete This Instance"),
|
|
|
|
"icon" => "delete",
|
2013-04-03 17:46:46 +02:00
|
|
|
"url" => $baseUrl."schedule/delete-show-instance");
|
2013-03-26 21:03:53 +01:00
|
|
|
|
|
|
|
$menu["del"]["items"]["following"] = array(
|
|
|
|
"name"=> _("Delete This Instance and All Following"),
|
|
|
|
"icon" => "delete",
|
2013-04-03 17:46:46 +02:00
|
|
|
"url" => $baseUrl."schedule/delete-show");
|
2013-03-26 21:03:53 +01:00
|
|
|
} else {
|
|
|
|
$menu["del"] = array(
|
|
|
|
"name"=> _("Delete"),
|
|
|
|
"icon" => "delete",
|
|
|
|
"url" => $baseUrl."schedule/delete-show");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $menu;
|
2013-03-15 21:56:22 +01:00
|
|
|
}
|
|
|
|
|
2013-04-05 23:44:30 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Enter description here ...
|
|
|
|
* @param DateTime $dateTime object to add deltas to
|
|
|
|
* @param int $deltaDay delta days show moved
|
|
|
|
* @param int $deltaMin delta minutes show moved
|
2013-04-03 17:46:46 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:44:30 +02:00
|
|
|
private function validateShowMove($deltaDay, $deltaMin)
|
|
|
|
{
|
|
|
|
if (!$this->currentUser->isAdminOrPM()) {
|
|
|
|
throw new Exception(_("Permission denied"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->ccShow->getFirstCcShowDay()->isRepeating()) {
|
|
|
|
throw new Exception(_("Can't drag and drop repeating shows"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$today_timestamp = time();
|
|
|
|
|
|
|
|
$startsDateTime = new DateTime($this->ccShowInstance->getDbStarts(), new DateTimeZone("UTC"));
|
|
|
|
$endsDateTime = new DateTime($this->ccShowInstance->getDbEnds(), new DateTimeZone("UTC"));
|
|
|
|
|
|
|
|
if ($today_timestamp > $startsDateTime->getTimestamp()) {
|
|
|
|
throw new Exception(_("Can't move a past show"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//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 localtime
|
|
|
|
//stamp and then converted back to UTC to avoid show time changes!
|
2013-10-09 22:55:56 +02:00
|
|
|
$showTimezone = $this->ccShow->getFirstCcShowDay()->getDbTimezone();
|
|
|
|
$startsDateTime->setTimezone(new DateTimeZone($showTimezone));
|
|
|
|
$endsDateTime->setTimezone(new DateTimeZone($showTimezone));
|
2013-04-05 23:44:30 +02:00
|
|
|
|
|
|
|
$newStartsDateTime = self::addDeltas($startsDateTime, $deltaDay, $deltaMin);
|
|
|
|
$newEndsDateTime = self::addDeltas($endsDateTime, $deltaDay, $deltaMin);
|
|
|
|
|
|
|
|
//convert our new starts/ends to UTC.
|
|
|
|
$newStartsDateTime->setTimezone(new DateTimeZone("UTC"));
|
|
|
|
$newEndsDateTime->setTimezone(new DateTimeZone("UTC"));
|
|
|
|
|
|
|
|
if ($today_timestamp > $newStartsDateTime->getTimestamp()) {
|
|
|
|
throw new Exception(_("Can't move show into past"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if show is overlapping
|
|
|
|
$overlapping = Application_Model_Schedule::checkOverlappingShows(
|
|
|
|
$newStartsDateTime, $newEndsDateTime, true, $this->ccShowInstance->getDbId());
|
|
|
|
if ($overlapping) {
|
|
|
|
throw new Exception(_("Cannot schedule overlapping shows"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->ccShow->isRecorded()) {
|
|
|
|
//rebroadcasts should start at max 1 hour after a recorded show has ended.
|
|
|
|
$minRebroadcastStart = self::addDeltas($newEndsDateTime, 0, 60);
|
|
|
|
//check if we are moving a recorded show less than 1 hour before any of its own rebroadcasts.
|
|
|
|
$rebroadcasts = CcShowInstancesQuery::create()
|
2013-10-09 23:11:39 +02:00
|
|
|
->filterByDbOriginalShow($this->ccShow->getDbId())
|
2013-04-05 23:44:30 +02:00
|
|
|
->filterByDbStarts($minRebroadcastStart->format('Y-m-d H:i:s'), Criteria::LESS_THAN)
|
|
|
|
->find();
|
|
|
|
|
|
|
|
if (count($rebroadcasts) > 0) {
|
|
|
|
throw new Exception(_("Can't move a recorded show less than 1 hour before its rebroadcasts."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->ccShow->isRebroadcast()) {
|
2013-10-09 23:38:06 +02:00
|
|
|
$recordedShow = CcShowInstancesQuery::create()
|
|
|
|
->filterByCcShow($this->ccShowInstance->getDbOriginalShow())
|
|
|
|
->findOne();
|
2013-04-05 23:44:30 +02:00
|
|
|
if (is_null($recordedShow)) {
|
|
|
|
$this->ccShowInstance->delete();
|
|
|
|
throw new Exception(_("Show was deleted because recorded show does not exist!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$recordEndDateTime = new DateTime($recordedShow->getDbEnds(), new DateTimeZone("UTC"));
|
|
|
|
$newRecordEndDateTime = self::addDeltas($recordEndDateTime, 0, 60);
|
|
|
|
|
|
|
|
if ($newStartsDateTime->getTimestamp() < $newRecordEndDateTime->getTimestamp()) {
|
|
|
|
throw new Exception(_("Must wait 1 hour to rebroadcast."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($newStartsDateTime, $newEndsDateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function moveShow($deltaDay, $deltaMin)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$con = Propel::getConnection();
|
|
|
|
$con->beginTransaction();
|
|
|
|
|
|
|
|
list($newStartsDateTime, $newEndsDateTime) = $this->validateShowMove(
|
|
|
|
$deltaDay, $deltaMin);
|
|
|
|
|
|
|
|
$this->ccShowInstance
|
|
|
|
->setDbStarts($newStartsDateTime)
|
|
|
|
->setDbEnds($newEndsDateTime)
|
|
|
|
->save();
|
|
|
|
|
|
|
|
if (!$this->ccShowInstance->getCcShow()->isRebroadcast()) {
|
|
|
|
//we can get the first show day because we know the show is
|
|
|
|
//not repeating, and therefore will only have one show day entry
|
|
|
|
$ccShowDay = $this->ccShow->getFirstCcShowDay();
|
2013-10-09 22:55:56 +02:00
|
|
|
$showTimezone = new DateTimeZone($ccShowDay->getDbTimezone());
|
2013-04-05 23:44:30 +02:00
|
|
|
$ccShowDay
|
2013-10-09 22:55:56 +02:00
|
|
|
->setDbFirstShow($newStartsDateTime->setTimezone($showTimezone))
|
|
|
|
->setDbLastShow($newEndsDateTime->setTimezone($showTimezone))
|
2013-04-05 23:44:30 +02:00
|
|
|
->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Application_Service_SchedulerService::updateScheduleStartTime(
|
|
|
|
array($this->ccShowInstance->getDbId()), null, $newStartsDateTime);
|
|
|
|
|
|
|
|
$con->commit();
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$con->rollback();
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resizeShow($deltaDay, $deltaMin)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$con = Propel::getConnection();
|
|
|
|
$con->beginTransaction();
|
|
|
|
|
|
|
|
$con->commit();
|
|
|
|
Application_Model_RabbitMq::PushSchedule();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 21:21:33 +02:00
|
|
|
}
|