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