This repository was archived by the owner on Jul 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.php
More file actions
277 lines (229 loc) · 10.7 KB
/
Copy pathProduct.php
File metadata and controls
277 lines (229 loc) · 10.7 KB
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
<?php
require_once __DIR__ . '/BaseModel.php';
/**
* Product model that handles all database interactions related to products.
* This includes CRUD operations and any product-specific queries.
*/
class Product extends BaseModel {
/**
* Creates a new product in the database.
* @param int $id The unique identifier for the product.
* @param string $name The name of the product.
* @param string|null $description A description of the product.
* @param float $price The price of the product.
* @param int $supplier_id The ID of the supplier providing the product.
* @return bool Returns true on successful creation, false otherwise.
*/
public function createProduct($id, $name, $description, $price, $supplier_id) {
$query = "INSERT INTO product (id, name, description, price, supplier_id)
VALUES (:id, :name, :description, :price, :supplier_id);";
$stmt = $this->conn->prepare($query);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->bindValue(":name", $name, PDO::PARAM_STR);
$stmt->bindValue(":description", $description, PDO::PARAM_STR);
$stmt->bindValue(":price", $price);
$stmt->bindValue(":supplier_id", $supplier_id, PDO::PARAM_INT);
return $stmt->execute();
}
/**
* Retrieves all products from the database.
*
* @return array An array of product objects.
*/
public function getAllProducts() {
$query = "SELECT *
FROM product;";
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
/**
* Retrieves all products along with their associated supplier and categories.
* This is used for the admin product listing page to show comprehensive product details.
*
* @return array An array of product objects, each containing supplier name and a comma-separated list of categories.
*/
public function getAllProductsWithSupplierAndCategories() {
$query = "SELECT p.*, s.name AS supplier_name,
GROUP_CONCAT(pc.name SEPARATOR ', ') AS categories
FROM product p
LEFT JOIN supplier s ON p.supplier_id = s.id
LEFT JOIN product_to_category ptc ON p.id = ptc.product_id
LEFT JOIN product_category pc ON ptc.category_id = pc.id
GROUP BY p.id
ORDER BY p.name";
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
/**
* Retrieves a product by its ID.
*
* @param int $id The ID of the product to retrieve.
* @return object|false The product object if found, or false if not found.
*/
public function getProductById($id) {
$query = "SELECT p.*,
s.name AS supplier_name,
s.email AS supplier_email,
s.phone AS supplier_phone
FROM product p
LEFT JOIN supplier s ON p.supplier_id = s.id
WHERE p.id = :id";;
$stmt = $this->conn->prepare($query);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function getFiltered($search, $sort) {
$query = "SELECT * FROM product p WHERE 1";
if(!empty($search)) {
$query .= " AND name like :search";
}
if($sort == "name_desc") {
$query .= " ORDER BY name DESC";
} else if ($sort == "name_asc") {
$query .= " ORDER BY name ASC";
}
$stmt = $this->conn->prepare($query);
if(!empty($search)) {
$stmt->bindValue(":search", "%$search%");
}
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
/**
* Reusable product details used by admin edit forms.
*/
public function getProductByIdWithSupplier($id) {
$query = "SELECT p.*, s.name AS supplier_name
FROM product p
LEFT JOIN supplier s ON p.supplier_id = s.id
WHERE p.id = :id";
$stmt = $this->conn->prepare($query);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
/**
* Creates a new product with associated categories.
*
* @param string $name The name of the product.
* @param string $description The description of the product.
* @param float $price The price of the product.
* @param int $supplierId The ID of the supplier.
* @param bool $hidden Whether the product is hidden.
* @param array $categoryIds An array of category IDs to associate with the product.
* @return bool True if the product was created successfully, false otherwise.
*/
public function createProductWithCategories($name, $description, $price, $supplierId, $hidden, array $categoryIds) {
// Start a transaction to ensure data integrity
$this->conn->beginTransaction();
// Insert the product first
try {
$query = "INSERT INTO product (name, description, price, supplier_id, hidden)
VALUES (:name, :description, :price, :supplierId, :hidden)";
$stmt = $this->conn->prepare($query);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':description', $description, PDO::PARAM_STR);
$stmt->bindValue(':price', $price);
$stmt->bindValue(':supplierId', $supplierId, PDO::PARAM_INT);
$stmt->bindValue(':hidden', (bool) $hidden, PDO::PARAM_BOOL);
$stmt->execute();
// Get the ID of the newly created product
$productId = (int) $this->conn->lastInsertId();
// Link product to categories if any are provided
if (!empty($categoryIds)) {
$linkQuery = "INSERT INTO product_to_category (product_id, category_id)
VALUES (:productId, :categoryId)";
$linkStmt = $this->conn->prepare($linkQuery);
// Loop through each category ID and create the link
foreach ($categoryIds as $categoryId) {
$linkStmt->bindValue(':productId', $productId, PDO::PARAM_INT);
$linkStmt->bindValue(':categoryId', (int) $categoryId, PDO::PARAM_INT);
$linkStmt->execute();
}
}
// Commit the transaction if everything was successful
$this->conn->commit();
return true;
} catch (Throwable $e) {
$this->conn->rollBack();
error_log('Product creation failed: ' . $e->getMessage());
return false;
}
}
/**
* Updates a product with associated categories.
*
* @param int $id The ID of the product to update.
* @param string $name The updated name of the product.
* @param string $description The updated description of the product.
* @param float $price The updated price of the product.
* @param int $supplierId The updated ID of the supplier.
* @param bool $hidden Whether the product is hidden.
* @param array $categoryIds An array of category IDs to associate with the product.
* @return bool True if the product was updated successfully, false otherwise.
*/
public function updateProductWithCategories($id, $name, $description, $price, $supplierId, $hidden, array $categoryIds) {
// Start a transaction to ensure data integrity
$this->conn->beginTransaction();
// Update the product details
try {
$query = "UPDATE product
SET name = :name,
description = :description,
price = :price,
supplier_id = :supplierId,
hidden = :hidden
WHERE id = :id";
$stmt = $this->conn->prepare($query);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':description', $description, PDO::PARAM_STR);
$stmt->bindValue(':price', $price);
$stmt->bindValue(':supplierId', $supplierId, PDO::PARAM_INT);
$stmt->bindValue(':hidden', (bool) $hidden, PDO::PARAM_BOOL);
$stmt->execute();
// Remove existing category links for the product
$deleteQuery = "DELETE FROM product_to_category
WHERE product_id = :productId";
$deleteStmt = $this->conn->prepare($deleteQuery);
$deleteStmt->bindValue(':productId', $id, PDO::PARAM_INT);
$deleteStmt->execute();
// Link product to categories if any are provided
if (!empty($categoryIds)) {
$linkQuery = "INSERT INTO product_to_category (product_id, category_id)
VALUES (:productId, :categoryId)";
$linkStmt = $this->conn->prepare($linkQuery);
// Loop through each category ID and create the link
foreach ($categoryIds as $categoryId) {
$linkStmt->bindValue(':productId', $id, PDO::PARAM_INT);
$linkStmt->bindValue(':categoryId', (int) $categoryId, PDO::PARAM_INT);
$linkStmt->execute();
}
}
// Commit the transaction if everything was successful
$this->conn->commit();
return true;
} catch (Throwable $e) {
// Rollback the transaction if any error occurred
$this->conn->rollBack();
error_log('Product update failed: ' . $e->getMessage());
return false;
}
}
/**
* Deletes a product by its ID.
*
* @param int $id The ID of the product to delete.
* @return bool True if the product was deleted successfully, false otherwise.
*/
public function deleteProduct($id) {
$query = "DELETE FROM product
WHERE id = :id";
$stmt = $this->conn->prepare($query);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
}
}