1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin\Operations; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Facades\Route; |
9
|
|
|
use Illuminate\View\View; |
10
|
|
|
|
11
|
|
|
trait SimpleReorderOperation |
12
|
|
|
{ |
13
|
|
|
protected function setupSimpleReorderRoutes(string $segment, string $routeName, string $controller): void |
14
|
|
|
{ |
15
|
|
|
Route::get("{$segment}/reorder", [ |
16
|
|
|
'as' => "{$routeName}.reorder", |
17
|
|
|
'uses' => "{$controller}@reorder", |
18
|
|
|
'operation' => 'reorder', |
19
|
|
|
]); |
20
|
|
|
|
21
|
|
|
Route::post("{$segment}/reorder", [ |
22
|
|
|
'as' => "{$routeName}.save.reorder", |
23
|
|
|
'uses' => "{$controller}@saveReorder", |
24
|
|
|
'operation' => 'reorder', |
25
|
|
|
]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function setupSimpleReorderDefaults() |
29
|
|
|
{ |
30
|
|
|
CRUD::set('reorder.enabled', true); |
|
|
|
|
31
|
|
|
CRUD::allowAccess('reorder'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
CRUD::operation('reorder', function () { |
|
|
|
|
34
|
|
|
CRUD::loadDefaultOperationSettingsFromConfig(); |
|
|
|
|
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
CRUD::operation('list', function () { |
38
|
|
|
CRUD::addButton('top', 'reorder', 'view', 'crud::buttons.reorder'); |
|
|
|
|
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function reorder(): View |
43
|
|
|
{ |
44
|
|
|
CRUD::hasAccessOrFail('reorder'); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$column = CRUD::get('reorder.column'); |
|
|
|
|
47
|
|
|
|
48
|
|
|
return view('crud::simple-reorder', [ |
49
|
|
|
'entries' => $this->crud->getEntries()->sortBy($column)->keyBy($this->crud->getModel()->getKeyName()), |
50
|
|
|
'crud' => $this->crud, |
51
|
|
|
'title' => $this->crud->getTitle() ?? trans('backpack::crud.reorder') . ' ' . $this->crud->entity_name, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function saveReorder(Request $request): string|false |
56
|
|
|
{ |
57
|
|
|
CRUD::hasAccessOrFail('reorder'); |
58
|
|
|
|
59
|
|
|
$entries = json_decode($request->input('tree'), true); |
60
|
|
|
|
61
|
|
|
if (empty($entries)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
DB::transaction(function () use ($entries) { |
66
|
|
|
$primaryKey = $this->crud->model->getKeyName(); |
67
|
|
|
$table = $this->crud->model->getTable(); |
68
|
|
|
$connection = $this->crud->model->getConnectionName(); |
69
|
|
|
$column = $this->crud->get('reorder.column'); |
70
|
|
|
|
71
|
|
|
$query = ''; |
72
|
|
|
$bindings = $itemKeys = []; |
73
|
|
|
$query .= "UPDATE `{$table}` SET `{$column}` = CASE "; |
74
|
|
|
foreach ($entries as $order => $item) { |
75
|
|
|
$itemKey = str_replace('list_', '', $item); |
76
|
|
|
$itemKeys[] = $itemKey; |
77
|
|
|
|
78
|
|
|
$query .= "WHEN `{$primaryKey}` = ? THEN ? "; |
79
|
|
|
$bindings[] = $itemKey; |
80
|
|
|
$bindings[] = $order; |
81
|
|
|
} |
82
|
|
|
// add the bind placeholders for the item keys at the end the array of bindings |
83
|
|
|
array_push($bindings, ...$itemKeys); |
84
|
|
|
$reorderItemsBindString = implode(',', array_fill(0, count($itemKeys), '?')); |
85
|
|
|
|
86
|
|
|
// add the where clause to the query to help match the items |
87
|
|
|
$query .= "ELSE `{$column}` END WHERE `{$primaryKey}` IN ({$reorderItemsBindString})"; |
88
|
|
|
|
89
|
|
|
DB::connection($connection)->statement($query, $bindings); |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$updatedNumber = count($entries); |
93
|
|
|
|
94
|
|
|
return "success for {$updatedNumber} items"; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|