2019-10-30 00:17:10 +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-11-10 17:36:42 +01:00
|
|
|
namespace controllers\publics;
|
2019-10-30 00:17:10 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page des groups.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
class Group extends \descartes\Controller
|
|
|
|
{
|
|
|
|
private $internal_group;
|
|
|
|
private $internal_contact;
|
|
|
|
private $internal_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-30 00:17:10 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$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_group = new \controllers\internals\Group($bdd);
|
|
|
|
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
|
|
|
$this->internal_event = new \controllers\internals\Event($bdd);
|
|
|
|
|
|
|
|
\controllers\internals\Tool::verifyconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les groups, sous forme d'un tableau permettant l'administration de ces groups.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
2020-04-07 03:02:33 +02:00
|
|
|
public function list()
|
2019-10-30 00:17:10 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->render('group/list');
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Return groups as json.
|
2020-09-23 03:02:13 +02:00
|
|
|
*/
|
|
|
|
public function list_json()
|
|
|
|
{
|
|
|
|
$entities = $this->internal_group->list_for_user($_SESSION['user']['id']);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['data' => $entities]);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction va supprimer une liste de groups.
|
|
|
|
*
|
2021-01-17 04:07:04 +01:00
|
|
|
* @param array int $_GET['group_ids'] : Les id des groups à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function delete($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-11-06 20:34:26 +01:00
|
|
|
|
2019-11-06 17:22:54 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-17 04:07:04 +01:00
|
|
|
$ids = $_GET['group_ids'] ?? [];
|
2019-11-15 06:30:23 +01:00
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
|
|
|
$this->internal_group->delete_for_user($_SESSION['user']['id'], $id);
|
|
|
|
}
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-11-06 17:22:54 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'ajout d'un group.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$this->render('group/add');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'édition des groups.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param int... $ids : Les id des groups à supprimer
|
|
|
|
*/
|
|
|
|
public function edit()
|
|
|
|
{
|
2021-01-17 04:07:04 +01:00
|
|
|
$ids = $_GET['group_ids'] ?? [];
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-11-14 22:33:00 +01:00
|
|
|
$groups = $this->internal_group->gets_in_for_user($_SESSION['user']['id'], $ids);
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($groups as $key => $group)
|
|
|
|
{
|
2019-11-06 17:22:54 +01:00
|
|
|
$groups[$key]['contacts'] = $this->internal_group->get_contacts($group['id']);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$this->render('group/edit', [
|
2019-10-30 00:17:10 +01:00
|
|
|
'groups' => $groups,
|
2019-10-30 00:30:39 +01:00
|
|
|
]);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction insert un nouveau group.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param string $_POST['name'] : Le nom du group
|
|
|
|
* @param array $_POST['contacts'] : Les ids des contacts à mettre dans le group
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
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-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'add'));
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$name = $_POST['name'] ?? false;
|
|
|
|
$contacts_ids = $_POST['contacts'] ?? false;
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$name || !$contacts_ids)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Des champs sont manquants !');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'add'));
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:33:00 +01:00
|
|
|
$id_group = $this->internal_group->create($_SESSION['user']['id'], $name, $contacts_ids);
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$id_group)
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce groupe.');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'add'));
|
|
|
|
}
|
|
|
|
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'Le groupe a bien été créé.');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction met à jour une group.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
|
|
|
* @param array $_POST['groups'] : Un tableau des groups avec leur nouvelle valeurs & une entrée 'contacts_id' avec les ids des contacts pour chaque group
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-30 00:17:10 +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-11-06 20:34:26 +01:00
|
|
|
|
2019-11-06 17:22:54 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$groups = $_POST['groups'] ?? [];
|
|
|
|
|
|
|
|
$nb_groups_update = 0;
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($groups as $id => $group)
|
|
|
|
{
|
2020-09-02 01:08:40 +02:00
|
|
|
foreach ($group['contacts_ids'] as $key => $value)
|
|
|
|
{
|
|
|
|
$group['contacts_ids'][$key] = (int) $value;
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:33:00 +01:00
|
|
|
$nb_groups_update += (int) $this->internal_group->update_for_user($_SESSION['user']['id'], $id, $group['name'], $group['contacts_ids']);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if ($nb_groups_update !== \count($groups))
|
|
|
|
{
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Certains groupes n\'ont pas pu êtres mis à jour.');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
|
|
|
}
|
|
|
|
|
2019-11-09 03:35:12 +01:00
|
|
|
\FlashMessage\FlashMessage::push('success', 'Tous les groupes ont été modifiés avec succès.');
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Group', 'list'));
|
|
|
|
}
|
|
|
|
|
2023-02-17 05:18:57 +01:00
|
|
|
/**
|
|
|
|
* Return contacts of a group as json array
|
|
|
|
* @param int $id_group = Group id
|
|
|
|
*
|
|
|
|
* @return json
|
|
|
|
*/
|
|
|
|
public function preview (int $id_group)
|
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'success' => false,
|
|
|
|
'result' => 'Une erreur inconnue est survenue.',
|
|
|
|
];
|
|
|
|
|
|
|
|
$group = $this->internal_group->get_for_user($_SESSION['user']['id'], $id_group);
|
|
|
|
|
|
|
|
if (!$group)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Ce groupe n\'existe pas.';
|
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contacts = $this->internal_group->get_contacts($id_group);
|
|
|
|
if (!$contacts)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Aucun contact dans le groupe.';
|
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return['success'] = true;
|
|
|
|
$return['result'] = $contacts;
|
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la liste des groups sous forme JSON.
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function json_list()
|
|
|
|
{
|
|
|
|
header('Content-Type: application/json');
|
2019-11-14 22:33:00 +01:00
|
|
|
echo json_encode($this->internal_group->list_for_user($_SESSION['user']['id']));
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
}
|