Skip to content

Commit

Permalink
Fix setting of ORG property in VCards (Fixes #317)
Browse files Browse the repository at this point in the history
The ORG property of a VCard is structured to reflect an organization and
a hierarchical structure of departments.

Example:
ORG:Company;Division;Department;Group

In roundcube, we only have organization and department as two single
value entries.

Now the above is converted such:
organization: Company
department: Division; Department; Group

The inverse way works as well, where different department levels are
separated by semicolon and optional surrounding whitespace in the
roundcube value.

Furthermore, we remove the handling of department given as an array in
save_data, because roundcube always passes a simple string value only,
and so now das RCMCardDAV.
  • Loading branch information
mstilkerich committed Nov 15, 2020
1 parent bff00ce commit 0f88700
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/DataConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,10 @@ public function toRoundcube(VCard $vcard, AddressbookCollection $davAbook): arra
}

$property = $vcard->ORG;
if ($property) {
if (isset($property)) {
$ORG = $property->getParts();
$save_data['organization'] = $ORG[0];
for ($i = 1; $i < count($ORG); $i++) {
$save_data['department'][] = $ORG[$i];
}
$save_data['organization'] = array_shift($ORG) ?? "";
$save_data['department'] = implode("; ", $ORG);
}

foreach (self::VCF2RC['multi'] as $key => $value) {
Expand Down Expand Up @@ -294,28 +292,25 @@ public function fromRoundcube(array $save_data, ?VCard $vcard = null, bool $isGr
];
}

$new_org_value = [];
if (
key_exists("organization", $save_data)
&& strlen($save_data['organization']) > 0
) {
$new_org_value[] = $save_data['organization'];
$orgParts = [];
if (!empty($save_data['organization'])) {
$orgParts[] = $save_data['organization'];
}

if (key_exists("department", $save_data)) {
if (is_array($save_data['department'])) {
foreach ($save_data['department'] as $value) {
$new_org_value[] = $value;
}
} elseif (strlen($save_data['department']) > 0) {
$new_org_value[] = $save_data['department'];
if (!empty($save_data['department'])) {
// the first element of ORG corresponds to organization, if that field is not filled but organization is
// we need to store an empty value explicitly (otherwise, department would become organization when reading
// back the VCard).
if (empty($orgParts)) {
$orgParts[] = "";
}
$orgParts = array_merge($orgParts, preg_split('/\s*;\s*/', $save_data['department']));
}

if (count($new_org_value) > 0) {
$vcard->ORG = $new_org_value;
} else {
if (empty($orgParts)) {
unset($vcard->ORG);
} else {
$vcard->ORG = $orgParts;
}

// normalize date fields to RFC2425 YYYY-MM-DD date values
Expand Down

0 comments on commit 0f88700

Please sign in to comment.