1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Backpack\Documents\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
6
|
|
|
use Webfactor\Laravel\Backpack\Documents\Requests\DocumentRequest as StoreRequest; |
7
|
|
|
use Webfactor\Laravel\Backpack\Documents\Requests\DocumentUpdateRequest as UpdateRequest; |
8
|
|
|
|
9
|
|
|
class DocumentCrudController extends CrudController |
10
|
|
|
{ |
11
|
|
|
protected $types = [ |
12
|
|
|
'plaintext' => [ |
13
|
|
|
'field' => 'textarea', |
14
|
|
|
'attrLabel' => 'textareaAttributes' |
15
|
|
|
], |
16
|
|
|
'html' => [ |
17
|
|
|
'field' => 'summernote', |
18
|
|
|
'attrLabel' => 'options' |
19
|
|
|
], |
20
|
|
|
'md' => [ |
21
|
|
|
'field' => 'simplemde', |
22
|
|
|
'attrLabel' => 'simplemdeAttributesRaw' |
23
|
|
|
], |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public function setup() |
27
|
|
|
{ |
28
|
|
|
/* |
29
|
|
|
|-------------------------------------------------------------------------- |
30
|
|
|
| BASIC CRUD INFORMATION |
31
|
|
|
|-------------------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
$this->crud->setModel(config('webfactor.documents.model_class')); |
34
|
|
|
$this->crud->setRoute(config('webfactor.documents.backend.route_prefix') . '/' . config('webfactor.documents.backend.route')); |
35
|
|
|
$this->crud->setEntityNameStrings(trans('webfactor::documents.entity_name_singular'), |
36
|
|
|
trans('webfactor::documents.entity_name_plural')); |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
|-------------------------------------------------------------------------- |
40
|
|
|
| BASIC CRUD INFORMATION |
41
|
|
|
|-------------------------------------------------------------------------- |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
// ------ CRUD FIELDS |
45
|
|
|
|
46
|
|
|
$this->crud->addField([ |
47
|
|
|
'name' => 'type', |
48
|
|
|
'label' => 'Dokumentenart', |
49
|
|
|
'type' => 'select_from_array', |
50
|
|
|
'options' => $this->getTypeOptions(), |
51
|
|
|
'allows_null' => false, |
52
|
|
|
'allows_multiple' => false, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$this->crud->addField([ |
56
|
|
|
'name' => 'title', |
57
|
|
|
'label' => trans('webfactor::documents.title'), |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->crud->addField([ |
61
|
|
|
'name' => 'body', |
62
|
|
|
'label' => trans('webfactor::documents.body'), |
63
|
|
|
'type' => $this->bodyFieldType(), |
64
|
|
|
$this->bodyFieldAttributesLabel() => $this->bodyFieldAttributes(), |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
// ------ CRUD COLUMNS |
68
|
|
|
|
69
|
|
|
$this->crud->addColumns([ |
70
|
|
|
[ |
71
|
|
|
'name' => 'translated_type', |
72
|
|
|
'label' => trans('webfactor::documents.type'), |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
'name' => 'title', |
76
|
|
|
'label' => trans('webfactor::documents.title'), |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
'name' => 'body', |
80
|
|
|
'label' => trans('webfactor::documents.body'), |
81
|
|
|
], |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
// ------ CRUD BUTTONS |
85
|
|
|
|
86
|
|
|
// ------ CRUD ACCESS |
87
|
|
|
|
88
|
|
|
collect(config('webfactor.documents.access', []))->each(function ($enabled, $key) { |
89
|
|
|
if ($enabled) { |
90
|
|
|
$this->crud->allowAccess($key); |
91
|
|
|
} else { |
92
|
|
|
$this->crud->denyAccess($key); |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
// ------ CRUD REORDER |
97
|
|
|
|
98
|
|
|
// ------ CRUD DETAILS ROW |
99
|
|
|
|
100
|
|
|
// ------ REVISIONS |
101
|
|
|
|
102
|
|
|
// ------ AJAX TABLE VIEW |
103
|
|
|
|
104
|
|
|
// ------ DATATABLE EXPORT BUTTONS |
105
|
|
|
|
106
|
|
|
// ------ ADVANCED QUERIES |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getTypeOptions() |
110
|
|
|
{ |
111
|
|
|
$types = collect(config('webfactor.documents.types')); |
112
|
|
|
|
113
|
|
|
return $types->mapWithKeys(function ($type) { |
114
|
|
|
return [$type => trans('webfactor::documents.types.' . $type)]; |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function bodyFieldType() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->types[config('webfactor.documents.body_type', 'plaintext')]['field']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function bodyFieldAttributesLabel() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return $this->types[config('webfactor.documents.body_type', 'plaintext')]['attrLabel']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function bodyFieldAttributes() |
129
|
|
|
{ |
130
|
|
|
$type = config('webfactor.documents.body_type', 'plaintext'); |
131
|
|
|
return config('webfactor.documents.field_editor_attributes.'.$type, []); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function store(StoreRequest $request) |
135
|
|
|
{ |
136
|
|
|
// your additional operations before save here |
137
|
|
|
$redirect_location = parent::storeCrud($request); |
|
|
|
|
138
|
|
|
// your additional operations after save here |
139
|
|
|
// use $this->data['entry'] or $this->crud->entry |
140
|
|
|
return $redirect_location; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function update(UpdateRequest $request) |
144
|
|
|
{ |
145
|
|
|
// your additional operations before save here |
146
|
|
|
$redirect_location = parent::updateCrud($request); |
|
|
|
|
147
|
|
|
// your additional operations after save here |
148
|
|
|
// use $this->data['entry'] or $this->crud->entry |
149
|
|
|
return $redirect_location; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.