raspisms/controllers/publics/Contact.php

464 lines
15 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;
2021-04-24 00:10:23 +02:00
private $internal_conditional_group;
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);
2021-04-24 00:10:23 +02:00
$this->internal_conditional_group = new \controllers\internals\ConditionalGroup($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.
2019-10-29 18:36:25 +01:00
*/
2020-04-07 03:02:33 +02:00
public function list()
2019-10-29 14:57:13 +01:00
{
return $this->render('contact/list');
}
2021-01-14 03:32:17 +01:00
/**
2021-01-14 03:32:17 +01:00
* Return contacts as json.
*/
public function list_json()
{
2021-07-19 21:14:56 +02:00
$draw = (int) ($_GET['draw'] ?? false);
$columns = [
0 => 'name',
1 => 'number',
2 => 'created_at',
3 => 'updated_at',
];
$search = $_GET['search']['value'] ?? null;
$order_column = $columns[$_GET['order'][0]['column']] ?? null;
$order_desc = ($_GET['order'][0]['dir'] ?? 'asc') == 'desc' ? true : false;
$offset = (int) ($_GET['start'] ?? 0);
$limit = (int) ($_GET['length'] ?? 25);
$entities = $this->internal_contact->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc);
$count_entities = $this->internal_contact->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, true);
foreach ($entities as &$entity)
{
$entity['number_formatted'] = \controllers\internals\Tool::phone_link($entity['number']);
}
2021-07-19 21:14:56 +02:00
$records_total = $this->internal_contact->count_for_user($_SESSION['user']['id']);
header('Content-Type: application/json');
2021-07-19 21:14:56 +02:00
echo json_encode([
'draw' => $draw,
'recordsTotal' => $records_total,
'recordsFiltered' => $count_entities,
'data' => $entities,
]);
2019-10-29 18:36:25 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction va supprimer une liste de contacts.
*
* @param array int $_GET['contact_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['contact_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
}
2021-06-17 00:51:33 +02:00
2021-04-24 00:10:23 +02:00
/**
2021-06-17 00:51:33 +02:00
* This function will delete a list of contacts depending on a condition.
2021-04-24 00:10:23 +02:00
*
* @param string $_POST['condition'] : Condition to use to delete contacts
2021-06-17 00:51:33 +02:00
* @param mixed $csrf
2021-04-24 00:10:23 +02:00
*
* @return boolean;
*/
public function conditional_delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$condition = $_POST['condition'] ?? false;
if (!$condition)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez fournir une condition !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$contacts_to_delete = $this->internal_conditional_group->get_contacts_for_condition_and_user($_SESSION['user']['id'], $condition);
foreach ($contacts_to_delete as $contact)
{
$this->internal_contact->delete_for_user($_SESSION['user']['id'], $contact['id']);
}
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['contact_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['data'])
{
$contact['data'] = json_decode($contact['data']);
}
}
$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'];
$data = $_POST['data'] ?? [];
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_data = [];
foreach ($data 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_data[$key] = (string) $value;
}
2020-01-17 18:19:25 +01:00
$clean_data = json_encode($clean_data);
if (!$this->internal_contact->create($id_user, $number, $name, $clean_data))
{
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'];
$data = $contact['data'] ?? [];
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
$clean_data = [];
foreach ($data 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_data[$key] = (string) $value;
2019-11-28 01:55:11 +01:00
}
$clean_data = json_encode($clean_data);
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_data);
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;
2021-01-14 03:32:17 +01:00
2019-11-30 05:31:20 +01:00
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;
2021-01-14 03:32:17 +01:00
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;
2021-01-14 03:32:17 +01:00
2020-01-17 18:19:25 +01:00
default:
2021-06-17 00:51:33 +02:00
if ('csv' === $read_file['extension'])
{
$result = $this->internal_contact->import_csv($id_user, $read_file['content']);
}
2021-06-17 00:51:33 +02:00
elseif ('json' === $read_file['extension'])
{
$result = $this->internal_contact->import_json($id_user, $read_file['content']);
}
else
{
$invalid_type = true;
$result = false;
}
2019-11-30 05:31:20 +01:00
}
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-06-23 21:06:13 +02:00
$msg = (1 === $result ? '1' : 'Pas de') . ' nouveau contact inséré.';
2019-11-30 05:31:20 +01:00
if ($result > 1)
{
$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;
2021-01-14 03:32:17 +01:00
2019-11-30 05:31:20 +01:00
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;
2021-01-14 03:32:17 +01:00
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;
2021-01-14 03:32:17 +01:00
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
}
}