1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
trait ListEntries |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Display all rows in the database for this entity. |
9
|
|
|
* |
10
|
|
|
* @return Response |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
public function index() |
13
|
|
|
{ |
14
|
|
|
$this->crud->hasAccessOrFail('list'); |
|
|
|
|
15
|
|
|
|
16
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
17
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
18
|
|
|
|
19
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
20
|
|
|
return view($this->crud->getListView(), $this->data); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The search function that is called by the data table. |
25
|
|
|
* |
26
|
|
|
* @return JSON Array of cells in HTML form. |
27
|
|
|
*/ |
28
|
|
|
public function search() |
29
|
|
|
{ |
30
|
|
|
$this->crud->hasAccessOrFail('list'); |
31
|
|
|
|
32
|
|
|
$totalRows = $filteredRows = $this->crud->count(); |
33
|
|
|
$startIndex = $this->request->input('start') ?: 0; |
|
|
|
|
34
|
|
|
// if a search term was present |
35
|
|
|
if ($this->request->input('search') && $this->request->input('search')['value']) { |
36
|
|
|
// filter the results accordingly |
37
|
|
|
$this->crud->applySearchTerm($this->request->input('search')['value']); |
38
|
|
|
// recalculate the number of filtered rows |
39
|
|
|
$filteredRows = $this->crud->count(); |
40
|
|
|
} |
41
|
|
|
// start the results according to the datatables pagination |
42
|
|
|
if ($this->request->input('start')) { |
43
|
|
|
$this->crud->skip($this->request->input('start')); |
44
|
|
|
} |
45
|
|
|
// limit the number of results according to the datatables pagination |
46
|
|
|
if ($this->request->input('length')) { |
47
|
|
|
$this->crud->take($this->request->input('length')); |
48
|
|
|
} |
49
|
|
|
// overwrite any order set in the setup() method with the datatables order |
50
|
|
|
if ($this->request->input('order')) { |
51
|
|
|
$column_number = $this->request->input('order')[0]['column']; |
52
|
|
|
$column_direction = $this->request->input('order')[0]['dir']; |
53
|
|
|
$column = $this->crud->findColumnById($column_number); |
54
|
|
|
if ($column['tableColumn']) { |
55
|
|
|
// clear any past orderBy rules |
56
|
|
|
$this->crud->query->getQuery()->orders = null; |
57
|
|
|
// apply the current orderBy rules |
58
|
|
|
$this->crud->orderBy($column['name'], $column_direction); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$entries = $this->crud->getEntries(); |
62
|
|
|
|
63
|
|
|
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows, $startIndex); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
|
|
|
68
|
|
|
* It defaults to showing some dummy text. |
69
|
|
|
* |
70
|
|
|
* It's enabled by: |
71
|
|
|
* - setting: $crud->details_row = true; |
72
|
|
|
* - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
|
|
|
73
|
|
|
* - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function showDetailsRow($id) |
76
|
|
|
{ |
77
|
|
|
$this->crud->hasAccessOrFail('details_row'); |
78
|
|
|
|
79
|
|
|
// get entry ID from Request (makes sure its the last ID for nested resources) |
80
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
81
|
|
|
|
82
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
83
|
|
|
$this->data['crud'] = $this->crud; |
84
|
|
|
|
85
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
86
|
|
|
return view($this->crud->getDetailsRowView(), $this->data); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.