-
Notifications
You must be signed in to change notification settings - Fork 0
/
modele.php
188 lines (169 loc) · 5.71 KB
/
modele.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
<?php
include_once 'config.php';
/**************************************************** USERS *******************************************/
function getUsers($nLimit = -1,)
{
$oCon = db();
$sQuery = "Select * FROM user";
$sQuery .= ' ORDER BY id DESC';
if ($nLimit > 0) $sQuery .= ' LIMIT ' . $nLimit;
$stmt = $oCon->prepare($sQuery);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function findConnectedUser($aPost, $bCheckEmail = false)
{
$oCon = db();
$sQuery = "Select * FROM user where email=:email";
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":email", $aPost['mail']);
$stmt->execute();
$aUser = $stmt->fetch(PDO::FETCH_ASSOC);
if($bCheckEmail) return $aUser;
if($aUser){
$bPassVerify = password_verify($aPost['password'], $aUser['password']);
if($bPassVerify) return $aUser;
}
return [];
}
function setUser($aData)
{
$oCon = db();
$bUserExist = false;
if (isset($aData['id']) && $aData['id']) {
$sQuery = "UPDATE user SET email=:email Where id=:id";
} else {
$sQuery = "INSERT INTO user SET firstname=:firstname, lastname=:lastname, email=:email, password=:password, admin=:admin";
if(findConnectedUser($aData, true)){
echo "<code>The user ".$aData["mail"]." already exist</code>";
$bUserExist = true;
}
}
$sPassword = password_hash($aData['password'], PASSWORD_BCRYPT, ['cost' => 12]);
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":firstname", $aData['firstname']);
$stmt->bindParam(":lastname", $aData['lastname']);
$stmt->bindParam(":email", $aData['mail']);
$stmt->bindParam(":password", $sPassword);
$bIsAdmin = isset($aData['admin']) && $aData['admin'] == '1' ? 1 : 0;
$stmt->bindParam(":admin", $bIsAdmin);
if (isset($aData['id']) && $aData['id'])
$stmt->bindParam(":id", $aData['id']);
if(!$bUserExist)
$stmt->execute();
}
function deleteUser($nId)
{
$oCon = db();
$sQuery = "DELETE FROM user Where id=:id";
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":id", $nId);
$stmt->execute();
}
/**************************************************** CATEGORIES *******************************************/
function getCategories($nLimit = -1,)
{
$oCon = db();
$sQuery = "Select * FROM category";
$sQuery .= ' ORDER BY id DESC';
if ($nLimit > 0) $sQuery .= ' LIMIT ' . $nLimit;
$stmt = $oCon->prepare($sQuery);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getCategory($nId = 0)
{
$oCon = db();
$sQuery = "Select * FROM category";
if ($nId > 0) {
$sQuery .= ' Where id=:id';
}
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":id", $nId);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function setCategories($aData)
{
$oCon = db();
if (isset($aData['id']) && $aData['id']) {
$sQuery = "UPDATE category SET name=:name Where id=:id";
} else {
$sQuery = "INSERT INTO category SET name=:name";
}
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":name", $aData['name']);
if (isset($aData['id']) && $aData['id'])
$stmt->bindParam(":id", $aData['id']);
$stmt->execute();
}
function deleteCategory($nId)
{
$oCon = db();
$sQuery = "DELETE FROM category Where id=:id";
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":id", $nId);
$stmt->execute();
}
/**************************************************** PRODUCTS *******************************************/
function getProducts($nLimit = -1, $nCategory = 0)
{
$oCon = db();
$sQuery = "Select * FROM product";
if($nCategory) $sQuery .= " Where category=:category";
$sQuery .= ' ORDER BY id DESC';
if ($nLimit > 0) $sQuery .= ' LIMIT ' . $nLimit;
$stmt = $oCon->prepare($sQuery);
if($nCategory) $stmt->bindParam(":category", $nCategory);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function setProduct($aData, $aFile)
{
$oCon = db();
if (isset($aData['id']) && $aData['id']) {
$sQuery = "UPDATE product SET name=:name, description=:description, price=:price, category=:category, filename=:filename Where id=:id";
} else {
$sQuery = "INSERT INTO product SET name=:name, description=:description, price=:price, category=:category, filename=:filename";
}
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":name", $aData['name']);
$stmt->bindParam(":description", $aData['description']);
$stmt->bindParam(":price", $aData['price']);
$stmt->bindParam(":category", $aData['category']);
$sFilename = (isset($aFile['file']) && $aFile['file']['tmp_name']) ? getFileNameUploaded($aFile) : $aData['filename'];
$stmt->bindParam(":filename", $sFilename);
if (isset($aData['id']) && $aData['id'])
$stmt->bindParam(":id", $aData['id']);
$stmt->execute();
}
function getFileNameUploaded($aFile)
{
$sUploadDir = __DIR__ . '/uploads/';
if (!is_dir($sUploadDir)) mkdir($sUploadDir, 0777);
$sFilename = 'product-'.rand(1,50).'.jpg';
$sUploadedFilename = $sUploadDir . $sFilename;
if (move_uploaded_file($aFile['file']['tmp_name'], $sUploadedFilename))
return $sFilename;
return ERROR_FILE;
}
function getProduct($nId = 0)
{
$oCon = db();
$sQuery = "Select * FROM product";
if ($nId > 0) {
$sQuery .= ' Where id=:id';
}
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":id", $nId);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function deleteProduct($nId)
{
$oCon = db();
$sQuery = "DELETE FROM product Where id=:id";
$stmt = $oCon->prepare($sQuery);
$stmt->bindParam(":id", $nId);
$stmt->execute();
}