diff --git a/lib/Console.php b/lib/Console.php index 7e964ff..a68a65c 100644 --- a/lib/Console.php +++ b/lib/Console.php @@ -16,6 +16,7 @@ class Console extends AbstractConsole { if (is_array($input)) { return json_encode($input); } + return $input; } public function execute($cmd, $args, $input = null, $envs = []) { diff --git a/lib/Logger.php b/lib/Logger.php index 3043d3b..01acf1b 100644 --- a/lib/Logger.php +++ b/lib/Logger.php @@ -45,6 +45,11 @@ class Logger { $this->logger->notice($message); } } + public function warn($message) { + if ($this->configs['enabled']) { + $this->logger->warning($message); + } + } public function error($message) { if ($this->configs['enabled']) { @@ -69,10 +74,4 @@ class Logger { } } - public function setLevel($level) { - if ($this->configs['enabled']) { - $this->logger->setLevel($level); - } - } - } diff --git a/lib/Plugin.php b/lib/Plugin.php index b5cca28..71c5352 100644 --- a/lib/Plugin.php +++ b/lib/Plugin.php @@ -61,7 +61,7 @@ class Plugin extends ServerPlugin { */ public function initializeStorages($configs) { - $taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs, new Logger($configs, 'Taskwarrior')); + $taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off', 'rc.confirmation=no']), $configs, new Logger($configs, 'Taskwarrior')); $this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior); } @@ -108,6 +108,9 @@ class Plugin extends ServerPlugin { break; case 'POST': $this->httpPost($request, $response); + break; + case 'DELETE': + $this->httpDelete($request); return; } } @@ -141,7 +144,6 @@ class Plugin extends ServerPlugin { * * @param RequestInterface $request * - * @return bool */ function httpPost(RequestInterface $request, ResponseInterface $response) { $postVars = $request->getPostData(); @@ -169,8 +171,34 @@ class Plugin extends ServerPlugin { $response->setStatus(302); $request->setBody($body); - } + /** + * This method handles the DELETE method. + * + * @param RequestInterface $request + * @param ResponseInterface $response + * + */ + + public function httpDelete(RequestInterface $request) { + try { + $body = $request->getBodyAsString(); + $path = $request->getPath(); + $paths = explode('/', $path); + if (sizeof($paths) > 1) { + $uid = str_replace('.ics', '', $paths[sizeof($paths)-1]); + $this->storageManager->remove($uid); + } + } catch(BadRequest $e){ + throw new BadRequest($e->getMessage(), null, $e); + } catch(\Exception $e){ + throw new \Exception($e->getMessage(), null, $e); + } + $request->setBody($body); + + } + + /** * Generates the 'general' configuration section diff --git a/lib/StorageManager.php b/lib/StorageManager.php index ecb132f..cfc3116 100644 --- a/lib/StorageManager.php +++ b/lib/StorageManager.php @@ -46,4 +46,17 @@ class StorageManager { $storage->save($calendar); } } + + public function remove($uid) { + if (!isset($this->configs)) { + throw new \Exception('StorageManger was not initialize or configs are not defined'); + } + foreach ($this->configs['storages'] as $key => $value) { + $storage = $this->storages[$key]; + if (!isset($storage)){ + throw new \Exception(); + } + $storage->remove($uid); + } + } } diff --git a/lib/Storages/IStorage.php b/lib/Storages/IStorage.php index 7b4233f..579397e 100644 --- a/lib/Storages/IStorage.php +++ b/lib/Storages/IStorage.php @@ -6,6 +6,7 @@ use Sabre\VObject\Component\VCalendar as Calendar; interface IStorage { public function save(Calendar $c); + public function remove($uid); public function refresh(); public function getConfigBrowser(); public function updateConfigs($postData); diff --git a/lib/Storages/Taskwarrior.php b/lib/Storages/Taskwarrior.php index cb57b78..303772b 100644 --- a/lib/Storages/Taskwarrior.php +++ b/lib/Storages/Taskwarrior.php @@ -98,13 +98,13 @@ class Taskwarrior implements IStorage { break; } } - array_push(['description' => $descriptionLine, 'entry' => $annotationEntry]); + array_push($annotations, ['description' => $descriptionLine, 'entry' => $annotationEntry]); + $task['annotations'] = $annotations; } } - if (!isset($task['entry'])){ $task['entry'] = $vtodo->DTSTAMP->getDateTime()->format(\DateTime::ISO8601); - } + } if (isset($vtodo->DTSTART)) { $task['start'] = $vtodo->DTSTART->getDateTime()->format(\DateTime::ISO8601); @@ -177,11 +177,6 @@ class Taskwarrior implements IStorage { } } - if (isset($vtodo->{'RELATED-TO'}) && isset($this->tasks[(string)$vtodo->{'RELATED-TO'}])) { - $relatedTask = $this->tasks[(string)$vtodo->{'RELATED-TO'}]; - $task['depends'] = $relatedTask['uuid']; - } - if (isset($vtodo->GEO)){ $task['geo'] = $vtodo->GEO->getRawMimeDirValue(); } @@ -209,4 +204,31 @@ class Taskwarrior implements IStorage { throw $e; } } + + public function remove($uid) { + try { + $this->logger->info(sprintf('Deleting iCal %s from taskwarrior', $uid)); + $this->refresh(); + $task = $this->tasks[(string)$uid]; + if (isset($task) && $task['status'] !== 'deleted') { + $uuid = $task['uuid']; + $this->logger->info( + sprintf('Executing TASKRC = %s TASKDATA = %s task delete %s', $this->configs['taskrc'], $this->configs['taskdata'], $uuid) + ); + $output = $this->console->execute('task', ['delete', (string)$uuid], null, + ['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]); + $this->logger->info($output); + } else if (isset($task) && $task['status'] === 'deleted') { + $this->logger->warn(sprintf('Task %s has already been deleted', $task['uuid'])); + } else { + $this->logger->error(sprintf('Could not find task for iCal %s to be deleted', $uid)); + } + + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + $this->logger->error($e->getTraceAsString()); + throw $e; + } + + } }