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
|
|
|
* Set order of data based on order value. |
17
|
|
|
* |
18
|
|
|
* @param array $data Data array containing 'order' index. |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Support\Collection |
21
|
|
|
*/ |
22
|
|
|
protected function setOrder(array $data) |
23
|
|
|
{ |
24
|
|
|
return collect($data)->sortBy('order') |
25
|
|
|
->map(function ($item, $key) { |
26
|
|
|
$item['order'] = $key; |
27
|
|
|
|
28
|
|
|
return $item; |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add a text block to a model. |
34
|
|
|
* |
35
|
|
|
* @param HasContent $model Model to add text block to. |
36
|
|
|
* @param array $blockData Array of text block data. |
37
|
|
|
* |
38
|
|
|
* @return HasContent |
39
|
|
|
*/ |
40
|
|
|
public function addBlockToModel(HasContent $model, array $blockData) |
41
|
|
|
{ |
42
|
|
|
return $model->blocks()->create($blockData); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update a text block. |
47
|
|
|
* |
48
|
|
|
* @param TextBlock $textBlock Text block to update. |
49
|
|
|
* @param array $blockData Array of text block data. |
50
|
|
|
* |
51
|
|
|
* @return TextBlock |
52
|
|
|
*/ |
53
|
|
|
public function updateTextBlock(TextBlock $textBlock, array $blockData) |
54
|
|
|
{ |
55
|
|
|
$textBlock->update($blockData); |
56
|
|
|
|
57
|
|
|
return $textBlock; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Update model text blocks by adding new ones and updating existing ones. |
62
|
|
|
* |
63
|
|
|
* @param Model $model Model that blocks belong to. |
64
|
|
|
* @param array $data Array of model information. |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function updateAllTextBlocks(Model $model, array $data) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$blockData = collect($data)->get('blocks', []); |
69
|
|
|
|
70
|
|
|
$blockData = $this->setOrder($blockData); |
71
|
|
|
|
72
|
|
|
foreach ($blockData as $singleBlockData) { |
73
|
|
|
if (isset($singleBlockData['resource_id'])) { |
74
|
|
|
$block = TextBlock::find($singleBlockData['id']); |
75
|
|
|
|
76
|
|
|
$this->updateTextBlock($block, $singleBlockData); |
77
|
|
|
} else { |
78
|
|
|
$this->addBlockToModel($model, $singleBlockData); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Remove a text block from a project. |
85
|
|
|
* |
86
|
|
|
* @param TextBlock $textBlock The text block to delete. |
87
|
|
|
* |
88
|
|
|
* @return bool|null |
89
|
|
|
*/ |
90
|
|
|
public function removeTextBlock(TextBlock $textBlock) |
91
|
|
|
{ |
92
|
|
|
return $textBlock->delete(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add image to a model. |
97
|
|
|
* |
98
|
|
|
* @param HasContent $model Model to add image to. |
99
|
|
|
* @param array $imageData Array of image infomation. |
100
|
|
|
* |
101
|
|
|
* @return HasContent |
102
|
|
|
*/ |
103
|
|
|
public function addImageToModel(HasContent $model, array $imageData) |
104
|
|
|
{ |
105
|
|
|
return $model->images()->create($imageData); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Update image name and caption. |
110
|
|
|
* |
111
|
|
|
* @param Image $image Image to update. |
112
|
|
|
* @param array $imageData Array of inmage information. |
113
|
|
|
* |
114
|
|
|
* @return Image |
115
|
|
|
*/ |
116
|
|
|
public function updateImageInfo(Image $image, array $imageData) |
117
|
|
|
{ |
118
|
|
|
$image->update($imageData); |
119
|
|
|
|
120
|
|
|
return $image; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Remove image from storage and delete database info. |
125
|
|
|
* |
126
|
|
|
* @param Image $image Image to remove. |
127
|
|
|
* |
128
|
|
|
* @return bool|null |
129
|
|
|
*/ |
130
|
|
|
public function removeImage(Image $image) |
131
|
|
|
{ |
132
|
|
|
Storage::delete($image->path()); |
133
|
|
|
|
134
|
|
|
return $image->delete(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add a link to a model. |
139
|
|
|
* |
140
|
|
|
* @param HasContent $model Model to add link to. |
141
|
|
|
* @param array $linkData Array of link info. |
142
|
|
|
*/ |
143
|
|
|
public function addLinkToModel(HasContent $model, array $linkData) |
144
|
|
|
{ |
145
|
|
|
return $model->links()->create($linkData); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Update a link. |
150
|
|
|
* |
151
|
|
|
* @param Link $link Link to update. |
152
|
|
|
* @param array $linkData Array of link data. |
153
|
|
|
* |
154
|
|
|
* @return Link |
155
|
|
|
*/ |
156
|
|
|
public function updateLink(Link $link, array $linkData) |
157
|
|
|
{ |
158
|
|
|
$link->update($linkData); |
159
|
|
|
|
160
|
|
|
return $link; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Update model links by adding new ones and updating existing ones. |
165
|
|
|
* |
166
|
|
|
* @param Model $model Model that links belong to. |
167
|
|
|
* @param array $data Array of model information. |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
public function updateAllLinks(Model $model, array $data) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$linkData = collect($data)->get('links', []); |
172
|
|
|
|
173
|
|
|
$linkData = $this->setOrder($linkData); |
174
|
|
|
|
175
|
|
|
foreach ($linkData as $singleLinkData) { |
176
|
|
|
if (isset($singleLinkData['resource_id'])) { |
177
|
|
|
$link = Link::find($singleLinkData['id']); |
178
|
|
|
|
179
|
|
|
$this->updateLink($link, $singleLinkData); |
180
|
|
|
} else { |
181
|
|
|
$this->addLinkToModel($model, $singleLinkData); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Remove link from a project. |
188
|
|
|
* |
189
|
|
|
* @param Link $link Link to remove. |
190
|
|
|
* |
191
|
|
|
* @return bool|null |
192
|
|
|
*/ |
193
|
|
|
public function removeLink(Link $link) |
194
|
|
|
{ |
195
|
|
|
return $link->delete(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.