TaskwarriorPlugin/src/iCalEventProcessor.php

160 lines
3.9 KiB
PHP

<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Config;
use Sabre\DAV\Exception;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Component\VTodo;
class iCalEventProcessor {
/**
* @var Config
*/
private $config;
/**
* @var string
*
*/
private $taskrc;
/**
* @var string
*/
private $taskDataDir;
/**
* @var array
*/
private $cachedTasks = [];
/**
* @var string
*/
private $taskBinFile;
/**
* @var Taskwarrior
*/
private $taskwarrior;
public function __construct(Config $taskConfig = null){
if(!is_null($taskConfig)){
$this->taskConfig = $taskConfig;
} else {
$this->taskConfig = new Config();
}
$this->taskwarrior = $this->taskConfig->getTaskwarriorInstance();
$this->logger = $this->taskConfig->getLogger();
}
public function importTask(VTodo $ToDoComponent = null){
echo $ToDoComponent->serialize();
if(!isset($ToDoComponent)){
$this->logger->error("vCal ToDo component is not defined");
throw new Exception("vCal Todo component is not defined");
}
if($this->taskwarrior->exists($ToDoComponent->UID)){
$this->taskwarrior->updateTask($ToDoComponent);
$this->logger->error("Updating task " . (string)$ToDoComponent->UID);
}
try {
$task = $this->taskwarrior->createiCalTask($ToDoComponent->UID);
// get description
if(!isset($ToDoComponent->DESCRIPTION) && isset($ToDoComponent->SUMMARY)){
$description = $summary;
} else if(!isset($ToDoComponent->DESCRIPTION) && !isset($ToDoComponent->SUMMARY)){
throw new Exception("Task must have a description or summary");
}
$task->setDescription($ToDoComponent->DESCRIPTION);
// get start time
if(!isset($ToDoComponent->DTSTAMP) && isset($ToDoComponent->DSTART)){
$start = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTSTAMP);
$entry = $start;
} else if (!isset($ToDoComponent->DTSTAMP) && !isset($ToDoComponent->DSTART)){
throw new Exception("Task must have an entry or start time");
} else {
$entry = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTSTAMP);
}
$this->taskwarrior->setValue($task, $this->taskwarrior::ENTRY, $entry);
if(isset($ToDoComponent->DSTART)){
$start = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DSTART);
$this->taskwarrior->setValue($task, $this->taskwarrior::START, $start);
}
if(isset($ToDoComponent->{'LAST-MODIFIED'})){
$modified = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->{'LAST-MODIFIED'});
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
}
if(isset($ToDoComponent->DTEND)){
$end = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTEND);
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
}
if(isset($ToDoComponent->DUE)){
$due = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DUE);
$task->setDue($due);
}
if(isset($ToDoComponent->DTEND)){
$end = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTEND);
$this->taskwarrior->setValue($task, $this->taskwarrior::END, $end);
}
if(isset($ToDoComponent->CATEGORIES)){
$tags = $this->taskwarrior->convertToStringArray($ToDoComponent->CATEGORIES);
$task->setTags($tags);
}
$this->taskwarrior->save($task);
} catch(Exception $e){
echo $e->getMessage();
$this->logger->error($e->message);
throw $e;
}
}
}
?>