-
Notifications
You must be signed in to change notification settings - Fork 1
/
Category.php
308 lines (243 loc) · 8.61 KB
/
Category.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
<?php
/**
* @license MIT
* boutique-en-ligne (maxaboom)
* Copyright (c) 2023 Abraham Ukachi, Axel Vair, Morgane Marechal, Catherine Tranchand. The Maxaboom Project Contributors.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @project boutique-en-ligne (maxaboom)
* @name User - Model
* @test test/user_model.php
* @file User.php
* @author: Morgane Marechal <[email protected]>
* @contributors: Abraham Ukachi <[email protected]>, Axel Vair <[email protected]>, Catherine Tranchand <[email protected]>
* @version: 0.0.1
*
* Example usage:
*
* 1+|> // Create a new category
* -|>
* -|> $category = Category::create([
* -|> 'title' => 'Guitars',
* -|> 'name' => 'guitars',
* -|> 'is_top' => 1
* -|> 'image' => 'guitars.jpg'
* -|> ]);
* -|>
* o=|> echo $category->id; // 2
* -|>
*
* 2+|> // Find a category by id
* -|>
* -|> $category = Category::find(2);
* -|>
* o=|> echo $category->name; // guitars
* -|>
*
* 3+|> // Get all categories
* -|>
* -|> $categories = Category::all();
* -|>
* -|> foreach ($categories as $category) {
* -|> echo $category->name;
* -|> }
* -|>
*/
// declare a namespace for this User class
namespace Maxaboom\Models;
// use these classes
use datetime;
use PDO;
use PDOException;
/**
* Class Category / Category Model
* A class that represents the `categories` table in the database.
*/
class Category extends Model {
// Define some properties here ;)
// protected properties
/**
* The table associated with this model
*
* @var string
*/
protected string $table = 'categories';
/**
* Indicates if the model should automatically connect to the database.
*
* @var bool
*/
protected bool $autoConnect = true;
/**
* All the supported fields in the `categories` table
* @var array
*/
protected array $fields = [
'id',
'title',
'name',
'is_top',
'image'
];
// public properties
public ?int $id = null;
public ?string $title = null;
public ?string $name = null;
public ?bool $is_top = null;
public ?string $image = null;
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public bool $timestamps = false;
/**
* Category constructor.
* NOTE: This constructor is called automatically when the class is instantiated
*/
public function __construct() {
// call the parent Model constructor
parent::__construct();
}
// PUBLIC STATIC SETTERS
// PUBLIC STATIC GETTERS
// PUBLIC STATIC METHODS
// PUBLIC SETTERS
// PUBLIC GETTERS
// PUBLIC METHODS
public function getAllCategories(): array {
$allProducts = $this->db->prepare("SELECT * FROM categories");
$allProducts->execute([
]);
$result = $allProducts->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function getCategoryIdByName(string $categoryName): int {
$allProducts = $this->db->prepare("SELECT id FROM categories WHERE name = '$categoryName'");
$allProducts->execute([
]);
$result = $allProducts->fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
public function getCategoryById(int $categoryId): string {
$allProducts = $this->db->prepare("SELECT name FROM categories WHERE id = $categoryId");
$allProducts->execute([
]);
$result = $allProducts->fetch(PDO::FETCH_ASSOC);
return $result['name'];
}
public function getCategoryTitreById(int $categoryId): string {
$allProducts = $this->db->prepare("SELECT titre FROM categories WHERE id = $categoryId");
$allProducts->execute([
]);
$result = $allProducts->fetch(PDO::FETCH_ASSOC);
return $result['titre'];
}
public function getSubcategoriesByCategoryId(int $categoryId) {
$subCategorie = $this->db->prepare("SELECT id, name, titre FROM sub_categories WHERE category_id=$categoryId");
$subCategorie->execute([]);
$result = $subCategorie->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function getSubcategoryIdByName(string $subCategoryName){
$allProducts = $this->db->prepare("SELECT id FROM sub_categories WHERE name = '$subCategoryName'");
$allProducts->execute([
]);
$result = $allProducts->fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
public function getSubcategoryNameById(string $subCategoryId){
$allProducts = $this->db->prepare("SELECT name FROM sub_categories WHERE id = '$subCategoryId'");
$allProducts->execute([
]);
$result = $allProducts->fetch(PDO::FETCH_ASSOC);
//var_dump($result);
return $result['name'];
}
public function getAllSubCategories(){
$subCategories = $this->db->prepare("SELECT id, name FROM sub_categories");
$subCategories->execute([]);
$result = $subCategories->fetchAll(PDO::FETCH_ASSOC);
$allSubCategories = $result;
return $allSubCategories;
}
public function createSubCategory($categoryId){
$sql = "INSERT INTO products (name, description, price, categories_id,
sub_categories_id, created_at, stock)
VALUES (:name, :description, :price, :categories_id, :sub_categories_id, :created_at, :stock)";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'name' => htmlspecialchars($name),
]);
if ($sql_exe) {
echo json_encode(['response' => 'ok', 'reussite' => 'Nouvelle sous-catégorie enregistrée']);
} else {
echo json_encode(['response' => 'not ok', 'echoue' => 'Problème enregistrement']);
}
}
public function registerSubCategory($name, $titre, $categoryId){
$sql = "INSERT INTO sub_categories (name, titre, category_id)
VALUES (:name, :titre, :category_id)";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'name' => htmlspecialchars($name),
'titre' => htmlspecialchars($titre),
'category_id' => $categoryId,
]);
if ($sql_exe) {
echo json_encode(['response' => 'ok', 'reussite' => 'Nouvelle catégorie enregistrée']);
} else {
echo json_encode(['response' => 'not ok', 'echoue' => 'Problème enregistrement']);
}
}
public function deleteSubCategory($idSubCategory){
$sql="DELETE FROM sub_categories WHERE id = :idsubcategory";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'idsubcategory' => $idSubCategory
]);
if ($sql_exe) {
return json_encode(['response' => 'ok', 'reussite' => 'Catégorie supprimée']);
} else {
return json_encode(['response' => 'not ok', 'echoue' => 'Problème']);
}
}
public function registerCategory($name, $titre){
$sql = "INSERT INTO categories (name, titre)
VALUES (:name, :titre)";
$sql_exe = $this->db->prepare($sql);
$sql_exe->execute([
'name' => htmlspecialchars($name),
'titre' => htmlspecialchars($titre),
]);
if ($sql_exe) {
echo json_encode(['response' => 'ok', 'reussite' => 'Nouvelle catégorie enregistrée']);
} else {
echo json_encode(['response' => 'not ok', 'echoue' => 'Problème enregistrement']);
}
}
// PRIVATE STATIC SETTERS
// PRIVATE STATIC GETTERS
// PRIVATE STATIC METHODS
// PRIVATE SETTERS
// PRIVATE GETTERS
// PRIVATE METHODS
}