2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
|
|
|
namespace controllers\publics;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Page des receiveds
|
|
|
|
*/
|
|
|
|
class Received 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->internalReceived = new \controllers\internals\Received($this->bdd);
|
|
|
|
$this->internalContact = new \controllers\internals\Contact($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 receiveds, sous forme d'un tableau permettant l'administration de ces receiveds
|
|
|
|
*/
|
|
|
|
public function list($page = 0)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$page = (int) $page;
|
|
|
|
$limit = 25;
|
|
|
|
$receiveds = $this->internalReceived->get_list($limit, $page);
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($receiveds as $key => $received) {
|
|
|
|
if (!$contact = $this->internalContact->get_by_number($received['origin'])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$receiveds[$key]['send_by'] = $contact['name'] . ' (' . $received['origin'] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->render('received/list', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => count($receiveds)]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction retourne tous les SMS reçus aujourd'hui pour la popup
|
|
|
|
* @return json : Un tableau des SMS reçus
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function popup()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
$now = new \DateTime();
|
|
|
|
$receiveds = $this->internalReceived->get_since_by_date($now->format('Y-m-d'));
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($receiveds as $key => $received) {
|
|
|
|
if (!$contact = $this->internalContact->get_by_number($received['origin'])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$receiveds[$key]['origin'] = $contact['name'] . ' (' . $received['origin'] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$nb_received = count($receiveds);
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!isset($_SESSION['popup_nb_receiveds']) || $_SESSION['popup_nb_receiveds'] > $nb_receiveds) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$_SESSION['popup_nb_receiveds'] = $nb_received;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newly_receiveds = array_slice($receiveds, $_SESSION['popup_nb_receiveds']);
|
|
|
|
|
|
|
|
$_SESSION['popup_nb_receiveds'] = $nb_receiveds;
|
|
|
|
|
|
|
|
echo json_encode($newly_receiveds);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-29 14:57:13 +01:00
|
|
|
* Cette fonction va supprimer une liste de receiveds
|
|
|
|
* @param array int $_GET['ids'] : Les id des receivedes à 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('Received', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!\controllers\internals\Tool::is_admin()) {
|
2019-10-29 14:57:13 +01:00
|
|
|
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez être administrateur pour effectuer cette action.');
|
2019-10-29 18:36:25 +01:00
|
|
|
return header('Location: ' . \descartes\Router::url('Received', '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->internalReceived->delete($id);
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return header('Location: ' . \descartes\Router::url('Received', 'list'));
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|