Update received & scheduled

This commit is contained in:
osaajani 2019-11-12 17:37:20 +01:00
parent f609fc175f
commit 23892f5983
20 changed files with 338 additions and 142 deletions

View File

@ -3,6 +3,11 @@
*/
/** GENERAL **/
.hidden
{
display: none;
}
.no-padding
{
padding: 0 !important;

View File

@ -64,6 +64,19 @@ namespace controllers\internals;
//Recupération des contactes
return $this->model_contact->get_by_number($number);
}
/**
* Return a contact for a user by a number
* @param int $id_user : user id
* @param string $number : Contact number
* @return array
*/
public function get_by_number_and_user($number, $id_user)
{
//Recupération des contactes
return $this->model_contact->get_by_number_and_user($number, $id_user);
}
/**
* Cette fonction retourne un contact par son name.

View File

@ -17,26 +17,47 @@ namespace controllers\internals;
class Received extends \descartes\InternalController
{
private $model_received;
private $internal_phone;
public function __construct(\PDO $bdd)
{
$this->model_received = new \models\Received($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
}
/**
* Cette fonction retourne une liste des receivedes sous forme d'un tableau.
*
* @param int $id_user : user id
* @param mixed(int|bool) $nb_entry : Le nombre d'entrées à retourner par page
* @param mixed(int|bool) $page : Le numéro de page en cours
*
* @return array : La liste des receivedes
*/
public function list($nb_entry = null, $page = null)
public function list($id_user, $nb_entry = null, $page = null)
{
//Recupération des receivedes
return $this->model_received->list($nb_entry, $nb_entry * $page);
$allowed_destinations = $this->internal_phone->gets_for_user($id_user);
foreach ($allowed_destinations as &$allowed_destination)
{
$allowed_destination = $allowed_destination['number'];
}
return $this->model_received->list_for_destinations($allowed_destinations, $nb_entry, $nb_entry * $page);
}
/**
* Return a received sms
* @param $id : received id
* @return array
*/
public function get($id)
{
return $this->model_received->get($id);
}
/**
* Cette fonction retourne une liste des receivedes sous forme d'un tableau.
*
@ -51,15 +72,14 @@ class Received extends \descartes\InternalController
}
/**
* Cette fonction retourne les X dernières entrées triées par date.
*
* @param mixed false|int $nb_entry : Nombre d'entrée à retourner ou faux pour tout
*
* Cette fonction retourne les X dernières entrées triées par date for a user.
* @param int $id_user
* @param int $nb_entry : Nombre d'entrée à retourner ou faux pour tout
* @return array : Les dernières entrées
*/
public function get_lasts_by_date($nb_entry = false)
public function get_lasts_for_user_by_date($id_user, $nb_entry)
{
return $this->model_received->get_lasts_by_date($nb_entry);
return $this->model_received->get_lasts_for_user_by_date($id_user, $nb_entry);
}
/**
@ -76,14 +96,13 @@ class Received extends \descartes\InternalController
/**
* Récupère les Sms reçus depuis une date.
*
* @param $date : La date depuis laquelle on veux les Sms (au format 2014-10-25 20:10:05)
*
* @param int $id_user : User id
* @return array : Tableau avec tous les Sms depuis la date
*/
public function get_since_by_date($date)
public function get_since_by_date_for_user($date, $id_user)
{
return $this->model_received->get_since_by_date($date);
return $this->model_received->get_since_by_date_for_user($date, $id_user);
}
/**
@ -159,25 +178,26 @@ class Received extends \descartes\InternalController
}
/**
* Cette fonction permet de compter le nombre de receiveds.
*
* @return int : Le nombre d'entrées dans la table
* Count number of received sms for user
* @param int $id_user : user id
* @return int : Number of received SMS for user
*/
public function count()
public function count($id_user)
{
return $this->model_received->count();
return $this->model_received->count($id_user);
}
/**
* Cette fonction compte le nombre de receiveds par jour depuis une date.
*
* @param int $id_user : user id
* @param mixed $date
*
* @return array : un tableau avec en clef la date et en valeure le nombre de sms envoyés
*/
public function count_by_day_since($date)
public function count_for_user_by_day_since($id_user, $date)
{
$counts_by_day = $this->model_received->count_by_day_since($date);
$counts_by_day = $this->model_received->count_for_user_by_day_since($id_user, $date);
$return = [];
foreach ($counts_by_day as $count_by_day)

View File

@ -12,7 +12,7 @@
namespace controllers\internals;
/**
* Classe des scheduledes.
* Classe des scheduleds.
*/
class Scheduled extends \descartes\InternalController
{
@ -26,29 +26,43 @@ namespace controllers\internals;
}
/**
* Cette fonction retourne une liste des scheduledes sous forme d'un tableau.
* Cette fonction retourne une liste des scheduleds sous forme d'un tableau.
*
* @param int $id_user : User id
* @param mixed(int|bool) $nb_entry : Le nombre d'entrées à retourner par page
* @param mixed(int|bool) $page : Le numéro de page en cours
*
* @return array : La liste des scheduledes
* @return array : La liste des scheduleds
*/
public function list($nb_entry = null, $page = null)
public function list($id_user, $nb_entry = null, $page = null)
{
//Recupération des scheduledes
return $this->model_scheduled->list($nb_entry, $nb_entry * $page);
//Recupération des scheduleds
return $this->model_scheduled->list($id_user, $nb_entry, $nb_entry * $page);
}
/**
* Cette fonction retourne une liste des scheduledes sous forme d'un tableau.
* Cette fonction retourne une liste des scheduleds sous forme d'un tableau.
*
* @param array int $ids : Les ids des entrées à retourner
*
* @return array : La liste des scheduledes
* @return array : La liste des scheduleds
*/
public function get ($id)
{
//Recupération des scheduleds
return $this->model_scheduled->get($id);
}
/**
* Cette fonction retourne une liste des scheduleds sous forme d'un tableau.
*
* @param array int $ids : Les ids des entrées à retourner
*
* @return array : La liste des scheduleds
*/
public function gets($ids)
{
//Recupération des scheduledes
//Recupération des scheduleds
return $this->model_scheduled->gets($ids);
}
@ -67,12 +81,12 @@ namespace controllers\internals;
/**
* Cette fonction permet de compter le nombre de scheduled.
*
* @param int $id_user : User id
* @return int : Le nombre d'entrées dans la table
*/
public function count()
public function count($id_user)
{
return $this->model_scheduled->count();
return $this->model_scheduled->count($id_user);
}
/**
@ -80,7 +94,7 @@ namespace controllers\internals;
*
* @param int $id : L'id du scheduled à supprimer
*
* @return int : Le nombre de scheduledes supprimées;
* @return int : Le nombre de scheduleds supprimées;
*/
public function delete($id)
{
@ -89,25 +103,26 @@ namespace controllers\internals;
/**
* Cette fonction insert un nouveau scheduled.
*
* @param array $scheduled : Le scheduled à créer avec at, content, flash, progress
* @param int $id_user
* @param mixed $at
* @param mixed $text
* @param mixed $origin
* @param mixed $flash
* @param mixed $progress
* @param array $numbers : Les numéros auxquels envoyer le scheduled
* @param array $contacts_ids : Les ids des contact auquels envoyer le scheduled
* @param array $groups_ids : Les ids des group auxquels envoyer le scheduled
* @param mixed $at
* @param mixed $text
* @param mixed $flash
* @param mixed $progress
*
* @return mixed bool|int : false si echec, sinon l'id du nouveau scheduled inséré
*/
public function create($at, $text, $flash = false, $progress = false, $numbers = [], $contacts_ids = [], $groups_ids = [])
public function create($id_user, $at, $text, $origin = null, $flash = false, $numbers = [], $contacts_ids = [], $groups_ids = [])
{
$scheduled = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
'origin' => $origin,
'flash' => $flash,
'progress' => $progress,
];
if (!$id_scheduled = $this->model_scheduled->insert($scheduled))
@ -137,28 +152,28 @@ namespace controllers\internals;
}
/**
* Cette fonction met à jour une série de scheduledes.
* Cette fonction met à jour une série de scheduleds.
*
* @param array $scheduleds : Un tableau de scheduled à modifier avec at, content, flash, progress + pour chaque scheduled number, contact_ids, group_ids
* @param mixed $id
* @param int $id_user : User id
* @param mixed $text
* @param mixed $at
* @param mixed $origin
* @param array $numbers : Les numéros auxquels envoyer le scheduled
* @param array $contacts_ids : Les ids des contact auquels envoyer le scheduled
* @param array $groups_ids : Les ids des group auxquels envoyer le scheduled
* @param mixed $id
* @param mixed $text
* @param mixed $at
* @param mixed $contact_ids
* @param mixed $flash
* @param mixed $progress
*
* @return int : le nombre de ligne modifiées
*/
public function update($id, $text, $at, $numbers = [], $contact_ids = [], $groups_ids = [], $flash = false, $progress = false)
public function update($id, $id_user, $at, $text, $origin = null, $flash = false, $numbers = [], $contacts_ids = [], $groups_ids = [])
{
$scheduled = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
'origin' => $origin,
'flash' => $flash,
'progress' => $progress,
];
$success = $this->model_scheduled->update($id, $scheduled);
@ -172,7 +187,7 @@ namespace controllers\internals;
$this->model_scheduled->insert_scheduled_number($id, $number);
}
foreach ($contact_ids as $contact_id)
foreach ($contacts_ids as $contact_id)
{
$this->model_scheduled->insert_scheduled_contact($id, $contact_id);
}
@ -190,11 +205,11 @@ namespace controllers\internals;
*
* @param int $id_scheduled : L'id du scheduled pour lequel on veux le numéro
*
* @return array : La liste des scheduledes
* @return array : La liste des scheduleds
*/
public function get_numbers($id_scheduled)
{
//Recupération des scheduledes
//Recupération des scheduleds
return $this->model_scheduled->get_numbers($id_scheduled);
}
@ -207,7 +222,7 @@ namespace controllers\internals;
*/
public function get_contacts($id_scheduled)
{
//Recupération des scheduledes
//Recupération des scheduleds
return $this->model_scheduled->get_contacts($id_scheduled);
}
@ -220,7 +235,7 @@ namespace controllers\internals;
*/
public function get_groups($id_scheduled)
{
//Recupération des scheduledes
//Recupération des scheduleds
return $this->model_scheduled->get_groups($id_scheduled);
}

