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.
|
|
|
|
*
|
|
|
|
* @param mixed $page
|
2019-10-30 00:17:10 +01:00
|
|
|
*/
|
|
|
|
public function list($page = 0)
|
|
|
|
{
|
|
|
|
$page = (int) $page;
|
2019-11-14 22:33:00 +01:00
|
|
|
$groups = $this->internal_group->list_for_user($_SESSION['user']['id'], 25, $page);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
foreach ($groups as $key => $group)
|
|
|
|
{
|
2019-11-06 17:22:54 +01:00
|
|
|
$contacts = $this->internal_group->get_contacts($group['id']);
|
2019-10-30 00:30:39 +01:00
|
|
|
$groups[$key]['nb_contacts'] = \count($contacts);
|
2019-10-30 00:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->render('group/list', ['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 va supprimer une liste de groups.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param array int $_GET['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
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
2019-11-14 22:33:00 +01:00
|
|
|
$this->internal_group->delete_for_user($_SESSION['user']['id'], $ids);
|
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()
|
|
|
|
{
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
|
|
|
|
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)
|
|
|
|
{
|
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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
}
|