baikal-storage-plugin/lib/Storages/Taskwarrior.php

155 lines
4.8 KiB
PHP
Raw Normal View History

2020-05-03 16:41:59 -05:00
<?php
namespace Aerex\BaikalStorage\Storages;
use Sabre\VObject\Component\VCalendar as Calendar;
use Aerex\BaikalStorage\Logger;
2020-05-03 16:41:59 -05:00
use Carbon\Carbon;
use Carbon\CarbonTimeZone;
2020-05-03 16:41:59 -05:00
class Taskwarrior implements IStorage {
public const NAME = 'taskwarrior';
private $tasks = [];
private $configs;
private $logger;
private $tz;
public function __construct($console, $configs) {
2020-05-03 16:41:59 -05:00
$this->console = $console;
$this->configs = $configs['storages']['taskwarrior'];
$this->logger = new Logger($configs, 'Taskwarrior');
$this->tz = new CarbonTimeZone($configs['general']['timezone']);
2020-05-03 16:41:59 -05:00
}
public function getConfig() {
return $this->config;
}
public function refresh() {
$output = $this->console->execute('task', ['sync'], null,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->tasks = json_decode($this->console->execute('task', ['export'], null,
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
foreach ($this->tasks as $task) {
if (isset($task['uid'])) {
$this->tasks[$task['uid']] = $task;
}
}
$this->logger->info($output);
2020-05-03 16:41:59 -05:00
}
public function vObjectToTask($vtodo) {
if (isset($this->tasks[(string)$vtodo->UID])) {
$task = $this->tasks[(string)$vtodo->UID];
2020-05-03 16:41:59 -05:00
} else {
$task = [];
$task['uid'] = (string)$vtodo->UID;
2020-05-03 16:41:59 -05:00
}
if (isset($vtodo->SUMMARY)){
$task['description'] = (string)$vtodo->SUMMARY;
} else if(isset($vtodo->DESCRIPTION)) {
$task['description'] = (string)$vtodo->DESCRIPTION;
2020-05-03 16:41:59 -05:00
}
if (isset($vtodo->DTSTAMP)){
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
2020-05-03 16:41:59 -05:00
}
if (isset($vtodo->DTSTART)) {
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C), $this->tz);
}
if (isset($vtodo->DTEND)){
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C), $this->tz);
}
if (isset($vtodo->{'LAST-MODIFIED'})) {
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C), $this->tz);
}
if (isset($vtodo->PRIORITY)) {
$priority = $vtodo->PRIORITY->getJsonValue();
if ($priority < 5) {
$task['priority'] = 'H';
} else if ($priority === 5) {
$task['priority'] = 'M';
} else if ($priority > 5 && $priority < 10) {
$task['priority'] = 'L';
}
}
2020-05-03 16:41:59 -05:00
if (isset($vtodo->DUE)){
$task['due'] = new Carbon($vtodo->DUE->getDateTime());
2020-05-03 16:41:59 -05:00
}
if (isset($vtodo->RRULE)) {
$rules = $vtodo->RRULE->getParts();
if (isset($rules['FREQ'])) {
$task['recu'] = $rules['FREQ'];
}
if (isset($rules['UNTIL'])) {
$task['until'] = $rules['UNTIL'];
}
}
if (isset($vtodo->STATUS)) {
switch((string)$vtodo->STATUS) {
case 'NEEDS-ACTION':
$task['status'] = 'pending';
break;
case 'COMPLETED':
$task['status'] = 'completed';
if (!isset($task['end'])) {
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
}
break;
case 'CANCELED':
$task['status'] = 'deleted';
if (!isset($task['end'])) {
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
}
break;
}
}
if (isset($vtodo->CATEGORIES)) {
$task['tags'] = [];
foreach ($vtodo->CATEGORIES as $category) {
if (isset($this->configs['project_tag_suffix'])) {
$projTagSuffixRegExp = sprintf('/^%s/', $this->configs['project_tag_suffix']);
if (preg_match($projTagSuffixRegExp, $category)) {
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
continue;
}
}
$task['tags'] = $category;
}
}
return $task;
}
2020-05-03 16:41:59 -05:00
public function save(Calendar $c) {
try {
if (!isset($c->VTODO)){
throw new \Exception('Calendar event does not contain VTODO');
}
$this->logger->info(json_encode($c->jsonSerialize()));
$this->refresh();
$task = $this->vObjectToTask($c->VTODO);
$this->logger->info(json_encode($task));
$this->logger->info(
sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], json_encode($task))
);
$output = $this->console->execute('task', ['import'], $task,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->logger->info($output);
} catch (\Exception $e) {
$this->logger->error($e->getTraceAsString());
throw $e;
2020-05-03 16:41:59 -05:00
}
}
}