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->addActionContext('version', 'json')
->addActionContext('recorded-shows', 'json')
->addActionContext('server-timestamp', 'json')
->addActionContext('calendar-init', 'json')
->addActionContext('upload-file', 'json')
->addActionContext('upload-recorded', 'json')
->addActionContext('media-monitor-setup', 'json')
@ -62,9 +62,25 @@ class ApiController extends Zend_Controller_Action
echo $jsonStr;
}
public function serverTimestampAction(){
/**
* Sets up and send init values used in the Calendar.
* This is only being used by schedule.js at the moment.
*/
public function calendarInitAction(){
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$this->view->serverTimestamp = array("timestamp"=>time(), "timezoneOffset"=> date("Z"));
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;
$timeNow = $date->getTimestamp();
$result = array("env"=>APPLICATION_ENV,
"schedulerTime"=>gmdate("Y-m-d H:i:s"),
"currentShow"=>Application_Model_Show::GetCurrentShow($timeNow),

View file

@ -28,6 +28,7 @@ class ScheduleController extends Zend_Controller_Action
->addActionContext('get-form', 'json')
->addActionContext('upload-to-sound-cloud', 'json')
->addActionContext('content-context-menu', 'json')
->addActionContext('set-time-scale', 'json')
->initContext();
$this->sched_sess = new Zend_Session_Namespace("schedule");
@ -728,6 +729,14 @@ class ScheduleController extends Zend_Controller_Action
//returns format jjmenu is looking for.
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
{
public static function SetValue($key, $value){
public static function SetValue($key, $value, $isUserValue = false){
global $CC_CONFIG, $CC_DBC;
//called from a daemon process
if(!class_exists("Zend_Auth", false) || !Zend_Auth::getInstance()->hasIdentity()) {
if(!Zend_Auth::getInstance()->hasIdentity()) {
$id = NULL;
}
else {
@ -21,6 +21,12 @@ class Application_Model_Preference
//Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref"
." WHERE keystr = '$key'";
//For user specific preference, check if id matches as well
if($isUserValue) {
$sql .= " AND subjid = '$id'";
}
$result = $CC_DBC->GetOne($sql);
if ($result == 1 && is_null($id)){
@ -29,10 +35,16 @@ class Application_Model_Preference
." WHERE keystr = '$key'";
}
else if ($result == 1 && !is_null($id)){
if($isUserValue) {
$sql = "UPDATE cc_pref"
." 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)) {
$sql = "INSERT INTO cc_pref (keystr, valstr)"
." VALUES ('$key', '$value')";
@ -44,11 +56,21 @@ class Application_Model_Preference
return $CC_DBC->query($sql);
}
public static function GetValue($key){
public static function GetValue($key, $isUserValue = false){
global $CC_CONFIG, $CC_DBC;
//Check if key already exists
$sql = "SELECT COUNT(*) FROM cc_pref"
." 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);
if ($result == 0)
@ -56,6 +78,12 @@ class Application_Model_Preference
else {
$sql = "SELECT valstr FROM cc_pref"
." 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);
return $result;
}
@ -457,5 +485,21 @@ class Application_Model_Preference
public static function GetSoundCloudDownloadbleOption() {
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,9 +293,24 @@ 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){
serverTimezoneOffset = data.serverTimestamp.timezoneOffset;
serverTimezoneOffset = data.calendarInit.timezoneOffset;
var mainHeight = document.documentElement.clientHeight - 200 - 50;
@ -305,7 +320,7 @@ function createFullCalendar(data){
center: 'title',
right: 'agendaDay, agendaWeek, month'
},
defaultView: 'month',
defaultView: getTimeScalePreference(data),
editable: false,
allDaySlot: false,
axisFormat: 'H:mm',
@ -316,8 +331,8 @@ function createFullCalendar(data){
contentHeight: mainHeight,
theme: true,
lazyFetching: false,
serverTimestamp: parseInt(data.serverTimestamp.timestamp, 10),
serverTimezoneOffset: parseInt(data.serverTimestamp.timezoneOffset, 10),
serverTimestamp: parseInt(data.calendarInit.timestamp, 10),
serverTimezoneOffset: parseInt(data.calendarInit.timezoneOffset, 10),
events: getFullCalendarEvents,
@ -330,11 +345,19 @@ function createFullCalendar(data){
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() {
$.ajax({ url: "/Api/server-timestamp/format/json", dataType:"json", success:createFullCalendar
$.ajax({ url: "/Api/calendar-init/format/json", dataType:"json", success:createFullCalendar
, error:function(jqXHR, textStatus, errorThrown){}});
});