|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Source\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use stdClass; |
|
6
|
|
|
use Source\Core\Request; |
|
7
|
|
|
use Source\Core\Response; |
|
8
|
|
|
use Source\Models\Product; |
|
9
|
|
|
use Source\Models\Category; |
|
10
|
|
|
use Source\Models\ProductImage; |
|
11
|
|
|
use Source\Core\Rabbit\RabbitSender; |
|
12
|
|
|
use Source\Controllers\ProductFunctions; |
|
13
|
|
|
|
|
14
|
|
|
class Products |
|
15
|
|
|
{ |
|
16
|
|
|
use ProductFunctions; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \stdClass $Message |
|
20
|
|
|
*/ |
|
21
|
|
|
private $Message; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Request $Request |
|
25
|
|
|
*/ |
|
26
|
|
|
private $Request; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array $validExtensions |
|
30
|
|
|
*/ |
|
31
|
|
|
private $validExtensions = [ |
|
32
|
|
|
'image/jpeg', |
|
33
|
|
|
'image/jpg', |
|
34
|
|
|
'image/png' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Products model constructor... |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->Message = new stdClass(); |
|
43
|
|
|
$this->Request = new Request(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* GET the list of all products |
|
48
|
|
|
* |
|
49
|
|
|
* @param array|null $data Receive the current page and limit of then |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function products($data): void |
|
53
|
|
|
{ |
|
54
|
|
|
$Product = new Product(); |
|
55
|
|
|
|
|
56
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$this->allProductsWithoutFilters($data); |
|
59
|
|
|
$this->validatePagination($data); |
|
60
|
|
|
|
|
61
|
|
|
$page = filter_var($data['page'], FILTER_VALIDATE_INT); |
|
62
|
|
|
$limit = filter_var($data['limit'], FILTER_VALIDATE_INT); |
|
63
|
|
|
|
|
64
|
|
|
$this->validateOrderAndDirection($data); |
|
65
|
|
|
|
|
66
|
|
|
if (is_null(($products = $Product->findAll($limit, ($page * $limit) - $limit)))) { |
|
67
|
|
|
(new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->Message->message = $products; |
|
71
|
|
|
$this->Message->count = $Product->coluntAllAvailableProducts(); |
|
72
|
|
|
|
|
73
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* GET one single product |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function product($data) |
|
83
|
|
|
{ |
|
84
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
85
|
|
|
|
|
86
|
|
|
$id = $this->validateSingleProductId($data); |
|
87
|
|
|
|
|
88
|
|
|
$Product = new Product(); |
|
89
|
|
|
|
|
90
|
|
|
if (is_null(($product = $Product->findByProductId($id)))) { |
|
91
|
|
|
(new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->Message->message = $product->data(); |
|
95
|
|
|
|
|
96
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* POST insert a new product |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $data |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addProduct($data) |
|
106
|
|
|
{ |
|
107
|
|
|
(new Auth())->validateLogin(); |
|
108
|
|
|
|
|
109
|
|
|
$Product = new Product(); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($Product->getRequired() as $required) { |
|
112
|
|
|
if (!isset($data[$required])) { |
|
113
|
|
|
$this->Message->message = "parâmetro '{$required}' é requerido"; |
|
114
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
120
|
|
|
|
|
121
|
|
|
$name = filter_var(trim($data["name"]), FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
|
|
|
|
|
|
122
|
|
|
$value = filter_var($data["value"], FILTER_VALIDATE_FLOAT); |
|
123
|
|
|
$description = filter_var(trim($data["description"]), FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
|
124
|
|
|
$available = filter_var($data["available"], FILTER_VALIDATE_INT); |
|
125
|
|
|
$product_type_id = filter_var($data["product_type_id"], FILTER_VALIDATE_INT); |
|
126
|
|
|
|
|
127
|
|
|
if (!$value || !$available || !$product_type_id || empty($name) || empty($description)) { |
|
128
|
|
|
$this->Message->message = 'parâmetro inválido'; |
|
129
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$Category = new Category(); |
|
133
|
|
|
|
|
134
|
|
|
if (is_null($Category->findById($product_type_id, 'id'))) { |
|
135
|
|
|
$this->Message->message = 'esta categoria não existe'; |
|
136
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$Product = new Product(); |
|
140
|
|
|
|
|
141
|
|
|
/** Criar produto */ |
|
142
|
|
|
$Product->name = $name; |
|
143
|
|
|
$Product->value = $value; |
|
144
|
|
|
$Product->description = $description; |
|
145
|
|
|
$Product->available = $available; |
|
146
|
|
|
$Product->product_type_id = $product_type_id; |
|
147
|
|
|
|
|
148
|
|
|
if (!$Product->save()) { |
|
149
|
|
|
$this->Message->message = $Product->message(); |
|
150
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$this->Message->message = 'Produto adicionado com sucesso'; |
|
154
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* POST insert a new product image to one product |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $data |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
public function addProductImage($data) |
|
164
|
|
|
{ |
|
165
|
|
|
(new Auth())->validateLogin(); |
|
166
|
|
|
|
|
167
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
168
|
|
|
|
|
169
|
|
|
$Product = $this->validateProductId($data); |
|
170
|
|
|
|
|
171
|
|
|
$ProductImage = new ProductImage(); |
|
172
|
|
|
$imageName = md5(date('Y-m-dH:i:s', time())); |
|
173
|
|
|
|
|
174
|
|
|
$base64Image = $this->validateImageUpload($data); |
|
175
|
|
|
|
|
176
|
|
|
error_log($Product->id); |
|
177
|
|
|
error_log($imageName); |
|
178
|
|
|
// error_log($base64Image); |
|
179
|
|
|
|
|
180
|
|
|
$ProductImage->product_id = $Product->id; |
|
181
|
|
|
$ProductImage->url_slug = $imageName; |
|
182
|
|
|
$ProductImage->image = $base64Image; |
|
183
|
|
|
|
|
184
|
|
|
if (!$ProductImage->save()) { |
|
185
|
|
|
$this->Message->message = $ProductImage->message(); |
|
186
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$this->Message->message = 'Imagem adicionada com sucesso'; |
|
190
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* DELETE delete one product image by id |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $data |
|
197
|
|
|
* @return void |
|
198
|
|
|
*/ |
|
199
|
|
|
public function deleteProductImage($data) |
|
200
|
|
|
{ |
|
201
|
|
|
(new Auth())->validateLogin(); |
|
202
|
|
|
|
|
203
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
204
|
|
|
|
|
205
|
|
|
$ProductImage = new ProductImage(); |
|
206
|
|
|
|
|
207
|
|
|
if (!isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) { |
|
208
|
|
|
$this->Message->message = 'ID da imagem invalida'; |
|
209
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$id = (int) $data['id']; |
|
213
|
|
|
|
|
214
|
|
|
if (is_null($ProductImage->findById($id, 'id'))) { |
|
215
|
|
|
$this->Message->message = 'Imagem nao encontrada'; |
|
216
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if (!$ProductImage->delete('id', $id)) { |
|
220
|
|
|
$this->Message->message = $ProductImage->message(); |
|
221
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function alterProduct($data) |
|
228
|
|
|
{ |
|
229
|
|
|
(new Auth())->validateLogin(); |
|
230
|
|
|
|
|
231
|
|
|
$Product = new Product(); |
|
232
|
|
|
|
|
233
|
|
|
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED); |
|
234
|
|
|
|
|
235
|
|
|
$id = $this->validateSingleProductId($data); |
|
236
|
|
|
|
|
237
|
|
|
if (is_null(($Product = $Product->findById($id, '*')))) { |
|
238
|
|
|
$this->Message->message = 'este produto não foi encontrado'; |
|
239
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$productData = &$Product->data(); |
|
243
|
|
|
|
|
244
|
|
|
$fields = [ |
|
245
|
|
|
'name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, |
|
|
|
|
|
|
246
|
|
|
'value' => FILTER_VALIDATE_FLOAT, |
|
247
|
|
|
'description' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, |
|
248
|
|
|
'available' => FILTER_VALIDATE_INT |
|
249
|
|
|
]; |
|
250
|
|
|
|
|
251
|
|
|
foreach ($fields as $key => $filter) { |
|
252
|
|
|
if (!isset($data[$key])) { |
|
253
|
|
|
continue; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$$key = filter_var(trim($data[$key]), $filter); |
|
257
|
|
|
|
|
258
|
|
|
if (!$$key) { |
|
259
|
|
|
$this->Message->message = "Parâmetro {$key} inválido!"; |
|
260
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$productData->$key = $$key; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if (isset($data["product_type_id"])) { |
|
267
|
|
|
$product_type_id = filter_var($data["product_type_id"], FILTER_VALIDATE_INT); |
|
268
|
|
|
|
|
269
|
|
|
if (!$product_type_id) { |
|
270
|
|
|
$this->Message->message = 'parâmetro inválido'; |
|
271
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$Category = new Category(); |
|
275
|
|
|
|
|
276
|
|
|
if (is_null($Category->findById($product_type_id, 'id'))) { |
|
277
|
|
|
$this->Message->message = 'esta categoria não existe'; |
|
278
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$productData->product_type_id = $product_type_id; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
if (!$Product->save()) { |
|
285
|
|
|
$this->Message->message = $Product->message(); |
|
286
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$this->Message->message = 'Produto atualizado com sucesso'; |
|
290
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function checkoutSuccess() |
|
294
|
|
|
{ |
|
295
|
|
|
(new Auth())->validateLogin(); |
|
296
|
|
|
|
|
297
|
|
|
(new RabbitSender('email', 'email'))->sendMessage(json_encode([ |
|
298
|
|
|
'type' => 'checkout', |
|
299
|
|
|
'content' => [ |
|
300
|
|
|
'product_id' => 0 |
|
301
|
|
|
] |
|
302
|
|
|
])); |
|
303
|
|
|
|
|
304
|
|
|
$this->Message->message = 'Produto foi pedido com sucesso'; |
|
305
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|