1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Models\Behaviors; |
4
|
|
|
|
5
|
|
|
use Kalnoy\Nestedset\NodeTrait; |
6
|
|
|
|
7
|
|
|
trait HasNesting |
8
|
|
|
{ |
9
|
|
|
use NodeTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Returns the combined slug for this item including all ancestors. |
13
|
|
|
* |
14
|
|
|
* @param string|null $locale |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function getNestedSlug($locale = null) |
18
|
|
|
{ |
19
|
|
|
return collect([$this->getAncestorsSlug($locale), $this->getSlug($locale)]) |
|
|
|
|
20
|
|
|
->filter() |
21
|
|
|
->implode('/'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function getNestedSlugAttribute() |
28
|
|
|
{ |
29
|
|
|
return $this->getNestedSlug(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the combined slug for all ancestors of this item. |
34
|
|
|
* |
35
|
|
|
* @param string|null $locale |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getAncestorsSlug($locale = null) |
39
|
|
|
{ |
40
|
|
|
return collect($this->ancestors ?? []) |
41
|
|
|
->map(function ($i) use ($locale) { return $i->getSlug($locale); }) |
42
|
|
|
->implode('/'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getAncestorsSlugAttribute() |
49
|
|
|
{ |
50
|
|
|
return $this->getAncestorsSlug(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function saveTreeFromIds($nodeTree) |
54
|
|
|
{ |
55
|
|
|
$nodeModels = self::all(); |
56
|
|
|
$nodeArrays = self::flattenTree($nodeTree); |
57
|
|
|
|
58
|
|
|
foreach ($nodeArrays as $nodeArray) { |
59
|
|
|
$nodeModel = $nodeModels->where('id', $nodeArray['id'])->first(); |
60
|
|
|
|
61
|
|
|
if ($nodeArray['parent_id'] === null) { |
62
|
|
|
if (!$nodeModel->isRoot() || $nodeModel->position !== $nodeArray['position']) { |
63
|
|
|
$nodeModel->position = $nodeArray['position']; |
64
|
|
|
$nodeModel->saveAsRoot(); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
if ($nodeModel->position !== $nodeArray['position'] || $nodeModel->parent_id !== $nodeArray['parent_id']) { |
68
|
|
|
$nodeModel->position = $nodeArray['position']; |
69
|
|
|
$nodeModel->parent_id = $nodeArray['parent_id']; |
70
|
|
|
$nodeModel->save(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function flattenTree(array $nodeTree, int $parentId = null) |
77
|
|
|
{ |
78
|
|
|
$nodeArrays = []; |
79
|
|
|
$position = 0; |
80
|
|
|
|
81
|
|
|
foreach ($nodeTree as $node) { |
82
|
|
|
$nodeArrays[] = [ |
83
|
|
|
'id' => $node['id'], |
84
|
|
|
'position' => $position++, |
85
|
|
|
'parent_id' => $parentId, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if (count($node['children']) > 0) { |
89
|
|
|
$childArrays = self::flattenTree($node['children'], $node['id']); |
90
|
|
|
$nodeArrays = array_merge($nodeArrays, $childArrays); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $nodeArrays; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|