1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Frontend\Project; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\Frontend\Project\ProjectUserRequest as StoreRequest; |
6
|
|
|
use App\Http\Requests\Frontend\Project\ProjectUserRequest as UpdateRequest; |
7
|
|
|
use App\Http\Traits\UsesPolicies; |
8
|
|
|
use App\Models\Access\User\User; |
9
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
10
|
|
|
use App\Models\Project; |
11
|
|
|
use App\Models\ProjectUser; |
12
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ProjectUserCrudController. |
18
|
|
|
*/ |
19
|
|
|
class ProjectUserCrudController extends CrudController |
20
|
|
|
{ |
21
|
|
|
use UsesPolicies; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function setup() |
27
|
|
|
{ |
28
|
|
|
$project_id = Route::current()->parameter('project_id') ?? Route::current()->parameter('project'); |
29
|
|
|
$this->crud->setEntityNameStrings('Project Member', 'Project Members'); |
30
|
|
|
|
31
|
|
|
if ($project_id) { |
32
|
|
|
$project = Project::findOrFail($project_id); |
33
|
|
|
// $this->crud->addClause('where', 'agent_id', '==', $project_id); |
34
|
|
|
// } |
35
|
|
|
if (request()->route()->getActionMethod() === 'search') { |
36
|
|
|
ProjectUser::addGlobalScope('project_id', |
37
|
|
|
function (Builder $builder) use ($project_id) { |
38
|
|
|
$builder->where('agent_id', $project_id); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
$this->data['parent'] = $project->title . ' Project'; |
42
|
|
|
$this->data['project'] = $project; |
43
|
|
|
$this->crud->setEntityNameStrings('Member', 'Members'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->crud->setModel(ProjectUser::class); |
47
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix') . '/projects/' . $project_id . '/members'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
|-------------------------------------------------------------------------- |
51
|
|
|
| BASIC CRUD INFORMATION |
52
|
|
|
|-------------------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
$this->crud->setFromDb(); |
56
|
|
|
|
57
|
|
|
// ------ CRUD FIELDS |
58
|
|
|
$this->crud->removeFields(array_keys($this->crud->create_fields), 'both'); |
59
|
|
|
|
60
|
|
|
$this->crud->addField([ |
61
|
|
|
'name' => 'user_id', |
62
|
|
|
'type' => 'hidden', |
63
|
|
|
], |
64
|
|
|
'update'); |
65
|
|
|
$this->crud->addField([ |
66
|
|
|
'name' => 'agent_id', |
67
|
|
|
'type' => 'hidden', |
68
|
|
|
'default' => $project_id, |
69
|
|
|
]); |
70
|
|
|
$this->crud->addField([ |
71
|
|
|
'name' => 'is_registrar_for', |
72
|
|
|
'type' => 'hidden', |
73
|
|
|
'default' => 1, |
74
|
|
|
]); |
75
|
|
|
$userName = ''; |
76
|
|
|
if (request()->route()->parameter('member') && $this->request->isMethod('GET')) { |
77
|
|
|
$projectUser = ProjectUser::with('user')->find(request()->route()->parameter('member')); |
78
|
|
|
if ($projectUser) { |
79
|
|
|
$user = $projectUser->user; |
80
|
|
|
if ($user) { |
|
|
|
|
81
|
|
|
$userName = User::getCombinedName($user); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
$this->crud->addField([ //plain |
86
|
|
|
'type' => 'custom_html', |
87
|
|
|
'name' => 'member_name', |
88
|
|
|
'value' => '<label>Member</label> <div>' . $userName . '</div>', |
89
|
|
|
], 'update'); |
90
|
|
|
|
91
|
|
|
$projectUsers = isset($project_id) ? ProjectUser::whereAgentId($project_id)->get(['user_id'])->keyBy('user_id') : []; |
92
|
|
|
|
93
|
|
|
$this->crud->addField([ // Select2 |
94
|
|
|
'label' => 'Member', |
95
|
|
|
'type' => 'select2_from_array', |
96
|
|
|
'name' => 'user_id', |
97
|
|
|
'options' => User::getUsersForSelect($projectUsers), |
98
|
|
|
'allows_null' => true, |
99
|
|
|
], 'create'); |
100
|
|
|
|
101
|
|
|
$this->crud->addField([ |
102
|
|
|
'name' => 'authorized_as', |
103
|
|
|
'label' => 'Authorized as', // the input label |
104
|
|
|
'default' => ProjectUser::AUTH_VIEWER, |
105
|
|
|
'type' => 'radio', |
106
|
|
|
'options' => [ // the key will be stored in the db, the value will be shown as label; |
107
|
|
|
ProjectUser::AUTH_VIEWER => 'Viewer ...canʼt maintain any of the projectʼs resources.', |
108
|
|
|
ProjectUser::AUTH_LANGUAGE_MAINTAINER => 'Language Maintainer ...can only maintain the projectʼs languages listed below', |
109
|
|
|
ProjectUser::AUTH_MAINTAINER => 'Project Maintainer ...can maintain any of this projectʼs languages', |
110
|
|
|
ProjectUser::AUTH_ADMIN => 'Project Administrator ...can do anything related to this project', |
111
|
|
|
], |
112
|
|
|
]); |
113
|
|
|
$this->crud->addField([ |
114
|
|
|
'name' => 'languages', |
115
|
|
|
'label' => 'Languages', |
116
|
|
|
'type' => 'select2_from_array', |
117
|
|
|
'allows_null' => false, |
118
|
|
|
'default' => [config('app.locale')], |
119
|
|
|
'allows_multiple' => true, |
120
|
|
|
'options' => $project->listLanguagesForSelect(), |
|
|
|
|
121
|
|
|
'hint' => 'All of the languages that this member is authorized to maintain<br />This can be set for each individual resource as well.', |
122
|
|
|
'attributes' => [ |
123
|
|
|
'placeholder' => 'Select one or more language codes', |
124
|
|
|
], |
125
|
|
|
]); |
126
|
|
|
$this->crud->addField([ |
127
|
|
|
'name' => 'default_language', |
128
|
|
|
'label' => 'Default Language', |
129
|
|
|
'type' => 'select2_from_array', |
130
|
|
|
'allows_null' => false, |
131
|
|
|
'default' => config('app.locale'), |
132
|
|
|
'options' => $project->listLanguagesForSelect(), |
133
|
|
|
'hint' => 'When creating new resources, this will be default language for this maintainer', |
134
|
|
|
'attributes' => [ |
135
|
|
|
'placeholder' => 'Select a language code. ', |
136
|
|
|
], |
137
|
|
|
]); |
138
|
|
|
// $this->crud->addFields($array_of_arrays, 'update/create/both'); |
139
|
|
|
// $this->crud->removeField('name', 'update/create/both'); |
140
|
|
|
$this->crud->removeFields(['is_admin_for', 'is_maintainer_for'], 'update/create/both'); |
141
|
|
|
|
142
|
|
|
// ------ CRUD COLUMNS |
143
|
|
|
$this->crud->addColumn([ |
144
|
|
|
'label' => 'Member Name', |
145
|
|
|
'type' => 'select', |
146
|
|
|
'name' => 'name', |
147
|
|
|
'entity' => 'user', |
148
|
|
|
'attribute' => 'name', |
149
|
|
|
'searchLogic' => function ($query, $column, $searchTerm) { |
150
|
|
|
$query->orWhereHas('user', |
151
|
|
|
function ($q) use ($column, $searchTerm) { |
|
|
|
|
152
|
|
|
$q->where('first_name', 'like', '%' . $searchTerm . '%') |
153
|
|
|
->orWhere('last_name', 'like', '%' . $searchTerm . '%'); |
154
|
|
|
}); |
155
|
|
|
}, |
156
|
|
|
])->afterColumn('user_id'); |
157
|
|
|
$this->crud->addColumn([ |
158
|
|
|
'label' => 'Member Nickname', |
159
|
|
|
'type' => 'select', |
160
|
|
|
'name' => 'nickname', |
161
|
|
|
'entity' => 'user', |
162
|
|
|
'attribute' => 'nickname', |
163
|
|
|
'searchLogic' => function ($query, $column, $searchTerm) { |
164
|
|
|
$query->orWhereHas('user', |
165
|
|
|
function ($q) use ($column, $searchTerm) { |
|
|
|
|
166
|
|
|
$q->where('nickname', 'like', '%' . $searchTerm . '%'); |
167
|
|
|
}); |
168
|
|
|
}, |
169
|
|
|
])->afterColumn('user_id'); |
170
|
|
|
// $this->crud->addColumns(); // add multiple columns, at the end of the stack |
171
|
|
|
// $this->crud->removeColumn('column_name'); // remove a column from the stack |
172
|
|
|
$this->crud->removeColumns(['is_admin_for', 'is_maintainer_for', 'current_language', 'id', 'created_at', 'deleted_at', 'updated_at']); // remove an array of columns from the stack |
173
|
|
|
$this->crud->setColumnDetails('authorized_as', |
174
|
|
|
[ // radio |
175
|
|
|
'label' => 'Authorized as', // the input label |
176
|
|
|
'type' => 'radio', |
177
|
|
|
'options' => [ // the key will be stored in the db, the value will be shown as label; |
178
|
|
|
ProjectUser::AUTH_VIEWER => 'Viewer', |
179
|
|
|
ProjectUser::AUTH_LANGUAGE_MAINTAINER => 'Language Maintainer', |
180
|
|
|
ProjectUser::AUTH_MAINTAINER => 'Project Maintainer', |
181
|
|
|
ProjectUser::AUTH_ADMIN => 'Project Administrator', |
182
|
|
|
], |
183
|
|
|
]); |
184
|
|
|
$this->crud->setColumnDetails('languages', |
185
|
|
|
[ |
186
|
|
|
'label' => 'Languages', // Table column heading |
187
|
|
|
'type' => 'model_function', |
188
|
|
|
'function_name' => 'showLanguagesCommaDelimited', |
189
|
|
|
]); |
190
|
|
|
$this->crud->setColumnDetails('is_registrar_for', |
191
|
|
|
[ |
192
|
|
|
'label' => 'Registrar', |
193
|
|
|
'type' => 'boolean', |
194
|
|
|
]); |
195
|
|
|
$this->crud->setColumnsDetails(['agent_id', 'default_language'], ['list' => false]); |
196
|
|
|
$this->crud->setColumnsDetails(['agent_id'], ['show' => false]); |
197
|
|
|
|
198
|
|
|
// ------ CRUD BUTTONS |
199
|
|
|
$this->crud->initButtons(); |
200
|
|
|
// possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others; |
201
|
|
|
// $this->crud->addButton($stack, $name, $type, $content, $position); // add a button; possible types are: view, model_function |
202
|
|
|
// $this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model |
203
|
|
|
// $this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons |
204
|
|
|
// $this->crud->removeButton($name); |
205
|
|
|
// $this->crud->removeButtonFromStack($name, $stack); |
206
|
|
|
// $this->crud->removeAllButtons(); |
207
|
|
|
// $this->crud->removeAllButtonsFromStack('line'); |
208
|
|
|
|
209
|
|
|
// ------ CRUD ACCESS |
210
|
|
|
$this->authorizeAll(); |
211
|
|
|
//this authorizes access for not-logged-in users |
212
|
|
|
$this->crud->allowAccess(['list', 'show']); |
213
|
|
|
|
214
|
|
|
// ------ CRUD REORDER |
215
|
|
|
// $this->crud->enableReorder('label_name', MAX_TREE_LEVEL); |
216
|
|
|
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder'); |
217
|
|
|
|
218
|
|
|
// ------ CRUD DETAILS ROW |
219
|
|
|
// $this->crud->enableDetailsRow(); |
220
|
|
|
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row'); |
221
|
|
|
// NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php |
222
|
|
|
|
223
|
|
|
// ------ REVISIONS |
224
|
|
|
// You also need to use \Venturecraft\Revisionable\RevisionableTrait; |
225
|
|
|
// Please check out: https://laravel-backpack.readme.io/docs/crud#revisions |
226
|
|
|
// $this->crud->allowAccess('revisions'); |
227
|
|
|
|
228
|
|
|
// ------ AJAX TABLE VIEW |
229
|
|
|
// Please note the drawbacks of this though: |
230
|
|
|
// - 1-n and n-n columns are not searchable |
231
|
|
|
// - date and datetime columns won't be sortable anymore |
232
|
|
|
// $this->crud->enableAjaxTable(); |
233
|
|
|
|
234
|
|
|
// ------ DATATABLE EXPORT BUTTONS |
235
|
|
|
// Show export to PDF, CSV, XLS and Print buttons on the table view. |
236
|
|
|
// Does not work well with AJAX datatables. |
237
|
|
|
// $this->crud->enableExportButtons(); |
238
|
|
|
|
239
|
|
|
// ------ ADVANCED QUERIES |
240
|
|
|
// $this->crud->addClause('active'); |
241
|
|
|
//$this->crud->addClause('where', 'agent_id', '==', $project_id); |
242
|
|
|
// $this->crud->addClause('where', 'name', '==', 'car'); |
243
|
|
|
// $this->crud->addClause('whereName', 'car'); |
244
|
|
|
// $this->crud->addClause('whereHas', 'posts', function($query) { |
245
|
|
|
// $query->activePosts(); |
246
|
|
|
// }); |
247
|
|
|
// $this->crud->addClause('withoutGlobalScopes'); |
248
|
|
|
// $this->crud->addClause('withoutGlobalScope', VisibleScope::class); |
249
|
|
|
$this->crud->with('user'); // eager load relationships |
250
|
|
|
// $this->crud->orderBy(); |
251
|
|
|
// $this->crud->groupBy(); |
252
|
|
|
// $this->crud->limit(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param UpdateRequest $request |
257
|
|
|
* |
258
|
|
|
* @return \Illuminate\Http\RedirectResponse |
259
|
|
|
*/ |
260
|
|
|
public function store(StoreRequest $request) |
261
|
|
|
{ |
262
|
|
|
// your additional operations before save here |
263
|
|
|
//registrar is always 0 when project member is created this way |
264
|
|
|
$request->merge(['is_registrar_for' => '0']); |
265
|
|
|
$this->request->merge(['is_registrar_for' => '0']); |
266
|
|
|
$redirect_location = parent::storeCrud(); |
267
|
|
|
// your additional operations after save here |
268
|
|
|
// use $this->data['entry'] or $this->crud->entry |
269
|
|
|
return $redirect_location; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param UpdateRequest $request |
274
|
|
|
* |
275
|
|
|
* @return \Illuminate\Http\RedirectResponse |
276
|
|
|
*/ |
277
|
|
|
public function update(UpdateRequest $request) |
278
|
|
|
{ |
279
|
|
|
// your additional operations before save here |
280
|
|
|
$redirect_location = parent::updateCrud(); |
281
|
|
|
// your additional operations after save here |
282
|
|
|
// use $this->data['entry'] or $this->crud->entry |
283
|
|
|
return $redirect_location; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function destroy($id) |
287
|
|
|
{ |
288
|
|
|
return parent::destroy($this->request->member); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function edit($id) |
292
|
|
|
{ |
293
|
|
|
return parent::edit($this->request->member); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function show($id) |
297
|
|
|
{ |
298
|
|
|
return parent::show($this->request->member); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|