Tasks: Update task list when adding or updating tasks

This commit is contained in:
Bart Visscher 2012-03-08 22:26:54 +01:00
parent 8f9c852325
commit d1b2c10422
6 changed files with 108 additions and 88 deletions

View File

@ -8,11 +8,7 @@ OC_JSON::checkAppEnabled('tasks');
$l10n = new OC_L10N('tasks'); $l10n = new OC_L10N('tasks');
$cid = $_POST['id']; $cid = $_POST['id'];
$calendar = OC_Calendar_Calendar::findCalendar( $cid ); $calendar = OC_Calendar_App::getCalendar( $cid );
if( $calendar === false || $calendar['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your calendar!'))));
exit();
}
$errors = OC_Task_App::validateRequest($_POST); $errors = OC_Task_App::validateRequest($_POST);
if (!empty($errors)) { if (!empty($errors)) {
@ -30,4 +26,7 @@ $tmpl->assign('details',$vcalendar->VTODO);
$tmpl->assign('id',$id); $tmpl->assign('id',$id);
$page = $tmpl->fetchPage(); $page = $tmpl->fetchPage();
OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page ))); $user_timezone = OC_Preferences::getValue(OC_USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
$task = OC_Task_App::arrayForJSON($id, $vcalendar->VTODO, $user_timezone);
OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page, 'task' => $task )));

View File

@ -8,8 +8,16 @@ OC_JSON::checkAppEnabled('tasks');
$l10n = new OC_L10N('tasks'); $l10n = new OC_L10N('tasks');
$calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser(), true); $calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser(), true);
$category_options = OC_Calendar_Object::getCategoryOptions($l10n);
$percent_options = range(0, 100, 10);
$priority_options = OC_Task_App::getPriorityOptions();
$tmpl = new OC_Template('tasks','part.addtaskform'); $tmpl = new OC_Template('tasks','part.addtaskform');
$tmpl->assign('calendars',$calendars); $tmpl->assign('calendars',$calendars);
$tmpl->assign('category_options', $category_options);
$tmpl->assign('percent_options', $percent_options);
$tmpl->assign('priority_options', $priority_options);
$tmpl->assign('details', new OC_VObject('VTODO'));
$tmpl->assign('categories', array());
$page = $tmpl->fetchPage(); $page = $tmpl->fetchPage();
OC_JSON::success(array('data' => array( 'page' => $page ))); OC_JSON::success(array('data' => array( 'page' => $page )));

View File

@ -26,4 +26,7 @@ $tmpl->assign('details', $vcalendar->VTODO);
$tmpl->assign('id', $id); $tmpl->assign('id', $id);
$page = $tmpl->fetchPage(); $page = $tmpl->fetchPage();
OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page ))); $user_timezone = OC_Preferences::getValue(OC_USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
$task = OC_Task_App::arrayForJSON($id, $vcalendar->VTODO, $user_timezone);
OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page, 'task' => $task )));

View File

@ -26,32 +26,7 @@ foreach( $calendars as $calendar ){
} }
$object = OC_VObject::parse($task['calendardata']); $object = OC_VObject::parse($task['calendardata']);
$vtodo = $object->VTODO; $vtodo = $object->VTODO;
$task = array( 'id' => $task['id'] ); $tasks[] = OC_Task_App::arrayForJSON($task['id'], $vtodo, $user_timezone);
$task['summary'] = $vtodo->getAsString('SUMMARY');
$task['description'] = $vtodo->getAsString('DESCRIPTION');
$task['location'] = $vtodo->getAsString('LOCATION');
$task['categories'] = $vtodo->getAsArray('CATEGORIES');
$due = $vtodo->DUE;
if ($due) {
$due = $due->getDateTime();
$due->setTimezone(new DateTimeZone($user_timezone));
$task['due'] = $due->format('Y-m-d H:i:s');
}
else {
$task['due'] = false;
}
$task['priority'] = $vtodo->getAsString('PRIORITY');
$completed = $vtodo->COMPLETED;
if ($completed) {
$completed = $completed->getDateTime();
$completed->setTimezone(new DateTimeZone($user_timezone));
$task['completed'] = $completed->format('Y-m-d H:i:s');
}
else {
$task['completed'] = false;
}
$task['complete'] = $vtodo->getAsString('PERCENT-COMPLETE');
$tasks[] = $task;
} }
} }

View File

