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
|
|
|
*/
|
2014-11-06 21:17:51 +00:00
|
|
|
public function getTasks($listID = 'all', $type = 'all'){
|
2014-11-08 15:12:37 +00:00
|
|
|
|
2014-04-05 23:09:35 +00:00
|
|
|
$user_timezone = \OC_Calendar_App::getTimezone();
|
2014-11-08 15:12:37 +00:00
|
|
|
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();
|
2014-11-06 21:17:51 +00:00
|
|
|
$lists = array();
|
2014-04-05 23:09:35 +00:00
|
|
|
foreach( $calendars as $calendar ) {
|
2014-11-08 15:12:37 +00:00
|
|
|
$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']);
|
2014-11-06 21:17:51 +00:00
|
|
|
switch($type){
|
|
|
|
case 'all':
|
2014-11-08 15:12:37 +00:00
|
|
|
$tasks[] = $task_data;
|
|
|
|
break;
|
|
|
|
case 'init':
|
|
|
|
if (!$task_data['completed']){
|
|
|
|
$tasks[] = $task_data;
|
|
|
|
} else {
|
|
|
|
$tasks_selected[] = $task_data;
|
2014-11-06 21:17:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'completed':
|
2014-11-08 15:12:37 +00:00
|
|
|
if ($task_data['completed']){
|
|
|
|
$tasks[] = $task_data;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'uncompleted':
|
2014-11-06 21:17:51 +00:00
|
|
|
if (!$task_data['completed']){
|
2014-11-08 15:12:37 +00:00
|
|
|
$tasks[] = $task_data;
|
2014-11-06 21:17:51 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-11-08 15:12:37 +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;
|
|
|
|
}
|
2014-11-06 21:17:51 +00:00
|
|
|
$lists[] = array(
|
|
|
|
'id' => $calendar['id'],
|
|
|
|
'notLoaded' => $notLoaded
|
|
|
|
);
|
2014-04-05 23:09:35 +00:00
|
|
|
}
|
|
|
|
$result = array(
|
|
|
|
'data' => array(
|
2014-11-06 21:17:51 +00:00
|
|
|
'tasks' => $tasks,
|
|
|
|
'lists' => $lists
|
2014-04-05 23:09:35 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = new JSONResponse();
|
|
|
|
$response->setData($result);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-03-13 23:13:32 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2015-02-07 11:56:20 +00:00
|
|
|
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']);
|
2015-02-07 11:56:20 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:12:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:15:03 +00:00
|
|
|
/**
|
|
|
|
* @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){
|
2015-05-15 10:13:05 +00:00
|
|
|
$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)) {
|
2015-05-15 10:13:05 +00:00
|
|
|
$vtodo->{'PERCENT-COMPLETE'} = $percent_complete;
|
2014-04-05 23:09:35 +00:00
|
|
|
}else{
|
|
|
|
$vtodo->__unset('PERCENT-COMPLETE');
|
|
|
|
}
|
|
|
|
if ($percent_complete == 100) {
|
2015-04-17 10:52:09 +00:00
|
|
|
$vtodo->STATUS = 'COMPLETED';
|
|
|
|
$vtodo->COMPLETED = new \DateTime('now', new \DateTimeZone('UTC'));
|
2015-02-28 19:53:12 +00:00
|
|
|
} elseif ($percent_complete != 0) {
|
2015-05-15 10:13:05 +00:00
|
|
|
$vtodo->STATUS = 'IN-PROCESS';
|
2015-02-28 19:53:12 +00:00
|
|
|
unset($vtodo->COMPLETED);
|
|
|
|
} else{
|
2015-05-15 10:13:05 +00:00
|
|
|
$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');
|
2014-04-19 18:28:22 +00:00
|
|
|
$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,
|
2014-04-19 18:28:22 +00:00
|
|
|
'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;
|
2015-05-15 10:13:05 +00:00
|
|
|
$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;
|
2015-05-15 10:13:05 +00:00
|
|
|
$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);
|
2015-04-17 10:52:09 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2014-04-19 18:28:22 +00:00
|
|
|
/**
|
|
|
|
* @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) {
|
2014-04-19 18:28:22 +00:00
|
|
|
$timezone = \OC_Calendar_App::getTimezone();
|
|
|
|
$timezone = new \DateTimeZone($timezone);
|
|
|
|
|
|
|
|
$start = new \DateTime('@'.$start);
|
|
|
|
$start->setTimezone($timezone);
|
2015-04-17 10:52:09 +00:00
|
|
|
$vtodo->DTSTART = $start;
|
2014-11-09 16:44:17 +00:00
|
|
|
} else {
|
|
|
|
unset($vtodo->DTSTART);
|
2014-04-19 18:28:22 +00:00
|
|
|
}
|
2014-07-31 16:18:28 +00:00
|
|
|
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
|
2014-04-19 18:28:22 +00:00
|
|
|
} 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(){
|
2014-04-19 18:28:22 +00:00
|
|
|
$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);
|
2015-04-17 10:52:09 +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());
|
|
|
|
}
|
|
|
|
elseif (in_array($type,$types)) {
|
|
|
|
try{
|
2014-04-19 18:28:22 +00:00
|
|
|
if($valarm == null) {
|
2015-04-17 10:52:09 +00:00
|
|
|
$valarm = $vcalendar->createComponent('VALARM');
|
2015-05-15 10:13:05 +00:00
|
|
|
$valarm->ACTION = $action;
|
|
|
|
$valarm->DESCRIPTION = 'Default Event Notification';
|
2014-04-27 15:34:35 +00:00
|
|
|
$vtodo->add($valarm);
|
2014-04-19 18:28:22 +00:00
|
|
|
} 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-04-19 18:28:22 +00:00
|
|
|
}
|
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
|
|
|
}
|
2015-04-17 10:52:09 +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-19 18:28:22 +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
|
|
|
*/
|
2015-05-24 10:09:05 +00:00
|
|
|
public function addCategory(){
|
2014-04-05 23:09:35 +00:00
|
|
|
$taskId = $this->params('taskID');
|
2015-05-24 10:09:05 +00:00
|
|
|
$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;
|
2015-05-24 10:09:05 +00:00
|
|
|
// 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());
|
2015-05-24 10:09:05 +00:00
|
|
|
}
|
|
|
|
} 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());
|
2015-05-24 10:09:05 +00:00
|
|
|
}
|
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;
|
2015-05-15 10:13:05 +00:00
|
|
|
$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);
|
2014-07-17 17:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$now = new \DateTime();
|
2015-05-17 17:19:46 +00:00
|
|
|
$vtodo->add('COMMENT',$comment,
|
2014-07-17 17:19:50 +00:00
|
|
|
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')
|
2014-07-17 17:19:50 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
|
2014-07-19 07:54:15 +00:00
|
|
|
$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,
|
2014-07-19 07:54:15 +00:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:19:50 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2014-07-19 07:54:15 +00:00
|
|
|
public function deleteComment(){
|
2014-07-17 17:19:50 +00:00
|
|
|
$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);
|
2014-07-19 09:55:39 +00:00
|
|
|
$comment = $vtodo->children[$commentIndex];
|
2015-05-17 17:19:46 +00:00
|
|
|
if($comment['X-OC-USERID']->getValue() == $this->userId){
|
2014-07-19 09:55:39 +00:00
|
|
|
unset($vtodo->children[$commentIndex]);
|
|
|
|
\OC_Calendar_Object::edit($taskId, $vcalendar->serialize());
|
|
|
|
}else{
|
|
|
|
throw new \Exception('Not allowed.');
|
|
|
|
}
|
2014-07-17 17:19:50 +00:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
// throw new BusinessLayerException($e->getMessage());
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
2014-04-05 23:09:35 +00:00
|
|
|
|
2014-07-17 17:19:50 +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 ) {
|
2014-07-17 17:19:50 +00:00
|
|
|
return $idx;
|
|
|
|
}
|
|
|
|
$idx += 1;
|
|
|
|
}
|
|
|
|
throw new \Exception('Commment not found.');
|
|
|
|
}
|
2015-04-17 10:52:09 +00:00
|
|
|
}
|