|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Menu; |
|
5
|
|
|
|
|
6
|
|
|
use Astrotomic\Translatable\Translatable as BaseTranslatable; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\GlobalMorphableScope; |
|
9
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphables; |
|
10
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\Translatable; |
|
11
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract; |
|
12
|
|
|
use Thinktomorrow\Chief\Pages\Page; |
|
13
|
|
|
use Vine\Source as VineSource; |
|
14
|
|
|
use Vine\Node; |
|
15
|
|
|
|
|
16
|
|
|
class MenuItem extends Model implements TranslatableContract, VineSource |
|
17
|
|
|
{ |
|
18
|
|
|
const TYPE_INTERNAL = 'internal'; |
|
19
|
|
|
const TYPE_CUSTOM = 'custom'; |
|
20
|
|
|
const TYPE_NOLINK = 'nolink'; |
|
21
|
|
|
|
|
22
|
|
|
use Translatable, |
|
|
|
|
|
|
23
|
|
|
BaseTranslatable; |
|
24
|
|
|
|
|
25
|
|
|
protected $translationModel = MenuItemTranslation::class; |
|
26
|
|
|
protected $translationForeignKey = 'menu_item_id'; |
|
27
|
|
|
protected $translatedAttributes = [ |
|
28
|
|
|
'label', |
|
29
|
|
|
'url', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
protected $with = ['page', 'translations']; |
|
33
|
|
|
|
|
34
|
|
|
public $timestamps = false; |
|
35
|
|
|
public $guarded = []; |
|
36
|
|
|
|
|
37
|
9 |
|
public function ofType($type): bool |
|
38
|
|
|
{ |
|
39
|
9 |
|
return $this->type == $type; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function menuType() |
|
43
|
|
|
{ |
|
44
|
4 |
|
return $this->menu_type; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
27 |
|
public function page() |
|
48
|
|
|
{ |
|
49
|
27 |
|
return $this->belongsTo(Page::class, 'page_id') |
|
50
|
27 |
|
->withoutGlobalScope(GlobalMorphableScope::class); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public function parent() |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->belongsTo(MenuItem::class, 'parent_id'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function children() |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->hasMany(MenuItem::class, 'parent_id')->orderBy('order', 'ASC'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function siblings() |
|
64
|
|
|
{ |
|
65
|
1 |
|
return static:: where('parent_id', $this->parent_id)->where('menu_type', $this->menuType())->where('id', '<>', $this->id)->orderBy('order', 'ASC')->get(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function siblingsIncludingSelf() |
|
69
|
|
|
{ |
|
70
|
|
|
return static:: where('parent_id', $this->parent_id)->where('menu_type', $this->menuType())->orderBy('order', 'ASC')->get(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function scopeOnlyGrandParents($query) |
|
74
|
|
|
{ |
|
75
|
|
|
return $query->whereNull('parent_id'); |
|
76
|
|
|
} |
|
77
|
13 |
|
|
|
78
|
|
|
public function url() |
|
79
|
13 |
|
{ |
|
80
|
|
|
if ($this->ofType(static::TYPE_INTERNAL) && $page = $this->page) { |
|
81
|
|
|
return $page->url(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->url; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Full array of original data rows |
|
89
|
|
|
* These are the rows to be converted to the tree model |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function nodeEntries($type = 'main'): array |
|
94
|
|
|
{ |
|
95
|
|
|
$items = $this->where('menu_type', $type)->get(); |
|
96
|
|
|
$collectionItems = collect([]); |
|
97
|
|
|
|
|
98
|
|
|
// Expose the collection items and populate them with the collection data |
|
99
|
|
|
foreach ($items as $k => $item) { |
|
100
|
|
|
|
|
101
|
|
|
// Fetch the urls of the internal links |
|
102
|
13 |
|
if ($item->ofType(static::TYPE_INTERNAL) && $page = $item->page) { |
|
103
|
|
|
if ($page->isArchived()) { |
|
104
|
13 |
|
unset($items[$k]); |
|
105
|
13 |
|
} else { |
|
106
|
|
|
$item->url = self::composePageUrl($item, $page); |
|
|
|
|
|
|
107
|
|
|
$item->page_label = $page->menuLabel(); |
|
|
|
|
|
|
108
|
13 |
|
$item->hidden_in_menu = $page->hidden_in_menu; |
|
109
|
|
|
$item->draft = $page->isDraft(); |
|
|
|
|
|
|
110
|
9 |
|
$items[$k] = $item; |
|
111
|
2 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
2 |
|
|
|
115
|
2 |
|
return array_merge($items->all(), $collectionItems->all()); |
|
116
|
2 |
|
} |
|
117
|
2 |
|
|
|
118
|
2 |
|
private static function composePageUrl(MenuItem $item, Page $page) |
|
|
|
|
|
|
119
|
2 |
|
{ |
|
120
|
2 |
|
return $page->url(); |
|
121
|
2 |
|
} |
|
122
|
2 |
|
|
|
123
|
|
|
/** |
|
124
|
2 |
|
* Attribute key of the primary identifier of each row. e.g. 'id' |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
9 |
|
*/ |
|
128
|
6 |
|
public function nodeKeyIdentifier(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return 'id'; |
|
131
|
6 |
|
} |
|
132
|
6 |
|
|
|
133
|
6 |
|
/** |
|
134
|
6 |
|
* Attribute key of the parent foreign identifier of each row. e.g. 'parent_id' |
|
135
|
9 |
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function nodeParentKeyIdentifier(): string |
|
139
|
|
|
{ |
|
140
|
13 |
|
return 'parent_id'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
7 |
|
/** |
|
144
|
|
|
* Convert entire models to condensed data arrays |
|
145
|
7 |
|
* |
|
146
|
|
|
* @param Node $node |
|
147
|
|
|
*/ |
|
148
|
|
|
public function entry(Node $node) |
|
149
|
|
|
{ |
|
150
|
|
|
return (object) [ |
|
151
|
|
|
'id' => $node->id, |
|
152
|
|
|
'type' => $node->type, |
|
153
|
|
|
'label' => $node->label, |
|
154
|
|
|
'page_label' => $node->page_label, // Extra info when dealing with internal links |
|
155
|
|
|
'url' => $node->url, |
|
156
|
|
|
'order' => $node->order, |
|
157
|
|
|
'page_id' => $node->page_id, |
|
158
|
|
|
'parent_id' => $node->parent_id, |
|
159
|
|
|
'morph_key' => $node->morph_key, |
|
160
|
|
|
'hidden_in_menu' => $node->hidden_in_menu, |
|
161
|
|
|
'draft' => $node->draft |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|