1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Eddy <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace App\Http\Controllers\Admin; |
7
|
|
|
|
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Http\Requests\Admin\ContentRequest; |
10
|
|
|
use App\Model\Admin\Content; |
11
|
|
|
use App\Model\Admin\Entity; |
12
|
|
|
use App\Model\Admin\EntityField; |
13
|
|
|
use App\Repository\Admin\ContentRepository; |
14
|
|
|
use App\Repository\Admin\EntityFieldRepository; |
15
|
|
|
use App\Repository\Admin\EntityRepository; |
16
|
|
|
use Illuminate\Database\QueryException; |
17
|
|
|
use Illuminate\Http\Request; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
use Illuminate\View\View; |
20
|
|
|
|
21
|
|
|
class ContentController extends Controller |
22
|
|
|
{ |
23
|
|
|
protected $formNames = []; |
24
|
|
|
|
25
|
|
|
protected $entity = null; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
parent::__construct(); |
30
|
|
|
$route = request()->route(); |
31
|
|
|
if (is_null($route)) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
$entity = $route->parameter('entity'); |
35
|
|
|
$this->entity = Entity::query()->findOrFail($entity); |
36
|
|
|
ContentRepository::setTable($this->entity->table_name); |
37
|
|
|
$this->breadcrumb[] = ['title' => '内容列表', 'url' => route('admin::content.index', ['entity' => $entity])]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 内容管理-内容列表 |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function index($entity) |
45
|
|
|
{ |
46
|
|
|
$result = $this->useUserDefinedIndexHandler($entity); |
47
|
|
|
if (!is_null($result)) { |
48
|
|
|
return $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->breadcrumb[] = ['title' => $this->entity->name . '内容列表', 'url' => '']; |
52
|
|
|
Content::$listField = [ |
53
|
|
|
'title' => '标题' |
54
|
|
|
]; |
55
|
|
|
return view('admin.content.index', [ |
56
|
|
|
'breadcrumb' => $this->breadcrumb, |
57
|
|
|
'entity' => $entity, |
58
|
|
|
'entityModel' => $this->entity, |
59
|
|
|
'autoMenu' => EntityRepository::systemMenu() |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 内容列表数据接口 |
65
|
|
|
* |
66
|
|
|
* @param Request $request |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function list(Request $request, $entity) |
70
|
|
|
{ |
71
|
|
|
$result = $this->useUserDefinedListHandler($request, $entity); |
72
|
|
|
if (!is_null($result)) { |
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$perPage = (int) $request->get('limit', 50); |
77
|
|
|
$condition = $request->only($this->formNames); |
78
|
|
|
|
79
|
|
|
$data = ContentRepository::list($entity, $perPage, $condition); |
80
|
|
|
|
81
|
|
|
return $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 内容管理-新增内容 |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function create($entity) |
89
|
|
|
{ |
90
|
|
|
$this->breadcrumb[] = ['title' => "新增{$this->entity->name}内容", 'url' => '']; |
91
|
|
|
$view = $this->getAddOrEditViewPath(); |
92
|
|
|
|
93
|
|
|
return view($view, [ |
94
|
|
|
'breadcrumb' => $this->breadcrumb, |
95
|
|
|
'entity' => $entity, |
96
|
|
|
'entityModel' => $this->entity, |
97
|
|
|
'entityFields' => EntityFieldRepository::getByEntityId($entity), |
98
|
|
|
'autoMenu' => EntityRepository::systemMenu() |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 内容管理-保存内容 |
104
|
|
|
* |
105
|
|
|
* @param ContentRequest $request |
106
|
|
|
* @param integer $entity |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function save(ContentRequest $request, $entity) |
110
|
|
|
{ |
111
|
|
|
$this->validateEntityRequest(); |
112
|
|
|
$result = $this->useUserDefinedSaveHandler($request, $entity); |
113
|
|
|
if (!is_null($result)) { |
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
ContentRepository::add($request->only( |
119
|
|
|
EntityFieldRepository::getByEntityId($entity)->pluck('name')->toArray() |
120
|
|
|
)); |
121
|
|
|
return [ |
122
|
|
|
'code' => 0, |
123
|
|
|
'msg' => '新增成功', |
124
|
|
|
'redirect' => route('admin::content.index', ['entity' => $entity]) |
125
|
|
|
]; |
126
|
|
|
} catch (QueryException $e) { |
127
|
|
|
\Log::error($e); |
128
|
|
|
return [ |
129
|
|
|
'code' => 1, |
130
|
|
|
'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前内容已存在' : '其它错误'), |
131
|
|
|
'redirect' => false |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* 内容管理-编辑内容 |
138
|
|
|
* |
139
|
|
|
* @param int $id |
140
|
|
|
* @return View |
141
|
|
|
*/ |
142
|
|
|
public function edit($entity, $id) |
143
|
|
|
{ |
144
|
|
|
$this->breadcrumb[] = ['title' => "编辑{$this->entity->name}内容", 'url' => '']; |
145
|
|
|
$view = $this->getAddOrEditViewPath(); |
146
|
|
|
$model = ContentRepository::find($id); |
147
|
|
|
|
148
|
|
|
return view($view, [ |
149
|
|
|
'id' => $id, |
150
|
|
|
'model' => $model, |
151
|
|
|
'breadcrumb' => $this->breadcrumb, |
152
|
|
|
'entity' => $entity, |
153
|
|
|
'entityModel' => $this->entity, |
154
|
|
|
'entityFields' => EntityFieldRepository::getByEntityId($entity), |
155
|
|
|
'autoMenu' => EntityRepository::systemMenu() |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 内容管理-更新内容 |
161
|
|
|
* |
162
|
|
|
* @param ContentRequest $request |
163
|
|
|
* @param integer $entity |
164
|
|
|
* @param int $id |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function update(ContentRequest $request, $entity, $id) |
168
|
|
|
{ |
169
|
|
|
$this->validateEntityRequest(); |
170
|
|
|
$result = $this->useUserDefinedUpdateHandler($request, $entity, $id); |
171
|
|
|
if (!is_null($result)) { |
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$fieldInfo = EntityFieldRepository::getByEntityId($entity) |
176
|
|
|
->where('is_edit', EntityField::EDIT_ENABLE) |
177
|
|
|
->pluck('form_type', 'name') |
178
|
|
|
->toArray(); |
179
|
|
|
$data = []; |
180
|
|
|
foreach ($fieldInfo as $k => $v) { |
181
|
|
|
if ($v === 'checkbox') { |
182
|
|
|
$data[$k] = ''; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
$data = array_merge($data, $request->only(array_keys($fieldInfo))); |
186
|
|
|
|
187
|
|
|
try { |
188
|
|
|
ContentRepository::update($id, $data); |
189
|
|
|
return [ |
190
|
|
|
'code' => 0, |
191
|
|
|
'msg' => '编辑成功', |
192
|
|
|
'redirect' => route('admin::content.index', ['entity' => $entity]) |
193
|
|
|
]; |
194
|
|
|
} catch (QueryException $e) { |
195
|
|
|
\Log::error($e); |
196
|
|
|
return [ |
197
|
|
|
'code' => 1, |
198
|
|
|
'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前内容已存在' : '其它错误'), |
199
|
|
|
'redirect' => false |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* 内容管理-删除内容 |
206
|
|
|
* |
207
|
|
|
* @param int $id |
208
|
|
|
*/ |
209
|
|
|
public function delete($entity, $id) |
210
|
|
|
{ |
211
|
|
|
try { |
212
|
|
|
ContentRepository::delete($id); |
213
|
|
|
return [ |
214
|
|
|
'code' => 0, |
215
|
|
|
'msg' => '删除成功', |
216
|
|
|
'redirect' => route('admin::content.index') |
217
|
|
|
]; |
218
|
|
|
} catch (\RuntimeException $e) { |
219
|
|
|
return [ |
220
|
|
|
'code' => 1, |
221
|
|
|
'msg' => '删除失败:' . $e->getMessage(), |
222
|
|
|
'redirect' => route('admin::content.index') |
223
|
|
|
]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* 内容管理-批量操作 |
229
|
|
|
* |
230
|
|
|
* @param Request $request |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
public function batch(Request $request) |
234
|
|
|
{ |
235
|
|
|
$type = $request->input('type', ''); |
236
|
|
|
$ids = $request->input('ids'); |
237
|
|
|
if (!is_array($ids)) { |
238
|
|
|
return [ |
239
|
|
|
'code' => 1, |
240
|
|
|
'msg' => '参数错误' |
241
|
|
|
]; |
242
|
|
|
} |
243
|
|
|
$ids = array_map(function ($item) { |
244
|
|
|
return intval($item); |
245
|
|
|
}, $ids); |
246
|
|
|
|
247
|
|
|
$message = ''; |
248
|
|
|
switch ($type) { |
249
|
|
|
case 'delete': |
250
|
|
|
ContentRepository::model()->whereIn('id', $ids)->delete(); |
251
|
|
|
break; |
252
|
|
|
default: |
253
|
|
|
break; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return [ |
257
|
|
|
'code' => 0, |
258
|
|
|
'msg' => '操作成功' . $message, |
259
|
|
|
'reload' => true |
260
|
|
|
]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
protected function validateEntityRequest() |
264
|
|
|
{ |
265
|
|
|
$entityRequestClass = '\\App\\Http\\Requests\\Admin\\Entity\\' . |
266
|
|
|
Str::ucfirst(Str::singular($this->entity->table_name)) . 'Request'; |
267
|
|
|
if (class_exists($entityRequestClass)) { |
268
|
|
|
$entityRequestClass::capture()->setContainer(app())->setRedirector(app()->make('redirect'))->validate(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
protected function useUserDefinedSaveHandler($request, $entity) |
273
|
|
|
{ |
274
|
|
|
$entityControllerClass = $this->userDefinedHandlerExists('save'); |
275
|
|
|
if ($entityControllerClass === false) { |
276
|
|
|
return null; |
277
|
|
|
} |
278
|
|
|
return call_user_func([new $entityControllerClass, 'save'], $request, $entity); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function useUserDefinedUpdateHandler($request, $entity, $id) |
282
|
|
|
{ |
283
|
|
|
$entityControllerClass = $this->userDefinedHandlerExists('update'); |
284
|
|
|
if ($entityControllerClass === false) { |
285
|
|
|
return null; |
286
|
|
|
} |
287
|
|
|
return call_user_func([new $entityControllerClass, 'update'], $request, $entity, $id); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
protected function useUserDefinedIndexHandler($entity) |
291
|
|
|
{ |
292
|
|
|
$entityControllerClass = $this->userDefinedHandlerExists('index'); |
293
|
|
|
if ($entityControllerClass === false) { |
294
|
|
|
return null; |
295
|
|
|
} |
296
|
|
|
return call_user_func([new $entityControllerClass, 'index'], $entity); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
protected function useUserDefinedListHandler($request, $entity) |
300
|
|
|
{ |
301
|
|
|
$entityControllerClass = $this->userDefinedHandlerExists('list'); |
302
|
|
|
if ($entityControllerClass === false) { |
303
|
|
|
return null; |
304
|
|
|
} |
305
|
|
|
return call_user_func([new $entityControllerClass, 'list'], $request, $entity); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* 判断自定义的处理方法是否存在 |
310
|
|
|
* |
311
|
|
|
* @param string $method 方法名 |
312
|
|
|
* @return string|boolean 存在返回控制器类名,不存在返回false |
313
|
|
|
*/ |
314
|
|
|
protected function userDefinedHandlerExists($method) |
315
|
|
|
{ |
316
|
|
|
$entityControllerClass = '\\App\\Http\\Controllers\\Admin\\Entity\\' . |
317
|
|
|
Str::ucfirst(Str::singular($this->entity->table_name)) . 'Controller'; |
318
|
|
|
if (class_exists($entityControllerClass) && method_exists($entityControllerClass, $method)) { |
319
|
|
|
return $entityControllerClass; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return false; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
protected function getAddOrEditViewPath() |
326
|
|
|
{ |
327
|
|
|
$view = 'admin.content.add'; |
328
|
|
|
// 自定义模板 |
329
|
|
|
$modelName = Str::singular($this->entity->table_name); |
330
|
|
|
$path = resource_path('views/admin/content/' . $modelName . '_add.blade.php'); |
331
|
|
|
if (file_exists($path)) { |
332
|
|
|
$view = 'admin.content.' . $modelName . '_add'; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $view; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|