raspisms/controllers/publics/Contact.php

264 lines
8.3 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* 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
/**
* Page des contacts.
2019-10-29 18:36:25 +01:00
*/
class Contact extends \descartes\Controller
{
private $internal_contact;
private $internal_event;
2019-10-29 18:36:25 +01:00
/**
* Cette fonction est appelée avant toute les autres :
* Elle vérifie que l'utilisateur est bien connecté.
*
2019-10-29 18:36:25 +01:00
* @return void;
*/
public function __construct()
2019-10-29 14:57:13 +01:00
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
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.
*
* @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-04 18:33:04 +01:00
return $this->render('contact/list', ['contacts' => $contacts]);
2019-10-29 18:36:25 +01:00
}
2019-10-29 18:36:25 +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
* @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
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +01:00
}
$ids = $_GET['ids'] ?? [];
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
/**
* 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');
}
/**
* 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'] ?? [];
$id_user = $_SESSION['user']['id'];
$contacts = $this->internal_contact->gets_in_for_user($id_user, $ids);
2019-10-29 14:57:13 +01:00
if (!$contacts)
{
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
2019-10-29 14:57:13 +01:00
foreach ($contacts as &$contact)
{
if ($contact['datas'])
{
$contact['datas'] = json_decode($contact['datas']);
}
}
$this->render('contact/edit', [
2019-10-29 14:57:13 +01:00
'contacts' => $contacts,
]);
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction insert un nouveau contact.
*
2019-10-29 18:36:25 +01:00
* @param $csrf : Le jeton CSRF
* @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)
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\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-11-12 19:19:55 +01:00
$id_user = $_SESSION['user']['id'];
$datas = empty($_POST['datas']) ? null : $_POST['datas'];
2019-10-29 14:57:13 +01:00
if (!$name || !$number)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Des champs sont manquants !');
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);
if (!$number)
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Numéro de téléphone incorrect.');
return $this->redirect(\descartes\Router::url('Contact', 'add'));
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +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-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce contact.');
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éé.');
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
/**
* 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-29 14:57:13 +01:00
* @return boolean;
*/
public function update($csrf)
{
if (!$this->verify_csrf($csrf))
{
2019-11-09 03:35:12 +01:00
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
2019-10-29 14:57:13 +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;
foreach ($_POST['contacts'] as $id_contact => $contact)
{
$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
}
if ($nb_contacts_update !== \count($_POST['contacts']))
{
\FlashMessage\FlashMessage::push('danger', 'Certains contacts n\'ont pas pu êtres mis à jour.');
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.');
return $this->redirect(\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.
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
}
}