Skip to content

Commit

Permalink
Fixes structure propagation support
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Oct 29, 2024
1 parent 86f891b commit 0b21726
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Database/Traits/SimpleTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public function initializeSimpleTree()
'key' => $this->getParentColumnName(),
'replicate' => false
];

// Multisite
if (
$this->isClassInstanceOf(\October\Contracts\Database\MultisiteInterface::class) &&
$this->isMultisiteSyncEnabled() &&
$this->getMultisiteConfig('structure', true)
) {
$this->addPropagatable(['children', 'parent']);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Database/Traits/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function setSortableOrder($itemIds, $referencePool = null)
throw new Exception('Invalid setSortableOrder call - count of itemIds do not match count of referencePool');
}

// Multisite
$keyName = $this->getKeyName();
if (
$this->isClassInstanceOf(\October\Contracts\Database\MultisiteInterface::class) &&
$this->isMultisiteSyncEnabled() &&
$this->getMultisiteConfig('structure', true)
) {
$keyName = 'site_root_id';
}

$upsert = [];
foreach ($itemIds as $id) {
$sortOrder = $sortKeyMap[$id] ?? null;
Expand All @@ -79,7 +89,7 @@ public function setSortableOrder($itemIds, $referencePool = null)
if ($upsert) {
foreach ($upsert as $update) {
$this->newQuery()
->where($this->getKeyName(), $update['id'])
->where($keyName, $update['id'])
->update([$this->getSortOrderColumn() => $update['sort_order']]);
}
}
Expand Down
22 changes: 19 additions & 3 deletions src/Database/TreeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ class TreeCollection extends Collection
*/
public function toNested($removeOrphans = true)
{
// Multisite
$keyMethod = 'getKey';
if (
($model = $this->first()) &&
$model->isClassInstanceOf(\October\Contracts\Database\MultisiteInterface::class) &&
$model->isAttributePropagatable('children') &&
$model->isAttributePropagatable('parent')
) {
$keyMethod = 'getMultisiteKey';
}

// Get dictionary
$collection = [];
foreach ($this as $item) {
$collection[$item->{$keyMethod}()] = $item;
}

// Set new collection for "children" relations
$collection = $this->getDictionary();
foreach ($collection as $key => $model) {
$model->setRelation('children', new Collection);
}
Expand All @@ -34,10 +50,10 @@ public function toNested($removeOrphans = true)

if (array_key_exists($parentKey, $collection)) {
$collection[$parentKey]->children[] = $model;
$nestedKeys[] = $model->getKey();
$nestedKeys[] = $model->{$keyMethod}();
}
elseif ($removeOrphans) {
$nestedKeys[] = $model->getKey();
$nestedKeys[] = $model->{$keyMethod}();
}
}

Expand Down

0 comments on commit 0b21726

Please sign in to comment.