-
Notifications
You must be signed in to change notification settings - Fork 1
/
modele_generique.php
197 lines (171 loc) · 6.82 KB
/
modele_generique.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
<?php
/*AudioDope - Version 1.0 - 2022
GNU GPL CopyLeft 2022-2032
Hugo COHEN - Ayoub BOUAZIZ - Steven YANG*/
if (constant("lala") != "layn")
die("wrong constant");
include_once('connexion.php');
class ModeleGenerique extends Connexion
{
public function creation_token() {
$bytes = random_bytes(20);
$_SESSION['token'] = bin2hex($bytes);
$_SESSION['token_date'] = time();
}
public function verif_token() {
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'] && time() - $_SESSION['token_date'] < 900) {
return true;
} else {
if (isset($_SESSION['token']))
$this->unset_token();
return false;
}
}
public function unset_token()
{
unset($_SESSION['token']);
unset($_SESSION['token_date']);
}
public function get_vote($idPost)
{
$vote = 0;
if (isset($_SESSION['idUser'])) {
$sql = self::$bdd->prepare('select vote from VoterPost where idUser = ? and idPost = ?');
$sql->execute(array($_SESSION['idUser'], $idPost));
if ($sql->rowcount() == 1)
$vote = $sql->fetch()['vote'];
}
return $vote;
}
public function get_votes($posts)
{
$votes = array_fill(0, count($posts), 0);
if (isset($_SESSION['idUser']) && count($posts) > 0) {
//récupère toutes les votes de l'utilisateur dans une seule requête sql
$sql = 'select VoterPost.idPost as idPost, vote from Posts left join VoterPost on Posts.idPost = VoterPost.idPost where VoterPost.idUser = ? and VoterPost.idPost in (';
for ($i = 0; $i < count($posts) - 1; $i++) {
$sql = $sql . $posts[$i]['idPost'] . ",";
}
$sql = $sql . $posts[$i]['idPost'] . ') order by datePost desc';
$tab = self::$bdd->prepare($sql);
$tab->execute(array($_SESSION['idUser']));
$tab = $tab->fetchAll();
//associe les votes au posts correspondants
$cpt = 0;
for ($i = 0; $i < count($posts) && $cpt < count($tab); $i++) {
if ($posts[$i]['idPost'] == $tab[$cpt]['idPost'])
$votes[$i] = $tab[$cpt++]['vote'];
}
}
return $votes;
}
public function get_nb_vote($idPost)
{
$nb_vote = self::$bdd->prepare('select sum(vote) as sum from VoterPost where idPost = ?');
$nb_vote->execute(array($idPost));
$nb_vote = $nb_vote->fetch()['sum'];
if ($nb_vote == NULL)
$nb_vote = 0;
return $nb_vote;
}
public function get_nb_votes($posts, $order)
{
$nb_votes = array_fill(0, count($posts), 0);
if (isset($_SESSION['idUser']) && count($posts) > 0) {
$sql = 'select VoterPost.idPost as idPost, sum(vote) as sum from Posts left join VoterPost on Posts.idPost = VoterPost.idPost where VoterPost.idPost in (';
for ($i = 0; $i < count($posts) - 1; $i++) {
$sql = $sql . $posts[$i]['idPost'] . ",";
}
if ($order==0)
$sql = $sql . $posts[$i]['idPost'] . ') group by VoterPost.idPost order by datePost desc';
else
$sql = $sql . $posts[$i]['idPost'] . ') group by VoterPost.idPost order by sum(vote) desc';
$tab = self::$bdd->prepare($sql);
$tab->execute(array($_SESSION['idUser']));
$tab = $tab->fetchAll();
$cpt = 0;
for ($i = 0; $i < count($posts) && $cpt < count($tab); $i++) {
if ($posts[$i]['idPost'] == $tab[$cpt]['idPost'])
$nb_votes[$i] = $tab[$cpt++]['sum'];
}
}
return $nb_votes;
}
public function get_tag($idPost)
{
$tags = array();
$sql = self::$bdd->prepare('select idTag, nomTag from Tags natural join AttribuerPost where idPost = ? order by idTag');
$sql->execute(array($idPost));
if ($sql->rowcount() > 0)
$tags = $sql->fetchAll();
return $tags;
}
public function aimer_tag($tags) {
$aimer_tags = array_fill(0, count($tags), 0);
if (isset($_SESSION['idUser']) && count($tags) > 0) {
$sql = 'select idTag from Apprecier where idUser = 6 and idTag in (';
for ($i = 0; $i < count($tags) - 1; $i++) {
$sql = $sql . $tags[$i]['idTag'] . ",";
}
$sql = $sql . $tags[$i]['idTag'] . ') order by idTag';
$tab = self::$bdd->prepare($sql);
$tab->execute(array($_SESSION['idUser']));
if ($tab->rowcount() > 0) {
$tab = $tab->fetchAll();
for ($i = 0; $i < count($tab); $i++) {
$aimer_tags[$i] = $tab[$i]['idTag'];
}
}
}
return $aimer_tags;
}
public function aimer_tags($tags) {
$aimer_tags = array();
if (isset($_SESSION['idUser']) && count($tags) > 0) {
$sql = 'select idTag from Apprecier where idUser = ? and idTag in (';
for ($i = 0; $i < count($tags); $i++) {
for ($j = 0; $j < count($tags[$i]); $j++) {
$sql = $sql . $tags[$i][$j]['idTag'] . ",";
}
}
$sql = $sql . ') order by idTag';
$sql = str_replace(',)', ')', $sql);
$tab = self::$bdd->prepare($sql);
$tab->execute(array($_SESSION['idUser']));
if ($tab->rowcount() > 0) {
$tab = $tab->fetchAll();
for ($i = 0; $i < count($tab); $i++) {
$aimer_tags[$i] = $tab[$i]['idTag'];
}
}
}
return $aimer_tags;
}
public function get_tags($posts)
{
$tags = array_fill(0, count($posts), array());
if (count($posts) > 0) {
$sql = 'select idPost, idTag, nomTag from Tags natural join AttribuerPost natural join Posts where idPost in (';
for ($i = 0; $i < count($posts) - 1; $i++) {
$sql = $sql . $posts[$i]['idPost'] . ",";
}
$sql = $sql . $posts[$i]['idPost'] . ') order by datePost desc, idTag';
$tab = self::$bdd->prepare($sql);
$tab->execute();
$tab = $tab->fetchAll();
$cpt = 0;
$taille = count($tab);
//divise le tab en tableau de tableau pour mettre dans tags
for ($i = 0; $i < count($posts); $i++) {
$tmp = array();
while ($cpt < $taille && $posts[$i]['idPost'] == $tab[$cpt]['idPost']) {
$tmp[] = $tab[$cpt];
unset($tab[$cpt]);
$cpt++;
}
$tags[$i] = $tmp;
}
}
return $tags;
}
}