|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Larafolio\Models\TextLine; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Facades\Storage; |
|
8
|
|
|
use Larafolio\Models\UserTraits\ManagesPages; |
|
9
|
|
|
use Larafolio\Models\UserTraits\ManagesProjects; |
|
10
|
|
|
|
|
11
|
|
|
trait ManagesPortfolio |
|
12
|
|
|
{ |
|
13
|
|
|
use ManagesPages, ManagesProjects; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Child relationships for HasContent models. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $children = [ |
|
21
|
|
|
'blocks' => 'addBlockToModel', |
|
22
|
|
|
'lines' => 'addLineToModel', |
|
23
|
|
|
'links' => 'addLinkToModel', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Add children resources to model. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Larafolio\Models\HasContent $model Model to add children to. |
|
30
|
|
|
* @param array $data Array of posted user data. |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Larafolio\Models\HasContent |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function addModelChildren(HasContent $model, array $data) |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($this->children as $key => $method) { |
|
37
|
|
|
foreach (collect($data)->get($key, []) as $childData) { |
|
38
|
|
|
$this->{$method}($model, $childData); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $model; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Update a HasContent model and its children. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Larafolio\Models\HasContent $model Model to update. |
|
49
|
|
|
* @param array $data Array of posted user data. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Larafolio\Models\HasContent |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function updateModel(HasContent $model, array $data) |
|
54
|
|
|
{ |
|
55
|
|
|
$model->update($data); |
|
56
|
|
|
|
|
57
|
|
|
$this->updateAllTextBlocks($model, $data); |
|
58
|
|
|
|
|
59
|
|
|
$this->updateAllTextLines($model, $data); |
|
60
|
|
|
|
|
61
|
|
|
$this->updateAllLinks($model, $data); |
|
62
|
|
|
|
|
63
|
|
|
return $model; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Permanently delete a model. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Larafolio\Models\HasContent $model Model to delete. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function purgeModel(HasContent $model) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($model->images as $image) { |
|
|
|
|
|
|
76
|
|
|
$this->removeImage($image); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$model->restore(); |
|
80
|
|
|
|
|
81
|
|
|
return $model->forceDelete(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set order of data based on order value. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $data Data array containing 'order' index. |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Illuminate\Support\Collection |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function setOrder(array $data) |
|
92
|
|
|
{ |
|
93
|
|
|
return collect($data)->sortBy('order') |
|
94
|
|
|
->map(function ($item, $key) { |
|
95
|
|
|
$item['order'] = $key; |
|
96
|
|
|
|
|
97
|
|
|
return $item; |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Add a text block to a model. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Larafolio\Models\HasContent $model Model to add text block to. |
|
105
|
|
|
* @param array $blockData Array of text block data. |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Larafolio\Models\HasContent |
|
108
|
|
|
*/ |
|
109
|
|
|
public function addBlockToModel(HasContent $model, array $blockData) |
|
110
|
|
|
{ |
|
111
|
|
|
return $model->blocks()->create($blockData); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Update a text block. |
|
116
|
|
|
* |
|
117
|
|
|
* @param \Larafolio\Models\TextBlock $textBlock Text block to update. |
|
118
|
|
|
* @param array $blockData Array of text block data. |
|
119
|
|
|
* |
|
120
|
|
|
* @return \Larafolio\Models\TextBlock |
|
121
|
|
|
*/ |
|
122
|
|
|
public function updateTextBlock(TextBlock $textBlock, array $blockData) |
|
123
|
|
|
{ |
|
124
|
|
|
$textBlock->update($blockData); |
|
125
|
|
|
|
|
126
|
|
|
return $textBlock; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Update model text blocks by adding new ones and updating existing ones. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \Larafolio\Models\HasContent $model Model that blocks belong to. |
|
133
|
|
|
* @param array $data Array of model information. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function updateAllTextBlocks(HasContent $model, array $data) |
|
136
|
|
|
{ |
|
137
|
|
|
$blockData = collect($data)->get('blocks', []); |
|
138
|
|
|
|
|
139
|
|
|
$type = TextBlock::class; |
|
140
|
|
|
|
|
141
|
|
|
$this->updateContent( |
|
142
|
|
|
$model, |
|
143
|
|
|
$type, |
|
144
|
|
|
$blockData, |
|
145
|
|
|
[$this, 'updateTextBlock'], |
|
146
|
|
|
[$this, 'addBlockToModel'] |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Remove a text block from a model. |
|
152
|
|
|
* |
|
153
|
|
|
* @param \Larafolio\Models\TextBlock $textBlock The text block to delete. |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool|null |
|
156
|
|
|
*/ |
|
157
|
|
|
public function removeTextBlock(TextBlock $textBlock) |
|
158
|
|
|
{ |
|
159
|
|
|
return $textBlock->delete(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Add a text line to a model. |
|
164
|
|
|
* |
|
165
|
|
|
* @param \Larafolio\Models\HasContent $model Model to add text line to. |
|
166
|
|
|
* @param array $lineData Array of text line data. |
|
167
|
|
|
* |
|
168
|
|
|
* @return \Larafolio\Models\HasContent |
|
169
|
|
|
*/ |
|
170
|
|
|
public function addLineToModel(HasContent $model, array $lineData) |
|
171
|
|
|
{ |
|
172
|
|
|
return $model->lines()->create($lineData); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Update a text line. |
|
177
|
|
|
* |
|
178
|
|
|
* @param \Larafolio\Models\TextLine $textLine Text line to update. |
|
179
|
|
|
* @param array $lineData Array of text line data. |
|
180
|
|
|
* |
|
181
|
|
|
* @return \Larafolio\Models\TextLine |
|
182
|
|
|
*/ |
|
183
|
|
|
public function updateTextLine(TextLine $textLine, array $lineData) |
|
184
|
|
|
{ |
|
185
|
|
|
$textLine->update($lineData); |
|
186
|
|
|
|
|
187
|
|
|
return $textLine; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Update model text lines by adding new ones and updating existing ones. |
|
192
|
|
|
* |
|
193
|
|
|
* @param \Larafolio\Models\HasContent $model Model that lines belong to. |
|
194
|
|
|
* @param array $data Array of model information. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function updateAllTextLines(HasContent $model, array $data) |
|
197
|
|
|
{ |
|
198
|
|
|
$lineData = collect($data)->get('lines', []); |
|
199
|
|
|
|
|
200
|
|
|
$type = TextLine::class; |
|
201
|
|
|
|
|
202
|
|
|
$this->updateContent( |
|
203
|
|
|
$model, |
|
204
|
|
|
$type, |
|
205
|
|
|
$lineData, |
|
206
|
|
|
[$this, 'updateTextLine'], |
|
207
|
|
|
[$this, 'addLineToModel'] |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Remove a text line from a model. |
|
213
|
|
|
* |
|
214
|
|
|
* @param \Larafolio\Models\TextLine $textLine The text line to delete. |
|
215
|
|
|
* |
|
216
|
|
|
* @return bool|null |
|
217
|
|
|
*/ |
|
218
|
|
|
public function removeTextLine(TextLine $textLine) |
|
219
|
|
|
{ |
|
220
|
|
|
return $textLine->delete(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Add image to a model. |
|
225
|
|
|
* |
|
226
|
|
|
* @param \Larafolio\Models\HasContent $model Model to add image to. |
|
227
|
|
|
* @param array $imageData Array of image infomation. |
|
228
|
|
|
* |
|
229
|
|
|
* @return \Larafolio\Models\HasContent |
|
230
|
|
|
*/ |
|
231
|
|
|
public function addImageToModel(HasContent $model, array $imageData) |
|
232
|
|
|
{ |
|
233
|
|
|
return $model->images()->create($imageData); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Update image name and caption. |
|
238
|
|
|
* |
|
239
|
|
|
* @param \Larafolio\Models\Image $image Image to update. |
|
240
|
|
|
* @param array $imageData Array of inmage information. |
|
241
|
|
|
* |
|
242
|
|
|
* @return \Larafolio\Models\Image |
|
243
|
|
|
*/ |
|
244
|
|
|
public function updateImageInfo(Image $image, array $imageData) |
|
245
|
|
|
{ |
|
246
|
|
|
$image->update($imageData); |
|
247
|
|
|
|
|
248
|
|
|
return $image; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Remove image from storage and delete database info if path is unique. |
|
253
|
|
|
* |
|
254
|
|
|
* @param \Larafolio\Models\Image $image Image to remove. |
|
255
|
|
|
* |
|
256
|
|
|
* @return bool|null |
|
257
|
|
|
*/ |
|
258
|
|
|
public function removeImage(Image $image) |
|
259
|
|
|
{ |
|
260
|
|
|
$count = Image::where('path', $image->path)->count(); |
|
261
|
|
|
|
|
262
|
|
|
if ($count <= 1) { |
|
263
|
|
|
Storage::delete($image->path()); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $image->delete(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Add a link to a model. |
|
271
|
|
|
* |
|
272
|
|
|
* @param \Larafolio\Models\HasContent $model Model to add link to. |
|
273
|
|
|
* @param array $linkData Array of link info. |
|
274
|
|
|
*/ |
|
275
|
|
|
public function addLinkToModel(HasContent $model, array $linkData) |
|
276
|
|
|
{ |
|
277
|
|
|
return $model->links()->create($linkData); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Update a link. |
|
282
|
|
|
* |
|
283
|
|
|
* @param \Larafolio\Models\Link $link Link to update. |
|
284
|
|
|
* @param array $linkData Array of link data. |
|
285
|
|
|
* |
|
286
|
|
|
* @return \Larafolio\Models\Link |
|
287
|
|
|
*/ |
|
288
|
|
|
public function updateLink(Link $link, array $linkData) |
|
289
|
|
|
{ |
|
290
|
|
|
$link->update($linkData); |
|
291
|
|
|
|
|
292
|
|
|
return $link; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Update model links by adding new ones and updating existing ones. |
|
297
|
|
|
* |
|
298
|
|
|
* @param \Larafolio\Models\HasContent $model Model that links belong to. |
|
299
|
|
|
* @param array $data Array of model information. |
|
300
|
|
|
*/ |
|
301
|
|
|
public function updateAllLinks(HasContent $model, array $data) |
|
302
|
|
|
{ |
|
303
|
|
|
$linkData = collect($data)->get('links', []); |
|
304
|
|
|
|
|
305
|
|
|
$type = Link::class; |
|
306
|
|
|
|
|
307
|
|
|
$this->updateContent( |
|
308
|
|
|
$model, |
|
309
|
|
|
$type, |
|
310
|
|
|
$linkData, |
|
311
|
|
|
[$this, 'updateLink'], |
|
312
|
|
|
[$this, 'addLinkToModel'] |
|
313
|
|
|
); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Remove link from a model. |
|
318
|
|
|
* |
|
319
|
|
|
* @param \Larafolio\Models\Link $link Link to remove. |
|
320
|
|
|
* |
|
321
|
|
|
* @return bool|null |
|
322
|
|
|
*/ |
|
323
|
|
|
public function removeLink(Link $link) |
|
324
|
|
|
{ |
|
325
|
|
|
return $link->delete(); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Update content associated with model. |
|
330
|
|
|
* |
|
331
|
|
|
* @param \Larafolio\Models\HasContent $model Model associated with content. |
|
332
|
|
|
* @param string $type Type of model. |
|
333
|
|
|
* @param array $data user posted data. |
|
334
|
|
|
* @param callable $updateCallback Callback to update the content. |
|
335
|
|
|
* @param callable $addCallback Callback to add new content. |
|
336
|
|
|
*/ |
|
337
|
|
|
protected function updateContent( |
|
338
|
|
|
HasContent $model, |
|
339
|
|
|
$type, |
|
340
|
|
|
array $data, |
|
341
|
|
|
callable $updateCallback, |
|
342
|
|
|
callable $addCallback |
|
343
|
|
|
) { |
|
344
|
|
|
$data = $this->setOrder($data); |
|
345
|
|
|
|
|
346
|
|
|
foreach ($data as $singleItemData) { |
|
347
|
|
|
if (isset($singleItemData['resource_id'])) { |
|
348
|
|
|
$block = $type::find($singleItemData['id']); |
|
349
|
|
|
|
|
350
|
|
|
$updateCallback($block, $singleItemData); |
|
351
|
|
|
} else { |
|
352
|
|
|
$addCallback($model, $singleItemData); |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
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.