2019-11-27 06:25:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\publics;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Page des groups.
|
|
|
|
*/
|
|
|
|
class ConditionalGroup extends \descartes\Controller
|
|
|
|
{
|
|
|
|
private $internal_conditional_group;
|
|
|
|
private $internal_contact;
|
|
|
|
private $internal_ruler;
|
|
|
|
private $internal_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction est appelée avant toute les autres :
|
|
|
|
* Elle vérifie que l'utilisateur est bien connecté.
|
|
|
|
*
|
|
|
|
* @return void;
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
|
|
|
|
$this->internal_conditional_group = new \controllers\internals\ConditionalGroup($bdd);
|
|
|
|
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
|
|
|
$this->internal_event = new \controllers\internals\Event($bdd);
|
2020-01-17 18:36:53 +01:00
|
|
|
$this->internal_ruler = new \controllers\internals\Ruler();
|
2019-11-27 06:25:33 +01:00
|
|
|
|
|
|
|
\controllers\internals\Tool::verifyconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return all conditionnals groups for administration.
|
2019-11-27 06:25:33 +01:00
|
|
|
*
|
|
|
|
* @param mixed $page
|
|
|
|
*/
|
2020-09-27 00:47:07 +02:00
|
|
|
public function list()
|
2019-11-27 06:25:33 +01:00
|
|
|
{
|
2020-09-27 00:47:07 +02:00
|
|
|
$this->render('conditional_group/list');
|
2019-11-27 06:25:33 +01:00
|
|
|
}
|
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 conditionnals groups as json.
|
2020-09-23 03:02:13 +02:00
|
|
|
*/
|
|
|
|
public function list_json()
|
|
|
|
{
|
|
|
|
$entities = $this->internal_conditional_group->list_for_user($_SESSION['user']['id']);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['data' => $entities]);
|
|
|
|
}
|
2019-11-27 06:25:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction va supprimer une liste de groups.
|
|
|
|
*
|
2021-01-17 04:07:04 +01:00
|
|
|
* @param array int $_GET['conditional_group_ids'] : Les id des groups à supprimer
|
2019-11-27 06:25:33 +01:00
|
|
|
* @param mixed $csrf
|
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function delete($csrf)
|
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
|
|
|
|
2021-01-17 04:07:04 +01:00
|
|
|
$ids = $_GET['conditional_group_ids'] ?? [];
|
2019-11-27 06:25:33 +01:00
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
|
|
|
$this->internal_conditional_group->delete_for_user($_SESSION['user']['id'], $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction retourne la page d'ajout d'un group.
|
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$this->render('conditional_group/add');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction retourne la page d'édition des groups.
|
|
|
|
*
|
|
|
|
* @param int... $ids : Les id des groups à supprimer
|
|
|
|
*/
|
|
|
|
public function edit()
|
|
|
|
{
|
2021-01-17 04:07:04 +01:00
|
|
|
$ids = $_GET['conditional_group_ids'] ?? [];
|
2019-11-27 06:25:33 +01:00
|
|
|
|
|
|
|
$groups = $this->internal_conditional_group->gets_in_for_user($_SESSION['user']['id'], $ids);
|
|
|
|
|
|
|
|
$this->render('conditional_group/edit', [
|
|
|
|
'groups' => $groups,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction insert un nouveau group.
|
|
|
|
*
|
|
|
|
* @param $csrf : Le jeton CSRF
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $_POST['name'] : Le nom du group
|
2019-11-27 06:25:33 +01:00
|
|
|
* @param array $_POST['condition'] : The condition to used
|
|
|
|
*/
|
|
|
|
public function create($csrf)
|
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'add'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $_POST['name'] ?? false;
|
|
|
|
$condition = $_POST['condition'] ?? false;
|
|
|
|
|
|
|
|
if (!$name || !$condition)
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Des champs sont manquants !');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'add'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$id_group = $this->internal_conditional_group->create($_SESSION['user']['id'], $name, $condition);
|
|
|
|
if (!$id_group)
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce groupe.');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'add'));
|
|
|
|
}
|
|
|
|
|
|
|
|
\FlashMessage\FlashMessage::push('success', 'Le groupe a bien été créé.');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction met à jour une group.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update($csrf)
|
|
|
|
{
|
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$groups = $_POST['groups'] ?? [];
|
|
|
|
|
|
|
|
$nb_groups_update = 0;
|
|
|
|
foreach ($groups as $id => $group)
|
|
|
|
{
|
|
|
|
$nb_groups_update += (int) $this->internal_conditional_group->update_for_user($_SESSION['user']['id'], $id, $group['name'], $group['condition']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($nb_groups_update !== \count($groups))
|
|
|
|
{
|
|
|
|
\FlashMessage\FlashMessage::push('danger', 'Certains groupes n\'ont pas pu êtres mis à jour.');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
|
|
|
|
|
|
|
\FlashMessage\FlashMessage::push('success', 'Tous les groupes ont été modifiés avec succès.');
|
|
|
|
|
|
|
|
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
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_conditional_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_conditional_group->get_contacts_for_condition_and_user($_SESSION['user']['id'], $group['condition']);
|
|
|
|
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-11-28 01:55:11 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Try to get the preview of contacts for a conditionnal group.
|
|
|
|
*
|
2019-11-28 01:55:11 +01:00
|
|
|
* @param string $_POST['condition'] : Condition to apply
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-17 18:36:53 +01:00
|
|
|
* @return mixed : False on error, json string ['success' => bool, 'result' => String with contacts]
|
2019-11-28 01:55:11 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function contacts_preview()
|
2019-11-28 01:55:11 +01:00
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'success' => false,
|
|
|
|
'result' => 'Une erreur inconnue est survenue.',
|
|
|
|
];
|
|
|
|
|
|
|
|
$condition = $_POST['condition'] ?? false;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
if (!$condition)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Vous devez renseigner une condition.';
|
|
|
|
echo json_encode($return);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$internal_ruler = new \controllers\internals\Ruler();
|
2021-01-17 03:16:57 +01:00
|
|
|
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['data' => (object) null], 'contact_metas' => (object) null]);
|
2019-11-28 01:55:11 +01:00
|
|
|
if (!$valid_condition)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Syntaxe de la condition invalide.';
|
|
|
|
echo json_encode($return);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contacts = $this->internal_conditional_group->get_contacts_for_condition_and_user($_SESSION['user']['id'], $condition);
|
|
|
|
if (!$contacts)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Aucun contact dans le groupe.';
|
|
|
|
echo json_encode($return);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contacts_name = [];
|
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
|
|
|
$contacts_name[] = $contact['name'];
|
|
|
|
}
|
|
|
|
|
2021-04-24 00:10:23 +02:00
|
|
|
$visible_names = array_slice($contacts_name, 0, 5);
|
|
|
|
$how_many_more = count($contacts_name) - count($visible_names);
|
|
|
|
|
|
|
|
$result_text = 'Contacts du groupe : ' . implode(', ', $visible_names);
|
|
|
|
|
|
|
|
if ($how_many_more > 0)
|
|
|
|
{
|
2021-06-17 00:51:33 +02:00
|
|
|
$result_text .= ", et {$how_many_more} autres.";
|
2021-04-24 00:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$return['result'] = $result_text;
|
2019-11-28 01:55:11 +01:00
|
|
|
$return['success'] = true;
|
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-28 01:55:11 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return the list of groups as JSON.
|
2019-11-28 01:55:11 +01:00
|
|
|
*/
|
|
|
|
public function json_list()
|
|
|
|
{
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode($this->internal_conditional_group->list_for_user($_SESSION['user']['id']));
|
|
|
|
}
|
2019-11-27 06:25:33 +01:00
|
|
|
}
|