Skip to content

Commit

Permalink
Ticket 33533: [Tasks] Fix .ics import
Browse files Browse the repository at this point in the history
  • Loading branch information
derjoachim committed Dec 16, 2024
1 parent 89a7126 commit bf0561a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Tasks: fix several bugs in ICS import

12-12-2024: 6.8.90
- Core: Show database error on upgrade
- Addressbook: Check if comments module is installed
Expand Down
11 changes: 6 additions & 5 deletions www/go/core/fs/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use go\core\ErrorHandler;
use go\core\http\Response;
use go\core\util\StringUtil;
use Throwable;


/**
Expand Down Expand Up @@ -58,10 +59,9 @@ public function getFolder(): ?Folder
public function isWritable(): bool
{
try {
if($this->exists()) {
if ($this->exists()) {
return is_writable($this->path);
}else
{
} else {
return $this->getFolder()->isWritable();
}
} catch(Throwable $e) {
Expand Down Expand Up @@ -471,11 +471,12 @@ private function rangeDownload() {

/**
* Open file pointer
*
*
* See php fopen function
*
*
* @param string $mode
* @return resource
* @throws Exception
*/
public function open(string $mode){

Expand Down
1 change: 0 additions & 1 deletion www/go/core/util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public static function camelCaseToUnderscore(string $camelCasedString): string
*
* @param ?string $str
* @param string|null $sourceCharset
* @param string
* @return string
*/
public static function cleanUtf8(?string $str, string $sourceCharset = null): string
Expand Down
57 changes: 46 additions & 11 deletions www/go/modules/community/tasks/convert/VCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use go\modules\community\tasks\model\Category;
use go\modules\community\tasks\model\Task;
use Sabre\VObject\Component\VCalendar as VCalendarComponent;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\ParseException;
use Sabre\VObject\Reader;
use Sabre\VObject\Splitter\ICalendar as VCalendarSplitter;

Expand All @@ -33,14 +35,15 @@ public function __construct()
const EMPTY_NAME = '(no name)';



/**
* Parse an Event object to a VObject
*
*
* @param task $task
* @throws \DateMalformedStringException
*/
public function export(Task $task) {
public function export(Task $task): \Sabre\VObject\Document|VCalendarComponent
{
if ($task->vcalendarBlobId) {
//Contact has a stored VCard
$blob = Blob::findById($task->vcalendarBlobId);
Expand Down Expand Up @@ -187,18 +190,29 @@ public function getFileExtension(): string

private $importSplitter;
private $currentRecord;

/**
* @throws ParseException
* @throws Exception
*/
protected function initImport(File $file): void
{
$contents = $file->getContents();
$this->importSplitter = new VCalendarSplitter(StringUtil::cleanUtf8($contents), Reader::OPTION_FORGIVING + Reader::OPTION_IGNORE_INVALID_LINES);
// $contents = $file->getContents();
// $this->importSplitter = new VCalendarSplitter(StringUtil::cleanUtf8($contents), Reader::OPTION_FORGIVING + Reader::OPTION_IGNORE_INVALID_LINES);
$this->importSplitter = new VCalendarSplitter($file->open('r'), Reader::OPTION_FORGIVING + Reader::OPTION_IGNORE_INVALID_LINES);

}
protected function nextImportRecord(): bool
{
$this->currentRecord = $this->importSplitter->getNext();
return $this->currentRecord;
return isset($this->currentRecord);
}
protected function importEntity() {

/**
* @throws InvalidDataException
*/
protected function importEntity(): ?Task
{
$vcal = $this->currentRecord;
$tasklistId = $this->clientParams['values']['tasklistId'];

Expand Down Expand Up @@ -279,15 +293,17 @@ private function importPriority($todo) {
* @return Task
* @throws \Sabre\VObject\InvalidDataException
*/
public function vtodoToTask(VCalendarComponent $vcal, $tasklistId, $task = null) {
public function vtodoToTask(VCalendarComponent $vcal, $tasklistId, $task = null)
{
$todo = $vcal->VTODO;
$categoryIds = Category::find()->selectSingleValue('id')
->where('name', 'IN', explode(",",(string)$todo->CATEGORIES))
->where('name', 'IN', explode(",", (string)$todo->CATEGORIES))
->all();
if(!empty($todo->RRULE) && !empty($todo->DTSTART))
if (!empty($todo->RRULE) && !empty($todo->DTSTART)) {
$rule = Recurrence::fromString((string)$todo->RRULE, $todo->DTSTART->getDateTime())->toArray();
else
$rule = null;
} else {
$rule = null;
}
if($task === null) {
$task = new Task();
}
Expand Down Expand Up @@ -355,6 +371,25 @@ public function vtodoToTask(VCalendarComponent $vcal, $tasklistId, $task = null)
return $task;
}

/**
*
* @param VCalendarComponent $VCalendarComponent
* @param int $taskListId
* @return Task | false
* @throws Exception
*/
private function findTask(VCalendarComponent $VCalendarComponent, int $taskListId): null|Task
{
$task = null;

if (isset($VCalendarComponent->VTODO->uid)) {
$task = Task::find()->where(['tasklistId' => $taskListId, 'uid' => (string)$VCalendarComponent->VTODO->UID])->single();
}

return $task;
}


public static function supportedExtensions(): array
{
return ['ics'];
Expand Down

0 comments on commit bf0561a

Please sign in to comment.