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
|
|
|
if (! method_exists($this, 'setupShowOperation')) { |
37
|
|
|
$this->autoSetupShowOperation(); |
38
|
|
|
} |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$this->crud->operation('list', function () { |
42
|
|
|
$this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning'); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
$this->crud->operation(['create', 'update'], function () { |
46
|
|
|
$this->crud->addSaveAction([ |
47
|
|
|
'name' => 'save_and_preview', |
48
|
|
|
'visible' => function ($crud) { |
49
|
|
|
return $crud->hasAccess('show'); |
50
|
|
|
}, |
51
|
|
|
'redirect' => function ($crud, $request, $itemId = null) { |
52
|
|
|
$itemId = $itemId ?: $request->input('id'); |
53
|
|
|
$redirectUrl = $crud->route.'/'.$itemId.'/show'; |
54
|
|
|
if ($request->has('_locale')) { |
55
|
|
|
$redirectUrl .= '?_locale='.$request->input('_locale'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $redirectUrl; |
59
|
|
|
}, |
60
|
|
|
'button_text' => trans('backpack::crud.save_action_save_and_preview'), |
61
|
|
|
]); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Display the specified resource. |
67
|
|
|
* |
68
|
|
|
* @param int $id |
69
|
|
|
* @return \Illuminate\Contracts\View\View |
70
|
|
|
*/ |
71
|
|
|
public function show($id) |
72
|
|
|
{ |
73
|
|
|
$this->crud->hasAccessOrFail('show'); |
74
|
|
|
|
75
|
|
|
// get entry ID from Request (makes sure its the last ID for nested resources) |
76
|
|
|
$id = $this->crud->getCurrentEntryId() ?? $id; |
77
|
|
|
|
78
|
|
|
// get the info for that entry (include softDeleted items if the trait is used) |
79
|
|
|
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) { |
80
|
|
|
$this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id); |
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
$this->data['entry'] = $this->crud->getEntryWithLocale($id); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->data['crud'] = $this->crud; |
86
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name; |
87
|
|
|
|
88
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
89
|
|
|
return view($this->crud->getShowView(), $this->data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Default behaviour for the Show Operation, in case none has been |
94
|
|
|
* provided by including a setupShowOperation() method in the CrudController. |
95
|
|
|
*/ |
96
|
|
|
protected function autoSetupShowOperation() |
97
|
|
|
{ |
98
|
|
|
// guess which columns to show, from the database table |
99
|
|
|
if ($this->crud->get('show.setFromDb')) { |
100
|
|
|
$this->crud->setFromDb(false, true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// if the model has timestamps, add columns for created_at and updated_at |
104
|
|
|
if ($this->crud->get('show.timestamps') && $this->crud->model->usesTimestamps()) { |
105
|
|
|
if ($this->crud->model->getCreatedAtColumn()) { |
106
|
|
|
$this->crud->column($this->crud->model->getCreatedAtColumn())->type('datetime'); |
107
|
|
|
} |
108
|
|
|
if ($this->crud->model->getUpdatedAtColumn()) { |
109
|
|
|
$this->crud->column($this->crud->model->getUpdatedAtColumn())->type('datetime'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// if the model has SoftDeletes, add column for deleted_at |
114
|
|
|
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) { |
115
|
|
|
$this->crud->column($this->crud->model->getDeletedAtColumn())->type('datetime'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// remove the columns that usually don't make sense inside the Show operation |
119
|
|
|
$this->removeColumnsThatDontBelongInsideShowOperation(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function removeColumnsThatDontBelongInsideShowOperation() |
123
|
|
|
{ |
124
|
|
|
// cycle through columns |
125
|
|
|
foreach ($this->crud->columns() as $key => $column) { |
126
|
|
|
// remove any autoset relationship columns |
127
|
|
|
if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) { |
128
|
|
|
$this->crud->removeColumn($column['key']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// remove any autoset table columns |
132
|
|
|
if ($column['type'] == 'table' && array_key_exists('autoset', $column) && $column['autoset']) { |
133
|
|
|
$this->crud->removeColumn($column['key']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// remove the row_number column, since it doesn't make sense in this context |
137
|
|
|
if ($column['type'] == 'row_number') { |
138
|
|
|
$this->crud->removeColumn($column['key']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// remove columns that have visibleInShow set as false |
142
|
|
|
if (isset($column['visibleInShow'])) { |
143
|
|
|
if ((is_callable($column['visibleInShow']) && $column['visibleInShow']($this->data['entry']) === false) || $column['visibleInShow'] === false) { |
144
|
|
|
$this->crud->removeColumn($column['key']); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// remove the character limit on columns that take it into account |
149
|
|
|
if (in_array($column['type'], ['text', 'email', 'model_function', 'model_function_attribute', 'phone', 'row_number', 'select'])) { |
150
|
|
|
$this->crud->modifyColumn($column['key'], ['limit' => ($column['limit'] ?? 999)]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// remove bulk actions columns |
155
|
|
|
$this->crud->removeColumns(['blank_first_column', 'bulk_actions']); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|