|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\CommandPalette; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Thinktomorrow\Chief\App\Http\Controllers\Controller; |
|
9
|
|
|
use Thinktomorrow\Chief\Managers\Register\Registry; |
|
10
|
|
|
|
|
11
|
|
|
class CommandPaletteController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->pageModels = $this->getAllPageModels(); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function search(Request $request, string $term = '') |
|
19
|
|
|
{ |
|
20
|
|
|
$lowercaseTerm = Str::lower($term); |
|
21
|
|
|
|
|
22
|
|
|
$results = [ |
|
23
|
|
|
...$this->searchThroughPageModels($lowercaseTerm), |
|
24
|
|
|
...$this->searchThroughAdminPages($lowercaseTerm), |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
return response()->view('chief::layout.nav.command-palette._result', [ |
|
28
|
|
|
'term' => $term, |
|
29
|
|
|
'results' => $results, |
|
30
|
|
|
]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function searchThroughPageModels($term) |
|
34
|
|
|
{ |
|
35
|
|
|
$results = []; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($this->pageModels as $modelGroup) { |
|
38
|
|
|
$resultGroup = []; |
|
39
|
|
|
|
|
40
|
|
|
foreach ($modelGroup['models'] as $model) { |
|
41
|
|
|
if ( |
|
42
|
|
|
Str::contains(Str::lower($model->adminConfig()->getModelName()), $term) || |
|
43
|
|
|
Str::contains(Str::lower($model->adminConfig()->getIndexTitle()), $term) || |
|
44
|
|
|
Str::contains(Str::lower($model->adminConfig()->getNavTitle()), $term) || |
|
45
|
|
|
Str::contains(Str::lower($model->adminConfig()->getPageTitle()), $term) || |
|
46
|
|
|
Str::contains(Str::lower($model->title ?? ''), $term) |
|
47
|
|
|
) { |
|
48
|
|
|
$resultGroup[$model->modelReference()->getShort()] = [ |
|
49
|
|
|
'label' => $model->title, |
|
50
|
|
|
'url' => '/admin/' . $model->managedModelKey() . '/' . $model->id . '/edit', |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (count($resultGroup) !== 0) { |
|
56
|
|
|
$firstModel = $modelGroup['models']->first(); |
|
57
|
|
|
|
|
58
|
|
|
array_push($resultGroup, [ |
|
59
|
|
|
'label' => ucfirst($modelGroup['label']) . ' overzicht', |
|
60
|
|
|
'url' => '/admin/' . $firstModel->managedModelKey(), |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
array_push($results, [ |
|
64
|
|
|
'label' => $modelGroup['label'], |
|
65
|
|
|
'models' => $resultGroup, |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $results; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function searchThroughAdminPages($term) |
|
74
|
|
|
{ |
|
75
|
|
|
$adminPages = collect([ |
|
76
|
|
|
[ |
|
77
|
|
|
'label' => 'Menu', |
|
78
|
|
|
'url' => route('chief.back.menus.index'), |
|
79
|
|
|
'permission' => 'update-page', |
|
80
|
|
|
'tags' => ['navigatie'], |
|
81
|
|
|
], [ |
|
82
|
|
|
'label' => 'Media', |
|
83
|
|
|
'url' => route('chief.mediagallery.index'), |
|
84
|
|
|
'permission' => 'update-page', |
|
85
|
|
|
'tags' => ['mediagalerij', 'mediabibliotheek', 'assets'], |
|
86
|
|
|
], [ |
|
87
|
|
|
'label' => 'Teksten', |
|
88
|
|
|
'url' => route('squanto.index'), |
|
89
|
|
|
'permission' => 'view-squanto', |
|
90
|
|
|
'tags' => ['squanto', 'mediagalerij', 'mediabibliotheek', 'assets'], |
|
91
|
|
|
], [ |
|
92
|
|
|
'label' => 'Sitemap', |
|
93
|
|
|
'url' => route('chief.back.sitemap.show'), |
|
94
|
|
|
'permission' => null, |
|
95
|
|
|
'tags' => [], |
|
96
|
|
|
], [ |
|
97
|
|
|
'label' => 'Admins', |
|
98
|
|
|
'url' => route('chief.back.users.index'), |
|
99
|
|
|
'permission' => 'view-user', |
|
100
|
|
|
'tags' => [], |
|
101
|
|
|
], [ |
|
102
|
|
|
'label' => 'Rechten', |
|
103
|
|
|
'url' => route('chief.back.roles.index'), |
|
104
|
|
|
'permission' => 'view-role', |
|
105
|
|
|
'tags' => ['roles'], |
|
106
|
|
|
], [ |
|
107
|
|
|
'label' => 'Settings', |
|
108
|
|
|
'url' => route('chief.back.settings.edit'), |
|
109
|
|
|
'permission' => 'update-setting', |
|
110
|
|
|
'tags' => ['instellingen'], |
|
111
|
|
|
], [ |
|
112
|
|
|
'label' => 'Audit', |
|
113
|
|
|
'url' => route('chief.back.audit.index'), |
|
114
|
|
|
'permission' => 'view-audit', |
|
115
|
|
|
'tags' => [], |
|
116
|
|
|
], [ |
|
117
|
|
|
'label' => chiefAdmin()->firstname, |
|
|
|
|
|
|
118
|
|
|
'url' => route('chief.back.you.edit'), |
|
119
|
|
|
'permission' => 'update-you', |
|
120
|
|
|
'tags' => ['account'], |
|
121
|
|
|
], [ |
|
122
|
|
|
'label' => 'Logout', |
|
123
|
|
|
'url' => route('chief.back.logout'), |
|
124
|
|
|
'permission' => null, |
|
125
|
|
|
'tags' => [], |
|
126
|
|
|
], |
|
127
|
|
|
]); |
|
128
|
|
|
|
|
129
|
|
|
$models = $adminPages->filter(function ($adminPage) use ($term) { |
|
130
|
|
|
// Check if label contains search term |
|
131
|
|
|
if (Str::contains(Str::lower($adminPage['label']), $term)) { |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Check if any of tags contain search term |
|
136
|
|
|
if (collect($adminPage['tags'])->contains(function ($tag) use ($term) { |
|
137
|
|
|
return Str::contains(Str::lower($tag), $term); |
|
138
|
|
|
})) { |
|
139
|
|
|
return true; |
|
140
|
|
|
}; |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
})->map(function ($adminPage) { |
|
144
|
|
|
return [ |
|
145
|
|
|
'label' => $adminPage['label'], |
|
146
|
|
|
'url' => $adminPage['url'], |
|
147
|
|
|
]; |
|
148
|
|
|
})->toArray(); |
|
149
|
|
|
|
|
150
|
|
|
if (count($models) === 0) { |
|
151
|
|
|
return []; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return [ |
|
155
|
|
|
[ |
|
156
|
|
|
'label' => 'Chief', |
|
157
|
|
|
'models' => $models, |
|
158
|
|
|
], |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function getAllPageModels(): Collection |
|
163
|
|
|
{ |
|
164
|
|
|
return collect(app(Registry::class)->models()) |
|
165
|
|
|
// Filter out fragment models |
|
166
|
|
|
->filter(function ($model) { |
|
167
|
|
|
return ! in_array('Thinktomorrow\Chief\Fragments\Fragmentable', class_implements($model)); |
|
168
|
|
|
// Return all instances of the models |
|
169
|
|
|
})->map(function ($model) { |
|
170
|
|
|
$models = $model::all(); |
|
171
|
|
|
|
|
172
|
|
|
return [ |
|
173
|
|
|
'label' => $model::make()->adminConfig()->getModelName(), |
|
174
|
|
|
'models' => $models, |
|
175
|
|
|
]; |
|
176
|
|
|
}); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// Add these pages to end of model groups search result |
|
180
|
|
|
public static function getAllPageModelIndices(): Collection |
|
181
|
|
|
{ |
|
182
|
|
|
return collect(app(Registry::class)->models()) |
|
183
|
|
|
// Filter out fragment models |
|
184
|
|
|
->filter(function ($model) { |
|
185
|
|
|
return ! in_array('Thinktomorrow\Chief\Fragments\Fragmentable', class_implements($model)); |
|
186
|
|
|
// Return all instances of the models |
|
187
|
|
|
})->map(function ($model) { |
|
188
|
|
|
$model = $model::make(); |
|
189
|
|
|
|
|
190
|
|
|
return [ |
|
191
|
|
|
'label' => $model->adminConfig()->getNavTitle(), |
|
192
|
|
|
'url' => '/admin/' . $model->managedModelKey(), |
|
193
|
|
|
]; |
|
194
|
|
|
}); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|