1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Admin\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Eloquent\Dojo\DojoPass; |
6
|
|
|
use App\Models\Eloquent\Dojo\Dojo; |
7
|
|
|
use App\Models\Eloquent\User; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use Illuminate\Support\MessageBag; |
10
|
|
|
use Encore\Admin\Controllers\HasResourceActions; |
11
|
|
|
use Encore\Admin\Form; |
12
|
|
|
use Encore\Admin\Grid; |
13
|
|
|
use Encore\Admin\Layout\Content; |
14
|
|
|
use Encore\Admin\Show; |
15
|
|
|
|
16
|
|
|
class DojoPassesController extends Controller |
17
|
|
|
{ |
18
|
|
|
use HasResourceActions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Index interface. |
22
|
|
|
* |
23
|
|
|
* @param Content $content |
24
|
|
|
* @return Content |
25
|
|
|
*/ |
26
|
|
|
public function index(Content $content) |
27
|
|
|
{ |
28
|
|
|
return $content |
29
|
|
|
->header(__('admin.dojopasses.index.header')) |
|
|
|
|
30
|
|
|
->description(__('admin.dojopasses.index.description')) |
|
|
|
|
31
|
|
|
->body($this->grid()->render()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Show interface. |
36
|
|
|
* |
37
|
|
|
* @param mixed $id |
38
|
|
|
* @param Content $content |
39
|
|
|
* @return Content |
40
|
|
|
*/ |
41
|
|
|
public function show($id, Content $content) |
42
|
|
|
{ |
43
|
|
|
return $content |
44
|
|
|
->header(__('admin.dojopasses.show.header')) |
|
|
|
|
45
|
|
|
->description(__('admin.dojopasses.show.description')) |
|
|
|
|
46
|
|
|
->body($this->detail($id)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Edit interface. |
51
|
|
|
* |
52
|
|
|
* @param mixed $id |
53
|
|
|
* @param Content $content |
54
|
|
|
* @return Content |
55
|
|
|
*/ |
56
|
|
|
public function edit($id, Content $content) |
57
|
|
|
{ |
58
|
|
|
return $content |
59
|
|
|
->header(__('admin.dojopasses.edit.header')) |
|
|
|
|
60
|
|
|
->description(__('admin.dojopasses.edit.description')) |
|
|
|
|
61
|
|
|
->body($this->form()->edit($id)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create interface. |
66
|
|
|
* |
67
|
|
|
* @param Content $content |
68
|
|
|
* @return Content |
69
|
|
|
*/ |
70
|
|
|
public function create(Content $content) |
71
|
|
|
{ |
72
|
|
|
return $content |
73
|
|
|
->header(__('admin.dojopasses.create.header')) |
|
|
|
|
74
|
|
|
->description(__('admin.dojopasses.create.description')) |
|
|
|
|
75
|
|
|
->body($this->form()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Make a grid builder. |
80
|
|
|
* |
81
|
|
|
* @return Grid |
82
|
|
|
*/ |
83
|
|
|
protected function grid() |
84
|
|
|
{ |
85
|
|
|
$grid=new Grid(new DojoPass); |
86
|
|
|
$grid->column('id', "ID")->sortable(); |
87
|
|
|
$grid->column("dojo_name", __('admin.dojopasses.dojo'))->display(function() { |
|
|
|
|
88
|
|
|
return $this->dojo->name; |
|
|
|
|
89
|
|
|
}); |
90
|
|
|
$grid->column("user_readable", __('admin.dojopasses.user'))->display(function() { |
91
|
|
|
return $this->user->readable_name; |
|
|
|
|
92
|
|
|
}); |
93
|
|
|
$grid->column('updated_at', __('admin.dojopasses.updated_at')); |
94
|
|
|
|
95
|
|
|
$grid->filter(function(Grid\Filter $filter) { |
96
|
|
|
$filter->disableIdFilter(); |
97
|
|
|
$filter->column(6, function($filter) { |
98
|
|
|
$filter->equal('dojo_id', __('admin.dojopasses.dojo'))->select(Dojo::all()->pluck('name', 'id')); |
99
|
|
|
}); |
100
|
|
|
$filter->column(6, function($filter) { |
101
|
|
|
$filter->equal('user_id', __('admin.dojopasses.user'))->select(function($id) { |
102
|
|
|
$user=User::find($id); |
103
|
|
|
if ($user) { |
104
|
|
|
return [$user->id => $user->readable_name]; |
105
|
|
|
} |
106
|
|
|
})->config('minimumInputLength', 4)->ajax(route('admin.api.users')); |
107
|
|
|
}); |
108
|
|
|
}); |
109
|
|
|
return $grid; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Make a show builder. |
114
|
|
|
* |
115
|
|
|
* @param mixed $id |
116
|
|
|
* @return Show |
117
|
|
|
*/ |
118
|
|
|
protected function detail($id) |
119
|
|
|
{ |
120
|
|
|
$show=new Show(DojoPass::findOrFail($id)); |
121
|
|
|
$show->field('id', "ID"); |
122
|
|
|
$show->field("dojo_name", __('admin.dojopasses.dojo'))->as(function() { |
|
|
|
|
123
|
|
|
return $this->dojo->name; |
|
|
|
|
124
|
|
|
}); |
125
|
|
|
$show->field("user_readable", __('admin.dojopasses.user'))->as(function() { |
126
|
|
|
return $this->user->readable_name; |
|
|
|
|
127
|
|
|
}); |
128
|
|
|
$show->field('updated_at', __('admin.dojopasses.updated_at')); |
129
|
|
|
return $show; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Make a form builder. |
134
|
|
|
* |
135
|
|
|
* @return Form |
136
|
|
|
*/ |
137
|
|
|
protected function form() |
138
|
|
|
{ |
139
|
|
|
$form=new Form(new DojoPass); |
140
|
|
|
$form->tab('Basic', function(Form $form) { |
141
|
|
|
$form->display('id', 'ID'); |
142
|
|
|
$form->select('dojo_id', __('admin.dojopasses.dojo'))->options(Dojo::all()->pluck('name', 'id'))->rules('required'); |
143
|
|
|
$form->select('user_id', __('admin.dojopasses.user'))->options(function($id) { |
144
|
|
|
$user=User::find($id); |
145
|
|
|
if ($user) { |
146
|
|
|
return [$user->id => $user->readable_name]; |
147
|
|
|
} |
148
|
|
|
})->config('minimumInputLength', 4)->ajax(route('admin.api.users'))->rules('required'); |
149
|
|
|
|
150
|
|
|
$form->saving(function(Form $form) { |
151
|
|
|
$err=function($msg, $title='Error occur.') { |
152
|
|
|
$error=new MessageBag([ |
153
|
|
|
'title' => $title, |
154
|
|
|
'message' => $msg, |
155
|
|
|
]); |
156
|
|
|
return back()->with(compact('error')); |
157
|
|
|
}; |
158
|
|
|
$user_id=$form->user_id; |
159
|
|
|
$dojo_id=$form->dojo_id; |
160
|
|
|
$pass=DojoPass::where([ |
161
|
|
|
"dojo_id" => $dojo_id, |
162
|
|
|
"user_id" => $user_id, |
163
|
|
|
])->first(); |
164
|
|
|
|
165
|
|
|
$pass_id=$form->model()->id ?? null; |
166
|
|
|
if (!blank($pass) && $pass->id!=$pass_id) { |
167
|
|
|
return $err('User has passed this dojo', 'Error occured.'); |
168
|
|
|
} |
169
|
|
|
}); |
170
|
|
|
}); |
171
|
|
|
return $form; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|