Skip to content

Commit

Permalink
tweak(TB MC) improve json record set converter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmhh committed Aug 23, 2024
1 parent de56354 commit 28bed7d
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tine20/Tinebase/Model/Converter/JsonRecordSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function convertToRecord($record, $key, $blob)
$blob = json_decode($blob, true);
}
if (is_array($blob)) {
if ($this->refId) {
if ($this->refId && (empty($blob) || !($blob[key($blob)]['id'] ?? false))) {
return $blob;
}
$rs = new Tinebase_Record_RecordSet($record::getConfiguration()
Expand All @@ -61,6 +61,25 @@ public function convertToRecord($record, $key, $blob)
*/
public function convertToData($record, $key, $fieldValue)
{
if ($this->refId) {
if ($fieldValue instanceof Tinebase_Record_RecordSet) {
return json_encode($fieldValue->getArrayOfIds());
} elseif (is_array($fieldValue)) {
if ($fieldValue[key($fieldValue)]['id'] ?? false) {
$ids = [];
foreach ($fieldValue as $record) {
if ($record['id'] ?? false) {
$ids[] = $record['id'];
}
}
$fieldValue = $ids;
}
return json_encode($fieldValue);
} else {
return null;
}
}

if (! $fieldValue instanceof Tinebase_Record_RecordSet) {
if (empty($fieldValue)) {
return null;
Expand All @@ -70,9 +89,7 @@ public function convertToData($record, $key, $fieldValue)
return $fieldValue;
}
}
if ($this->refId) {
return json_encode($fieldValue->getArrayOfIds());
}

$fieldValue->runConvertToData();
return json_encode($fieldValue->toArray());
}
Expand Down

0 comments on commit 28bed7d

Please sign in to comment.