tasks/controller/taskscontroller.php

684 lines
17 KiB
PHP
Raw Normal View History

2014-04-05 23:09:35 +00:00
<?php
/**
* ownCloud - Tasks
*
* @author Raimund Schlüßler
* @copyright 2013 Raimund Schlüßler raimund.schluessler@googlemail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
2014-07-30 19:03:54 +00:00
namespace OCA\Tasks\Controller;
2014-04-05 23:09:35 +00:00
2014-11-18 17:48:18 +00:00
use \OCP\IRequest;
2014-08-17 20:35:34 +00:00
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\JSONResponse;
use \OCA\Tasks\Controller\Helper;
2014-04-05 23:09:35 +00:00
class TasksController extends Controller {
2014-08-17 20:35:34 +00:00
private $userId;
public function __construct($appName, IRequest $request, $userId){
parent::__construct($appName, $request);
$this->userId = $userId;
}
2014-04-05 23:09:35 +00:00
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function getTasks($listID = 'all', $type = 'all'){
2014-04-05 23:09:35 +00:00
$user_timezone = \OC_Calendar_App::getTimezone();
if ($listID == 'all'){
$calendars = \OC_Calendar_Calendar::allCalendars($this->userId, true);
} else {
$calendar = \OC_Calendar_App::getCalendar($listID, true, false);
$calendars = array($calendar);
}
2014-04-05 23:09:35 +00:00
$tasks = array();
$lists = array();
2014-04-05 23:09:35 +00:00
foreach( $calendars as $calendar ) {
$calendar_entries = \OC_Calendar_Object::all($calendar['id']);
$tasks_selected = array();
foreach( $calendar_entries as $task ) {
2014-04-05 23:09:35 +00:00
if($task['objecttype']!='VTODO') {
continue;
}
if(is_null($task['summary'])) {
continue;
}
$vtodo = Helper::parseVTODO($task['calendardata']);
try {
2015-02-25 20:14:44 +00:00
$task_data = Helper::arrayForJSON($task['id'], $vtodo, $user_timezone, $calendar['id']);
switch($type){
case 'all':
$tasks[] = $task_data;
break;
case 'init':
if (!$task_data['completed']){
$tasks[] = $task_data;
} else {
$tasks_selected[] = $task_data;
}
break;
case 'completed':
if ($task_data['completed']){
$tasks[] = $task_data;
}
break;
case 'uncompleted':
if (!$task_data['completed']){
$tasks[] = $task_data;
}
break;
}
2014-04-05 23:09:35 +00:00
} catch(\Exception $e) {
2014-07-30 19:03:54 +00:00
\OCP\Util::writeLog('tasks', $e->getMessage(), \OCP\Util::ERROR);
2014-04-05 23:09:35 +00:00
}
}
$nrCompleted = 0;
$notLoaded = 0;
usort($tasks_selected, array($this, 'sort_completed'));
foreach( $tasks_selected as $task_selected){
$nrCompleted++;
if ($nrCompleted > 5){
$notLoaded++;
continue;
}
$tasks[] = $task_selected;
}
$lists[] = array(
'id' => $calendar['id'],
'notLoaded' => $notLoaded
);
2014-04-05 23:09:35 +00:00
}
$result = array(
'data' => array(
'tasks' => $tasks,
'lists' => $lists
2014-04-05 23:09:35 +00:00
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
}
/**
* @NoAdminRequired
*/
public function getTask($taskID){
$object = \OC_Calendar_App::getEventObject($taskID);
$user_timezone = \OC_Calendar_App::getTimezone();
$task = array();
if($object['objecttype']=='VTODO' && !is_null($object['summary'])) {
$vtodo = Helper::parseVTODO($object['calendardata']);
try {
2015-02-28 20:39:06 +00:00
$task_data = Helper::arrayForJSON($object['id'], $vtodo, $user_timezone, $object['calendarid']);
$task[] = $task_data;
} catch(\Exception $e) {
\OCP\Util::writeLog('tasks', $e->getMessage(), \OCP\Util::ERROR);
}
}
$result = array(
'data' => array(
'tasks' => $task
)
);
$response = new JSONResponse();
$response->setData($result);
return $response;
}
private static function sort_completed($a, $b){
$t1 = \DateTime::createFromFormat('Ymd\THis', $a['completed_date']);
$t2 = \DateTime::createFromFormat('Ymd\THis', $b['completed_date']);
if ($t1 == $t2) {
return 0;
}
return $t1 < $t2 ? 1 : -1;
}
/**
* @param boolean $isStarred
*/
2014-04-05 23:09:35 +00:00
private function setStarred($isStarred){
$taskId = (int) $this->params('taskID');
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
if($isStarred){
$vtodo->PRIORITY = 1;
2014-04-05 23:09:35 +00:00
}else{
$vtodo->__unset('PRIORITY');
}
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function starTask(){
$response = new JSONResponse();
try {
$this->setStarred(true);
return $response;
} catch(\Exception $e) {
return $response;
// return $this->renderJSON(array(), $e->getMessage());
}
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function unstarTask(){
$response = new JSONResponse();
try {
$this->setStarred(false);
return $response;
} catch(\Exception $e) {
return $response;
// return $this->renderJSON(array(), $e->getMessage());
}
}
2014-07-11 20:50:01 +00:00
private function setPercentComplete($percent_complete){
2014-04-05 23:09:35 +00:00
$taskId = (int) $this->params('taskID');
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
if (!empty($percent_complete)) {
$vtodo->{'PERCENT-COMPLETE'} = $percent_complete;
2014-04-05 23:09:35 +00:00
}else{
$vtodo->__unset('PERCENT-COMPLETE');
}
if ($percent_complete == 100) {
$vtodo->STATUS = 'COMPLETED';
$vtodo->COMPLETED = new \DateTime('now', new \DateTimeZone('UTC'));
2015-02-28 19:53:12 +00:00
} elseif ($percent_complete != 0) {
$vtodo->STATUS = 'IN-PROCESS';
2015-02-28 19:53:12 +00:00
unset($vtodo->COMPLETED);
} else{
$vtodo->STATUS = 'NEEDS-ACTION';
2014-04-05 23:09:35 +00:00
unset($vtodo->COMPLETED);
}
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
}
2014-07-13 16:17:37 +00:00
/**
* @NoAdminRequired
*/
2014-07-16 18:44:15 +00:00
public function percentComplete(){
2014-07-11 20:50:01 +00:00
$response = new JSONResponse();
try{
2014-07-13 16:17:37 +00:00
$percent_complete = $this->params('complete');
2014-07-11 20:50:01 +00:00
$this->setPercentComplete( $percent_complete );
return $response;
2014-07-13 16:17:37 +00:00
} catch(\Exception $e) {
return $response;
// return $this->renderJSON(array(), $e->getMessage());
}
}
2014-07-11 20:50:01 +00:00
2014-04-05 23:09:35 +00:00
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function completeTask(){
$response = new JSONResponse();
try {
2014-07-11 20:50:01 +00:00
$this->setPercentComplete(100);
2014-04-05 23:09:35 +00:00
return $response;
} catch(\Exception $e) {
return $response;
// return $this->renderJSON(array(), $e->getMessage());
}
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function uncompleteTask(){
$response = new JSONResponse();
try {
2014-07-11 20:50:01 +00:00
$this->setPercentComplete(0);
2014-04-05 23:09:35 +00:00
return $response;
} catch(\Exception $e) {
return $response;
// return $this->renderJSON(array(), $e->getMessage());
}
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function addTask(){
$taskName = $this->params('name');
$calendarId = $this->params('calendarID');
$starred = $this->params('starred');
$due = $this->params('due');
$start = $this->params('start');
2014-04-05 23:09:35 +00:00
$response = new JSONResponse();
$user_timezone = \OC_Calendar_App::getTimezone();
$request = array(
'summary' => $taskName,
'categories' => null,
'priority' => $starred,
'location' => null,
'due' => $due,
'start' => $start,
2014-04-05 23:09:35 +00:00
'description' => null
);
$vcalendar = Helper::createVCalendarFromRequest($request);
$taskId = \OC_Calendar_Object::add($calendarId, $vcalendar->serialize());
2015-02-28 20:39:06 +00:00
$task = Helper::arrayForJSON($taskId, $vcalendar->VTODO, $user_timezone, $calendarId);
2014-04-05 23:09:35 +00:00
$task['tmpID'] = $this->params('tmpID');
$result = array(
'data' => array(
'task' => $task
)
);
$response->setData($result);
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function deleteTask(){
$response = new JSONResponse();
$taskId = $this->params('taskID');
\OC_Calendar_Object::delete($taskId);
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function setTaskName(){
$taskId = (int) $this->params('taskID');
$taskName = $this->params('name');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
$vtodo->SUMMARY = $taskName;
2014-04-05 23:09:35 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function setTaskCalendar(){
$taskId = $this->params('taskID');
$calendarId = $this->params('calendarID');
$response = new JSONResponse();
try {
$data = \OC_Calendar_App::getEventObject($taskId);
2014-07-31 16:18:28 +00:00
if ($data['calendarid'] != $calendarId) {
2014-04-05 23:09:35 +00:00
\OC_Calendar_Object::moveToCalendar($taskId, $calendarId);
}
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function setTaskNote(){
$taskId = $this->params('taskID');
$note = $this->params('note');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
$vtodo->DESCRIPTION = $note;
2014-04-05 23:09:35 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function setDueDate(){
$taskId = $this->params('taskID');
$due = $this->params('due');
$response = new JSONResponse();
try{
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
2014-11-09 16:44:17 +00:00
if ($due != false) {
2014-04-05 23:09:35 +00:00
$timezone = \OC_Calendar_App::getTimezone();
$timezone = new \DateTimeZone($timezone);
$due = new \DateTime('@'.$due);
$due->setTimezone($timezone);
$vtodo->DUE = $due;
2014-11-09 16:44:17 +00:00
} else {
unset($vtodo->DUE);
2014-04-05 23:09:35 +00:00
}
2014-07-31 16:18:28 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
2014-04-05 23:09:35 +00:00
} catch (\Exception $e) {
}
return $response;
}
/**
* @NoAdminRequired
*/
public function setStartDate(){
$taskId = $this->params('taskID');
$start = $this->params('start');
$response = new JSONResponse();
try{
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
2014-11-09 16:44:17 +00:00
if ($start != false) {
$timezone = \OC_Calendar_App::getTimezone();
$timezone = new \DateTimeZone($timezone);
$start = new \DateTime('@'.$start);
$start->setTimezone($timezone);
$vtodo->DTSTART = $start;
2014-11-09 16:44:17 +00:00
} else {
unset($vtodo->DTSTART);
}
2014-07-31 16:18:28 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch (\Exception $e) {
}
return $response;
}
2014-04-05 23:09:35 +00:00
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function setReminderDate(){
$taskId = $this->params('taskID');
2014-04-27 15:34:35 +00:00
$type = $this->params('type');
$action = $this->params('action');
2014-04-05 23:09:35 +00:00
$response = new JSONResponse();
2014-04-27 15:34:35 +00:00
$types = array('DATE-TIME','DURATION');
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
$valarm = $vtodo->VALARM;
if ($type == false){
unset($vtodo->VALARM);
$vtodo->__get('LAST-MODIFIED')->setValue(new \DateTime('now', new \DateTimeZone('UTC')));
$vtodo->DTSTAMP = new \DateTime('now', new \DateTimeZone('UTC'));
2014-04-27 15:34:35 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
}
elseif (in_array($type,$types)) {
try{
if($valarm == null) {
$valarm = $vcalendar->createComponent('VALARM');
$valarm->ACTION = $action;
$valarm->DESCRIPTION = 'Default Event Notification';
2014-04-27 15:34:35 +00:00
$vtodo->add($valarm);
} else {
unset($valarm->TRIGGER);
}
2014-05-06 18:12:03 +00:00
$tv = '';
$related = null;
2014-04-27 15:34:35 +00:00
if ($type == 'DATE-TIME') {
$date = new \DateTime('@'.$this->params('date'));
2014-05-06 18:12:03 +00:00
$tv = $date->format('Ymd\THis\Z');
2014-04-27 15:34:35 +00:00
} elseif ($type == 'DURATION') {
2014-05-06 18:12:03 +00:00
$invert = $this->params('invert');
$related= $this->params('related');
$week = (int)$this->params('week');
$day = (int)$this->params('day');
$hour = (int)$this->params('hour');
$minute = (int)$this->params('minute');
$second = (int)$this->params('second');
// Create duration string
if($week || $day || $hour || $minute || $second) {
if ($invert){
$tv.='-';
}
$tv.='P';
if ($week){
$tv.=$week.'W';
}
if ($day){
$tv.=$day.'D';
}
$tv.='T';
if ($hour){
$tv.=$hour.'H';
}
if ($minute){
$tv.=$minute.'M';
}
if ($second){
$tv.=$second.'S';
}
}else{
$tv = 'PT0S';
}
}
2014-05-06 18:12:03 +00:00
if($related == 'END'){
2015-05-17 17:31:41 +00:00
$valarm->add('TRIGGER', $tv, array('VALUE' => $type, 'RELATED' => $related));
2014-05-06 18:12:03 +00:00
} else {
2015-05-17 17:31:41 +00:00
$valarm->add('TRIGGER', $tv, array('VALUE' => $type));
2014-05-06 18:12:03 +00:00
}
$vtodo->__get('LAST-MODIFIED')->setValue(new \DateTime('now', new \DateTimeZone('UTC')));
$vtodo->DTSTAMP = new \DateTime('now', new \DateTimeZone('UTC'));
2014-04-27 15:34:35 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch (\Exception $e) {
2014-04-05 23:09:35 +00:00
2014-04-27 15:34:35 +00:00
}
}
2014-04-05 23:09:35 +00:00
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
public function addCategory(){
2014-04-05 23:09:35 +00:00
$taskId = $this->params('taskID');
$category = $this->params('category');
2014-04-05 23:09:35 +00:00
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
// fetch categories from TODO
$categories = $vtodo->CATEGORIES;
if ($categories){
$taskcategories = $categories->getParts();
}
// add category
if (!in_array($category, $taskcategories)){
$taskcategories[] = $category;
2015-05-24 10:16:41 +00:00
$vtodo->CATEGORIES = $taskcategories;
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
}
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
* @NoAdminRequired
*/
public function removeCategory(){
$taskId = $this->params('taskID');
$category = $this->params('category');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
// fetch categories from TODO
$categories = $vtodo->CATEGORIES;
if ($categories){
$taskcategories = $categories->getParts();
}
// remove category
$key = array_search($category, $taskcategories);
if ($key !== null && $key !== false){
unset($taskcategories[$key]);
2015-05-25 11:36:23 +00:00
if(count($taskcategories)){
$vtodo->CATEGORIES = $taskcategories;
} else{
$vtodo->__unset('CATEGORIES');
}
2015-05-24 10:16:41 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
}
2014-04-05 23:09:35 +00:00
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
2014-04-10 18:25:48 +00:00
* @NoAdminRequired
2014-04-05 23:09:35 +00:00
*/
2014-07-16 18:44:15 +00:00
public function setLocation(){
2014-04-05 23:09:35 +00:00
$taskId = $this->params('taskID');
$location = $this->params('location');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
$vtodo->LOCATION = $location;
2014-04-05 23:09:35 +00:00
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
2014-07-16 18:44:15 +00:00
/**
* @NoAdminRequired
*/
public function addComment(){
$taskId = $this->params('taskID');
$comment = $this->params('comment');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
2015-05-18 12:42:23 +00:00
if($vtodo->COMMENT == "") {
// if this is the first comment set the id to 0
$commentId = 0;
} else {
// Determine new commentId by looping through all comments
$commentIds = array();
foreach($vtodo->COMMENT as $com) {
$commentIds[] = (int)$com['X-OC-ID']->getValue();
}
$commentId = 1+max($commentIds);
}
$now = new \DateTime();
2015-05-17 17:19:46 +00:00
$vtodo->add('COMMENT',$comment,
array(
2014-11-23 17:17:51 +00:00
'X-OC-ID' => $commentId,
'X-OC-USERID' => $this->userId,
'X-OC-DATE-TIME' => $now->format('Ymd\THis\Z')
)
);
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
$user_timezone = \OC_Calendar_App::getTimezone();
$now->setTimezone(new \DateTimeZone($user_timezone));
$comment = array(
'taskID' => $taskId,
'id' => $commentId,
'tmpID' => $this->params('tmpID'),
2014-08-17 20:35:34 +00:00
'name' => \OCP\User::getDisplayName(),
'userID' => $this->userId,
'comment' => $comment,
'time' => $now->format('Ymd\THis')
);
$result = array(
'data' => array(
'comment' => $comment
)
);
$response->setData($result);
2014-07-16 18:44:15 +00:00
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
/**
* @NoAdminRequired
*/
public function deleteComment(){
$taskId = $this->params('taskID');
$commentId = $this->params('commentID');
$response = new JSONResponse();
try {
$vcalendar = \OC_Calendar_App::getVCalendar($taskId);
$vtodo = $vcalendar->VTODO;
$commentIndex = $this->getCommentById($vtodo,$commentId);
$comment = $vtodo->children[$commentIndex];
2015-05-17 17:19:46 +00:00
if($comment['X-OC-USERID']->getValue() == $this->userId){
unset($vtodo->children[$commentIndex]);
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
}else{
throw new \Exception('Not allowed.');
}
} catch(\Exception $e) {
// throw new BusinessLayerException($e->getMessage());
}
return $response;
}
2014-04-05 23:09:35 +00:00
/**
* @NoAdminRequired
*/
public function getCommentById($vtodo,$commentId) {
$idx = 0;
foreach ($vtodo->children as $i => &$property) {
2015-05-17 17:19:46 +00:00
if ( $property->name == 'COMMENT' && $property['X-OC-ID']->getValue() == $commentId ) {
return $idx;
}
$idx += 1;
}
throw new \Exception('Commment not found.');
}
}