-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathListOperation.php
More file actions
142 lines (119 loc) · 4.97 KB
/
Copy pathListOperation.php
File metadata and controls
142 lines (119 loc) · 4.97 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
<?php
namespace Backpack\CRUD\app\Http\Controllers\Operations;
use Illuminate\Support\Facades\Route;
trait ListOperation
{
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupListRoutes($segment, $routeName, $controller)
{
Route::get($segment.'/', [
'as' => $routeName.'.index',
'uses' => $controller.'@index',
'operation' => 'list',
]);
Route::post($segment.'/search', [
'as' => $routeName.'.search',
'uses' => $controller.'@search',
'operation' => 'list',
]);
if (! isset($this->setupDetailsRowRoute) || $this->setupDetailsRowRoute === true) {
Route::get($segment.'/{id}/details', [
'as' => $routeName.'.showDetailsRow',
'uses' => $controller.'@showDetailsRow',
'operation' => 'list',
]);
}
}
/**
* Add the default settings, buttons, etc that this operation needs.
*/
protected function setupListDefaults()
{
$this->crud->allowAccess('list');
$this->crud->operation('list', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
$this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute());
});
}
/**
* Display all rows in the database for this entity.
*
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
$this->crud->hasAccessOrFail('list');
$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? mb_ucfirst($this->crud->entity_name_plural);
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getListView(), $this->data);
}
/**
* The search function that is called by the data table.
*
* @return array JSON Array of cells in HTML form.
*/
public function search()
{
$this->crud->hasAccessOrFail('list');
$this->crud->applyUnappliedFilters();
$start = (int) request()->input('start');
$length = (int) request()->input('length');
$search = request()->input('search');
// check if length is allowed by developer
if ($length && ! in_array($length, $this->crud->getPageLengthMenu()[0])) {
return response()->json([
'error' => 'Unknown page length.',
], 400);
}
// if a search term was present
if ($search && $search['value'] ?? false) {
// filter the results accordingly
$this->crud->applySearchTerm($search['value']);
}
// start the results according to the datatables pagination
if ($start) {
$this->crud->skip($start);
}
// limit the number of results according to the datatables pagination
if ($length) {
$this->crud->take($length);
}
// overwrite any order set in the setup() method with the datatables order
$this->crud->applyDatatableOrder();
$entries = $this->crud->getEntries();
// if show entry count is disabled we use the "simplePagination" technique to move between pages.
if ($this->crud->getOperationSetting('showEntryCount')) {
$totalEntryCount = (int) (request()->get('totalEntryCount') ?: $this->crud->getTotalQueryCount());
$filteredEntryCount = $this->crud->getFilteredQueryCount() ?? $totalEntryCount;
} else {
$totalEntryCount = $length;
$filteredEntryCount = $entries->count() < $length ? 0 : $length + $start + 1;
}
// store the totalEntryCount in CrudPanel so that multiple blade files can access it
$this->crud->setOperationSetting('totalEntryCount', $totalEntryCount);
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalEntryCount, $filteredEntryCount, $start);
}
/**
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table.
* It defaults to showing some dummy text.
*
* @return \Illuminate\Contracts\View\View
*/
public function showDetailsRow($id)
{
$this->crud->hasAccessOrFail('list');
// get entry ID from Request (makes sure its the last ID for nested resources)
$id = $this->crud->getCurrentEntryId() ?? $id;
$this->data['entry'] = $this->crud->getEntry($id);
$this->data['crud'] = $this->crud;
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getDetailsRowView(), $this->data);
}
}