@ -1,4 +1,52 @@
OC.Tasks = { OC.Tasks = {
create_task_div:function(task) {
var task_container = $('<div>')
.addClass('task')
.data('task', task)
.data('show_count', 0)
.attr('data-id', task.id)
.append($('<p>')
.html('<a href="index.php?id='+task.id+'">'+task.summary+'</a>')
.addClass('summary')
.attr('title', task.description)
)
//.append(actions.clone().removeAttr('id'))
;
var checkbox = $('<input type="checkbox">');
if (task.completed) {
checkbox.attr('checked', 'checked');
}
$('<div>')
.addClass('completed')
.append(checkbox)
.prependTo(task_container);
var priority = task.priority;
$('<div>')
.addClass('tag')
.addClass('priority')
.addClass('priority-'+(priority?priority:'n'))
.text(priority)
.prependTo(task_container);
if (task.location) {
$('<div>')
.addClass('tag')
.addClass('location')
.text(task.location)
.appendTo(task_container);
}
if (task.categories.length > 0) {
var categories = $('<div>')
.addClass('categories')
.appendTo(task_container);
$(task.categories).each(function(i, category){
categories.append($('<a>')
.addClass('tag')
.text(category)
);
});
}
return task_container;
},
filter:function(tag, find_filter) { filter:function(tag, find_filter) {
var tag_text = $(tag).text(); var tag_text = $(tag).text();
var filter = !$(tag).hasClass('active'); var filter = !$(tag).hasClass('active');
@ -41,50 +89,7 @@ $(document).ready(function(){
var tasks = $('#tasks').empty().data('show_count', 0); var tasks = $('#tasks').empty().data('show_count', 0);
var actions = $('#task_actions_template'); var actions = $('#task_actions_template');
$(jsondata).each(function(i, task) { $(jsondata).each(function(i, task) {
var task_container = $('<div>').appendTo(tasks) tasks.append(OC.Tasks.create_task_div(task));
.addClass('task')
.data('task', task)
.data('show_count', 0)
.attr('data-id', task.id)
.append($('<p>')
.html('<a href="index.php?id='+task.id+'">'+task.summary+'</a>')
.addClass('summary')
)
//.append(actions.clone().removeAttr('id'))
;
var checkbox = $('<input type="checkbox">');
if (task.completed) {
checkbox.attr('checked', 'checked');
}
$('<div>')
.addClass('completed')
.append(checkbox)
.prependTo(task_container);
var priority = task.priority;
$('<div>')
.addClass('tag')
.addClass('priority')
.addClass('priority-'+(priority?priority:'n'))
.text(priority)
.prependTo(task_container);
if (task.location) {
$('<div>')
.addClass('tag')
.addClass('location')
.text(task.location)
.appendTo(task_container);
}
if (task.categories.length > 0) {
var categories = $('<div>')
.addClass('categories')
.appendTo(task_container);
$(task.categories).each(function(i, category){
categories.append($('<a>')
.addClass('tag')
.text(category)
);
});
}
}); });
if( $('#tasks div').length > 0 ){ if( $('#tasks div').length > 0 ){
$('#tasks div').first().addClass('active'); $('#tasks div').first().addClass('active');
@ -162,6 +167,7 @@ $(document).ready(function(){
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
$('#task_details').data('id',jsondata.data.id); $('#task_details').data('id',jsondata.data.id);
$('#task_details').html(jsondata.data.page); $('#task_details').html(jsondata.data.page);
$('#tasks').append(OC.Tasks.create_task_div(jsondata.data.task));
} }
else{ else{
alert(jsondata.data.message); alert(jsondata.data.message);
@ -196,8 +202,13 @@ $(document).ready(function(){
$('.error_msg').remove(); $('.error_msg').remove();
$('.error').removeClass('error'); $('.error').removeClass('error');
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
$('#task_details').data('id',jsondata.data.id); var id = jsondata.data.id;
$('#task_details').data('id',id);
$('#task_details').html(jsondata.data.page); $('#task_details').html(jsondata.data.page);
var task = jsondata.data.task;
$('#tasks .task[data-id='+id+']')
.data('task', task)
.html(OC.Tasks.create_task_div(task).html());
} }
else{ else{
var errors = jsondata.data.errors; var errors = jsondata.data.errors;

View File

@ -43,6 +43,36 @@ class OC_Task_App {
); );
} }
public static function arrayForJSON($id, $vtodo, $user_timezone)
{
$task = array( 'id' => $id );
$task['summary'] = $vtodo->getAsString('SUMMARY');
$task['description'] = $vtodo->getAsString('DESCRIPTION');
$task['location'] = $vtodo->getAsString('LOCATION');
$task['categories'] = $vtodo->getAsArray('CATEGORIES');
$due = $vtodo->DUE;
if ($due) {
$due = $due->getDateTime();
$due->setTimezone(new DateTimeZone($user_timezone));
$task['due'] = $due->format('Y-m-d H:i:s');
}
else {
$task['due'] = false;
}
$task['priority'] = $vtodo->getAsString('PRIORITY');
$completed = $vtodo->COMPLETED;
if ($completed) {
$completed = $completed->getDateTime();
$completed->setTimezone(new DateTimeZone($user_timezone));
$task['completed'] = $completed->format('Y-m-d H:i:s');
}
else {
$task['completed'] = false;
}
$task['complete'] = $vtodo->getAsString('PERCENT-COMPLETE');
return $task;
}
public static function validateRequest($request) public static function validateRequest($request)
{ {
$errors = array(); $errors = array();
@ -84,22 +114,16 @@ class OC_Task_App {
public static function createVCalendarFromRequest($request) public static function createVCalendarFromRequest($request)
{ {
$vcalendar = new Sabre_VObject_Component('VCALENDAR'); $vcalendar = new OC_VObject('VCALENDAR');
$vcalendar->add('PRODID', 'ownCloud Calendar'); $vcalendar->add('PRODID', 'ownCloud Calendar');
$vcalendar->add('VERSION', '2.0'); $vcalendar->add('VERSION', '2.0');
$now = new DateTime(); $vtodo = new OC_VObject('VTODO');
$vtodo = new Sabre_VObject_Component('VTODO');
$vcalendar->add($vtodo); $vcalendar->add($vtodo);
$created = new Sabre_VObject_Element_DateTime('CREATED'); $vtodo->setDateTime('CREATED', 'now', Sabre_VObject_Element_DateTime::UTC);
$created->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
$vtodo->add($created);
$uid = self::createUID();
$vtodo->add('UID',$uid);
$vtodo->setUID();
return self::updateVCalendarFromRequest($request, $vcalendar); return self::updateVCalendarFromRequest($request, $vcalendar);
} }