View File

@ -32,7 +32,7 @@ namespace controllers\internals;
*/
public function list(?int $nb_entry = null, ?int $page = null)
{
return $this->model_user->list($nb_entry, $page);
return $this->model_user->list($nb_entry, $page * $nb_entry);
}
/**

View File

@ -52,13 +52,15 @@ namespace controllers\publics;
*/
public function show()
{
$id_user = $_SESSION['user']['id'];
//Recupération des nombres des 4 panneaux d'accueil
$nb_contacts = $this->internal_contact->count();
$nb_groups = $this->internal_group->count();
$nb_scheduleds = $this->internal_scheduled->count();
$nb_commands = $this->internal_command->count();
$nb_sendeds = $this->internal_sended->count();
$nb_receiveds = $this->internal_received->count();
$nb_contacts = $this->internal_contact->count($id_user);
$nb_groups = $this->internal_group->count($id_user);
$nb_scheduleds = $this->internal_scheduled->count($id_user);
$nb_commands = $this->internal_command->count($id_user);
$nb_sendeds = $this->internal_sended->count($id_user);
$nb_receiveds = $this->internal_received->count($id_user);
//Création de la date d'il y a une semaine
$now = new \DateTime();
@ -68,12 +70,12 @@ namespace controllers\publics;
//Récupération des 10 derniers Sms envoyés, Sms reçus et evenements enregistrés. Par date.
$sendeds = $this->internal_sended->get_lasts_by_date(10);
$receiveds = $this->internal_received->get_lasts_by_date(10);
$receiveds = $this->internal_received->get_lasts_for_user_by_date($id_user, 10);
$events = $this->internal_event->get_lasts_by_date(10);
//Récupération du nombre de Sms envoyés et reçus depuis les 7 derniers jours
$nb_sendeds_by_day = $this->internal_sended->count_by_day_since($formated_date);
$nb_receiveds_by_day = $this->internal_received->count_by_day_since($formated_date);
$nb_receiveds_by_day = $this->internal_received->count_for_user_by_day_since($id_user, $formated_date);
//On va traduire ces données pour les afficher en graphique
$array_area_chart = [];

View File

@ -162,6 +162,7 @@ namespace controllers\publics;
$now = new \DateTime();
$now = $now->format('Y-m-d H:i:s');
$id_user = $_SESSION['user']['id'];
$at = $now;
$text = $_POST['text'] ?? '';
$numbers = $_POST['numbers'] ?? false;
@ -175,7 +176,7 @@ namespace controllers\publics;
return false;
}
if (!$this->internal_scheduled->create($at, $text, false, false, $numbers))
if (!$this->internal_scheduled->create($id_user, $at, $text, false, false, $numbers))
{
$return['success'] = false;
$return['message'] = 'Impossible de créer le Sms';

View File

@ -18,6 +18,7 @@ namespace controllers\publics;
{
private $internal_received;
private $internal_contact;
private $internal_phone;
/**
* Cette fonction est appelée avant toute les autres :
@ -30,6 +31,7 @@ namespace controllers\publics;
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_received = new \controllers\internals\Received($bdd);
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -43,7 +45,7 @@ namespace controllers\publics;
{
$page = (int) $page;
$limit = 25;
$receiveds = $this->internal_received->list($limit, $page);
$receiveds = $this->internal_received->list($_SESSION['user']['id'], $limit, $page);
foreach ($receiveds as $key => $received)
{
@ -58,6 +60,43 @@ namespace controllers\publics;
$this->render('received/list', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($receiveds)]);
}
/**
* Delete Receiveds
* @param array int $_GET['ids'] : Ids of receiveds to delete
* @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('Received', 'list'));
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$received = $this->internal_received->get($id);
if (!$received)
{
continue;
}
$is_owner = (bool) $this->internal_phone->get_by_number_and_user($received['destination'], $_SESSION['user']['id']);
if (!$is_owner)
{
continue;
}
$this->internal_received->delete($id);
}
return $this->redirect(\descartes\Router::url('Received', 'list'));
}
/**
* Cette fonction retourne tous les Sms reçus aujourd'hui pour la popup.
*
@ -66,7 +105,7 @@ namespace controllers\publics;
public function popup()
{
$now = new \DateTime();
$receiveds = $this->internal_received->get_since_by_date($now->format('Y-m-d'));
$receiveds = $this->internal_received->get_since_by_date_for_user($now->format('Y-m-d'), $_SESSION['user']['id']);
foreach ($receiveds as $key => $received)
{
@ -93,37 +132,4 @@ namespace controllers\publics;
return true;
}
/**
* Cette fonction va supprimer une liste de receiveds.
*
* @param array int $_GET['ids'] : Les id des receivedes à supprimer
* @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('Received', 'list'));
}
if (!\controllers\internals\Tool::is_admin())
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez être administrateur pour effectuer cette action.');
return $this->redirect(\descartes\Router::url('Received', 'list'));
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$this->internal_received->delete($id);
}
return $this->redirect(\descartes\Router::url('Received', 'list'));
}
}

View File

@ -17,6 +17,7 @@ namespace controllers\publics;
class Scheduled extends \descartes\Controller
{
private $internal_scheduled;
private $internal_phone;
/**
* Cette fonction est appelée avant toute les autres :
@ -28,6 +29,7 @@ namespace controllers\publics;
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -40,7 +42,7 @@ namespace controllers\publics;
public function list($page = 0)
{
$page = (int) $page;
$scheduleds = $this->internal_scheduled->list(25, $page);
$scheduleds = $this->internal_scheduled->list($_SESSION['user']['id'], 25, $page);
$this->render('scheduled/list', ['scheduleds' => $scheduleds]);
}
@ -64,6 +66,12 @@ namespace controllers\publics;
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$scheduled = $this->internal_scheduled->get($id);
if (!$scheduled || $scheduled['id_user'] !== $_SESSION['user']['id'])
{
continue;
}
$this->internal_scheduled->delete($id);
}
@ -79,8 +87,11 @@ namespace controllers\publics;
$less_one_minute = new \DateInterval('PT1M');
$now->sub($less_one_minute);
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
$this->render('scheduled/add', [
'now' => $now->format('Y-m-d H:i'),
'phones' => $phones,
]);
}
@ -93,11 +104,23 @@ namespace controllers\publics;
{
$ids = $_GET['ids'] ?? [];
if (!$ids)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez choisir des messages à mettre à jour !');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
$scheduleds = $this->internal_scheduled->gets($ids);
//Pour chaque message on ajoute les numéros, les contacts & les groups
foreach ($scheduleds as $key => $scheduled)
{
if (!$scheduled || $scheduled['id_user'] !== $_SESSION['user']['id'])
{
continue;
}
$scheduleds[$key]['numbers'] = [];
$scheduleds[$key]['contacts'] = [];
$scheduleds[$key]['groups'] = [];
@ -123,6 +146,7 @@ namespace controllers\publics;
$this->render('scheduled/edit', [
'scheduleds' => $scheduleds,
'phones' => $phones,
]);
}
@ -145,8 +169,11 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$id_user = $_SESSION['user']['id'];
$at = $_POST['at'] ?? false;
$text = $_POST['text'] ?? false;
$flash = $_POST['flash'] ?? false;
$origin = empty($_POST['origin']) ? null : $_POST['origin'];
$numbers = $_POST['numbers'] ?? [];
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
@ -182,15 +209,21 @@ namespace controllers\publics;
if (!$numbers && !$contacts && !$groups)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner au moins un destinataire pour le Sms.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$scheduled_id = $this->internal_scheduled->create($at, $text, false, false, $numbers, $contacts, $groups);
if ($origin && !$this->internal_phone->get_by_number_and_user($origin, $_SESSION['user']['id']))
{
\FlashMessage\FlashMessage::push('danger', 'Ce numéro n\'existe pas ou vous n\'en êtes pas propriétaire.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $origin, $flash, $numbers, $contacts, $groups);
if (!$scheduled_id)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
@ -222,12 +255,23 @@ namespace controllers\publics;
foreach ($scheduleds as $id_scheduled => $scheduled)
{
$id_user = $_SESSION['user']['id'];
$at = $scheduled['at'] ?? false;
$text = $scheduled['text'] ?? false;
$origin = empty($scheduled['origin']) ? null : $scheduled['origin'];
$flash = $scheduled['flash'] ?? false;
$numbers = $scheduled['numbers'] ?? [];
$contacts = $scheduled['contacts'] ?? [];
$groups = $scheduled['groups'] ?? [];
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
{
$all_update_ok = false;
continue;
}
if (empty($text))
{
$all_update_ok = false;
@ -245,7 +289,6 @@ namespace controllers\publics;
foreach ($numbers as $key => $number)
{
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
{
unset($numbers[$key]);
@ -262,8 +305,15 @@ namespace controllers\publics;
continue;
}
if ($origin && !$this->internal_phone->get_by_number_and_user($origin, $_SESSION['user']['id']))
{
\FlashMessage\FlashMessage::push('danger', 'Ce numéro n\'existe pas ou vous n\'en êtes pas propriétaire.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$success = $this->internal_scheduled->update($id_scheduled, $text, $at, $numbers, $contacts, $groups);
$success = $this->internal_scheduled->update($id_scheduled, $id_user, $at, $text, $origin, $flash, $numbers, $contacts, $groups);
if (!$success)
{
$all_update_ok = false;
@ -275,12 +325,10 @@ namespace controllers\publics;
if (!$all_update_ok)
{
\FlashMessage\FlashMessage::push('danger', 'Certains SMS n\'ont pas pu êtres mis à jour.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Tous les SMS ont été mis à jour.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
}

View File

@ -68,6 +68,9 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}
$settings = $this->internal_setting->gets_for_user($_SESSION['user']['id']);
$_SESSION['user']['settings'] = $settings;
\FlashMessage\FlashMessage::push('success', 'Le réglage a bien été mis à jour.');
return $this->redirect(\descartes\Router::url('Setting', 'show'));
}

View File

@ -149,9 +149,9 @@ CREATE TABLE IF NOT EXISTS phone
id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
number VARCHAR(25) NOT NULL,
platform VARCHAR(100) NOT NULL,
platform_datas TEXT NOT NULL,
CHECK (JSON_VALID(platform_datas)),
adapter VARCHAR(100) NOT NULL,
adapter_datas TEXT DEFAULT NULL,
CHECK (JSON_VALID(adapter_datas)),
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE
);

View File

@ -43,6 +43,17 @@ namespace models;
return isset($contacts[0]) ? $contacts[0] : false;
}
/**
* Return a contact for a user by a number
* @param int $id_user : user id
* @param string $number : Contact number
* @return array
*/
public function get_by_number_and_user($number, $id_user)
{
return $this->_select_one('contact', ['number' => $number, 'id_user' => $id_user]);
}
/**
* Retourne une entrée par son numéro de tel.

View File

@ -25,32 +25,59 @@ namespace models;
*/
public function get($id)
{
$receiveds = $this->_select('received', ['id' => $id]);
return isset($receiveds[0]) ? $receiveds[0] : false;
return $this->_select_one('received', ['id' => $id]);
}
/**
* Retourne une liste de receivedes sous forme d'un tableau.
*
* Return a list of sms where destination in array allowed_destinations
* @param array $allowed_destinations : Allowed destinations for sms
* @param int $limit : Nombre de résultat maximum à retourner
* @param int $offset : Nombre de résultat à ingnorer
*/
public function list($limit, $offset)
public function list_for_destinations($allowed_destinations, $limit, $offset)
{
return $this->_select('received', [], null, false, $limit, $offset);
$limit = (int) $limit;
$offset = (int) $offset;
$query = '
SELECT * FROM received
WHERE destination ';
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
$generated_in = $this->_generate_in_from_array($allowed_destinations);
$query .= $generated_in['QUERY'];
$query .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = $generated_in['PARAMS'];
return $this->_run_query($query, $params);
}
/**
* Cette fonction retourne les X dernières entrées triées par date.
* Cette fonction retourne les X dernières entrées triées par date pour un utilisateur.
*
* @param int $id_user : User id
* @param int $nb_entry : Nombre d'entrée à retourner
*
* @return array : Les dernières entrées
*/
public function get_lasts_by_date($nb_entry)
public function get_lasts_for_user_by_date($id_user, $nb_entry)
{
return $this->_select('received', [], 'at', true, $nb_entry);
$nb_entry = (int) $nb_entry;
$query = '
SELECT *
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
ORDER BY at ASC
LIMIT ' . $nb_entry;
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
}
/**
@ -138,33 +165,45 @@ namespace models;
}
/**
* Compte le nombre d'entrées dans la table.
*
* @return int : Le nombre d'entrées
* Count number of received sms for user
* @param int $id_user : user id
* @return int : Number of received SMS for user
*/
public function count()
public function count($id_user)
{
return $this->_count('received');
$query = '
SELECT COUNT(id) as nb
FROM received
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
';
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
}
/**
* Récupère le nombre de SMS envoyés pour chaque jour depuis une date.
*
* @param int $id_user : user id
* @param \DateTime $date : La date depuis laquelle on veux les SMS
*
* @return array : Tableau avec le nombre de SMS depuis la date
*/
public function count_by_day_since($date)
public function count_for_user_by_day_since($id_user, $date)
{
$query = "
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
FROM received
WHERE at > :date
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
GROUP BY at_ymd
";
$params = [
'date' => $date,
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
@ -186,23 +225,23 @@ namespace models;
}
/**
* Récupère les SMS reçus depuis une date.
*
* Get SMS received since a date for a user
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
*
* @param int $id_user : User id
* @return array : Tableau avec tous les SMS depuis la date
*/
public function get_since_by_date($date)
public function get_since_by_date_for_user($date, $id_user)
{
$query = "
SELECT *
FROM received
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
ORDER BY at ASC
";
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
ORDER BY at ASC";
$params = [
'date' => $date,
'id_user' => $id_user,
];
return $this->_run_query($query, $params);

View File

@ -32,13 +32,13 @@ namespace models;
/**
* Retourne une liste de scheduledes sous forme d'un tableau.
*
* @param int $id_user : User id
* @param int $limit : Nombre de résultat maximum à retourner
* @param int $offset : Nombre de résultat à ingnorer
*/
public function list($limit, $offset)
public function list($id_user, $limit, $offset)
{
return $this->_select('scheduled', [], null, false, $limit, $offset);
return $this->_select('scheduled', ['id_user' => $id_user], null, false, $limit, $offset);
}
/**
@ -184,9 +184,9 @@ namespace models;
*
* @return int : Le nombre d'entrées
*/
public function count()
public function count($id_user)
{
return $this->_count('scheduled');
return $this->_count('scheduled', ['id_user' => $id_user]);
}
/**

View File

@ -129,6 +129,16 @@
'create' => '/user/create/{csrf}/',
'delete' => '/user/delete/{csrf}/',
],
'Phone' => [
'list' => [
'/phone/',
'/phone/p/{page}/',
],
'add' => '/phone/add/',
'create' => '/phone/create/{csrf}/',
'delete' => '/phone/delete/{csrf}/',
],
);
define('ROUTES', $routes);

View File

@ -14,7 +14,7 @@
<?php if (ENV == 'dev') { ?>
<script>
<?php while ($message = \FlashMessage\FlashMessage::next()) { ?>
alert('<?php echo $message['type'] . ' : ' . $message['text']; ?>');
alert('<?php $this->s($message['type'] . ' : ' . $message['text']); ?>');
<?php } ?>
</script>
<?php } ?>

View File

@ -78,6 +78,9 @@
<li <?php echo $page == 'users' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('User', 'list'); ?>"><i class="fa fa-fw fa-user"></i> Utilisateurs</a>
</li>
<li <?php echo $page == 'phones' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Phone', 'list'); ?>"><i class="fa fa-fw fa-phone"></i> Téléphones</a>
</li>
<?php if ($_SESSION['user']['admin']) { ?>
<li <?php echo $page == 'settings' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Setting', 'show'); ?>"><i class="fa fa-fw fa-cogs"></i> Réglages</a>

View File

@ -43,7 +43,8 @@
<thead>
<tr>
<th>#</th>
<th>Numéro</th>
<th>De</th>
<th>À</th>
<th>Message</th>
<th>Date</th>
<th>Commande</th>
@ -55,6 +56,7 @@
<tr>
<td><?php $this->s($received['id']); ?></td>
<td><?php $this->s($received['origin']); ?></td>
<td><?php $this->s($received['destination']); ?></td>
<td><?php $this->s($received['text']); ?></td>
<td><?php $this->s($received['at']); ?></td>
<td><?php echo $received['command'] ? 'Oui' : 'Non'; ?></td>

View File

@ -73,6 +73,15 @@
</div>
</div>
<?php } ?>
<div class="form-group">
<label>Téléphone à employer : </label>
<select name="origin" class="form-control">
<option value="">N'importe lequel</option>
<?php foreach ($phones as $phone) { ?>
<option value="<?php $this->s($phone['number']); ?>"><?php $this->s($phone['number']); ?></option>
<?php } ?>
</select>
</div>
<a class="btn btn-danger" href="<?php echo \descartes\Router::url('Scheduled', 'list'); ?>">Annuler</a>
<input type="submit" class="btn btn-success" value="Enregistrer le SMS" />
</form>

View File

@ -76,6 +76,15 @@
</div>
</div>
<?php } ?>
<div class="form-group">
<label>Téléphone à employer : </label>
<select name="scheduleds[<?php $this->s($scheduled['id']); ?>][origin]" class="form-control">
<option <?php echo ($scheduled['origin'] ? '' : 'selected="selected"'); ?> value="">N'importe lequel</option>
<?php foreach ($phones as $phone) { ?>
<option <?php echo ($scheduled['origin'] == $phone['number'] ? 'selected="selected"' : '' ); ?> value="<?php $this->s($phone['number']); ?>"><?php $this->s($phone['number']); ?></option>
<?php } ?>
</select>
</div>
<hr/>
<?php } ?>
<a class="btn btn-danger" href="<?php echo \descartes\Router::url('Scheduled', 'list'); ?>">Annuler</a>