Skip to content

Commit b58f971

Browse files
authored
Merge pull request #5677 from Laravel-Backpack/allow-customization-of-reorder-column-names
Allow customization of reorder column names
2 parents 3714d7d + b2da3a5 commit b58f971

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/app/Http/Controllers/Operations/ReorderOperation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ protected function setupReorderDefaults()
3838

3939
$this->crud->operation('reorder', function () {
4040
$this->crud->loadDefaultOperationSettingsFromConfig();
41+
$this->crud->setOperationSetting('reorderColumnNames', [
42+
'parent_id' => 'parent_id',
43+
'lft' => 'lft',
44+
'rgt' => 'rgt',
45+
'depth' => 'depth',
46+
]);
4147
});
4248

4349
$this->crud->operation('list', function () {

src/app/Library/CrudPanel/Traits/Reorder.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function updateTreeOrder($request)
1919
{
2020
$primaryKey = $this->model->getKeyName();
2121

22+
$columns = $this->getOperationSetting('reorderColumnNames');
23+
2224
// we use the upsert method that should update the values of the matching ids.
2325
// it has the drawback of creating new entries when the id is not found
2426
// for that reason we get a list of all the ids and filter the ones
@@ -28,14 +30,29 @@ public function updateTreeOrder($request)
2830
// filter the items that are not in the database and map the request
2931
$reorderItems = collect($request)->filter(function ($item) use ($itemKeys) {
3032
return $item['item_id'] !== '' && $item['item_id'] !== null && $itemKeys->contains($item['item_id']);
31-
})->map(function ($item) use ($primaryKey) {
33+
})->map(function ($item) use ($primaryKey, $columns) {
3234
$item[$primaryKey] = $item['item_id'];
33-
$item['parent_id'] = empty($item['parent_id']) ? null : $item['parent_id'];
34-
$item['depth'] = empty($item['depth']) ? null : (int) $item['depth'];
35-
$item['lft'] = empty($item['left']) ? null : (int) $item['left'];
36-
$item['rgt'] = empty($item['right']) ? null : (int) $item['right'];
35+
$item[$columns['parent_id']] = empty($item['parent_id']) ? null : $item['parent_id'];
36+
$item[$columns['depth']] = empty($item['depth']) ? null : (int) $item['depth'];
37+
$item[$columns['lft']] = empty($item['left']) ? null : (int) $item['left'];
38+
$item[$columns['rgt']] = empty($item['right']) ? null : (int) $item['right'];
39+
3740
// unset mapped items properties.
38-
unset($item['item_id'], $item['left'], $item['right']);
41+
if ($columns['parent_id'] !== 'parent_id') {
42+
unset($item['parent_id']);
43+
}
44+
if ($columns['depth'] !== 'depth') {
45+
unset($item['depth']);
46+
}
47+
if ($columns['lft'] !== 'left') {
48+
unset($item['left']);
49+
}
50+
if ($columns['rgt'] !== 'right') {
51+
unset($item['right']);
52+
}
53+
54+
// unset the item_id property
55+
unset($item['item_id']);
3956

4057
return $item;
4158
})->toArray();
@@ -47,13 +64,13 @@ public function updateTreeOrder($request)
4764
});
4865

4966
// wrap the queries in a transaction to avoid partial updates
50-
DB::connection($this->model->getConnectionName())->transaction(function () use ($reorderItems, $primaryKey, $itemKeys) {
67+
DB::connection($this->model->getConnectionName())->transaction(function () use ($reorderItems, $primaryKey, $itemKeys, $columns) {
5168
// create a string of ?,?,?,? to use as bind placeholders for item keys
5269
$reorderItemsBindString = implode(',', array_fill(0, count($reorderItems), '?'));
5370

5471
// each of this properties will be updated using a single query with a CASE statement
5572
// this ensures that only 4 queries are run, no matter how many items are reordered
56-
foreach (['parent_id', 'depth', 'lft', 'rgt'] as $column) {
73+
foreach (array_values($columns) as $column) {
5774
$query = '';
5875
$bindings = [];
5976
$query .= "UPDATE {$this->model->getTable()} SET {$column} = CASE ";

src/resources/views/crud/reorder.blade.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
trans('backpack::crud.reorder') => false,
88
];
99
10+
$columns = $crud->getOperationSetting('reorderColumnNames');
11+
1012
// if breadcrumbs aren't defined in the CrudController, use the default breadcrumbs
1113
$breadcrumbs = $breadcrumbs ?? $defaultBreadcrumbs;
1214
@endphp
@@ -24,9 +26,11 @@
2426
@endsection
2527

2628
@section('content')
27-
<?php
28-
function tree_element($entry, $key, $all_entries, $crud)
29-
{
29+
<?php
30+
if(!function_exists('tree_element')) {
31+
function tree_element($entry, $key, $all_entries, $crud) {
32+
$columns = $crud->getOperationSetting('reorderColumnNames');
33+
3034
if (! isset($entry->tree_element_shown)) {
3135
// mark the element as shown
3236
$all_entries[$key]->tree_element_shown = true;
@@ -39,12 +43,12 @@ function tree_element($entry, $key, $all_entries, $crud)
3943
// see if this element has any children
4044
$children = [];
4145
foreach ($all_entries as $key => $subentry) {
42-
if ($subentry->parent_id == $entry->getKey()) {
46+
if ($subentry->{$columns['parent_id']} == $entry->getKey()) {
4347
$children[] = $subentry;
4448
}
4549
}
4650
47-
$children = collect($children)->sortBy('lft');
51+
$children = collect($children)->sortBy($columns['lft']);
4852
4953
// if it does have children, show them
5054
if (count($children)) {
@@ -59,6 +63,7 @@ function tree_element($entry, $key, $all_entries, $crud)
5963
6064
return $entry;
6165
}
66+
}
6267
6368
?>
6469

@@ -68,15 +73,15 @@ function tree_element($entry, $key, $all_entries, $crud)
6873
<p>{{ trans('backpack::crud.reorder_text') }}</p>
6974

7075
<ol class="sortable mt-0 mb-0">
71-
<?php
72-
$all_entries = collect($entries->all())->sortBy('lft')->keyBy($crud->getModel()->getKeyName());
73-
$root_entries = $all_entries->filter(function ($item) {
74-
return $item->parent_id == 0;
75-
});
76-
foreach ($root_entries as $key => $entry) {
77-
$root_entries[$key] = tree_element($entry, $key, $all_entries, $crud);
78-
}
79-
?>
76+
<?php
77+
$all_entries = collect($entries->all())->sortBy($columns['lft'])->keyBy($crud->getModel()->getKeyName());
78+
$root_entries = $all_entries->filter(function ($item) use ($columns) {
79+
return $item->{$columns['parent_id']} == 0;
80+
});
81+
foreach ($root_entries as $key => $entry) {
82+
$root_entries[$key] = tree_element($entry, $key, $all_entries, $crud);
83+
}
84+
?>
8085
</ol>
8186

8287
</div>{{-- /.card --}}

tests/Unit/CrudPanel/CrudPanelReorderOperationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public function testSaveReorderTree()
1616
$this->createReorderItems();
1717
$this->crudPanel->setModel(Reorder::class);
1818

19+
$this->crudPanel->setOperationSetting('reorderColumnNames', [
20+
'parent_id' => 'parent_id',
21+
'depth' => 'depth',
22+
'lft' => 'lft',
23+
'rgt' => 'rgt',
24+
]);
25+
1926
$this->crudPanel->updateTreeOrder(
2027
[
2128
[

0 commit comments

Comments
 (0)