raspisms/controllers/publics/Contact.php

153 lines
5.8 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
/**
* Page des contacts
*/
class Contact extends \descartes\Controller
{
/**
* Cette fonction est appelée avant toute les autres :
* Elle vérifie que l'utilisateur est bien connecté
* @return void;
*/
public function _before()
2019-10-29 14:57:13 +01:00
{
global $bdd;
$this->bdd = $bdd;
$this->internalContact = new \controllers\internals\Contact($this->bdd);
$this->internalEvent = new \controllers\internals\Event($this->bdd);
2019-10-29 18:36:25 +01:00
\controllers\internals\Tool::verify_connect();
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne tous les contacts, sous forme d'un tableau permettant l'administration de ces contacts
*/
public function list($page = 0)
2019-10-29 14:57:13 +01:00
{
$page = (int) $page;
$contacts = $this->internalContact->get_list(25, $page);
$this->render('contact/list', ['contacts' => $contacts]);
2019-10-29 18:36:25 +01:00
}
/**
2019-10-29 14:57:13 +01:00
* Cette fonction va supprimer une liste de contacts
* @param array int $_GET['ids'] : Les id des contactes à supprimer
* @return boolean;
*/
2019-10-29 18:36:25 +01:00
public function delete($csrf)
2019-10-29 14:57:13 +01:00
{
2019-10-29 18:36:25 +01:00
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
}
$ids = $_GET['ids'] ?? [];
2019-10-29 18:36:25 +01:00
foreach ($ids as $id) {
2019-10-29 14:57:13 +01:00
$this->internalContact->delete($id);
}
2019-10-29 18:36:25 +01:00
header('Location: ' . \descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
return true;
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne la page d'ajout d'un contact
*/
public function add()
{
$this->render('contact/add');
}
/**
* Cette fonction retourne la page d'édition des contacts
* @param int... $ids : Les id des contactes à supprimer
*/
public function edit()
2019-10-29 14:57:13 +01:00
{
global $db;
$ids = $_GET['ids'] ?? [];
$contacts = $this->internalContact->get_by_ids($ids);
$this->render('contact/edit', array(
'contacts' => $contacts,
));
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction insert un nouveau contact
* @param $csrf : Le jeton CSRF
* @param string $_POST['name'] : Le nom du contact
* @param string $_POST['phone'] : Le numero de téléphone du contact
*/
public function create($csrf)
{
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
$name = $_POST['name'] ?? false;
$number = $_POST['number'] ?? false;
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
if (!$name || !$number) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Des champs sont manquants !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'add'));
}
2019-10-29 14:57:13 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
2019-10-29 18:36:25 +01:00
if (!$number) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Numéro de téléphone incorrect.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'add'));
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
if (!$this->internalContact->create($number, $name)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de créer ce contact.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'add'));
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le contact a bien été créé.');
return header('Location: ' . \descartes\Router::url('Contact', 'list'));
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
2019-10-29 14:57:13 +01:00
* Cette fonction met à jour une contacte
* @param $csrf : Le jeton CSRF
* @param array $_POST['contacts'] : Un tableau des contactes avec leur nouvelle valeurs
* @return boolean;
*/
public function update($csrf)
{
2019-10-29 18:36:25 +01:00
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
}
$nb_contacts_update = 0;
2019-10-29 18:36:25 +01:00
foreach ($_POST['contacts'] as $contact) {
2019-10-29 14:57:13 +01:00
$nb_contacts_update += $this->internalContact->update($contact['id'], $contact['number'], $contact['name']);
}
2019-10-29 18:36:25 +01:00
if ($nb_contacts_update != count($_POST['contacts'])) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Certais contacts n\'ont pas pu êtres mis à jour.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
}
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Tous les contacts ont été modifiés avec succès.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne la liste des contacts sous forme JSON
*/
public function json_list()
2019-10-29 14:57:13 +01:00
{
header('Content-Type: application/json');
echo json_encode($this->internalContact->get_list());
2019-10-29 18:36:25 +01:00
}
}