|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Back; |
|
4
|
|
|
|
|
5
|
|
|
use Activity; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Spatie\EloquentSortable\SortableInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ModuleController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
protected $modelName = null; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $moduleName = null; |
|
17
|
|
|
|
|
18
|
|
|
/** @return \Illuminate\Database\Eloquent\Model */ |
|
19
|
|
|
abstract protected function make(); |
|
20
|
|
|
|
|
21
|
|
|
public function index() |
|
22
|
|
|
{ |
|
23
|
|
|
$models = $this->query()->get(); |
|
24
|
|
|
|
|
25
|
|
|
$data = [ |
|
26
|
|
|
$this->moduleName => $models, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
return view("back.{$this->moduleName}.index", $data); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function create() |
|
33
|
|
|
{ |
|
34
|
|
|
$model = $this->make(); |
|
35
|
|
|
|
|
36
|
|
|
return redirect()->action("Back\\{$this->modelName}Controller@edit", [$model->id]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function show($id) |
|
40
|
|
|
{ |
|
41
|
|
|
return redirect()->action("Back\\{$this->modelName}Controller@edit", [$id]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function edit(Request $request, int $id) |
|
45
|
|
|
{ |
|
46
|
|
|
$model = $this->find($id); |
|
47
|
|
|
|
|
48
|
|
|
if ($request->has('revert')) { |
|
49
|
|
|
$model->clearTemporaryMedia(); |
|
50
|
|
|
|
|
51
|
|
|
return redirect()->action("Back\\{$this->modelName}Controller@edit", [$id]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$data = [ |
|
55
|
|
|
'model' => $model, |
|
56
|
|
|
'module' => $this->moduleName, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
return view("back.{$this->moduleName}.edit", $data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function update(int $id) |
|
63
|
|
|
{ |
|
64
|
|
|
$request = app()->make("App\\Http\\Requests\\Back\\{$this->modelName}Request"); |
|
65
|
|
|
|
|
66
|
|
|
$model = $this->find($id); |
|
67
|
|
|
|
|
68
|
|
|
call_user_func("App\\Models\\Updaters\\{$this->modelName}Updater::update", $model, $request); |
|
69
|
|
|
|
|
70
|
|
|
$model->save(); |
|
71
|
|
|
app('cache')->flush(); |
|
72
|
|
|
|
|
73
|
|
|
$eventDescription = $this->getUpdatedEventDescription($model); |
|
74
|
|
|
Activity::log($eventDescription); |
|
75
|
|
|
flash()->success(strip_tags($eventDescription)); |
|
76
|
|
|
|
|
77
|
|
|
return redirect()->action("Back\\{$this->modelName}Controller@edit", [$model->id]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function destroy($id) |
|
81
|
|
|
{ |
|
82
|
|
|
$model = $this->query()->find($id); |
|
83
|
|
|
|
|
84
|
|
|
$eventDescription = $this->getDeletedEventDescription($model); |
|
85
|
|
|
Activity::log($eventDescription); |
|
86
|
|
|
flash()->success(strip_tags($eventDescription)); |
|
87
|
|
|
|
|
88
|
|
|
$model->delete(); |
|
89
|
|
|
app('cache')->flush(); |
|
90
|
|
|
|
|
91
|
|
|
return redirect()->action("Back\\{$this->modelName}Controller@index"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function changeOrder(Request $request) |
|
95
|
|
|
{ |
|
96
|
|
|
$model = "\\App\\Models\\{$this->modelName}"; |
|
97
|
|
|
|
|
98
|
|
|
$model::setNewOrder($request->get('ids')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function getUpdatedEventDescription($model) |
|
102
|
|
|
{ |
|
103
|
|
|
$modelName = fragment("back.{$this->moduleName}.singular"); |
|
104
|
|
|
|
|
105
|
|
|
$linkToModel = link_to_action("Back\\{$this->modelName}Controller@edit", $model->name, ['id' => $model->id]); |
|
106
|
|
|
|
|
107
|
|
|
if ($model->wasDraft) { |
|
108
|
|
|
return fragment('back.events.created', ['model' => $modelName, 'name' => $linkToModel]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return fragment('back.events.updated', ['model' => $modelName, 'name' => $linkToModel]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function getDeletedEventDescription($model) |
|
115
|
|
|
{ |
|
116
|
|
|
$modelName = fragment("back.{$this->moduleName}.singular"); |
|
117
|
|
|
|
|
118
|
|
|
return fragment('back.events.deleted', ['model' => $modelName, 'name' => $model->name]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function find(int $id) |
|
122
|
|
|
{ |
|
123
|
|
|
$class = "App\\Models\\{$this->modelName}"; |
|
124
|
|
|
|
|
125
|
|
|
return call_user_func("{$class}::find", $id); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function query() |
|
129
|
|
|
{ |
|
130
|
|
|
$class = "App\\Models\\{$this->modelName}"; |
|
131
|
|
|
|
|
132
|
|
|
$query = call_user_func("{$class}::query")->nonDraft(); |
|
133
|
|
|
|
|
134
|
|
|
if (array_key_exists(SortableInterface::class, class_implements($class))) { |
|
135
|
|
|
return $query->orderBy('order_column', 'asc'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $query; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|