feat: Converted vcalendar todo events to taskwarrior tasks

This commit is contained in:
Aerex
2020-05-12 23:56:22 -05:00
parent 45ea34da94
commit 40b66fb38b
13 changed files with 198 additions and 114 deletions

View File

@@ -8,25 +8,22 @@ use Carbon\Carbon;
class Taskwarrior implements IStorage {
private const DATA_FILES = ['pending.data', 'completed.data', 'undo.data'];
private $rawConfigs;
public const NAME = 'taskwarrior';
private $tasks = [];
public function __construct($console, $config) {
private $configDir;
private $configs;
public function __construct($console, $configDir, $configs) {
$this->console = $console;
$this->config = $config;
$this->configDir = $configDir;
$this->configs = $configs['taskwarrior'];
}
public function getConfig() {
return $this->config;
}
public function setRawConfigs($rawConfigs) {
$this->rawConfigs = $rawConfigs;
}
public function refresh() {
$dataDir = $this->rawConfigs['data_dir'];
$fp = fopen(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir), 'a');
$fp = fopen(sprintf('%s/taskwarrior-baikal-storage.lock', $this->configDir), 'a');
if (!$fp || !flock($fp, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
fputs(STDERR, 'Could not get lock');
@@ -35,55 +32,125 @@ class Taskwarrior implements IStorage {
$mtime = 0;
$tasksUpdated = false;
foreach (Taskwarrior::DATA_FILES as $dataFile) {
$fmtime = filemtime(sprintf('%s/%s', $this->config['data_dir'], $dataFile));
if ($fmtime > $mtime) {
$mtime = $fmtime;
$tasksUpdated = true;
}
$fmtime = filemtime(sprintf('%s/%s', $this->configs['taskdata'], $dataFile));
if ($fmtime > $mtime) {
$mtime = $fmtime;
$tasksUpdated = true;
}
}
if ($tasksUpdated) {
$tasks = $this->console->execute('task', ['export']);
$tasks = json_decode($this->console->execute('task', ['export'], null,
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
foreach ($tasks as $task) {
$this->tasks[$task['uuid']] = $task;
}
}
fclose($fp);
unlink(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir));
unlink(sprintf('%s/taskwarrior-baikal-storage.lock', $this->configDir));
}
public function vObjectToTask($vtodo) {
if ($this->tasks['uid'] == $vtodo->UID) {
if (isset($this->tasks['uid']) && $this->tasks['uid'] == $vtodo->UID) {
$task = $this->tasks['uid'];
} else {
$task = [];
$task['uid'] = $vtodo->UID;
$task['uid'] = (string)$vtodo->UID;
}
if (!isset($vtodo->DESCRIPTION) && isset($vtodo->SUMMARY)){
$task['description'] = $vtodo->SUMMARY;
$task['description'] = (string)$vtodo->SUMMARY;
} else {
$task['description'] = $vtodo->DESCRIPTION;
$task['description'] = (string)$vtodo->DESCRIPTION;
}
if (isset($vtodo->DTSTAMP)){
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
}
if (isset($vtodo->DTSTART)) {
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C));
}
if (isset($vtodo->DTEND)){
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C));
}
if (isset($vtodo->{'LAST-MODIFIED'})) {
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C));
}
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';
}
}
if (isset($vtodo->DUE)){
$task['due'] = new Carbon($vtodo->DUE->getDateTime()->format(\DateTime::W3C));
}
return $task;
}
public function save(Calendar $c) {
if (!isset($c->VTODO)){
throw new \Exception('Calendar event does not contain VTODO');
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));
}
break;
case 'CANCELED':
$task['status'] = 'deleted';
if (!isset($task['end'])) {
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
}
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($category, $projTagSuffixRegExp)) {
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
continue;
}
}
$task['tags'] = $category;
}
}
return $task;
}
public function save(Calendar $c) {
if (!isset($c->VTODO)){
throw new \Exception('Calendar event does not contain VTODO');
}
$this->refresh();
$task = $this->vObjectToTask($c->VTODO);
$this->console->execute('task', ['import'], $task,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
}
}
$this->refresh();
$task = $this->vObjectToTask($c->VTODO);
$this->console->execute('task', ['import'], $task);
}
}