Skip to content

Commit

Permalink
Merge branch '3.x' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Oct 31, 2024
2 parents b5d7637 + 0b21726 commit b42f857
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Database/Traits/Multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait Multisite
* Set to an array of options for more granular controls:
*
* - **sync** - logic to sync specific sites, available options: `all`, `group`, `locale`
* - **structure** - enable the sync of tree/sortable structures, default: `true`
* - **delete** - delete all linked records when any record is deleted, default: `true`
*
* protected $propagatableSync = false;
Expand Down Expand Up @@ -355,6 +356,9 @@ public function getMultisiteSyncSites()
* For example, finding a model using attributes from another site, or finding
* all connected models for all sites.
*
* If the value is provided as a string, it must be the ID from the primary record,
* in other words: taken from `site_root_id` not from the `id` column.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|\Illuminate\Database\Eloquent\Model $idOrModel
* @return \Illuminate\Database\Eloquent\Builder
Expand Down
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 b42f857

Please sign in to comment.