CC2646: Set a calendar view default (Day/week/month) that's remembered

Updated the calendar page so that when user chooses a different time scale to display(day/week/month),
the new setting is stored in the pref database. Each user id has its own entry in the database.

When visiting the calendar, we retrieves the entry from database for current user
and show that time scale; defaults to monthly view if no entry found.
This commit is contained in:
Yuchen Wang 2011-10-14 14:17:06 -04:00
parent 5a83c5b81e
commit 8b2a23b88a
4 changed files with 116 additions and 23 deletions

View file

@ -9,7 +9,7 @@ class ApiController extends Zend_Controller_Action
$context = $this->_helper->getHelper('contextSwitch'); $context = $this->_helper->getHelper('contextSwitch');
$context->addActionContext('version', 'json') $context->addActionContext('version', 'json')
->addActionContext('recorded-shows', 'json') ->addActionContext('recorded-shows', 'json')
->addActionContext('server-timestamp', 'json') ->addActionContext('calendar-init', 'json')
->addActionContext('upload-file', 'json') ->addActionContext('upload-file', 'json')
->addActionContext('upload-recorded', 'json') ->addActionContext('upload-recorded', 'json')
->addActionContext('media-monitor-setup', 'json') ->addActionContext('media-monitor-setup', 'json')
@ -62,10 +62,26 @@ class ApiController extends Zend_Controller_Action
echo $jsonStr; echo $jsonStr;
} }
public function serverTimestampAction(){ /**
* Sets up and send init values used in the Calendar.
$this->view->serverTimestamp = array("timestamp"=>time(), "timezoneOffset"=> date("Z")); * This is only being used by schedule.js at the moment.
*/
public function calendarInitAction(){
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
if(is_null(Zend_Auth::getInstance()->getStorage()->read())) {
header('HTTP/1.0 401 Unauthorized');
print 'You are not allowed to access this resource.';
return;
}
$this->view->calendarInit = array(
"timestamp"=>time(),
"timezoneOffset"=> date("Z"),
"timeScale"=>Application_Model_Preference::GetCalendarTimeScale()
);
} }
/** /**
@ -163,6 +179,7 @@ class ApiController extends Zend_Controller_Action
$date = new Application_Model_DateHelper; $date = new Application_Model_DateHelper;
$timeNow = $date->getTimestamp(); $timeNow = $date->getTimestamp();
$result = array("env"=>APPLICATION_ENV, $result = array("env"=>APPLICATION_ENV,
"schedulerTime"=>gmdate("Y-m-d H:i:s"), "schedulerTime"=>gmdate("Y-m-d H:i:s"),
"currentShow"=>Application_Model_Show::GetCurrentShow($timeNow), "currentShow"=>Application_Model_Show::GetCurrentShow($timeNow),

View file

@ -28,6 +28,7 @@ class ScheduleController extends Zend_Controller_Action
->addActionContext('get-form', 'json') ->addActionContext('get-form', 'json')
->addActionContext('upload-to-sound-cloud', 'json') ->addActionContext('upload-to-sound-cloud', 'json')
->addActionContext('content-context-menu', 'json') ->addActionContext('content-context-menu', 'json')
->addActionContext('set-time-scale', 'json')
->initContext(); ->initContext();
$this->sched_sess = new Zend_Session_Namespace("schedule"); $this->sched_sess = new Zend_Session_Namespace("schedule");
@ -728,6 +729,14 @@ class ScheduleController extends Zend_Controller_Action
//returns format jjmenu is looking for. //returns format jjmenu is looking for.
die(json_encode($menu)); die(json_encode($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'));
}
} }

View file

@ -3,11 +3,11 @@
class Application_Model_Preference class Application_Model_Preference
{ {
public static function SetValue($key, $value){ public static function SetValue($key, $value, $isUserValue = false){
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
//called from a daemon process //called from a daemon process
if(!class_exists("Zend_Auth", false) || !Zend_Auth::getInstance()->hasIdentity()) { if(!Zend_Auth::getInstance()->hasIdentity()) {
$id = NULL; $id = NULL;
} }
else { else {
@ -21,6 +21,12 @@ class Application_Model_Preference
//Check if key already exists //Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref" $sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'"; ." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue) {
$sql .= " AND subjid = '$id'";
}
$result = $CC_DBC->GetOne($sql); $result = $CC_DBC->GetOne($sql);
if ($result == 1 && is_null($id)){ if ($result == 1 && is_null($id)){
@ -29,9 +35,15 @@ class Application_Model_Preference
." WHERE keystr = '$key'"; ." WHERE keystr = '$key'";
} }
else if ($result == 1 && !is_null($id)){ else if ($result == 1 && !is_null($id)){
$sql = "UPDATE cc_pref" if($isUserValue) {
." SET subjid = $id, valstr = '$value'" $sql = "UPDATE cc_pref"
." WHERE keystr = '$key'"; ." SET valstr = '$value'"
." WHERE keystr = '$key' AND subjid = $id";
} else {
$sql = "UPDATE cc_pref"
." SET subjid = $id, valstr = '$value'"
." WHERE keystr = '$key'";
}
} }
else if(is_null($id)) { else if(is_null($id)) {
$sql = "INSERT INTO cc_pref (keystr, valstr)" $sql = "INSERT INTO cc_pref (keystr, valstr)"
@ -44,11 +56,21 @@ class Application_Model_Preference
return $CC_DBC->query($sql); return $CC_DBC->query($sql);
} }
public static function GetValue($key){ public static function GetValue($key, $isUserValue = false){
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
//Check if key already exists //Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref" $sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'"; ." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue) {
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) {
$id = $auth->getIdentity()->id;
$sql .= " AND subjid = '$id'";
}
}
$result = $CC_DBC->GetOne($sql); $result = $CC_DBC->GetOne($sql);
if ($result == 0) if ($result == 0)
@ -56,6 +78,12 @@ class Application_Model_Preference
else { else {
$sql = "SELECT valstr FROM cc_pref" $sql = "SELECT valstr FROM cc_pref"
." WHERE keystr = '$key'"; ." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue && $auth->hasIdentity()) {
$sql .= " AND subjid = '$id'";
}
$result = $CC_DBC->GetOne($sql); $result = $CC_DBC->GetOne($sql);
return $result; return $result;
} }
@ -441,7 +469,7 @@ class Application_Model_Preference
public static function GetAirtimeVersion(){ public static function GetAirtimeVersion(){
return self::GetValue("system_version"); return self::GetValue("system_version");
} }
public static function SetUploadToSoundcloudOption($upload) { public static function SetUploadToSoundcloudOption($upload) {
self::SetValue("soundcloud_upload_option", $upload); self::SetValue("soundcloud_upload_option", $upload);
} }
@ -449,7 +477,7 @@ class Application_Model_Preference
public static function GetUploadToSoundcloudOption() { public static function GetUploadToSoundcloudOption() {
return self::GetValue("soundcloud_upload_option"); return self::GetValue("soundcloud_upload_option");
} }
public static function SetSoundCloudDownloadbleOption($upload) { public static function SetSoundCloudDownloadbleOption($upload) {
self::SetValue("soundcloud_downloadable", $upload); self::SetValue("soundcloud_downloadable", $upload);
} }
@ -457,5 +485,21 @@ class Application_Model_Preference
public static function GetSoundCloudDownloadbleOption() { public static function GetSoundCloudDownloadbleOption() {
return self::GetValue("soundcloud_downloadable"); return self::GetValue("soundcloud_downloadable");
} }
/**
* Sets the time scale preference (day/week/month) in Calendar.
*
* @param $timeScale new time scale
*/
public static function SetCalendarTimeScale($timeScale) {
return self::SetValue("calendar_time_scale", $timeScale, true /* user specific */);
}
/**
* Retrieves the time scale preference for the current user.
*/
public static function GetCalendarTimeScale() {
return self::GetValue("calendar_time_scale", true /* user specific */);
}
} }

