raspisms/controllers/publics/Scheduled.php

250 lines
9.7 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
/**
* Page des scheduleds
*/
class Scheduled extends \descartes\Controller
{
/**
* Cette fonction est appelée avant toute les autres :
* Elle vérifie que l'utilisateur est bien connecté
* @return void;
*/
public function _before()
2019-10-29 14:57:13 +01:00
{
global $bdd;
$this->bdd = $bdd;
$this->internalScheduled = new \controllers\internals\Scheduled($this->bdd);
$this->internalEvent = new \controllers\internals\Event($this->bdd);
2019-10-29 18:36:25 +01:00
\controllers\internals\Tool::verify_connect();
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction retourne tous les scheduleds, sous forme d'un tableau permettant l'administration de ces scheduleds
*/
public function list($page = 0)
2019-10-29 14:57:13 +01:00
{
$page = (int) $page;
$scheduleds = $this->internalScheduled->get_list(25, $page);
$this->render('scheduled/list', ['scheduleds' => $scheduleds]);
2019-10-29 18:36:25 +01:00
}
/**
2019-10-29 14:57:13 +01:00
* Cette fonction va supprimer une liste de scheduleds
* @param array int $_GET['ids'] : Les id des scheduledes à supprimer
* @return boolean;
*/
2019-10-29 18:36:25 +01:00
public function delete($csrf)
2019-10-29 14:57:13 +01:00
{
2019-10-29 18:36:25 +01:00
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
$ids = $_GET['ids'] ?? [];
2019-10-29 18:36:25 +01:00
foreach ($ids as $id) {
2019-10-29 14:57:13 +01:00
$this->internalScheduled->delete($id);
}
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', '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 scheduled
*/
public function add()
2019-10-29 14:57:13 +01:00
{
$now = new \DateTime();
$less_one_minute = new \DateInterval('PT1M');
$now->sub($less_one_minute);
$this->render('scheduled/add', [
'now' => $now->format('Y-m-d H:i'),
]);
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 retourne la page d'édition des scheduleds
* @param int... $ids : Les id des scheduledes à supprimer
*/
public function edit()
2019-10-29 14:57:13 +01:00
{
$ids = $_GET['ids'] ?? [];
$scheduleds = $this->internalScheduled->get_by_ids($ids);
//Pour chaque message on ajoute les numéros, les contacts & les groupes
2019-10-29 18:36:25 +01:00
foreach ($scheduleds as $key => $scheduled) {
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['numbers'] = [];
$scheduleds[$key]['contacts'] = [];
$scheduleds[$key]['groupes'] = [];
$numbers = $this->internalScheduled->get_numbers($scheduled['id']);
2019-10-29 18:36:25 +01:00
foreach ($numbers as $number) {
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['numbers'][] = $number['number'];
}
$contacts = $this->internalScheduled->get_contacts($scheduled['id']);
2019-10-29 18:36:25 +01:00
foreach ($contacts as $contact) {
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['contacts'][] = (int) $contact['id'];
}
$groupes = $this->internalScheduled->get_groupes($scheduled['id']);
2019-10-29 18:36:25 +01:00
foreach ($groupes as $groupe) {
2019-10-29 14:57:13 +01:00
$scheduleds[$key]['groupes'][] = (int) $groupe['id'];
}
}
$this->render('scheduled/edit', array(
'scheduleds' => $scheduleds,
));
}
2019-10-29 18:36:25 +01:00
/**
* Cette fonction insert un nouveau scheduled
* @param $csrf : Le jeton CSRF
* @param string $_POST['name'] : Le nom du scheduled
* @param string $_POST['date'] : La date d'envoie du scheduled
* @param string $_POST['numbers'] : Les numeros de téléphone du scheduled
* @param string $_POST['contacts'] : Les contacts du scheduled
* @param string $_POST['groupes'] : Les groupes du scheduled
*/
public function create($csrf)
{
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
$date = $_POST['date'] ?? false;
2019-10-29 14:57:13 +01:00
$content = $_POST['content'] ?? false;
$numbers = $_POST['numbers'] ?? [];
$contacts = $_POST['contacts'] ?? [];
$groupes = $_POST['groupes'] ?? [];
2019-10-29 18:36:25 +01:00
if (!$content) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous ne pouvez pas créer un SMS sans message.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!\controllers\internals\Tool::validate_date($date, 'Y-m-d H:i:s') && !\controllers\internals\Tool::validate_date($date, 'Y-m-d H:i')) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez fournir une date valide.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
foreach ($numbers as $key => $number) {
2019-10-29 14:57:13 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
2019-10-29 18:36:25 +01:00
if (!$number) {
2019-10-29 14:57:13 +01:00
unset($numbers[$key]);
continue;
}
2019-10-29 18:36:25 +01:00
$numbers[$key] = $number;
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!$numbers && !$contacts && !$groupes) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez renseigner au moins un destinataire pour le SMS.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
$scheduled = [
'at' => $date,
'content' => $content,
'flash' => false,
'progress' => false,
];
2019-10-29 18:36:25 +01:00
if (!$scheduled_id = $this->internalScheduled->create($scheduled, $numbers, $contacts, $groupes)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de créer le SMS.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'add'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le SMS a bien été créé pour le ' . $date . '.');
return header('Location: ' . \descartes\Router::url('Scheduled', 'list'));
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
2019-10-29 14:57:13 +01:00
* Cette fonction met à jour une schedulede
* @param $csrf : Le jeton CSRF
* @param array $_POST['scheduleds'] : Un tableau des scheduledes avec leur nouvelle valeurs + les numbers, contacts et groupes liées
* @return boolean;
*/
public function update($csrf)
{
2019-10-29 18:36:25 +01:00
if (!$this->verifyCSRF($csrf)) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
$scheduleds = $_POST['scheduleds'] ?? [];
$all_update_ok = true;
2019-10-29 18:36:25 +01:00
foreach ($scheduleds as $id_scheduled => $scheduled) {
2019-10-29 14:57:13 +01:00
$date = $scheduled['date'] ?? false;
$content = $scheduled['content'] ?? false;
$numbers = $scheduled['numbers'] ?? [];
$contacts = $scheduled['contacts'] ?? [];
$groupes = $scheduled['groupes'] ?? [];
2019-10-29 18:36:25 +01:00
if (!$content) {
2019-10-29 14:57:13 +01:00
$all_update_ok = false;
continue;
}
2019-10-29 18:36:25 +01:00
if (!\controllers\internals\Tool::validate_date($date, 'Y-m-d H:i:s') && !\controllers\internals\Tool::validate_date($date, 'Y-m-d H:i')) {
2019-10-29 14:57:13 +01:00
$all_update_ok = false;
continue;
}
2019-10-29 18:36:25 +01:00
foreach ($numbers as $key => $number) {
2019-10-29 14:57:13 +01:00
$number = \controllers\internals\Tool::parse_phone($number);
2019-10-29 18:36:25 +01:00
if (!$number) {
2019-10-29 14:57:13 +01:00
unset($numbers[$key]);
continue;
}
2019-10-29 18:36:25 +01:00
$numbers[$key] = $number;
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!$numbers && !$contacts && !$groupes) {
2019-10-29 14:57:13 +01:00
$all_update_ok = false;
continue;
}
$scheduled = [
'scheduled' => [
'id' => $id_scheduled,
'at' => $date,
'content' => $content,
'flash' => false,
'progress' => false,
],
'numbers' => $numbers,
'contacts_ids' => $contacts,
'groupes_ids' => $groupes,
];
2019-10-29 18:36:25 +01:00
if (!$this->internalScheduled->update([$scheduled])) {
2019-10-29 14:57:13 +01:00
$all_update_ok = false;
continue;
}
}
2019-10-29 18:36:25 +01:00
if (!$all_update_ok) {
2019-10-29 14:57:13 +01:00
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Certains SMS n\'ont pas pu êtres mis à jour.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Tous les SMS ont été mis à jour.');
2019-10-29 18:36:25 +01:00
return header('Location: ' . \descartes\Router::url('Scheduled', 'list'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
}