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
|
|
|
|
12
|
|
|
trait ProductFunctions |
13
|
|
|
{ |
14
|
|
|
private function validateSingleProductId($data) |
15
|
|
|
{ |
16
|
|
|
if (empty($data['id'])) { |
17
|
|
|
$this->Message->message = 'ID do produto inválido'; |
|
|
|
|
18
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$id = filter_var($data['id'], FILTER_VALIDATE_INT); |
22
|
|
|
|
23
|
|
|
if (!$id) { |
24
|
|
|
$this->Message->message = 'parâmetro inválido'; |
25
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $id; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function validateProductId($data) |
32
|
|
|
{ |
33
|
|
|
if (!isset($data['id'])) { |
34
|
|
|
$this->Message->message = 'Id do produto inválido!'; |
|
|
|
|
35
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$id = filter_var($data["id"], FILTER_VALIDATE_INT); |
39
|
|
|
|
40
|
|
|
if (!$id) { |
41
|
|
|
$this->Message->message = 'Id do produto inválido!'; |
42
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$Product = new Product(); |
46
|
|
|
$Product = $Product->findById($id, 'id'); |
47
|
|
|
|
48
|
|
|
if (is_null($Product)) { |
49
|
|
|
$this->Message->message = 'Produto não encontrado!'; |
50
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $Product; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function allProductsWithoutFilters($data) |
57
|
|
|
{ |
58
|
|
|
if (!empty($data)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_null(($products = $Product->findAll()))) { |
|
|
|
|
63
|
|
|
(new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->Message->message = $products; |
67
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function validatePagination($data) |
71
|
|
|
{ |
72
|
|
|
if (!isset($data['page']) || !isset($data['limit'])) { |
73
|
|
|
$this->Message->message = [ |
|
|
|
|
74
|
|
|
'message' => 'Missing page or limit argument' |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function validateOrderAndDirection($data) |
82
|
|
|
{ |
83
|
|
|
if (!isset($data['order']) || !isset($data['direction'])) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$page = filter_var($data['page'], FILTER_VALIDATE_INT); |
88
|
|
|
$limit = filter_var($data['limit'], FILTER_VALIDATE_INT); |
89
|
|
|
|
90
|
|
|
$find = $this->validateSearchFilters($data); |
91
|
|
|
|
92
|
|
|
$find = $find->order($data['order'] . ' ' . $data['direction']); |
93
|
|
|
$find = $find->limit($limit); |
94
|
|
|
$find = $find->offset(($page * $limit) - $limit); |
95
|
|
|
$products = $find->fetch(true); |
96
|
|
|
|
97
|
|
|
if (empty($products)) { |
98
|
|
|
(new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($products as $key => $value) { |
102
|
|
|
$products[$key] = $value->data(); |
103
|
|
|
|
104
|
|
|
$products[$key]->category = (new Category())->findById((int) $products[$key]->product_type_id)->data(); |
105
|
|
|
$products[$key]->ProductImage = (new ProductImage())->findAllByProductId((int) $products[$key]->id); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->Message->message = $products; |
109
|
|
|
(new Response())->setStatusCode(HTTP_OK)->send($this->Message); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function validateSearchFilters($data) |
113
|
|
|
{ |
114
|
|
|
$Product = new Product(); |
115
|
|
|
|
116
|
|
|
if (empty($data['filterColumn'])) { |
117
|
|
|
return $Product->find(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (empty($data['selfId'])) { |
121
|
|
|
return $Product->find("{$data['filterColumn']} = {$data['filterValue']}"); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $Product->find("{$data['filterColumn']} = {$data['filterValue']} AND id != {$data['selfId']}"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function validateImageUpload($data) |
128
|
|
|
{ |
129
|
|
|
if (empty($data['image'])) { |
130
|
|
|
$this->Message->message = 'imagem nao encontrada'; |
|
|
|
|
131
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$base64Image = $data['image']; |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$base64 = getimagesizefromstring(base64_decode(explode(',', $base64Image)[1])); |
138
|
|
|
} catch (\Exception $e) { |
139
|
|
|
$this->Message->message = 'imagem invalida'; |
140
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!$base64) { |
|
|
|
|
144
|
|
|
$this->Message->message = 'imagem invalida'; |
145
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!empty($base64[0]) && !empty($base64[0]) && !empty($base64['mime'])) { |
149
|
|
|
if (!in_array($base64['mime'], $this->validExtensions)) { |
150
|
|
|
$this->Message->message = 'tipo de imagem invalida'; |
151
|
|
|
(new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $base64Image; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|