|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Larafolio\Models\HasContent; |
|
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
|
|
|
* Add a blocks and links to model. |
|
17
|
|
|
* |
|
18
|
|
|
* @param HasContent $model Model to add extras to. |
|
19
|
|
|
* @param array $data Array of posted user data. |
|
20
|
|
|
* |
|
21
|
|
|
* @return HasContent |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function addModelExtras(HasContent $model, array $data) |
|
24
|
|
|
{ |
|
25
|
|
|
foreach (collect($data)->get('blocks', []) as $block) { |
|
26
|
|
|
$this->addBlockToModel($model, $block); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
foreach (collect($data)->get('links', []) as $link) { |
|
30
|
|
|
$this->addLinkToModel($model, $link); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $model; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Update a HasContent model and its children. |
|
38
|
|
|
* |
|
39
|
|
|
* @param HasContent $model Model to update. |
|
40
|
|
|
* @param array $data Array of posted user data. |
|
41
|
|
|
* |
|
42
|
|
|
* @return HasContent |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function updateModel(HasContent $model, array $data) |
|
45
|
|
|
{ |
|
46
|
|
|
$model->update($data); |
|
47
|
|
|
|
|
48
|
|
|
$this->updateAllTextBlocks($model, $data); |
|
49
|
|
|
|
|
50
|
|
|
$this->updateAllLinks($model, $data); |
|
51
|
|
|
|
|
52
|
|
|
return $model; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Permanently delete a model. |
|
57
|
|
|
* |
|
58
|
|
|
* @param HasContent $model Model to delete. |
|
59
|
|
|
* |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function purgeModel(HasContent $model) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($model->images as $image) { |
|
|
|
|
|
|
65
|
|
|
$this->removeImage($image); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$model->restore(); |
|
69
|
|
|
|
|
70
|
|
|
return $model->forceDelete(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set order of data based on order value. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $data Data array containing 'order' index. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Illuminate\Support\Collection |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function setOrder(array $data) |
|
81
|
|
|
{ |
|
82
|
|
|
return collect($data)->sortBy('order') |
|
83
|
|
|
->map(function ($item, $key) { |
|
84
|
|
|
$item['order'] = $key; |
|
85
|
|
|
|
|
86
|
|
|
return $item; |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add a text block to a model. |
|
92
|
|
|
* |
|
93
|
|
|
* @param HasContent $model Model to add text block to. |
|
94
|
|
|
* @param array $blockData Array of text block data. |
|
95
|
|
|
* |
|
96
|
|
|
* @return HasContent |
|
97
|
|
|
*/ |
|
98
|
|
|
public function addBlockToModel(HasContent $model, array $blockData) |
|
99
|
|
|
{ |
|
100
|
|
|
return $model->blocks()->create($blockData); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Update a text block. |
|
105
|
|
|
* |
|
106
|
|
|
* @param TextBlock $textBlock Text block to update. |
|
107
|
|
|
* @param array $blockData Array of text block data. |
|
108
|
|
|
* |
|
109
|
|
|
* @return TextBlock |
|
110
|
|
|
*/ |
|
111
|
|
|
public function updateTextBlock(TextBlock $textBlock, array $blockData) |
|
112
|
|
|
{ |
|
113
|
|
|
$textBlock->update($blockData); |
|
114
|
|
|
|
|
115
|
|
|
return $textBlock; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Update model text blocks by adding new ones and updating existing ones. |
|
120
|
|
|
* |
|
121
|
|
|
* @param Model $model Model that blocks belong to. |
|
122
|
|
|
* @param array $data Array of model information. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function updateAllTextBlocks(Model $model, array $data) |
|
125
|
|
|
{ |
|
126
|
|
|
$blockData = collect($data)->get('blocks', []); |
|
127
|
|
|
|
|
128
|
|
|
$type = TextBlock::class; |
|
129
|
|
|
|
|
130
|
|
|
$this->updateContent( |
|
131
|
|
|
$model, |
|
|
|
|
|
|
132
|
|
|
$type, |
|
133
|
|
|
$blockData, |
|
134
|
|
|
[$this, 'updateTextBlock'], |
|
135
|
|
|
[$this, 'addBlockToModel'] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Remove a text block from a model. |
|
141
|
|
|
* |
|
142
|
|
|
* @param TextBlock $textBlock The text block to delete. |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool|null |
|
145
|
|
|
*/ |
|
146
|
|
|
public function removeTextBlock(TextBlock $textBlock) |
|
147
|
|
|
{ |
|
148
|
|
|
return $textBlock->delete(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Add image to a model. |
|
153
|
|
|
* |
|
154
|
|
|
* @param HasContent $model Model to add image to. |
|
155
|
|
|
* @param array $imageData Array of image infomation. |
|
156
|
|
|
* |
|
157
|
|
|
* @return HasContent |
|
158
|
|
|
*/ |
|
159
|
|
|
public function addImageToModel(HasContent $model, array $imageData) |
|
160
|
|
|
{ |
|
161
|
|
|
return $model->images()->create($imageData); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Update image name and caption. |
|
166
|
|
|
* |
|
167
|
|
|
* @param Image $image Image to update. |
|
168
|
|
|
* @param array $imageData Array of inmage information. |
|
169
|
|
|
* |
|
170
|
|
|
* @return Image |
|
171
|
|
|
*/ |
|
172
|
|
|
public function updateImageInfo(Image $image, array $imageData) |
|
173
|
|
|
{ |
|
174
|
|
|
$image->update($imageData); |
|
175
|
|
|
|
|
176
|
|
|
return $image; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Remove image from storage and delete database info. |
|
181
|
|
|
* |
|
182
|
|
|
* @param Image $image Image to remove. |
|
183
|
|
|
* |
|
184
|
|
|
* @return bool|null |
|
185
|
|
|
*/ |
|
186
|
|
|
public function removeImage(Image $image) |
|
187
|
|
|
{ |
|
188
|
|
|
Storage::delete($image->path()); |
|
189
|
|
|
|
|
190
|
|
|
return $image->delete(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Add a link to a model. |
|
195
|
|
|
* |
|
196
|
|
|
* @param HasContent $model Model to add link to. |
|
197
|
|
|
* @param array $linkData Array of link info. |
|
198
|
|
|
*/ |
|
199
|
|
|
public function addLinkToModel(HasContent $model, array $linkData) |
|
200
|
|
|
{ |
|
201
|
|
|
return $model->links()->create($linkData); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Update a link. |
|
206
|
|
|
* |
|
207
|
|
|
* @param Link $link Link to update. |
|
208
|
|
|
* @param array $linkData Array of link data. |
|
209
|
|
|
* |
|
210
|
|
|
* @return Link |
|
211
|
|
|
*/ |
|
212
|
|
|
public function updateLink(Link $link, array $linkData) |
|
213
|
|
|
{ |
|
214
|
|
|
$link->update($linkData); |
|
215
|
|
|
|
|
216
|
|
|
return $link; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Update model links by adding new ones and updating existing ones. |
|
221
|
|
|
* |
|
222
|
|
|
* @param Model $model Model that links belong to. |
|
223
|
|
|
* @param array $data Array of model information. |
|
224
|
|
|
*/ |
|
225
|
|
|
public function updateAllLinks(Model $model, array $data) |
|
226
|
|
|
{ |
|
227
|
|
|
$linkData = collect($data)->get('links', []); |
|
228
|
|
|
|
|
229
|
|
|
$type = Link::class; |
|
230
|
|
|
|
|
231
|
|
|
$this->updateContent( |
|
232
|
|
|
$model, |
|
|
|
|
|
|
233
|
|
|
$type, |
|
234
|
|
|
$linkData, |
|
235
|
|
|
[$this, 'updateLink'], |
|
236
|
|
|
[$this, 'addLinkToModel'] |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Remove link from a model. |
|
242
|
|
|
* |
|
243
|
|
|
* @param Link $link Link to remove. |
|
244
|
|
|
* |
|
245
|
|
|
* @return bool|null |
|
246
|
|
|
*/ |
|
247
|
|
|
public function removeLink(Link $link) |
|
248
|
|
|
{ |
|
249
|
|
|
return $link->delete(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Update content associated with model. |
|
254
|
|
|
* |
|
255
|
|
|
* @param HasContent $model Model associated with content. |
|
256
|
|
|
* @param string $type Type of model. |
|
257
|
|
|
* @param array $data user posted data. |
|
258
|
|
|
* @param callable $updateCallback Callback to update the content. |
|
259
|
|
|
* @param callable $addCallback Callback to add new content. |
|
260
|
|
|
*/ |
|
261
|
|
|
protected function updateContent( |
|
262
|
|
|
HasContent $model, |
|
263
|
|
|
$type, |
|
264
|
|
|
array $data, |
|
265
|
|
|
callable $updateCallback, |
|
266
|
|
|
callable $addCallback |
|
267
|
|
|
) { |
|
268
|
|
|
$data = $this->setOrder($data); |
|
269
|
|
|
|
|
270
|
|
|
foreach ($data as $singleItemData) { |
|
271
|
|
|
if (isset($singleItemData['resource_id'])) { |
|
272
|
|
|
$block = $type::find($singleItemData['id']); |
|
273
|
|
|
|
|
274
|
|
|
$updateCallback($block, $singleItemData); |
|
275
|
|
|
} else { |
|
276
|
|
|
$addCallback($model, $singleItemData); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
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.