View file

@ -293,19 +293,34 @@ function buildEditDialog(json){
} }
/**
* Use user preference for time scale; defaults to month if preference was never set
*/
function getTimeScalePreference(data) {
var timeScale = data.calendarInit.timeScale;
if(timeScale == 'day') {
timeScale = 'agendaDay';
} else if(timeScale == 'week') {
timeScale = 'agendaWeek';
} else {
timeScale = 'month';
}
return timeScale;
}
function createFullCalendar(data){ function createFullCalendar(data){
serverTimezoneOffset = data.serverTimestamp.timezoneOffset; serverTimezoneOffset = data.calendarInit.timezoneOffset;
var mainHeight = document.documentElement.clientHeight - 200 - 50; var mainHeight = document.documentElement.clientHeight - 200 - 50;
$('#schedule_calendar').fullCalendar({ $('#schedule_calendar').fullCalendar({
header: { header: {
left: 'prev, next, today', left: 'prev, next, today',
center: 'title', center: 'title',
right: 'agendaDay, agendaWeek, month' right: 'agendaDay, agendaWeek, month'
}, },
defaultView: 'month', defaultView: getTimeScalePreference(data),
editable: false, editable: false,
allDaySlot: false, allDaySlot: false,
axisFormat: 'H:mm', axisFormat: 'H:mm',
@ -316,8 +331,8 @@ function createFullCalendar(data){
contentHeight: mainHeight, contentHeight: mainHeight,
theme: true, theme: true,
lazyFetching: false, lazyFetching: false,
serverTimestamp: parseInt(data.serverTimestamp.timestamp, 10), serverTimestamp: parseInt(data.calendarInit.timestamp, 10),
serverTimezoneOffset: parseInt(data.serverTimestamp.timezoneOffset, 10), serverTimezoneOffset: parseInt(data.calendarInit.timezoneOffset, 10),
events: getFullCalendarEvents, events: getFullCalendarEvents,
@ -330,11 +345,19 @@ function createFullCalendar(data){
eventResize: eventResize eventResize: eventResize
}); });
//Update time scale preference when day/week/month button is clicked
$(".fc-button-content").click(function() {
url = '/Schedule/set-time-scale/format/json';
$.post(url, {timeScale: $(this).text()},
function(json){
if(json.error) {
alert(json.error);
}
});
});
} }
$(window).load(function() { $(window).load(function() {
$.ajax({ url: "/Api/calendar-init/format/json", dataType:"json", success:createFullCalendar
$.ajax({ url: "/Api/server-timestamp/format/json", dataType:"json", success:createFullCalendar , error:function(jqXHR, textStatus, errorThrown){}});
, error:function(jqXHR, textStatus, errorThrown){}}); });
});