Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix dav objects import/export #2336

Merged
merged 6 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function parseDateTime($date, $timezone = null)
}
if ($date instanceof Carbon) {
// ok
} elseif ($date instanceof \DateTime) {
} elseif ($date instanceof \DateTimeInterface) {
$date = Carbon::instance($date);
} else {
try {
Expand Down
31 changes: 16 additions & 15 deletions app/Services/VCalendar/ExportTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use App\Models\Contact\Task;
use App\Services\BaseService;
use Sabre\VObject\Component\VTodo;
use Illuminate\Support\Facades\Auth;
use Sabre\VObject\Component\VCalendar;

Expand Down Expand Up @@ -53,12 +54,12 @@ private function export(Task $task) : VCalendar
}

// Basic information
$vcal = new VCalendar([
//'UID' => $task->uuid,
]);
$vcal = new VCalendar();
$vtodo = $vcal->create('VTODO');
$vcal->add($vtodo);

$this->exportTimezone($vcal);
$this->exportVTodo($task, $vcal);
$this->exportVTodo($task, $vtodo);

return $vcal;
}
Expand All @@ -75,30 +76,30 @@ private function exportTimezone(VCalendar $vcal)

/**
* @param Task $task
* @param VCalendar $vcard
* @param VTodo $vtodo
*/
private function exportVTodo(Task $task, VCalendar $vcal)
private function exportVTodo(Task $task, VTodo $vtodo)
{
$contact = $task->contact;

$vcal->add('VTODO', [
'UID' => $task->uuid,
'SUMMARY' => $task->title,
]);
$vtodo->UID = $task->uuid;
$vtodo->SUMMARY = $task->title;

if ($task->created_at) {
$vcal->VTODO->add('CREATED', $task->created_at);
$vtodo->DTSTAMP = $task->created_at;
$vtodo->CREATED = $task->created_at;
}
if (! empty($task->description)) {
$vcal->VTODO->add('DESCRIPTION', $task->description);
$vtodo->DESCRIPTION = $task->description;
}
if ($contact) {
$vcal->VTODO->add('ATTACH', route('people.show', $contact));
$vtodo->ATTACH = route('people.show', $contact);
}
if ($task->completed) {
$vcal->VTODO->add('STATUS', 'COMPLETED');
$vtodo->STATUS = 'COMPLETED';
}
if ($task->completed_at) {
$vcal->VTODO->add('COMPLETED', $task->completed_at);
$vtodo->COMPLETED = $task->completed_at;
}
}
}
46 changes: 25 additions & 21 deletions app/Services/VCalendar/ExportVCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Services\VCalendar;

use App\Helpers\DateHelper;
use Illuminate\Support\Str;
use App\Services\BaseService;
use Sabre\VObject\Component\VEvent;
Expand Down Expand Up @@ -55,12 +54,12 @@ private function export(SpecialDate $date) : VCalendar
}

// Basic information
$vcal = new VCalendar([
'UID' => $date->uuid,
]);
$vcal = new VCalendar();
$vevent = $vcal->create('VEVENT');
$vcal->add($vevent);

$this->exportTimezone($vcal);
$this->exportBirthday($date, $vcal);
$this->exportBirthday($date, $vevent);

return $vcal;
}
Expand All @@ -78,25 +77,30 @@ private function exportTimezone(VCalendar $vcal)

/**
* @param SpecialDate $date
* @param VCalendar $vcard
* @param VEvent $vevent
* @return void
*/
private function exportBirthday(SpecialDate $date, VCalendar $vcal)
private function exportBirthday(SpecialDate $date, VEvent $vevent)
{
$contact = $date->contact;
$name = $contact->name;
$vcal->add('VEVENT', [
'UID' => $date->uuid,
'SUMMARY' => trans('people.reminders_birthday', ['name' => $name]),
'DTSTART' => $date->date->format('Ymd'),
'DTEND' => $date->date->addDays(1)->format('Ymd'),
'RRULE' => 'FREQ=YEARLY',
'CREATED' => DateHelper::parseDateTime($date->created_at, Auth::user()->timezone),
'DTSTAMP' => DateHelper::parseDateTime($date->created_at),
'DESCRIPTION' => trans('mail.footer_contact_info2_link', [
'name' => $name,
'url' => route('people.show', $contact),
]),
]);

$vevent->UID = $date->uuid;
$vevent->DTSTART = $date->date->format('Ymd');
$vevent->DTEND = $date->date->addDays(1)->format('Ymd');
$vevent->RRULE = 'FREQ=YEARLY';

if ($date->created_at) {
$vevent->DTSTAMP = $date->created_at;
$vevent->CREATED = $date->created_at;
}
if ($contact) {
$name = $contact->name;
$vevent->SUMMARY = trans('people.reminders_birthday', ['name' => $name]);
$vevent->ATTACH = route('people.show', $contact);
$vevent->DESCRIPTION = trans('mail.footer_contact_info2_link', [
'name' => $name,
'url' => route('people.show', $contact),
]);
}
}
}
10 changes: 7 additions & 3 deletions app/Services/VCalendar/ImportTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@ private function importUid(Task $task, VCalendar $entry): void
*/
private function importTimestamp(Task $task, VCalendar $entry): void
{
if (empty($task->created_at) && $entry->VTODO->CREATED) {
$task->created_at = DateHelper::parseDateTime((string) $entry->VTODO->CREATED);
if (empty($task->created_at)) {
if ($entry->VTODO->DTSTAMP) {
$task->created_at = DateHelper::parseDateTime($entry->VTODO->DTSTAMP->getDateTime());
} elseif ($entry->VTODO->CREATED) {
$task->created_at = DateHelper::parseDateTime($entry->VTODO->CREATED->getDateTime());
}
}
}

Expand All @@ -175,7 +179,7 @@ private function importCompleted(Task $task, VCalendar $entry)
if (! $task->completed) {
$task->completed_at = null;
} elseif ($entry->VTODO->COMPLETED) {
$task->completed_at = DateHelper::parseDateTime((string) $entry->VTODO->COMPLETED);
$task->completed_at = DateHelper::parseDateTime($entry->VTODO->COMPLETED->getDateTime());
}
}
}
10 changes: 10 additions & 0 deletions app/Services/VCard/ExportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function export(Contact $contact) : VCard
$this->exportBirthday($contact, $vcard);
$this->exportAddress($contact, $vcard);
$this->exportContactFields($contact, $vcard);
$this->exportTimestamp($contact, $vcard);

