2020-05-03 16:41:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aerex\BaikalStorage\Storages;
|
|
|
|
|
|
|
|
use Sabre\VObject\Component\VCalendar as Calendar;
|
2020-05-28 11:54:21 -05:00
|
|
|
use Aerex\BaikalStorage\Logger;
|
2020-05-03 16:41:59 -05:00
|
|
|
use Carbon\Carbon;
|
2020-06-03 00:46:06 -05:00
|
|
|
use Carbon\CarbonTimeZone;
|
2020-05-03 16:41:59 -05:00
|
|
|
|
|
|
|
class Taskwarrior implements IStorage {
|
|
|
|
|
|
|
|
public const NAME = 'taskwarrior';
|
|
|
|
private $tasks = [];
|
2020-05-12 23:56:22 -05:00
|
|
|
private $configs;
|
2020-05-28 11:54:21 -05:00
|
|
|
private $logger;
|
2020-06-03 00:46:06 -05:00
|
|
|
private $tz;
|
2020-05-28 11:54:21 -05:00
|
|
|
|
|
|
|
public function __construct($console, $configs) {
|
2020-05-03 16:41:59 -05:00
|
|
|
$this->console = $console;
|
2020-06-11 11:10:45 -05:00
|
|
|
$this->configs = $configs['storages']['taskwarrior'];
|
2020-05-28 11:54:21 -05:00
|
|
|
$this->logger = new Logger($configs, 'Taskwarrior');
|
2020-06-03 00:46:06 -05:00
|
|
|
$this->tz = new CarbonTimeZone($configs['general']['timezone']);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfig() {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refresh() {
|
2020-05-28 11:54:21 -05:00
|
|
|
$output = $this->console->execute('task', ['sync'], null,
|
|
|
|
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
2020-06-03 00:04:43 -05:00
|
|
|
$this->tasks = json_decode($this->console->execute('task', ['export'], null,
|
2020-06-02 21:16:18 -05:00
|
|
|
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
|
2020-06-03 00:04:43 -05:00
|
|
|
foreach ($this->tasks as $task) {
|
2020-06-02 21:16:18 -05:00
|
|
|
if (isset($task['uid'])) {
|
|
|
|
$this->tasks[$task['uid']] = $task;
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 11:54:21 -05:00
|
|
|
$this->logger->info($output);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function vObjectToTask($vtodo) {
|
2020-05-28 11:54:21 -05:00
|
|
|
if (isset($this->tasks[(string)$vtodo->UID])) {
|
|
|
|
$task = $this->tasks[(string)$vtodo->UID];
|
2020-05-03 16:41:59 -05:00
|
|
|
} else {
|
|
|
|
$task = [];
|
2020-05-12 23:56:22 -05:00
|
|
|
$task['uid'] = (string)$vtodo->UID;
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
2020-06-02 21:16:18 -05:00
|
|
|
if (isset($vtodo->SUMMARY)){
|
2020-05-12 23:56:22 -05:00
|
|
|
$task['description'] = (string)$vtodo->SUMMARY;
|
2020-05-28 11:54:21 -05:00
|
|
|
} else if(isset($vtodo->DESCRIPTION)) {
|
2020-05-12 23:56:22 -05:00
|
|
|
$task['description'] = (string)$vtodo->DESCRIPTION;
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vtodo->DTSTAMP)){
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
2020-05-12 23:56:22 -05:00
|
|
|
if (isset($vtodo->DTSTART)) {
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vtodo->DTEND)){
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vtodo->{'LAST-MODIFIED'})) {
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)){
|
2020-06-11 11:10:45 -05:00
|
|
|
$task['due'] = new Carbon($vtodo->DUE->getDateTime());
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
2020-05-12 23:56:22 -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) {
|
2020-05-28 11:54:21 -05:00
|
|
|
case 'NEEDS-ACTION':
|
|
|
|
$task['status'] = 'pending';
|
|
|
|
break;
|
|
|
|
case 'COMPLETED':
|
|
|
|
$task['status'] = 'completed';
|
|
|
|
if (!isset($task['end'])) {
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-28 11:54:21 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'CANCELED':
|
|
|
|
$task['status'] = 'deleted';
|
|
|
|
if (!isset($task['end'])) {
|
2020-06-03 00:46:06 -05:00
|
|
|
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
2020-05-28 11:54:21 -05:00
|
|
|
}
|
|
|
|
break;
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vtodo->CATEGORIES)) {
|
|
|
|
$task['tags'] = [];
|
2020-05-28 11:54:21 -05:00
|
|
|
foreach ($vtodo->CATEGORIES as $category) {
|
|
|
|
if (isset($this->configs['project_tag_suffix'])) {
|
2020-06-02 21:16:18 -05:00
|
|
|
$projTagSuffixRegExp = sprintf('/^%s/', $this->configs['project_tag_suffix']);
|
|
|
|
if (preg_match($projTagSuffixRegExp, $category)) {
|
2020-05-28 11:54:21 -05:00
|
|
|
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
2020-05-28 11:54:21 -05:00
|
|
|
$task['tags'] = $category;
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:54:21 -05:00
|
|
|
return $task;
|
|
|
|
}
|
2020-05-03 16:41:59 -05:00
|
|
|
|
2020-05-28 11:54:21 -05:00
|
|
|
public function save(Calendar $c) {
|
2020-06-02 21:16:18 -05:00
|
|
|
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
|
|
|
}
|
2020-05-28 11:54:21 -05:00
|
|
|
}
|
|
|
|
}
|