2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of PHP CS Fixer.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT 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
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page des scheduleds.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
class Scheduled extends \descartes\Controller
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_scheduled;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* 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-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function __construct()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
\controllers\internals\Tool::verifyconnect();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne tous les scheduleds, sous forme d'un tableau permettant l'administration de ces scheduleds.
|
|
|
|
*
|
|
|
|
* @param mixed $page
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function list($page = 0)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$page = (int) $page;
|
2019-10-30 00:17:10 +01:00
|
|
|
$scheduleds = $this->internal_scheduled->get_list(25, $page);
|
2019-10-29 14:57:13 +01:00
|
|
|
$this->render('scheduled/list', ['scheduleds' => $scheduleds]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction va supprimer une liste de scheduleds.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array int $_GET['ids'] : Les id des scheduledes à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
* @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
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$this->internal_scheduled->delete($id);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'ajout d'un scheduled.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne la page d'édition des scheduleds.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param int... $ids : Les id des scheduledes à supprimer
|
|
|
|
*/
|
|
|
|
public function edit()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$ids = $_GET['ids'] ?? [];
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$scheduleds = $this->internal_scheduled->get_by_ids($ids);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//Pour chaque message on ajoute les numéros, les contacts & les groups
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($scheduleds as $key => $scheduled)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$scheduleds[$key]['numbers'] = [];
|
|
|
|
$scheduleds[$key]['contacts'] = [];
|
2019-10-30 00:17:10 +01:00
|
|
|
$scheduleds[$key]['groups'] = [];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$numbers = $this->internal_scheduled->get_numbers($scheduled['id']);
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($numbers as $number)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$scheduleds[$key]['numbers'][] = $number['number'];
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$contacts = $this->internal_scheduled->get_contacts($scheduled['id']);
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($contacts as $contact)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$scheduleds[$key]['contacts'][] = (int) $contact['id'];
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$groups = $this->internal_scheduled->get_groups($scheduled['id']);
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($groups as $group)
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$scheduleds[$key]['groups'][] = (int) $group['id'];
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$this->render('scheduled/edit', [
|
2019-10-29 14:57:13 +01:00
|
|
|
'scheduleds' => $scheduleds,
|
2019-10-30 00:30:39 +01:00
|
|
|
]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction insert un nouveau scheduled.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
2019-10-30 00:30:39 +01:00
|
|
|
* @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
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $_POST['contacts'] : Les contacts du scheduled
|
2019-10-30 00:30:39 +01:00
|
|
|
* @param string $_POST['groups'] : Les groups du scheduled
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function create($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +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'] ?? [];
|
2019-10-30 00:17:10 +01:00
|
|
|
$groups = $_POST['groups'] ?? [];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$content)
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous ne pouvez pas créer un Sms sans message.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +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-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
foreach ($numbers as $key => $number)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$number = \controllers\internals\Tool::parse_phone($number);
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$number)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
unset($numbers[$key]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
$numbers[$key] = $number;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$numbers && !$contacts && !$groups)
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez renseigner au moins un destinataire pour le Sms.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$scheduled = [
|
|
|
|
'at' => $date,
|
|
|
|
'content' => $content,
|
|
|
|
'flash' => false,
|
|
|
|
'progress' => false,
|
|
|
|
];
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$scheduled_id = $this->internal_scheduled->create($scheduled, $numbers, $contacts, $groups))
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de créer le Sms.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le Sms a bien été créé pour le '.$date.'.');
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', '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
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction met à jour une schedulede.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param $csrf : Le jeton CSRF
|
2019-10-30 00:17:10 +01:00
|
|
|
* @param array $_POST['scheduleds'] : Un tableau des scheduledes avec leur nouvelle valeurs + les numbers, contacts et groups liées
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return boolean;
|
|
|
|
*/
|
|
|
|
public function update($csrf)
|
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
$scheduleds = $_POST['scheduleds'] ?? [];
|
|
|
|
|
|
|
|
$all_update_ok = true;
|
|
|
|
|
2019-10-30 00:30:39 +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'] ?? [];
|
2019-10-30 00:17:10 +01:00
|
|
|
$groups = $scheduled['groups'] ?? [];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$content)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$all_update_ok = false;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +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;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
foreach ($numbers as $key => $number)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$number = \controllers\internals\Tool::parse_phone($number);
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$number)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
unset($numbers[$key]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
$numbers[$key] = $number;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$numbers && !$contacts && !$groups)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$all_update_ok = false;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheduled = [
|
|
|
|
'scheduled' => [
|
|
|
|
'id' => $id_scheduled,
|
|
|
|
'at' => $date,
|
|
|
|
'content' => $content,
|
|
|
|
'flash' => false,
|
|
|
|
'progress' => false,
|
|
|
|
],
|
|
|
|
'numbers' => $numbers,
|
|
|
|
'contacts_ids' => $contacts,
|
2019-10-30 00:17:10 +01:00
|
|
|
'groups_ids' => $groups,
|
2019-10-29 14:57:13 +01:00
|
|
|
];
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->internal_scheduled->update([$scheduled]))
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$all_update_ok = false;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$all_update_ok)
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Certains Sms n\'ont pas pu êtres mis à jour.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Tous les Sms ont été mis à jour.');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|