raspisms/controllers/publics/Contact.php

377 lines
12 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 = $_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
2019-11-28 01:55:11 +01:00
$clean_datas = [];
foreach ($datas as $key => $value)
{
2020-01-17 18:19:25 +01:00
if ('' === $value)
{
2019-11-28 01:55:11 +01:00
continue;
}
2019-11-28 01:55:11 +01:00
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
}
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
$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
}
2020-01-17 18:19:25 +01:00
if (![$_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'];
2019-11-28 01:55:11 +01:00
$datas = $contact['datas'] ?? [];
2020-01-17 18:19:25 +01:00
if (!$name || !$number)
{
continue;
}
2020-01-17 18:19:25 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
{
continue;
}
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
$clean_datas = [];
foreach ($datas as $key => $value)
{
2020-01-17 18:19:25 +01:00
if ('' === $value)
{
2019-11-28 01:55:11 +01:00
continue;
}
2019-11-28 01:55:11 +01:00
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
}
$clean_datas = json_encode($clean_datas);
2020-01-17 18:19:25 +01:00
$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-11-30 05:31:20 +01:00
/**
2020-01-17 18:19:25 +01:00
* Allow to import a contacts list.
*
2019-11-30 05:31:20 +01:00
* @param string $csrf : Csrf token
* @param $_FILES['contacts_list_file'] : A csv file of the contacts to import
*/
2020-01-17 18:19:25 +01:00
public function import(string $csrf)
2019-11-30 05:31:20 +01:00
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$id_user = $_SESSION['user']['id'];
$upload_array = $_FILES['contacts_list_file'] ?? false;
if (!$upload_array)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez fournir un fichier de contacts à importer.');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$read_file = \controllers\internals\Tool::read_uploaded_file($upload_array);
if (!$read_file['success'])
{
\FlashMessage\FlashMessage::push('danger', $read_file['content']);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
//Try to import file
$invalid_type = false;
switch ($read_file['mime_type'])
{
2020-01-17 18:19:25 +01:00
case 'text/csv':
2019-11-30 05:31:20 +01:00
$result = $this->internal_contact->import_csv($id_user, $read_file['content']);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
break;
2020-01-17 18:19:25 +01:00
case 'application/json':
2019-11-30 05:31:20 +01:00
$result = $this->internal_contact->import_json($id_user, $read_file['content']);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
break;
2020-01-17 18:19:25 +01:00
default:
2019-11-30 05:31:20 +01:00
$invalid_type = true;
}
if ($invalid_type)
{
\FlashMessage\FlashMessage::push('danger', 'Le type de fichier n\'est pas valide.');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
2020-01-17 18:19:25 +01:00
if (false === $result)
2019-11-30 05:31:20 +01:00
{
\FlashMessage\FlashMessage::push('danger', 'Le fichier contient des erreurs. Impossible d\'importer les contacts.');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
2020-01-17 18:19:25 +01:00
$msg = $result.' nouveau contact a été inséré.';
2019-11-30 05:31:20 +01:00
if ($result > 1)
{
2020-01-17 18:19:25 +01:00
$msg = $result.' nouveaux contacts ont été insérés.';
2019-11-30 05:31:20 +01:00
}
\FlashMessage\FlashMessage::push('success', $msg);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
/**
2020-01-17 18:19:25 +01:00
* Allow to export a contacts list.
*
2019-11-30 05:31:20 +01:00
* @param $format : Format to export contacts to
*/
2020-01-17 18:19:25 +01:00
public function export(string $format)
2019-11-30 05:31:20 +01:00
{
$id_user = $_SESSION['user']['id'];
//Try to export contacts
$invalid_type = false;
switch ($format)
{
2020-01-17 18:19:25 +01:00
case 'csv':
2019-11-30 05:31:20 +01:00
$result = $this->internal_contact->export_csv($id_user);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
break;
2020-01-17 18:19:25 +01:00
case 'json':
2019-11-30 05:31:20 +01:00
$result = $this->internal_contact->export_json($id_user);
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
break;
2020-01-17 18:19:25 +01:00
default:
2019-11-30 05:31:20 +01:00
$invalid_type = true;
}
if ($invalid_type)
{
\FlashMessage\FlashMessage::push('danger', 'Le format demandé n\'est pas supporté.');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
2020-01-17 18:19:25 +01:00
if (false === $result)
2019-11-30 05:31:20 +01:00
{
\FlashMessage\FlashMessage::push('danger', 'Nous ne sommes par parveu à exporté les contacts.');
2020-01-17 18:19:25 +01:00
2019-11-30 05:31:20 +01:00
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$result['headers'] = $result['headers'] ?? [];
foreach ($result['headers'] as $header)
{
header($header);
}
echo $result['content'];
}
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
}
}