2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
namespace controllers\publics;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page des contacts.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
class Contact extends \descartes\Controller
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_contact;
|
|
|
|
private $internal_event;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* Cette fonction est appelée avant toute les autres :
|
2019-10-30 00:30:39 +01:00
|
|
|
* Elle vérifie que l'utilisateur est bien connecté.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function __construct()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
|
|
|
$this->internal_event = new \controllers\internals\Event($bdd);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
\controllers\internals\Tool::verifyconnect();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les contacts, sous forme d'un tableau permettant l'administration de ces contacts.
|
|
|
|
*
|
|
|
|
* @param mixed $page
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function list($page = 0)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$page = (int) $page;
|
2019-11-14 22:33:00 +01:00
|
|
|
$contacts = $this->internal_contact->list_for_user($_SESSION['user']['id'], 25, $page);
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-11-04 18:33:04 +01:00
|
|
|
return $this->render('contact/list', ['contacts' => $contacts]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction va supprimer une liste de contacts.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array int $_GET['ids'] : Les id des contactes à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function delete($csrf)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
2019-11-14 22:33:00 +01:00
|
|
|
$this->internal_contact->delete_for_user($_SESSION['user']['id'], $id);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 18:33:04 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'ajout d'un contact.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$this->render('contact/add');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'édition des contacts.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param int... $ids : Les id des contactes à supprimer
|
|
|
|
*/
|
|
|
|
public function edit()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
2019-11-15 06:30:23 +01:00
|
|
|
$id_user = $_SESSION['user']['id'];
|
|
|
|
|
|
|
|
$contacts = $this->internal_contact->gets_in_for_user($id_user, $ids);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
if (!$contacts)
|
|
|
|
{
|
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
foreach ($contacts as &$contact)
|
|
|
|
{
|
|
|
|
if ($contact['datas'])
|
|
|
|
{
|
|
|
|
$contact['datas'] = json_decode($contact['datas']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$this->render('contact/edit', [
|
2019-10-29 14:57:13 +01:00
|
|
|
'contacts' => $contacts,
|
2019-10-30 00:30:39 +01:00
|
|
|
]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction insert un nouveau contact.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param string $_POST['name'] : Le nom du contact
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $_POST['phone'] : Le numero de téléphone du contact
|
|
|
|
*/
|
|
|
|
public function create($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
$name = $_POST['name'] ?? false;
|
|
|
|
$number = $_POST['number'] ?? false;
|
2019-11-12 19:19:55 +01:00
|
|
|
$id_user = $_SESSION['user']['id'];
|
2019-11-20 02:45:00 +01:00
|
|
|
$datas = empty($_POST['datas']) ? null : $_POST['datas'];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$name || !$number)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Des champs sont manquants !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'add'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$number = \controllers\internals\Tool::parse_phone($number);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$number)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Numéro de téléphone incorrect.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'add'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
$clean_datas = null;
|
|
|
|
if ($datas)
|
|
|
|
{
|
|
|
|
$clean_datas = [];
|
|
|
|
foreach ($datas as $key => $value)
|
|
|
|
{
|
|
|
|
if ($value === "")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = mb_ereg_replace('[\W]', '', $key);
|
|
|
|
$clean_datas[$key] = (string) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$clean_datas = json_encode($clean_datas);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->internal_contact->create($id_user, $number, $name, $clean_datas))
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce contact.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'add'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'Le contact a bien été créé.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction met à jour une contacte.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
|
|
|
* @param array $_POST['contacts'] : Un tableau des contactes avec leur nouvelle valeurs
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
if (!array($_POST['contacts']))
|
|
|
|
{
|
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
$nb_contacts_update = 0;
|
2019-11-20 02:45:00 +01:00
|
|
|
foreach ($_POST['contacts'] as $id_contact => $contact)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-20 02:45:00 +01:00
|
|
|
$name = $contact['name'] ?? false;
|
|
|
|
$number = $contact['number'] ?? false;
|
|
|
|
$id_user = $_SESSION['user']['id'];
|
|
|
|
$datas = empty($contact['datas']) ? null : $contact['datas'];
|
|
|
|
|
|
|
|
if (!$name || !$number)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$number = \controllers\internals\Tool::parse_phone($number);
|
|
|
|
if (!$number)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$clean_datas = null;
|
|
|
|
if ($datas)
|
|
|
|
{
|
|
|
|
$clean_datas = [];
|
|
|
|
foreach ($datas as $key => $value)
|
|
|
|
{
|
|
|
|
if ($value === "")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = mb_ereg_replace('[\W]', '', $key);
|
|
|
|
$clean_datas[$key] = (string) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$clean_datas = json_encode($clean_datas);
|
|
|
|
}
|
|
|
|
|
|
|
|
$nb_contacts_update += (int) $this->internal_contact->update_for_user($id_user, $id_contact, $number, $name, $clean_datas);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
if ($nb_contacts_update !== \count($_POST['contacts']))
|
|
|
|
{
|
2019-11-20 02:45:00 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Certains contacts n\'ont pas pu êtres mis à jour.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'Tous les contacts ont été modifiés avec succès.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la liste des contacts sous forme JSON.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function json_list()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
header('Content-Type: application/json');
|
2019-11-14 22:33:00 +01:00
|
|
|
echo json_encode($this->internal_contact->list_for_user($_SESSION['user']['id']));
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
}
|