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
switch($action)
{
case "add": //On veut ajouter un ami
if (!isset($_POST['pseudo']))
{
echo '<form action="amis.php?action=add" method="post">
<p><label for="pseudo">Entrez le pseudo</label>
<input type="text" name="pseudo" id="pseudo" />
<input type="submit" value="Envoyer" />
</p></form>';
}
else
{
$pseudo_d = $_POST['pseudo'];
//On vérifie que le pseudo renvoit bien quelque chose :o
$query=$db->prepare('SELECT id, COUNT(*) AS nbr FROM membre
WHERE LOWER(login) = :pseudo GROUP BY login');
$query->bindValue(':pseudo',strtolower($pseudo_d),PDO::PARAM_STR);
$query->execute();
$data = $query->fetch();
$pseudo_exist = $data['nbr'];
$i = 0;
$id_to=$data['id'];
if(!$pseudo_exist)
{
echo '<p>Ce membre ne semble pas exister<br />
Cliquez <a href="amis.php?action=add">ici</a> pour réessayer</p>';
$i++;
}
$query->CloseCursor();
$query = $db->prepare('SELECT COUNT(*) AS nbr FROM amis
WHERE ami_from = :id AND ami_to = :id_to
OR ami_from = :id AND ami_to = :id_to');
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->bindValue(':id_to', $id_to, PDO::PARAM_INT);
$query->execute();
$deja_ami=$query->fetchColumn();
$query->CloseCursor();
if ($deja_ami != 0)
{
echo '<p>Ce membre fait déjà parti de vos amis ou a déjà proposé son amitié :p<br />
Cliquez <a href=amis.php?action=add">ici</a> pour réessayer</p>';
$i++;
}
if ($id_to == $id)
{
echo '<p>Vous ne pouvez pas vous ajouter vous même<br />
Cliquez <a href="amis.php?action=add">ici</a> pour réessayer</p>';
$i++;
}
if ($i == 0)
{
$query=$db->prepare('INSERT INTO amis (ami_from, ami_to, ami_confirm, ami_date)
VALUES(:id, :id_to, :conf, :temps)');
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->bindValue(':id_to', $id_to, PDO::PARAM_INT);
$query->bindValue(':conf','0',PDO::PARAM_STR);
$query->bindValue(':temps', time(), PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
echo '<p><a href="profile.php?id='.$data['id'].'">'.stripslashes(htmlspecialchars($pseudo_d)).'</a>
a bien été ajouté à vos amis, il faut toutefois qu il donne son accord.<br />
Cliquez <a href="amis.php">ici</a> pour retourner à la page de gestion des amis</p>';
}
}
?>
<?php
case "check":
$add = (isset($_GET['add']))?htmlspecialchars($_GET['add']):0;
if (empty($add))
{
$query = $db->prepare('SELECT ami_from, ami_date, login FROM amis
LEFT JOIN membre ON id = ami_from
WHERE ami_to = :id AND ami_confirm = :conf
ORDER BY ami_date DESC');
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->bindValue(':conf','0',PDO::PARAM_STR);
$query->execute();
echo '<table align="center"><tr>
<th class="pseudo"><strong>Pseudo</strong></th>
<th class="inscrit"><strong>Date d ajout</strong></th>
<th><strong>Action</strong></th></tr>';
if ($query->rowCount() == 0)
{
echo '<td colspan="3" align="center">Vous n avez aucune proposition</td>';
}
while ($data = $query->fetch())
{
echo '<tr><td><a href="profile.php?id='.$data['ami_from'].'&action=consulter">'.stripslashes(htmlspecialchars($data['login'])).'</a></td>
<td>'.date('d/m/Y',$data['ami_date']).'</td>
<td><a href="amis.php?action=check&add=ok&m='.$data['ami_from'].'">Accepter</a> -
<a href="amis.php?action=delete&m='.$data['ami_from'].'">Refuser</a>
</td></tr>';
}
$query->CloseCursor();
}
else
{
$membre = (int) $_GET['m'];
$query = $db->prepare('UPDATE amis SET ami_confirm = :conf
WHERE ami_from = :membre AND ami_to = :id');
$query->bindValue(':conf','1',PDO::PARAM_STR);
$query->bindValue(':membre',$membre,PDO::PARAM_INT);
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->execute();
$query->closeCursor();
echo '<p>Le membre a bien été ajouté à votre liste d ami<br />
Cliquez <a href="amis.php">ici</a> pour retourner à la liste des amis';
}
break;
?><?php
case "delete":
$membre = (int) $_GET['m'];
if (!isset($_GET['ok']))
{
echo '<p>Etes vous certain de vouloir supprimer ce membre ?<br />
<a href="amis.php?action=delete&ok=ok&m='.$membre.'">oui</a> - <a href="amis.php">non</a></p>';
}
else
{
$query = $db->prepare('DELETE FROM amis WHERE ami_from = :membre AND ami_to = :id');
$query->bindValue(':membre',$membre,PDO::PARAM_INT);
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->execute();
$query->closeCursor();
$query = $db->prepare('DELETE FROM amis WHERE ami_to = :membre AND ami_from = :id');
$query->bindValue(':membre',$membre,PDO::PARAM_INT);
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->execute();
$query->closeCursor();
echo '<p>Membre correctement supprimé :D <br />
Cliquez <a href="amis.php">ici</a> pour retourner à la liste des amis</p>';
}
break;
?><?php
default:
$query = $db->prepare('SELECT (ami_from + ami_to - :id) AS ami_id, ami_date, login
FROM amis
LEFT JOIN membre ON id = (ami_from + ami_to - :id)
WHERE (ami_from = :id OR ami_to = :id) AND ami_confirm = :conf ORDER BY login');
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->bindValue(':conf','1',PDO::PARAM_STR);
$query->execute();
echo '<table align="center"><tr>
<th class="pseudo"><strong>Pseudo</strong></th>
<th class="inscrit"><strong>Date d ajout</strong></th>
<th><strong>Action</strong></th>
<th><strong>Connecté</strong></th></tr>';
if ($query->rowCount() == 0)
{
echo '<td colspan="4" align="center">Vous n avez aucun ami pour l instant</td>';
}
while ($data = $query->fetch())
{
echo '<tr><td><a href="profile.php?id='.$data['ami_id'].'&action=consulter">'.stripslashes(htmlspecialchars($data['login'])).'</a></td>
<td>'.date('d/m/Y',$data['ami_date']).'</td>
<td><a href="new_pm.php">Envoyer un MP</a><br />
<a href="amis.php?action=delete&m='.$data['ami_id'].'">Supprimer</a></td>';
echo '<td>Non</td>';
echo '</tr>';
}
echo '</table>';
$query->CloseCursor();
//On compte le nombre de demande en cours et on met quelques liens
$query=$db->prepare('SELECT COUNT(*) FROM amis
WHERE ami_to = :id AND ami_confirm = :conf');
$query->bindValue(':id',$id,PDO::PARAM_INT);
$query->bindValue(':conf','0', PDO::PARAM_STR);
$query->execute();
$demande_ami=$query->fetchColumn();
//Cette ligne va permettre d'afficher 0 plutôt qu'un vide
if (empty($demande_ami)) {
$demande_ami=0;
echo '<br /><ul>
<li><a href="amis.php?action=add">Ajouter un ami</a></li>
<li><a href="amis.php?action=check">Voir les demandes d\'ajout ('.$demande_ami.')</a></li></ul>';
break;
}
}
?> |
Partager