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