Created calendar service and moved the context menu creation there

This commit is contained in:
denise 2013-03-26 16:03:53 -04:00
parent 230d1a6aa9
commit 418bf0b5ff
6 changed files with 218 additions and 139 deletions

View File

@ -266,7 +266,7 @@ class Application_Common_DateHelper
public static function ConvertToLocalDateTime($p_dateString){
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
$dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$dateTime->setTimezone(new DateTimeZone(Application_Model_Preference::GetTimezone()));
return $dateTime;
}

View File

@ -255,107 +255,12 @@ class ScheduleController extends Zend_Controller_Action
public function makeContextMenuAction()
{
$id = $this->_getParam('id');
$menu = array();
$epochNow = time();
$baseUrl = Application_Common_OsPath::getBaseDir();
$instanceId = $this->_getParam('instanceId');
$showId = $this->_getParam('showId');
$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;
$service_calendar = new Application_Service_CalendarService($instanceId);
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;
$this->view->items = $service_calendar->makeContextMenu();
}
public function clearShowAction()
@ -462,9 +367,7 @@ class ScheduleController extends Zend_Controller_Action
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(){
public function populateShowInstanceFormAction(){
$formWhat = new Application_Form_AddShowWhat();
$formWho = new Application_Form_AddShowWho();
$formWhen = new Application_Form_AddShowWhen();
@ -527,7 +430,7 @@ class ScheduleController extends Zend_Controller_Action
$this->view->action = "edit-show-instance";
$this->view->newForm = $this->view->render('schedule/add-show-form.phtml');
}*/
}
public function populateShowFormAction()
{
@ -538,13 +441,13 @@ class ScheduleController extends Zend_Controller_Action
$this->view->action = "edit-show";
$isAdminOrPM = $this->currentUser->isAdminOrPM();
$isHostOfShow = $this->currentUser->isHostOfShow($showId);
/*$isHostOfShow = $this->currentUser->isHostOfShow($showId);
// 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 ($isHostOfShow && !$isAdminOrPM) {
$this->view->action = "dj-edit-show";
}
}*/
$forms = $this->createShowFormAction();
@ -568,7 +471,7 @@ class ScheduleController extends Zend_Controller_Action
}
}
public function djEditShowAction()
/*public function djEditShowAction()
{
$js = $this->_getParam('data');
$data = array();
@ -586,7 +489,7 @@ class ScheduleController extends Zend_Controller_Action
$show->setCustomPassword($data["custom_password"]);
$this->view->edit = true;
}
}*/
/*public function editShowInstanceAction(){
$js = $this->_getParam('data');
@ -782,32 +685,9 @@ class ScheduleController extends Zend_Controller_Action
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";
}
$service_showForm = new Application_Service_ShowFormService();
$result = $service_showForm->calculateDuration($this->_getParam('startTime'),
$this->_getParam('endTime'));
echo Zend_Json::encode($result);
exit();

View File

@ -175,4 +175,14 @@ class CcShowInstances extends BaseCcShowInstances {
return true;
}
public function isRecorded()
{
return $this->getDbRecord() == 1 ? true : false;
}
public function isRebroadcast()
{
return $this->getDbRebroadcast() == 1 ? true : false;
}
} // CcShowInstances

View File

@ -2,9 +2,169 @@
class Application_Service_CalendarService
{
public function __construct()
private $currentUser;
private $ccShowInstance;
private $showId;
public function __construct($instanceId = null)
{
if (!is_null($instanceId)) {
$this->ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
$this->showId = $this->ccShowInstance->getDbShowId();
}
$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();
$isHostOfShow = $this->currentUser->isHostOfShow($this->showId);
//DateTime objects in UTC
$startDT = new DateTime($this->ccShowInstance->getDbStarts(),
new DateTimeZone("UTC"));
$endDT = new DateTime($this->ccShowInstance->getDbEnds(),
new DateTimeZone("UTC"));
//timestamps
$start = $startDT->getTimestamp();
$end = $endDT->getTimestamp();
//show has ended
if ($now > $end) {
if ($this->ccShowInstance->isRecorded()) {
$ccFile = $this->ccShowInstance->getCcFiles();
$menu["view_recorded"] = array(
"name" => _("View Recorded File Metadata"),
"icon" => "overview",
"url" => $baseUrl."library/edit-file-md/id/".$ccFile->getDbId());
//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");
}
}
} else {
//Show content can be modified from the calendar if:
// the show has not started,
// the user is admin or hosting the show,
// the show is not recorded or rebroadcasted
if ($now < $start && ($isAdminOrPM || $isHostOfShow) &&
!$this->ccShowInstance->isRecorded() && !$this->ccShowInstance->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");
}
//"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");
}
}
$isRepeating = $this->ccShowInstance->getCcShow()->getFirstCcShowDay()->isRepeating();
if (!$this->ccShowInstance->isRebroadcast()) {
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",
"url" => $baseUrl."Schedule/populate-show-instance-form");
} 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) {
//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");
} else {
$menu["del"] = array(
"name"=> _("Delete"),
"icon" => "delete",
"url" => $baseUrl."schedule/delete-show");
}
}
}
return $menu;
}
}

View File

@ -149,6 +149,8 @@ class Application_Service_ShowFormService
//subtract one day
$repeatEndDate->sub(new DateInterval("P1D"));
//default monthly repeat type
$monthlyRepeatType = 2;
$repeatType = $ccShowDays[0]->getDbRepeatType();
if ($repeatType == REPEAT_MONTHLY_WEEKLY) {
$monthlyRepeatType = $repeatType;
@ -360,4 +362,31 @@ SQL;
return false;
}
}
public function calculateDuration($start, $end)
{
try {
$startDateTime = new DateTime($start);
$endDateTime = new DateTime($end);
$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');
return sprintf('%s%02dh %02dm', $sign, $hour, $min);
} else {
return $duration->format('%r%Hh %Im');
}
} catch (Exception $e) {
return "Invalid Date";
}
}
}

View File

@ -379,7 +379,7 @@ $(document).ready(function() {
//edit a single instance
callback = function() {
$.get(edit.instance.url, {format: "json", id: data.id, type: "instance"}, function(json){
$.get(edit.instance.url, {format: "json", showId: data.showId, instanceId: data.id, type: "instance"}, function(json){
beginEditShow(json);
});
};
@ -497,11 +497,11 @@ $(document).ready(function() {
items = oItems;
}
$.ajax({
url: baseUrl+"schedule/make-context-menu",
type: "GET",
data: {id : data.id, format: "json"},
data: {instanceId : data.id, showId: data.showId, format: "json"},
dataType: "json",
async: false,
success: function(json){