1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; |
6
|
|
|
use Illuminate\Support\Facades\Route; |
7
|
|
|
use Backpack\CRUD\app\Library\Support\DatatableCache; |
8
|
|
|
|
9
|
|
|
trait ListOperation |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Define which routes are needed for this operation. |
13
|
|
|
* |
14
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
15
|
|
|
* @param string $routeName Prefix of the route name. |
16
|
|
|
* @param string $controller Name of the current CrudController. |
17
|
|
|
*/ |
18
|
|
|
protected function setupListRoutes($segment, $routeName, $controller) |
19
|
|
|
{ |
20
|
|
|
Route::get($segment.'/', [ |
21
|
|
|
'as' => $routeName.'.index', |
22
|
|
|
'uses' => $controller.'@index', |
23
|
|
|
'operation' => 'list', |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
Route::post($segment.'/search', [ |
27
|
|
|
'as' => $routeName.'.search', |
28
|
|
|
'uses' => $controller.'@search', |
29
|
|
|
'operation' => 'list', |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
if (! isset($this->setupDetailsRowRoute) || $this->setupDetailsRowRoute === true) { |
33
|
|
|
Route::get($segment.'/{id}/details', [ |
34
|
|
|
'as' => $routeName.'.showDetailsRow', |
35
|
|
|
'uses' => $controller.'@showDetailsRow', |
36
|
|
|
'operation' => 'list', |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
43
|
|
|
*/ |
44
|
|
|
protected function setupListDefaults() |
45
|
|
|
{ |
46
|
|
|
$this->crud->allowAccess('list'); |
47
|
|
|
|
48
|
|
|
LifecycleHook::hookInto('list:before_setup', function () { |
|
|
|
|
49
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
50
|
|
|
$this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute()); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Display all rows in the database for this entity. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Contracts\View\View |
58
|
|
|
*/ |
59
|
|
|
public function index() |
60
|
|
|
{ |
61
|
|
|
$this->crud->hasAccessOrFail('list'); |
62
|
|
|
|
63
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
64
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? mb_ucfirst($this->crud->entity_name_plural); |
65
|
|
|
$this->data['controller'] = get_class($this); |
66
|
|
|
|
67
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
68
|
|
|
return view($this->crud->getListView(), $this->data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The search function that is called by the data table. |
73
|
|
|
* |
74
|
|
|
* @return array JSON Array of cells in HTML form. |
75
|
|
|
*/ |
76
|
|
|
public function search() |
77
|
|
|
{ |
78
|
|
|
$this->crud->hasAccessOrFail('list'); |
79
|
|
|
|
80
|
|
|
// If there's a config closure in the cache for this CRUD, run that configuration closure. |
81
|
|
|
// This is done in order to allow the developer to configure the datatable component. |
82
|
|
|
$this->applyCachedDatatableSetup(); |
83
|
|
|
|
84
|
|
|
$this->crud->applyUnappliedFilters(); |
85
|
|
|
|
86
|
|
|
$start = (int) request()->input('start'); |
87
|
|
|
$length = (int) request()->input('length'); |
88
|
|
|
$search = request()->input('search'); |
89
|
|
|
|
90
|
|
|
// check if length is allowed by developer |
91
|
|
|
if ($length && ! in_array($length, $this->crud->getPageLengthMenu()[0])) { |
92
|
|
|
return response()->json([ |
|
|
|
|
93
|
|
|
'error' => 'Unknown page length.', |
94
|
|
|
], 400); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// if a search term was present |
98
|
|
|
if ($search && $search['value'] ?? false) { |
99
|
|
|
// filter the results accordingly |
100
|
|
|
$this->crud->applySearchTerm($search['value']); |
101
|
|
|
} |
102
|
|
|
// start the results according to the datatables pagination |
103
|
|
|
if ($start) { |
104
|
|
|
$this->crud->skip($start); |
105
|
|
|
} |
106
|
|
|
// limit the number of results according to the datatables pagination |
107
|
|
|
if ($length) { |
108
|
|
|
$this->crud->take($length); |
109
|
|
|
} |
110
|
|
|
// overwrite any order set in the setup() method with the datatables order |
111
|
|
|
$this->crud->applyDatatableOrder(); |
112
|
|
|
|
113
|
|
|
$entries = $this->crud->getEntries(); |
114
|
|
|
$requestTotalEntryCount = request()->get('totalEntryCount') ? (int) request()->get('totalEntryCount') : null; |
115
|
|
|
// if show entry count is disabled we use the "simplePagination" technique to move between pages. |
116
|
|
|
if ($this->crud->getOperationSetting('showEntryCount')) { |
117
|
|
|
$totalEntryCount = (int) ($requestTotalEntryCount ?: $this->crud->getTotalQueryCount()); |
118
|
|
|
$filteredEntryCount = $this->crud->getFilteredQueryCount() ?? $totalEntryCount; |
119
|
|
|
} else { |
120
|
|
|
$totalEntryCount = $length; |
121
|
|
|
$entryCount = $entries->count(); |
122
|
|
|
$filteredEntryCount = $entryCount < $length ? $entryCount : $length + $start + 1; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// store the totalEntryCount in CrudPanel so that multiple blade files can access it |
126
|
|
|
$this->crud->setOperationSetting('totalEntryCount', $totalEntryCount); |
127
|
|
|
|
128
|
|
|
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalEntryCount, $filteredEntryCount, $start); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Apply the cached datatable setup configuration directly using DatatableCache. |
133
|
|
|
* |
134
|
|
|
* @return bool Whether the cached setup was successfully applied |
135
|
|
|
*/ |
136
|
|
|
protected function applyCachedDatatableSetup() |
137
|
|
|
{ |
138
|
|
|
return DatatableCache::instance()->applyFromRequest($this->crud); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
143
|
|
|
* It defaults to showing some dummy text. |
144
|
|
|
* |
145
|
|
|
* @return \Illuminate\Contracts\View\View |
146
|
|
|
*/ |
147
|
|
|
public function showDetailsRow($id) |
148
|
|
|
{ |
149
|
|
|
$this->crud->hasAccessOrFail('list'); |
150
|
|
|
|
151
|
|
|
// get entry ID from Request (makes sure its the last ID for nested resources) |
152
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
153
|
|
|
|
154
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
|
|
|
|
155
|
|
|
$this->data['crud'] = $this->crud; |
156
|
|
|
|
157
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
158
|
|
|
return view($this->crud->getDetailsRowView(), $this->data); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|