1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\HtmlBlock; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use File; |
7
|
|
|
use DbView; |
|
|
|
|
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\HtmlBlock\Entity\HtmlBlockRepository; |
9
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
10
|
|
|
use Hideyo\Ecommerce\Framework\Services\Shop\ShopFacade as ShopService; |
11
|
|
|
|
12
|
|
|
class HtmlBlockService extends BaseService |
13
|
|
|
{ |
14
|
|
|
public function __construct(HtmlBlockRepository $htmlBlock) |
15
|
|
|
{ |
16
|
|
|
$this->repo = $htmlBlock; |
|
|
|
|
17
|
|
|
$this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/html_block/"; |
|
|
|
|
18
|
|
|
$this->publicImagePath = public_path() .config('hideyo.public_path'). "/html_block/"; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The validation rules for the model. |
23
|
|
|
* |
24
|
|
|
* @param integer $htmlBlockId id attribute model |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function rules($htmlBlockId = false, $attributes = false) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$rules = array( |
30
|
|
|
'title' => 'required|between:4,65' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
if ($htmlBlockId) { |
|
|
|
|
34
|
|
|
$rules['title'] = 'required|between:4,65'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $rules; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function create(array $attributes) |
41
|
|
|
{ |
42
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
43
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
44
|
|
|
|
45
|
|
|
if ($validator->fails()) { |
46
|
|
|
return $validator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->model->fill($attributes); |
52
|
|
|
$this->model->save(); |
53
|
|
|
|
54
|
|
|
if (isset($attributes['image'])) { |
55
|
|
|
$attributes['image_file_extension'] = $attributes['image']->getClientOriginalExtension(); |
56
|
|
|
$attributes['image_file_size'] = $attributes['image']->getSize(); |
57
|
|
|
|
58
|
|
|
$rules = array( |
59
|
|
|
'file'=>'image|max:1000' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$validator = Validator::make($attributes, $rules); |
63
|
|
|
|
64
|
|
|
if ($validator->fails()) { |
65
|
|
|
return $validator; |
66
|
|
|
} else { |
67
|
|
|
$destinationPath = $this->storageImagePath.$this->model->id; |
68
|
|
|
$filename = str_replace(" ", "_", strtolower($attributes['image']->getClientOriginalName())); |
69
|
|
|
$uploadSuccess = $attributes['image']->move($destinationPath, $filename); |
70
|
|
|
|
71
|
|
|
if ($uploadSuccess) { |
72
|
|
|
$attributes['image_file_name'] = $filename; |
73
|
|
|
$attributes['image_file_path'] = $uploadSuccess->getRealPath(); |
74
|
|
|
|
75
|
|
|
$this->model->fill($attributes); |
76
|
|
|
$this->model->save(); |
77
|
|
|
|
78
|
|
|
if ($this->model->thumbnail_height and $this->model->thumbnail_width) { |
79
|
|
|
$image = Image::make($uploadSuccess->getRealPath()); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$image->resize($this->model->thumbnail_width, $this->model->thumbnail_height); |
82
|
|
|
$image->interlace(); |
83
|
|
|
|
84
|
|
|
if (!File::exists($this->publicImagePath.$this->model->id."/")) { |
85
|
|
|
File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true); |
86
|
|
|
} |
87
|
|
|
$image->save($this->publicImagePath.$this->model->id."/".$filename); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($this->model->thumbnail_height and $this->model->thumbnail_width and $this->model->image_file_name) { |
95
|
|
|
File::deleteDirectory($this->publicImagePath.$this->model->id."/"); |
96
|
|
|
$image = Image::make($this->model->image_file_path); |
97
|
|
|
|
98
|
|
|
$image->resize($this->model->thumbnail_width, $this->model->thumbnail_height); |
99
|
|
|
$image->interlace(); |
100
|
|
|
|
101
|
|
|
if (!File::exists($this->publicImagePath.$this->model->id."/")) { |
102
|
|
|
File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true); |
103
|
|
|
} |
104
|
|
|
$image->save($this->publicImagePath.$this->model->id."/".$this->model->image_file_name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
return $this->model; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function createCopy(array $attributes, $htmlBlockId) |
112
|
|
|
{ |
113
|
|
|
$product = $this->find($htmlBlockId); |
|
|
|
|
114
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
115
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
116
|
|
|
|
117
|
|
|
if ($validator->fails()) { |
118
|
|
|
return $validator; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->model->sluggify(); |
124
|
|
|
$this->model->fill($attributes); |
125
|
|
|
|
126
|
|
|
$this->model->save(); |
127
|
|
|
|
128
|
|
|
return $this->model; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function changeActive($htmlBlockId) |
132
|
|
|
{ |
133
|
|
|
$this->model = $this->find($htmlBlockId); |
|
|
|
|
134
|
|
|
|
135
|
|
|
if ($this->model) { |
136
|
|
|
$active = 1; |
137
|
|
|
|
138
|
|
|
if ($this->model->active) { |
139
|
|
|
$active = 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$attributes = array( |
143
|
|
|
'active' => $active |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$this->model->fill($attributes); |
147
|
|
|
|
148
|
|
|
$this->model->sluggify(); |
149
|
|
|
|
150
|
|
|
return $this->model->save(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
public function updateById(array $attributes, $htmlBlockId) |
159
|
|
|
{ |
160
|
|
|
$validator = Validator::make($attributes, $this->rules($htmlBlockId, $attributes)); |
161
|
|
|
|
162
|
|
|
if ($validator->fails()) { |
163
|
|
|
return $validator; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
167
|
|
|
$this->model = $this->find($htmlBlockId); |
|
|
|
|
168
|
|
|
return $this->updateEntity($attributes); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function updateEntity(array $attributes = array()) |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
$shopId = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
175
|
|
|
$shop = ShopService::find($shopId); |
|
|
|
|
176
|
|
|
|
177
|
|
|
if (count($attributes) > 0) { |
178
|
|
|
$this->model->fill($attributes); |
179
|
|
|
$this->model->save(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
if (isset($attributes['image'])) { |
184
|
|
|
$attributes['image_file_extension'] = $attributes['image']->getClientOriginalExtension(); |
185
|
|
|
$attributes['image_file_size'] = $attributes['image']->getSize(); |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
$rules = array( |
189
|
|
|
'file'=>'image|max:1000' |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$validator = Validator::make($attributes, $rules); |
193
|
|
|
|
194
|
|
|
if ($validator->fails()) { |
195
|
|
|
return $validator; |
196
|
|
|
} else { |
197
|
|
|
$destinationPath = $this->storageImagePath.$this->model->id; |
198
|
|
|
$filename = str_replace(" ", "_", strtolower($attributes['image']->getClientOriginalName())); |
199
|
|
|
File::deleteDirectory($destinationPath); |
200
|
|
|
|
201
|
|
|
$uploadSuccess = $attributes['image']->move($destinationPath, $filename); |
202
|
|
|
|
203
|
|
|
if ($uploadSuccess) { |
204
|
|
|
$attributes['image_file_name'] = $filename; |
205
|
|
|
$attributes['image_file_path'] = $uploadSuccess->getRealPath(); |
206
|
|
|
|
207
|
|
|
$this->model->fill($attributes); |
208
|
|
|
$this->model->save(); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (File::exists($this->model->image_file_path) AND $this->model->thumbnail_height and $this->model->thumbnail_width and $this->model->image_file_name) { |
214
|
|
|
$image = Image::make($this->model->image_file_path); |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
File::deleteDirectory($this->publicImagePath.$this->model->id); |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
$image->resize($this->model->thumbnail_width, $this->model->thumbnail_height); |
221
|
|
|
$image->interlace(); |
222
|
|
|
|
223
|
|
|
if (!File::exists($this->publicImagePath.$this->model->id."/")) { |
224
|
|
|
File::makeDirectory($this->publicImagePath.$this->model->id."/", 0777, true); |
225
|
|
|
} |
226
|
|
|
$image->save($this->publicImagePath.$this->model->id."/".$this->model->image_file_name); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
return $this->model; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function destroy($htmlBlockId) |
234
|
|
|
{ |
235
|
|
|
$this->model = $this->find($htmlBlockId); |
|
|
|
|
236
|
|
|
File::deleteDirectory($this->publicImagePath.$this->model->id); |
237
|
|
|
|
238
|
|
|
$destinationPath = $this->storageImagePath.$this->model->id; |
239
|
|
|
|
240
|
|
|
File::deleteDirectory($destinationPath); |
241
|
|
|
$this->model->save(); |
|
|
|
|
242
|
|
|
|
243
|
|
|
return $this->model->delete(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function selectByLimitAndOrderBy($shopId, $limit, $orderBy) |
247
|
|
|
{ |
248
|
|
|
return $this->repo->selectByLimitAndOrderBy($shopId, $limit, $orderBy); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function selectOneBySlug($shopId, $slug) |
252
|
|
|
{ |
253
|
|
|
return $this->repo->selectOneBySlug($shopId, $slug); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function selectAllActiveGroupsByShopId($shopId) |
257
|
|
|
{ |
258
|
|
|
return $this->repo->selectAllActiveGroupsByShopId($shopId); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function selectOneByShopIdAndPosition($position, $shopId) { |
262
|
|
|
return $this->repo->selectOneByShopIdAndPosition($position, $shopId); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function findByPosition($position) |
266
|
|
|
{ |
267
|
|
|
$result = $this->repo->selectOneByShopIdAndPosition($position, config()->get('app.shop_id')); |
268
|
|
|
|
269
|
|
|
if ($result) { |
270
|
|
|
return DbView::make($result)->with($result->toArray())->render(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return ''; |
274
|
|
|
} |
275
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths