-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandleNesting.php
45 lines (36 loc) · 1.15 KB
/
HandleNesting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Jobs\ReorderNestedModuleItems;
use A17\Twill\Models\Contracts\TwillModelContract;
trait HandleNesting
{
/**
* The Laravel queue name to be used for the reordering operation.
*/
protected string $reorderNestedModuleItemsJobQueue = 'default';
public function forNestedSlug(
string $nestedSlug,
array $with = [],
array $withCount = [],
array $scopes = []
): ?TwillModelContract {
$targetSlug = collect(explode('/', $nestedSlug))->last();
$targetItem = $this->forSlug($targetSlug, $with, $withCount, $scopes);
if (!$targetItem || $nestedSlug !== $targetItem->nestedSlug) {
return null;
}
return $targetItem;
}
public function setNewOrder(array $ids): void
{
ReorderNestedModuleItems::dispatch($this->model, $ids)
->onQueue($this->reorderNestedModuleItemsJobQueue);
}
public function afterRestore(TwillModelContract $object): void
{
if (!$object->parent) {
$object->parent_id = null;
$object->save();
}
}
}