1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
|
7
|
|
|
trait ShowOperation |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Define which routes are needed for this operation. |
11
|
|
|
* |
12
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
13
|
|
|
* @param string $routeName Prefix of the route name. |
14
|
|
|
* @param string $controller Name of the current CrudController. |
15
|
|
|
*/ |
16
|
|
|
protected function setupShowRoutes($segment, $routeName, $controller) |
17
|
|
|
{ |
18
|
|
|
Route::get($segment.'/{id}/show', [ |
19
|
|
|
'as' => $routeName.'.show', |
20
|
|
|
'uses' => $controller.'@show', |
21
|
|
|
'operation' => 'show', |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
27
|
|
|
*/ |
28
|
|
|
protected function setupShowDefaults() |
29
|
|
|
{ |
30
|
|
|
$this->crud->allowAccess('show'); |
31
|
|
|
$this->crud->setOperationSetting('setFromDb', true); |
32
|
|
|
|
33
|
|
|
$this->crud->operation('show', function () { |
34
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$this->crud->operation('list', function () { |
38
|
|
|
$this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning'); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$this->crud->operation(['create', 'update'], function () { |
42
|
|
|
$this->crud->addSaveAction([ |
43
|
|
|
'name' => 'save_and_preview', |
44
|
|
|
'visible' => function ($crud) { |
45
|
|
|
return $crud->hasAccess('show'); |
46
|
|
|
}, |
47
|
|
|
'redirect' => function ($crud, $request, $itemId = null) { |
48
|
|
|
$itemId = $itemId ?: $request->input('id'); |
49
|
|
|
$redirectUrl = $crud->route.'/'.$itemId.'/show'; |
50
|
|
|
if ($request->has('locale')) { |
51
|
|
|
$redirectUrl .= '?locale='.$request->input('locale'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $redirectUrl; |
55
|
|
|
}, |
56
|
|
|
'button_text' => trans('backpack::crud.save_action_save_and_preview'), |
57
|
|
|
]); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Display the specified resource. |
63
|
|
|
* |
64
|
|
|
* @param int $id |
65
|
|
|
* |
66
|
|
|
* @return Response |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function show($id) |
69
|
|
|
{ |
70
|
|
|
$this->crud->hasAccessOrFail('show'); |
71
|
|
|
|
72
|
|
|
// get entry ID from Request (makes sure its the last ID for nested resources) |
73
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
74
|
|
|
|
75
|
|
|
// get the info for that entry |
76
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
|
|
|
|
77
|
|
|
$this->data['crud'] = $this->crud; |
78
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name; |
|
|
|
|
79
|
|
|
|
80
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
81
|
|
|
return view($this->crud->getShowView(), $this->data); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Default behaviour for setupShowOperation, in case none has been |
86
|
|
|
* provided by including a setupShowOperation() method in the CrudController. |
87
|
|
|
* |
88
|
|
|
* @return [type] [description] |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
protected function setupShowOperation() |
91
|
|
|
{ |
92
|
|
|
// guess which columns to show, from the database table |
93
|
|
|
if ($this->crud->get('show.setFromDb')) { |
94
|
|
|
$this->crud->setFromDb(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// if the developer has chosen to include a modifyShowOperation() method |
98
|
|
|
// in his/her CrudController, make sure to call it because they probably want to |
99
|
|
|
// add a new column or remove an autoset column |
100
|
|
|
if (method_exists($this, 'modifyShowOperation')) { |
101
|
|
|
$this->modifyShowOperation(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// remove the columns that usually don't make sense inside the Show operation |
105
|
|
|
$this->removeColumnsThatDontBelongInsideShowOperation(); |
106
|
|
|
|
107
|
|
|
// remove preview button from stack:line |
108
|
|
|
$this->crud->removeButton('show'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function removeColumnsThatDontBelongInsideShowOperation() |
112
|
|
|
{ |
113
|
|
|
// cycle through columns |
114
|
|
|
foreach ($this->crud->columns() as $key => $column) { |
115
|
|
|
|
116
|
|
|
// remove any autoset relationship columns |
117
|
|
|
if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) { |
118
|
|
|
$this->crud->removeColumn($column['key']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// remove any autoset table columns |
122
|
|
|
if ($column['type'] == 'table' && array_key_exists('autoset', $column) && $column['autoset']) { |
123
|
|
|
$this->crud->removeColumn($column['key']); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// remove the row_number column, since it doesn't make sense in this context |
127
|
|
|
if ($column['type'] == 'row_number') { |
128
|
|
|
$this->crud->removeColumn($column['key']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// remove columns that have visibleInShow set as false |
132
|
|
|
if (isset($column['visibleInShow']) && $column['visibleInShow'] == false) { |
133
|
|
|
$this->crud->removeColumn($column['key']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// remove the character limit on columns that take it into account |
137
|
|
|
if (in_array($column['type'], ['text', 'email', 'model_function', 'model_function_attribute', 'phone', 'row_number', 'select'])) { |
138
|
|
|
$this->crud->modifyColumn($column['key'], ['limit' => ($column['limit'] ?? 999)]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// remove bulk actions colums |
143
|
|
|
$this->crud->removeColumns(['blank_first_column', 'bulk_actions']); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|