1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Admin\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Eloquent\Group; |
6
|
|
|
use App\Models\Eloquent\User; |
7
|
|
|
use App\Http\Controllers\Controller; |
8
|
|
|
use Encore\Admin\Controllers\HasResourceActions; |
9
|
|
|
use Encore\Admin\Form; |
10
|
|
|
use Encore\Admin\Grid; |
11
|
|
|
use Encore\Admin\Layout\Content; |
12
|
|
|
use Encore\Admin\Show; |
13
|
|
|
use Illuminate\Support\MessageBag; |
14
|
|
|
use App\Models\Eloquent\GroupMember; |
15
|
|
|
|
16
|
|
|
class GroupController 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('Groups') |
30
|
|
|
->description('all groups') |
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('Group Detail') |
45
|
|
|
->description('the detail of groups') |
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('Edit Group') |
60
|
|
|
->description('edit the detail of groups') |
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('Create New Group') |
74
|
|
|
->description('create a new group') |
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 Group); |
86
|
|
|
$grid->column('gid', "ID")->sortable(); |
87
|
|
|
$grid->column("gcode", "Group Code"); |
88
|
|
|
$grid->img("Focus Image")->display(function($url) { |
89
|
|
|
return '<img src="'.$url.'" style="max-width:200px;max-height:200px" class="img img-thumbnail">'; |
90
|
|
|
}); |
91
|
|
|
$grid->name("Name")->editable(); |
92
|
|
|
$grid->public("Publicity")->display(function($public) { |
93
|
|
|
return $public ? "Public" : "Private"; |
94
|
|
|
}); |
95
|
|
|
$grid->verified("Verified")->display(function($verified) { |
96
|
|
|
return $verified ? "Yes" : "No"; |
97
|
|
|
}); |
98
|
|
|
$grid->join_policy("Join Policy"); |
99
|
|
|
// $grid->custom_icon("Custom Icon")->image(); |
100
|
|
|
// $grid->custom_title("Custom Title"); |
101
|
|
|
$grid->filter(function(Grid\Filter $filter) { |
102
|
|
|
$filter->like('gcode'); |
103
|
|
|
$filter->like('name'); |
104
|
|
|
}); |
105
|
|
|
return $grid; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Make a show builder. |
110
|
|
|
* |
111
|
|
|
* @param mixed $id |
112
|
|
|
* @return Show |
113
|
|
|
*/ |
114
|
|
|
protected function detail($id) |
115
|
|
|
{ |
116
|
|
|
$show=new Show(Group::findOrFail($id)); |
117
|
|
|
return $show; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Make a form builder. |
122
|
|
|
* |
123
|
|
|
* @return Form |
124
|
|
|
*/ |
125
|
|
|
protected function form() |
126
|
|
|
{ |
127
|
|
|
$form=new Form(new Group); |
128
|
|
|
$form->model()->makeVisible('password'); |
129
|
|
|
$form->tab('Basic', function(Form $form) { |
130
|
|
|
$form->text('gcode')->rules('required|alpha_dash|min:3|max:50'); |
131
|
|
|
$form->text('name')->rules('required|min:3|max:50'); |
132
|
|
|
$form->switch('public')->default(true); |
133
|
|
|
$form->textarea('description')->rules('nullable|max:60000'); |
134
|
|
|
$form->select('join_policy', 'Join Policy')->options([ |
135
|
|
|
0 => "Cannot Join", |
136
|
|
|
1 => "Invite Only", |
137
|
|
|
2 => "Apply Only", |
138
|
|
|
3 => "Invite & Apply" |
139
|
|
|
])->default(1); |
140
|
|
|
$form->image('img', 'Custom Group Focus Image')->uniqueName()->move("static/img/group"); |
141
|
|
|
if ($form->isCreating()) { |
142
|
|
|
$form->select('leader_uid', 'Group Leader')->options(function ($id) { |
143
|
|
|
$user = User::find($id); |
144
|
|
|
if ($user) { |
145
|
|
|
return [$user->id => $user->readable_name]; |
146
|
|
|
} |
147
|
|
|
})->config('minimumInputLength', 4)->ajax(route('admin.api.users'))->required(); |
148
|
|
|
} |
149
|
|
|
$form->ignore(['leader_uid']); |
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
|
|
|
$gcode=$form->gcode; |
159
|
|
|
$g=Group::where('gcode', $gcode)->first(); |
160
|
|
|
//check gcode has been token. |
161
|
|
|
$gid=$form->model()->gid ?? null; |
162
|
|
|
if (!empty($gcode) && !blank($g) && $g->gid!=$gid) { |
163
|
|
|
return $err('Gcode has been token', 'Error occur.'); |
164
|
|
|
} |
165
|
|
|
}); |
166
|
|
|
$form->saved(function(Form $form) { |
167
|
|
|
if ($form->isCreating()) { |
168
|
|
|
$form->model()->members()->saveMany([new GroupMember([ |
169
|
|
|
'gid' => $form->model()->gid, |
170
|
|
|
'uid' => request('leader_uid'), |
171
|
|
|
'role' => 3, |
172
|
|
|
'ranking' => 1500, |
173
|
|
|
])]); |
174
|
|
|
} |
175
|
|
|
}); |
176
|
|
|
}); |
177
|
|
|
return $form; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|