|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\Http\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Larafolio\Models\Page; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Larafolio\Models\Project; |
|
8
|
|
|
use Larafolio\Models\HasContent; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class ContentCrud |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get all projects, sorted by oreder. |
|
15
|
|
|
* |
|
16
|
|
|
* @return \Illuminate\Support\Collection |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getDashboardProjects() |
|
19
|
|
|
{ |
|
20
|
|
|
return Project::all()->sortBy('order')->values(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get collection of project image info: [project name => small url]. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \Illuminate\Support\Collection $projects Collection of projects. |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Illuminate\Support\Collection |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getDashboardProjectImages(Collection $projects) |
|
31
|
|
|
{ |
|
32
|
|
|
return $projects->mapWithKeys(function (Project $project) { |
|
33
|
|
|
return [$project->name => $project->getProjectImageUrl()]; |
|
34
|
|
|
})->objectIfEmpty(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get collection of project block info: [project name => block text]. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Illuminate\Support\Collection $projects Collection of projects. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Support\Collection |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getDashboardProjectBlocks(Collection $projects) |
|
45
|
|
|
{ |
|
46
|
|
|
return $projects->mapWithKeys(function (Project $project) { |
|
47
|
|
|
return [$project->name => $project->getProjectBlockText()]; |
|
48
|
|
|
})->objectIfEmpty(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get all pages, sorted by oreder. |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Illuminate\Support\Collection |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getDashboardPages() |
|
57
|
|
|
{ |
|
58
|
|
|
return Page::all()->sortBy('order')->values(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Show an individual resource in the manager. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Larafolio\Models\HasContent $model Resource to show. |
|
65
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Illuminate\Http\Response |
|
68
|
|
|
*/ |
|
69
|
|
|
public function show(HasContent $model, $type) |
|
70
|
|
|
{ |
|
71
|
|
|
$images = $model->imagesWithProps(); |
|
72
|
|
|
|
|
73
|
|
|
return view($this->makeRoute('show', $type), [ |
|
74
|
|
|
$type => $model, |
|
75
|
|
|
'images' => $images, |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return the create resource page. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Illuminate\Http\Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function create($type) |
|
87
|
|
|
{ |
|
88
|
|
|
return view($this->makeRoute('add', $type)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add a new resource to the portfolio. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Illuminate\Http\Request $request Form request. |
|
95
|
|
|
* @param User $user User object. |
|
96
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Http\Response |
|
99
|
|
|
*/ |
|
100
|
|
|
public function store(Request $request, $user, $type) |
|
101
|
|
|
{ |
|
102
|
|
|
$addMethod = $this->makeMethod('add', $type); |
|
103
|
|
|
|
|
104
|
|
|
$model = $user->{$addMethod}($request->all()); |
|
105
|
|
|
|
|
106
|
|
|
if ($request->ajax()) { |
|
107
|
|
|
return response()->json([$type => $model]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return redirect(route("show-{$type}", [$type => $model])); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return the resource edit form view. |
|
115
|
|
|
* |
|
116
|
|
|
* @param \Larafolio\Models\HasContent $model Resource to show edit form for. |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Illuminate\Http\Response |
|
119
|
|
|
*/ |
|
120
|
|
|
public function edit(HasContent $model) |
|
121
|
|
|
{ |
|
122
|
|
|
$type = $this->getTypeFromModel($model); |
|
123
|
|
|
|
|
124
|
|
|
$nextBlock = $model->blocks->pluck('order')->max() + 1; |
|
125
|
|
|
|
|
126
|
|
|
$nextLink = $model->links->pluck('order')->max() + 1; |
|
127
|
|
|
|
|
128
|
|
|
$nextLine = $model->lines->pluck('order')->max() + 1; |
|
129
|
|
|
|
|
130
|
|
|
return view($this->makeRoute('edit', $type), [ |
|
131
|
|
|
$type => $model, |
|
132
|
|
|
'nextBlock' => $nextBlock, |
|
133
|
|
|
'nextLink' => $nextLink, |
|
134
|
|
|
'nextLine' => $nextLine, |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Update a resource. |
|
140
|
|
|
* |
|
141
|
|
|
* @param \Illuminate\Http\Request $request Request data. |
|
142
|
|
|
* @param \Larafolio\Models\HasContent $model Resource to update. |
|
143
|
|
|
* @param User $user User object. |
|
144
|
|
|
* |
|
145
|
|
|
* @return \Illuminate\Http\Response |
|
146
|
|
|
*/ |
|
147
|
|
|
public function update(Request $request, HasContent $model, $user) |
|
148
|
|
|
{ |
|
149
|
|
|
$type = $this->getTypeFromModel($model); |
|
150
|
|
|
|
|
151
|
|
|
if ($model->trashed()) { |
|
152
|
|
|
$restoreMethod = $this->makeMethod('restore', $type); |
|
153
|
|
|
|
|
154
|
|
|
$user->{$restoreMethod}($model); |
|
155
|
|
|
} else { |
|
156
|
|
|
$updateMethod = $this->makeMethod('update', $type); |
|
157
|
|
|
|
|
158
|
|
|
$user->{$updateMethod}($model, $request->all()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if ($request->ajax()) { |
|
162
|
|
|
return response()->json([$type => $model]); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return redirect(route("show-{$type}", [$type => $model])); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Remove a resource from the portfolio. |
|
170
|
|
|
* |
|
171
|
|
|
* @param \Illuminate\Http\Request $request Request data. |
|
172
|
|
|
* @param \Larafolio\Models\HasContent $model Resource to update. |
|
173
|
|
|
* @param User $user User object. |
|
174
|
|
|
* |
|
175
|
|
|
* @return \Illuminate\Http\Response|bool |
|
176
|
|
|
*/ |
|
177
|
|
|
public function destroy(Request $request, HasContent $model, $user) |
|
178
|
|
|
{ |
|
179
|
|
|
$type = $this->getTypeFromModel($model); |
|
180
|
|
|
|
|
181
|
|
|
if ($model->trashed()) { |
|
182
|
|
|
$purgeMethod = $this->makeMethod('purge', $type); |
|
183
|
|
|
|
|
184
|
|
|
$user->{$purgeMethod}($model); |
|
185
|
|
|
} else { |
|
186
|
|
|
$removeMethod = $this->makeMethod('remove', $type); |
|
187
|
|
|
|
|
188
|
|
|
$user->{$removeMethod}($model); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ($request->ajax()) { |
|
192
|
|
|
return response()->json(true); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return redirect(route('dashboard')); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Make HasContent method. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $verb Action to perform. |
|
202
|
|
|
* @param string $type Name of resource. |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function makeMethod($verb, $type) |
|
207
|
|
|
{ |
|
208
|
|
|
return $verb.ucfirst($type); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Make route name. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $verb Route action to perform. |
|
215
|
|
|
* @param string $type Name of resource. |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function makeRoute($verb, $type) |
|
220
|
|
|
{ |
|
221
|
|
|
return "larafolio::{$type}s.{$verb}"; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get the model type (short class name) from the model. |
|
226
|
|
|
* |
|
227
|
|
|
* @param \Larafolio\Models\HasContent $model Model to get name of. |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function getTypeFromModel(HasContent $model) |
|
232
|
|
|
{ |
|
233
|
|
|
$reflection = new \ReflectionClass($model); |
|
234
|
|
|
|
|
235
|
|
|
return lcfirst($reflection->getShortName()); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Icons needed for dashboard. |
|
240
|
|
|
* |
|
241
|
|
|
* @return array |
|
242
|
|
|
*/ |
|
243
|
|
|
public function dashboardIcons() |
|
244
|
|
|
{ |
|
245
|
|
|
return [ |
|
246
|
|
|
'down' => $this->getIcon('vendor/larafolio/zondicons/arrow-thin-down.svg'), |
|
247
|
|
|
'up' => $this->getIcon('vendor/larafolio/zondicons/arrow-thin-up.svg'), |
|
248
|
|
|
'hidden' => $this->getIcon('vendor/larafolio/zondicons/view-hide.svg'), |
|
249
|
|
|
'visible' => $this->getIcon('vendor/larafolio/zondicons/view-show.svg'), |
|
250
|
|
|
]; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Get icon file contents. |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $path Path to icon file, relative to public path. |
|
257
|
|
|
* |
|
258
|
|
|
* @return string |
|
259
|
|
|
*/ |
|
260
|
|
|
protected function getIcon($path) |
|
261
|
|
|
{ |
|
262
|
|
|
return file_get_contents(public_path($path)); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: