|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Brand; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use Image; |
|
7
|
|
|
use File; |
|
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\Brand\Entity\BrandRepository; |
|
9
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
|
10
|
|
|
use Hideyo\Ecommerce\Framework\Services\Shop\ShopFacade as ShopService; |
|
11
|
|
|
|
|
12
|
|
|
class BrandService extends BaseService |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct(BrandRepository $brand) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->repo = $brand; |
|
|
|
|
|
|
17
|
|
|
$this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/brand/"; |
|
|
|
|
|
|
18
|
|
|
$this->publicImagePath = public_path() .config('hideyo.public_path'). "/brand/"; |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The validation rules for the model. |
|
23
|
|
|
* |
|
24
|
|
|
* @param integer $brandId id attribute model |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
private function rules($brandId = false, $attributes = false) |
|
28
|
|
|
{ |
|
29
|
|
|
if (isset($attributes['seo'])) { |
|
30
|
|
|
$rules = array( |
|
31
|
|
|
'meta_title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id' |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
return $rules; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$rules = array( |
|
38
|
|
|
'title' => 'required|between:4,65|unique_with:'.$this->repo->getModel()->getTable().', shop_id', |
|
39
|
|
|
'rank' => 'required|integer' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
if ($brandId) { |
|
|
|
|
|
|
43
|
|
|
$rules['title'] = $rules['title'].', '.$brandId.' = id'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $rules; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function create(array $attributes) |
|
50
|
|
|
{ |
|
51
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
52
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
|
53
|
|
|
|
|
54
|
|
|
if ($validator->fails()) { |
|
55
|
|
|
return $validator; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$this->repo->getModel()->fill($attributes); |
|
61
|
|
|
$this->repo->getModel()->save(); |
|
62
|
|
|
|
|
63
|
|
|
return $this->repo->getModel(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function createImage(array $attributes, $brandId) |
|
67
|
|
|
{ |
|
68
|
|
|
$userId = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
69
|
|
|
$shopId = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
70
|
|
|
$shop = ShopService::find($shopId); |
|
71
|
|
|
|
|
72
|
|
|
$rules = array( |
|
73
|
|
|
'file'=>'required|image|max:1000', |
|
74
|
|
|
'rank' => 'required' |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$validator = Validator::make($attributes, $rules); |
|
78
|
|
|
|
|
79
|
|
|
if ($validator->fails()) { |
|
80
|
|
|
return $validator; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$attributes['modified_by_user_id'] = $userId; |
|
84
|
|
|
|
|
85
|
|
|
$destinationPath = $this->storageImagePath.$brandId; |
|
86
|
|
|
$attributes['user_id'] = $userId; |
|
87
|
|
|
$attributes['brand_id'] = $brandId; |
|
88
|
|
|
$attributes['extension'] = $attributes['file']->getClientOriginalExtension(); |
|
89
|
|
|
$attributes['size'] = $attributes['file']->getSize(); |
|
90
|
|
|
|
|
91
|
|
|
$filename = str_replace(" ", "_", strtolower($attributes['file']->getClientOriginalName())); |
|
92
|
|
|
$uploadSuccess = $attributes['file']->move($destinationPath, $filename); |
|
93
|
|
|
|
|
94
|
|
|
if ($uploadSuccess) { |
|
95
|
|
|
$attributes['file'] = $filename; |
|
96
|
|
|
$attributes['path'] = $uploadSuccess->getRealPath(); |
|
97
|
|
|
|
|
98
|
|
|
$this->repo->getModelImage()->fill($attributes); |
|
99
|
|
|
$this->repo->getModelImage()->save(); |
|
100
|
|
|
|
|
101
|
|
|
if ($shop->thumbnail_square_sizes) { |
|
102
|
|
|
$sizes = explode(',', $shop->thumbnail_square_sizes); |
|
103
|
|
|
if ($sizes) { |
|
|
|
|
|
|
104
|
|
|
foreach ($sizes as $valueSize) { |
|
105
|
|
|
$image = Image::make($uploadSuccess->getRealPath()); |
|
106
|
|
|
$explode = explode('x', $valueSize); |
|
107
|
|
|
$image->resize($explode[0], $explode[1]); |
|
108
|
|
|
$image->interlace(); |
|
109
|
|
|
|
|
110
|
|
|
if (!File::exists($this->publicImagePath.$valueSize."/".$brandId."/")) { |
|
111
|
|
|
File::makeDirectory($this->publicImagePath.$valueSize."/".$brandId."/", 0777, true); |
|
112
|
|
|
} |
|
113
|
|
|
$image->save($this->publicImagePath.$valueSize."/".$brandId."/".$filename); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->repo->getModelImage(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function updateById(array $attributes, $brandId) |
|
123
|
|
|
{ |
|
124
|
|
|
$validator = Validator::make($attributes, $this->rules($brandId, $attributes)); |
|
125
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
126
|
|
|
if ($validator->fails()) { |
|
127
|
|
|
return $validator; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
131
|
|
|
$model = $this->find($brandId); |
|
132
|
|
|
$model->fill($attributes); |
|
133
|
|
|
$model->save; |
|
134
|
|
|
return $model; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function updateImageById(array $attributes, $brandId, $imageId) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->model = $this->find($brandId); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
if($this->model) { |
|
142
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
143
|
|
|
$image = $this->findImage($imageId); |
|
144
|
|
|
$image->fill($attributes); |
|
145
|
|
|
$image->save(); |
|
146
|
|
|
|
|
147
|
|
|
return $image; |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function destroy($brandId) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->model = $this->find($brandId); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// $url = $this->model->shop->url.route('brand.item', ['slug' => $this->model->slug], null); |
|
159
|
|
|
// $newUrl = $this->model->shop->url.route('brand.overview', array(), null); |
|
160
|
|
|
// $redirectResult = $this->redirect->create(array('active' => 1, 'url' => $url, 'redirect_url' => $newUrl, 'shop_id' => $this->model->shop_id)); |
|
161
|
|
|
|
|
162
|
|
|
$this->model->save(); |
|
|
|
|
|
|
163
|
|
|
return $this->model->delete(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function destroyImage($imageId) |
|
167
|
|
|
{ |
|
168
|
|
|
$image = $this->findImage($imageId); |
|
169
|
|
|
$filename = $this->storageImagePath.$image->brand_id."/".$image->file; |
|
170
|
|
|
$shopId = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
171
|
|
|
$shop = ShopService::find($shopId); |
|
172
|
|
|
|
|
173
|
|
|
if (File::exists($filename)) { |
|
174
|
|
|
File::delete($filename); |
|
175
|
|
|
if ($shop->thumbnail_square_sizes) { |
|
176
|
|
|
$sizes = explode(',', $shop->thumbnail_square_sizes); |
|
177
|
|
|
if ($sizes) { |
|
|
|
|
|
|
178
|
|
|
foreach ($sizes as $valueSize) { |
|
179
|
|
|
File::delete($this->publicImagePath.$valueSize."/".$image->brand_id."/".$image->file); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $image->delete(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
public function findImage($imageId) |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->repo->findImage($imageId); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function getModelImage() |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->repo->getModelImage(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function refactorAllImagesByShopId($shopId) |
|
200
|
|
|
{ |
|
201
|
|
|
$result = $this->repo->getModelImage()->get(); |
|
202
|
|
|
$shop = ShopService::find($shopId); |
|
203
|
|
|
if($result AND $shop->thumbnail_square_sizes) { |
|
204
|
|
|
foreach ($result as $productImage) { |
|
205
|
|
|
if ($shop->thumbnail_square_sizes) { |
|
206
|
|
|
$sizes = explode(',', $shop->thumbnail_square_sizes); |
|
207
|
|
|
if ($sizes) { |
|
|
|
|
|
|
208
|
|
|
foreach ($sizes as $valueSize) { |
|
209
|
|
|
if (!File::exists($this->publicImagePath.$valueSize."/".$productImage->brand_id."/")) { |
|
210
|
|
|
File::makeDirectory($this->publicImagePath.$valueSize."/".$productImage->brand_id."/", 0777, true); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if (!File::exists($this->publicImagePath.$valueSize."/".$productImage->brand_id."/".$productImage->file)) { |
|
214
|
|
|
if (File::exists($this->storageImagePath.$productImage->brand_id."/".$productImage->file)) { |
|
215
|
|
|
$image = Image::make($this->storageImagePath.$productImage->brand_id."/".$productImage->file); |
|
216
|
|
|
$explode = explode('x', $valueSize); |
|
217
|
|
|
$image->fit($explode[0], $explode[1]); |
|
218
|
|
|
$image->interlace(); |
|
219
|
|
|
$image->save($this->publicImagePath.$valueSize."/".$productImage->brand_id."/".$productImage->file); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} |