-
Notifications
You must be signed in to change notification settings - Fork 1
/
Product.php
352 lines (295 loc) · 11.4 KB
/
Product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace Maxaboom\Models;
use Maxaboom\Models\Helpers\Database;
use datetime;
use PDO;
use PDOException;
class Product extends Database
{
public function __construct()
{
parent::__construct();
// $this->setDatabasePort(8888);
// $this->setDatabaseUsername('root');
// $this->setDatabasePassword('root');
// connect to the database
$this->dbConnect();
}
/**
* Returns a product with the given `productId`
*
* @param int $productId id of the product
* @return array $productData
*/
public function getProductById(int $productId): array {
$productData = [];
try {
$query = "SELECT *, products.name as productName FROM `products` INNER JOIN categories ON products.category_id = categories.id WHERE products.id = '$productId'";
$pdo_stmt = $this->db->query($query, PDO::FETCH_ASSOC);
$result = $pdo_stmt->fetch();
// update the product data
$productData = $result;
} catch (PDOException $e) {
// TODO: handle the exception
echo $e->getMessage();
die();
}
return $productData;
}
public function getReviewByProductId(int $productId): array {
$review = "SELECT *, comments.created_at as reviewDate FROM comments INNER JOIN users ON comments.user_id = users.id WHERE product_id = :productId";
$review_exe = $this->db->prepare($review);
$review_exe->execute([
'productId' => $productId
]);
$result = $review_exe->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
/**
* Returns all products
* @return array $allProducts
*/
public function getAllProducts(){
$allProducts = $this->db->prepare("SELECT * FROM `products` WHERE deleted_at IS NULL");
$allProducts->execute([
]);
$result = $allProducts->fetchAll(PDO::FETCH_ASSOC);
$allProducts = $result;
return $allProducts;
}
/**
* Returns all name from categories table
* @return array $allCategories
*/
/*
public function getAllCategories(){
$categories = $this->db->prepare("SELECT name FROM categories");
$categories->execute([]);
$result = $categories->fetchAll(PDO::FETCH_ASSOC);
$allCategories = $result;
return $allCategories;
}
*/
/**
* Returns all name from sub_categories table
* @return array $AllSubCategories
*/
/*
public function getAllSubCategories(){
$subCategories = $this->db->prepare("SELECT name FROM sub_categories");
$subCategories->execute([]);
$result = $subCategories->fetchAll(PDO::FETCH_ASSOC);
$allSubCategories = $result;
return $allSubCategories;
}
*/
/**
* Returns products by their category ID
* @return array $result
*/
public function getProductsByCategoryId(int $categoryId){
$productsCategories = $this->db->prepare("SELECT * FROM products WHERE category_id=$categoryId");
$productsCategories->execute([
]);
$result = $productsCategories->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
/**
* Returns id of category name
* @return array $result['id']
*/
/*
public function getCategoryIdByName(string $categoryName){
$selectNameCategory = $this->db->prepare("SELECT id FROM categories WHERE name = '$categoryName'");
$selectNameCategory->execute([]);
$result = $selectNameCategory->fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
*/
/**
* Returns id of sub category name
* @return array $result['id']
*/
/*
public function getSubCategoryIdByName(string $subCategoryName){
$selectNameSubCategory = $this->db->prepare("SELECT id from sub_categories WHERE name='$subCategoryName'");
$selectNameSubCategory->execute([]);
$result = $selectNameSubCategory->fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
*/
public function getProductsBySubCategoryId(int $subCategoryId){
$subCategories = $this->db->prepare("SELECT * FROM products WHERE sub_category_id=$subCategoryId");
$subCategories->execute([]);
$result = $subCategories->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function registerProduct($name,$description, $price, $category_id, $sub_category_id, $stock){
$created_at = date('Y-m-d H:i:s');
$sql = "INSERT INTO products (name, description, price, category_id,
sub_category_id, created_at, stock)
VALUES (:name, :description, :price, :category_id, :sub_category_id, :created_at, :stock)";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'name' => htmlspecialchars($name),
'description' => htmlspecialchars($description),
'price' => htmlspecialchars($price),
'category_id' => htmlspecialchars($category_id),
'sub_category_id' => htmlspecialchars($sub_category_id),
'created_at' => $created_at,
'stock' => htmlspecialchars($stock)
]);
if ($sql_exe) {
echo json_encode(['response' => 'ok', 'reussite' => 'Nouveau produit enregistré']);
} else {
echo json_encode(['response' => 'not ok', 'echoue' => 'Problème enregistrement']);
}
}
//update specific product
public function updateProduct($id, $name,$description, $price, $category_id, $sub_category_id, $stock){
$update_at = date('Y-m-d H:i:s');
$sql = "UPDATE products SET name = :name, description = :description, price = :price, category_id = :category_id,
sub_category_id = :sub_category_id, update_at = :update_at, stock = :stock WHERE id = :id";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'id' => $id,
'name' => htmlspecialchars($name),
'description' => htmlspecialchars($description),
'price' => htmlspecialchars($price),
'category_id' => htmlspecialchars($category_id),
'sub_category_id' => htmlspecialchars($sub_category_id),
'update_at' => $update_at,
'stock' => htmlspecialchars($stock)
]);
if ($sql_exe) {
echo json_encode(['response' => 'ok', 'reussite' => 'Produit modifié']);
} else {
echo json_encode(['response' => 'not ok', 'echoue' => 'Problème enregistrement']);
}
}
//get last id in DB for image name
public function getLastId(){
$getLastId = $this->db->prepare("SELECT MAX(id) FROM `products`");
$getLastId->execute([
]);
$result = $getLastId->fetchAll(PDO::FETCH_ASSOC);
return $result[0]['MAX(id)'];
}
//ADD image name with ID of the product inside
function updateImageProduct($lastProductId){
$sqlupdate = $this -> db -> prepare("UPDATE products SET image = 'img_$lastProductId.png' WHERE id = :id ");
$sqlupdate->execute([
'id' => $lastProductId,
]);
}
function getProductsByDate(int $limit = 10){
$allProducts = $this->db->prepare("SELECT * FROM products ORDER BY created_at DESC LIMIT $limit");
$allProducts->execute([]);
$result = $allProducts->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function getProductHigherPrice(int $limit = 10){
$allProducts = $this->db->prepare("SELECT * FROM products ORDER BY price DESC LIMIT $limit");
$allProducts->execute([
]);
$result = $allProducts->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function getProductLowerPrice(int $limit = 10){
$allProducts = $this->db->prepare("SELECT * FROM products ORDER BY price ASC LIMIT $limit");
$allProducts->execute([
]);
$result = $allProducts->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function deleteProductByID($productId){
if ($this->verifDeleteProduct($productId)===NULL){
$deleted_at = date('Y-m-d H:i:s');
$sqlupdate = $this -> db -> prepare("UPDATE products SET deleted_at = '$deleted_at' WHERE id = :id ");
$sqlupdate->execute([
'id' => $productId,
]);
if ($sqlupdate) {
return json_encode(['response' => 'ok', 'reussite' => 'Produit supprimé']);
}
}else {
return json_encode(['response' => 'not ok', 'echoue' => 'Le produit a déjà été supprimé']);
}
}
function verifDeleteProduct($Id){
$sql = $this-> db -> prepare("SELECT deleted_at FROM products WHERE id = :id");
$sql->execute([
'id' => $Id
]);
$results = $sql->fetch(PDO::FETCH_ASSOC);
return $results['deleted_at'];
}
function getLatestProducts($limit){
$sql_exe = $this->db->prepare("SELECT * FROM products WHERE deleted_at IS NULL ORDER BY created_at DESC LIMIT $limit");
$sql_exe->execute([]);
$results = $sql_exe->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
function getAvgRatings($productsId){
$sql_exe = $this->db->prepare("SELECT * avg(ratings), count(comments.id)
FROM comments
INNER JOIN products
ON comments.product_id = products.id
WHERE products.id = $productsId");
$sql_exe->execute([]);
$results = $sql_exe->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
function getPopularProducts($limit){
$sql_exe = $this->db->prepare("
SELECT products.*, avg(comments.ratings) AS avg_rating, COUNT(comments.id) AS nb_comments
FROM products
INNER JOIN comments
ON products.id = comments.product_id
GROUP BY products.id
ORDER BY avg_rating DESC
LIMIT $limit");
$sql_exe->execute([]);
$results = $sql_exe->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
public function productsCount(){
$displayUsers = $this->db->prepare("SELECT COUNT(*) FROM products WHERE deleted_at IS NULL");
$displayUsers->execute([
]);
$result = $displayUsers->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function getProductByName($string, int $limite = 10, ?int $category_id = null){
$sql = "SELECT id, name FROM products WHERE name LIKE '%{$string}%' LIMIT $limite";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([]);
$results = $sql_exe->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
/**
* Method used to get random products from the database
*
* @param int $limit : number of products to get
*
* @return array : array of products
*/
public function getRandomProducts(int $limit = 10): array {
// create our random sql query with a limit as `$sql`
$sql = <<<SQL
SELECT * FROM `products`
WHERE deleted_at IS NULL
AND stock > 0
ORDER BY RAND()
LIMIT $limit
SQL;
// prepare the query
$query = $this->db->prepare($sql);
// execute the query
$query->execute();
// fetch the results in an associative array
$results = $query->fetchAll(PDO::FETCH_ASSOC);
// return the results
return $results;
}
}