|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\Http\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Larafolio\Models\HasContent; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Larafolio\Http\Requests\AddResourceRequest; |
|
9
|
|
|
|
|
10
|
|
|
class ContentCrud |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Return all. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Collection $collection Colelction of all resources. |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
18
|
|
|
*/ |
|
19
|
|
|
public function index(Collection $collection) |
|
20
|
|
|
{ |
|
21
|
|
|
return response()->json($collection); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Show an individual resource in the manager. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Larafolio\Models\HasContent $model Resource to show. |
|
28
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Http\Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function show(HasContent $model, $type) |
|
33
|
|
|
{ |
|
34
|
|
|
$images = $model->imagesWithProps(); |
|
35
|
|
|
|
|
36
|
|
|
return view($this->makeRoute('show', $type), [ |
|
37
|
|
|
$type => $model, |
|
38
|
|
|
'images' => $images, |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return the create resource page. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Http\Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function create($type) |
|
50
|
|
|
{ |
|
51
|
|
|
return view($this->makeRoute('add', $type)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add a new resource to the portfolio. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Larafolio\Http\Requests\AddResourceRequest $request Form request. |
|
58
|
|
|
* @param User $user User object. |
|
59
|
|
|
* @param string $type Type of resource (page, project etc.). |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Illuminate\Http\Response |
|
62
|
|
|
*/ |
|
63
|
|
|
public function store(AddResourceRequest $request, $user, $type) |
|
64
|
|
|
{ |
|
65
|
|
|
$addMethod = $this->makeMethod('add', $type); |
|
66
|
|
|
|
|
67
|
|
|
$model = $user->{$addMethod}($request->all()); |
|
68
|
|
|
|
|
69
|
|
|
if ($request->ajax()) { |
|
70
|
|
|
return response()->json([$type => $model]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return redirect(route("show-{$type}", [$type => $model])); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the resource edit form view. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Larafolio\Models\HasContent $model Resource to show edit form for. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Illuminate\Http\Response |
|
82
|
|
|
*/ |
|
83
|
|
|
public function edit(HasContent $model) |
|
84
|
|
|
{ |
|
85
|
|
|
$type = $this->getTypeFromModel($model); |
|
86
|
|
|
|
|
87
|
|
|
$nextBlock = $model->blocks->pluck('order')->max() + 1; |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$nextLink = $model->links->pluck('order')->max() + 1; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return view($this->makeRoute('edit', $type), [ |
|
92
|
|
|
$type => $model, |
|
93
|
|
|
'nextBlock' => $nextBlock, |
|
94
|
|
|
'nextLink' => $nextLink, |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Update a resource. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Illuminate\Http\Request $request Request data. |
|
102
|
|
|
* @param Larafolio\Models\HasContent $model Resource to update. |
|
103
|
|
|
* @param User $user User object. |
|
104
|
|
|
* |
|
105
|
|
|
* @return \Illuminate\Http\Response |
|
106
|
|
|
*/ |
|
107
|
|
|
public function update(Request $request, HasContent $model, $user) |
|
108
|
|
|
{ |
|
109
|
|
|
$type = $this->getTypeFromModel($model); |
|
110
|
|
|
|
|
111
|
|
|
if ($model->trashed()) { |
|
112
|
|
|
$restoreMethod = $this->makeMethod('restore', $type); |
|
113
|
|
|
|
|
114
|
|
|
$user->{$restoreMethod}($model); |
|
115
|
|
|
} else { |
|
116
|
|
|
$updateMethod = $this->makeMethod('update', $type); |
|
117
|
|
|
|
|
118
|
|
|
$user->{$updateMethod}($model, $request->all()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($request->ajax()) { |
|
122
|
|
|
return response()->json([$type => $model]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return redirect(route("show-{$type}", [$type => $model])); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Remove a resource from the portfolio. |
|
130
|
|
|
* |
|
131
|
|
|
* @param \Illuminate\Http\Request $request Request data. |
|
132
|
|
|
* @param Larafolio\Models\HasContent $model Resource to update. |
|
133
|
|
|
* @param User $user User object. |
|
134
|
|
|
* |
|
135
|
|
|
* @return \Illuminate\Http\Response |
|
136
|
|
|
*/ |
|
137
|
|
|
public function destroy(Request $request, HasContent $model, $user) |
|
138
|
|
|
{ |
|
139
|
|
|
$type = $this->getTypeFromModel($model); |
|
140
|
|
|
|
|
141
|
|
|
if ($model->trashed()) { |
|
142
|
|
|
$purgeMethod = $this->makeMethod('purge', $type); |
|
143
|
|
|
|
|
144
|
|
|
$user->{$purgeMethod}($model); |
|
145
|
|
|
} else { |
|
146
|
|
|
$removeMethod = $this->makeMethod('remove', $type); |
|
147
|
|
|
|
|
148
|
|
|
$user->{$removeMethod}($model); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($request->ajax()) { |
|
152
|
|
|
return response()->json(true); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return redirect(route('dashboard')); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Make HasContent method. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $verb Action to perform. |
|
162
|
|
|
* @param string $type Name of resource. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function makeMethod($verb, $type) |
|
167
|
|
|
{ |
|
168
|
|
|
return $verb.ucfirst($type); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Make route name. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $verb Route action to perform. |
|
175
|
|
|
* @param string $type Name of resource. |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function makeRoute($verb, $type) |
|
180
|
|
|
{ |
|
181
|
|
|
return "larafolio::{$type}s.{$verb}"; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the model type (short class name) from the model. |
|
186
|
|
|
* |
|
187
|
|
|
* @param HasContent $model Model to get name of. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function getTypeFromModel(HasContent $model) |
|
192
|
|
|
{ |
|
193
|
|
|
$reflection = new \ReflectionClass($model); |
|
194
|
|
|
|
|
195
|
|
|
return lcfirst($reflection->getShortName()); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.