return $vcard;
}
Expand Down Expand Up @@ -211,4 +212,13 @@ private function exportContactFields(Contact $contact, VCard $vcard)
}
}
}

/**
* @param Contact $contact
* @param VCard $vcard
*/
private function exportTimestamp(Contact $contact, VCard $vcard)
{
$vcard->REV = $contact->updated_at->format('Ymd\\THis\\Z');
}
}
22 changes: 12 additions & 10 deletions app/Services/VCard/ImportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,17 @@ private function importNames(Contact $contact, VCard $entry): void
private function name($entry): string
{
if ($this->hasFirstnameInN($entry)) {
$count = count($entry->N->getParts());
$parts = $entry->N->getParts();
$count = count($parts);
$name = '';
if ($count >= 2) {
$name .= $this->formatValue($entry->N->getParts()[1]);
$name .= $this->formatValue($parts[1]);
}
if ($count >= 3 && ! empty($entry->N->getParts()[2])) {
$name .= ' '.$this->formatValue($entry->N->getParts()[2]);
if ($count >= 3 && ! empty($parts[2])) {
$name .= ' '.$this->formatValue($parts[2]);
}
if ($count >= 1 && ! empty($entry->N->getParts()[0])) {
$name .= ' '.$this->formatValue($entry->N->getParts()[0]);
if ($count >= 1 && ! empty($parts[0])) {
$name .= ' '.$this->formatValue($parts[0]);
}
$name .= ' '.$this->formatValue($entry->EMAIL);
} elseif ($this->hasNICKNAME($entry)) {
Expand All @@ -444,16 +445,17 @@ private function name($entry): string
*/
private function importFromN(Contact $contact, VCard $entry): void
{
$count = count($entry->N->getParts());
$parts = $entry->N->getParts();
$count = count($parts);

if ($count >= 1) {
$contact->last_name = $this->formatValue($entry->N->getParts()[0]);
$contact->last_name = $this->formatValue($parts[0]);
}
if ($count >= 2) {
$contact->first_name = $this->formatValue($entry->N->getParts()[1]);
$contact->first_name = $this->formatValue($parts[1]);
}
if ($count >= 3) {
$contact->middle_name = $this->formatValue($entry->N->getParts()[2]);
$contact->middle_name = $this->formatValue($parts[2]);
}
// prefix [3]
// suffix [4]
Expand Down
32 changes: 28 additions & 4 deletions tests/Api/DAV/CardEtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function getCard(Contact $contact, bool $realFormat = false): string
{
$url = route('people.show', $contact);
$sabreversion = \Sabre\VObject\Version::VERSION;
$timestamp = $contact->updated_at->format('Ymd\THis\Z');

$data = "BEGIN:VCARD
VERSION:4.0
Expand All @@ -35,6 +36,29 @@ protected function getCard(Contact $contact, bool $realFormat = false): string
FN:{$contact->name}
N:{$contact->last_name};{$contact->first_name};{$contact->middle_name};;
GENDER:O;
";
foreach ($contact->addresses as $address) {
$data .= 'ADR:;;';
$data .= $address->place->street.';';
$data .= $address->place->city.';';
$data .= $address->place->province.';';
$data .= $address->place->postal_code.';';
$data .= $address->place->country;
$data .= "\n";
}
foreach ($contact->contactFields as $contactField) {
switch ($contactField->contactFieldType->type) {
case 'phone':
$data .= "TEL:{$contactField->data}\n";
break;
case 'email':
$data .= "EMAIL:{$contactField->data}\n";
break;
default:
break;
}
}
$data .= "REV:{$timestamp}
END:VCARD
";

Expand Down Expand Up @@ -63,18 +87,18 @@ protected function getCal(SpecialDate $specialDate, bool $realFormat = false): s
VERSION:2.0
PRODID:-//Sabre//Sabre VObject {$sabreversion}//EN
CALSCALE:GREGORIAN
UID:{$specialDate->uuid}
BEGIN:VTIMEZONE
TZID:UTC
END:VTIMEZONE
BEGIN:VEVENT
UID:{$specialDate->uuid}
DTSTAMP:{$timestamp}
SUMMARY:Birthday of {$contact->name}
DTSTART:{$start}
DTEND:{$end}
RRULE:FREQ=YEARLY
DTSTAMP:{$timestamp}
CREATED:{$timestamp}
SUMMARY:Birthday of {$contact->name}
ATTACH:{$url}
DESCRIPTION:{$description1}
{$description2}
END:VEVENT
Expand Down Expand Up @@ -103,8 +127,8 @@ protected function getVTodo(Task $task, bool $realFormat = false): string
END:VTIMEZONE
BEGIN:VTODO
UID:{$task->uuid}
DTSTAMP:{$timestamp}
SUMMARY:{$task->title}
DTSTAMP:{$timestamp}
CREATED:{$timestamp}
DESCRIPTION:{$task->description}
";
Expand Down
Loading