Repository navigation
Expand file tree
/
Copy pathNodeController.php
More file actions
316 lines (259 loc) · 10.9 KB
/
Copy pathNodeController.php
File metadata and controls
316 lines (259 loc) · 10.9 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Node\StoreNodeRequest;
use App\Http\Requests\Node\UpdateNodeRequest;
use App\Models\Node;
use App\Models\Subject;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
class NodeController extends Controller
{
public function show(Subject $subject, $path = null)
{
if (! $path) {
$nodes = Node::where('subject_id', $subject->id)
->orderBy('sort_order')
->whereNull('parent_id')
->get();
return Inertia::render('admin/Node', [
'subject' => $subject,
'nodes' => $nodes,
'resources' => [],
'breadcrumb' => [],
]);
}
$slugs = explode('/', trim($path, '/'));
$node = Node::where('subject_id', $subject->id)
->orderBy('sort_order')
->whereNull('parent_id')
->where('slug', $slugs[0])
->first();
if (! $node) {
abort(404);
}
foreach (array_slice($slugs, 1) as $slug) {
$node = $node->children()->where('slug', $slug)->first();
if (! $node) {
abort(404);
}
}
$resources = $node->resources()
->where('status', 'approved')
->with(['user:id,name,username'])
->get();
return Inertia::render('admin/Node', [
'subject' => $subject,
'nodes' => $node->children,
'resources' => $resources,
'parent' => $node->append('is_effectively_frozen'),
'breadcrumb' => $node->breadcrumb(),
]);
}
public function store(StoreNodeRequest $request, Subject $subject)
{
$validated = $request->validated();
$parent = null;
if (! empty($validated['parent_id'])) {
$parent = Node::where('id', $validated['parent_id'])
->where('subject_id', $subject->id)
->firstOrFail();
}
$slug = ! empty($validated['slug'])
? Str::slug($validated['slug'])
: Str::slug($validated['name']);
$exists = Node::where('subject_id', $subject->id)
->where('parent_id', $parent?->id)
->where('slug', $slug)
->exists();
if ($exists) {
return back()
->withErrors([
'slug' => 'Folder with this slug already exists in this location.',
])
->withInput();
}
Node::create([
'user_id' => auth()->id(),
'subject_id' => $subject->id,
'parent_id' => $parent?->id,
'name' => $validated['name'],
'slug' => $slug,
'sort_order' => $validated['sort_order'] ?? 0,
'is_trackable' => (bool) ($validated['is_trackable'] ?? false),
'weight' => (int) ($validated['weight'] ?? 2),
]);
return back()->with('success', 'Folder created successfully.');
}
public function update(UpdateNodeRequest $request, Subject $subject, Node $node)
{
$validated = $request->validated();
if (array_key_exists('parent_id', $validated)) {
$parent = null;
if (! empty($validated['parent_id'])) {
$parent = Node::where('id', $validated['parent_id'])
->where('subject_id', $subject->id)
->where('id', '!=', $node->id)
->firstOrFail();
}
$node->parent_id = $parent?->id;
}
if (array_key_exists('name', $validated)) {
$node->name = $validated['name'];
}
if (array_key_exists('name', $validated) || array_key_exists('slug', $validated) || array_key_exists('parent_id', $validated)) {
$rawSlug = ! empty($validated['slug'])
? $validated['slug']
: ($validated['name'] ?? $node->name);
$slug = Str::slug($rawSlug);
$exists = Node::where('subject_id', $subject->id)
->where('parent_id', $node->parent_id)
->where('slug', $slug)
->where('id', '!=', $node->id)
->exists();
if ($exists) {
return back()->withErrors([
'slug' => 'Folder with this slug already exists in this location.',
])->withInput();
}
$node->slug = $slug;
}
if (array_key_exists('sort_order', $validated)) {
$node->sort_order = $validated['sort_order'] ?? 0;
}
if (array_key_exists('is_trackable', $validated)) {
$node->is_trackable = (bool) $validated['is_trackable'];
}
if (array_key_exists('weight', $validated)) {
$node->weight = (int) $validated['weight'];
}
$node->save();
return back()->with('success', 'Folder updated successfully.');
}
public function batchStore(Request $request, Subject $subject)
{
$validated = $request->validate([
'parent_id' => 'nullable|exists:nodes,id',
'nodes' => 'required|array|min:1',
'nodes.*.name' => 'required|string|max:255',
'nodes.*.slug' => 'nullable|string|max:255',
'nodes.*.children' => 'nullable|array',
'should_track_top_folders' => 'nullable|boolean',
]);
$shouldTrackTopFolders = (bool) ($validated['should_track_top_folders'] ?? false);
$parentId = $request->input('parent_id');
$parent = null;
if ($parentId) {
$parent = Node::where('id', $parentId)->where('subject_id', $subject->id)->firstOrFail();
if ($parent->isEffectivelyFrozen()) {
abort(403, 'This folder is frozen and cannot be modified.');
}
}
$inputNodes = $request->input('nodes');
// 1. Check duplicate slugs among incoming root items
$incomingRootSlugs = [];
foreach ($inputNodes as $nodeData) {
$nodeName = trim($nodeData['name'] ?? '');
if (empty($nodeName)) {
continue;
}
$rawSlug = ! empty($nodeData['slug']) ? $nodeData['slug'] : $nodeName;
$slug = Str::slug($rawSlug);
if (in_array($slug, $incomingRootSlugs, true)) {
return back()->withErrors([
'slug' => "Duplicate slug '{$slug}' found in your submitted chapter list.",
])->withInput();
}
$incomingRootSlugs[] = $slug;
// Check if slug already exists in database for this subject and parent
$exists = Node::where('subject_id', $subject->id)
->where('parent_id', $parent?->id)
->where('slug', $slug)
->exists();
if ($exists) {
return back()->withErrors([
'slug' => "Folder with slug '{$slug}' already exists in this location.",
])->withInput();
}
// Check duplicate child slugs within this chapter
if (! empty($nodeData['children']) && is_array($nodeData['children'])) {
$incomingChildSlugs = [];
foreach ($nodeData['children'] as $childItem) {
$childName = is_array($childItem) ? ($childItem['name'] ?? '') : (string) $childItem;
$childName = trim($childName);
if (empty($childName)) {
continue;
}
$childRawSlug = is_array($childItem) && ! empty($childItem['slug']) ? $childItem['slug'] : $childName;
$childSlug = Str::slug($childRawSlug);
if (in_array($childSlug, $incomingChildSlugs, true)) {
return back()->withErrors([
'slug' => "Duplicate sub-folder slug '{$childSlug}' found under chapter '{$nodeName}'.",
])->withInput();
}
$incomingChildSlugs[] = $childSlug;
}
}
}
$baseSortOrder = Node::where('subject_id', $subject->id)
->where('parent_id', $parent?->id)
->max('sort_order') ?? 0;
$createdCount = 0;
\DB::transaction(function () use ($inputNodes, $subject, $parent, $baseSortOrder, $shouldTrackTopFolders, &$createdCount) {
foreach ($inputNodes as $index => $nodeData) {
$nodeName = trim($nodeData['name']);
if (empty($nodeName)) {
continue;
}
$rawSlug = ! empty($nodeData['slug']) ? $nodeData['slug'] : $nodeName;
$slug = Str::slug($rawSlug);
$node = Node::create([
'user_id' => auth()->id(),
'subject_id' => $subject->id,
'parent_id' => $parent?->id,
'name' => $nodeName,
'slug' => $slug,
'sort_order' => $baseSortOrder + $index + 1,
'is_trackable' => $shouldTrackTopFolders,
]);
$createdCount++;
// Create children if any
if (! empty($nodeData['children']) && is_array($nodeData['children'])) {
foreach ($nodeData['children'] as $childIndex => $childItem) {
$childName = is_array($childItem) ? ($childItem['name'] ?? '') : (string) $childItem;
$childName = trim($childName);
if (empty($childName)) {
continue;
}
$childRawSlug = is_array($childItem) && ! empty($childItem['slug']) ? $childItem['slug'] : $childName;
$childSlug = Str::slug($childRawSlug);
Node::create([
'user_id' => auth()->id(),
'subject_id' => $subject->id,
'parent_id' => $node->id,
'name' => $childName,
'slug' => $childSlug,
'sort_order' => $childIndex,
'is_trackable' => false,
]);
}
}
}
});
return back()->with('success', "Successfully generated {$createdCount} folders with subfolders!");
}
public function toggleFreeze(Node $node)
{
$node->update([
'is_frozen' => ! $node->is_frozen,
]);
$status = $node->is_frozen ? 'frozen' : 'unfrozen';
return back()->with('success', "Folder has been {$status} successfully.");
}
public function destroy(Node $node)
{
$node->delete();
return redirect()->back()->with('success', 'Node deleted successfully.');
}
}