From 65dacb5302ae167ca04568f27d964987508d76dc Mon Sep 17 00:00:00 2001 From: osaajani Date: Tue, 12 Nov 2019 17:58:07 +0100 Subject: [PATCH] Update sended, uniformize received --- controllers/internals/Received.php | 19 +++++-------------- controllers/internals/Sended.php | 26 +++++++++++++++++--------- controllers/publics/Sended.php | 16 +++++++++++++++- models/Received.php | 21 +++++++++------------ models/Sended.php | 24 ++++++++++++++++++------ templates/sended/list.php | 4 +++- 6 files changed, 67 insertions(+), 43 deletions(-) diff --git a/controllers/internals/Received.php b/controllers/internals/Received.php index 8ab4a95..c521167 100755 --- a/controllers/internals/Received.php +++ b/controllers/internals/Received.php @@ -26,24 +26,15 @@ class Received extends \descartes\InternalController } /** - * Cette fonction retourne une liste des receivedes sous forme d'un tableau. + * List sms for a user * @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 + * @param mixed(int|bool) $nb_entry : Number of entry to return + * @param mixed(int|bool) $page : Pagination, will offset $nb_entry * $page results + * @return array */ public function list($id_user, $nb_entry = null, $page = null) { - //Recupération des receivedes - $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 $this->model_received->list_for_user($id_user, $nb_entry, $nb_entry * $page); } diff --git a/controllers/internals/Sended.php b/controllers/internals/Sended.php index edfb468..356b301 100755 --- a/controllers/internals/Sended.php +++ b/controllers/internals/Sended.php @@ -24,17 +24,25 @@ namespace controllers\internals; } /** - * Cette fonction retourne une liste des sendedes sous forme d'un tableau. - * - * @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 sendedes + * List sms for a user + * @param int $id_user : user id + * @param mixed(int|bool) $nb_entry : Number of entry to return + * @param mixed(int|bool) $page : Pagination, will offset $nb_entry * $page results + * @return array */ - public function list($nb_entry = null, $page = null) + public function list($id_user, $nb_entry = null, $page = null) { - //Recupération des sendedes - return $this->model_sended->list($nb_entry, $nb_entry * $page); + return $this->model_sended->list_for_user($id_user, $nb_entry, $nb_entry * $page); + } + + /** + * Return a sended sms + * @param $id : received id + * @return array + */ + public function get($id) + { + return $this->model_sended->get($id); } /** diff --git a/controllers/publics/Sended.php b/controllers/publics/Sended.php index 7c86687..bdb2757 100755 --- a/controllers/publics/Sended.php +++ b/controllers/publics/Sended.php @@ -17,6 +17,7 @@ namespace controllers\publics; class Sended extends \descartes\Controller { private $internal_sended; + 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_sended = new \controllers\internals\Sended($bdd); + $this->internal_phone = new \controllers\internals\Phone($bdd); \controllers\internals\Tool::verifyconnect(); } @@ -41,7 +43,7 @@ namespace controllers\publics; { $page = (int) $page; $limit = 25; - $sendeds = $this->internal_sended->list($limit, $page); + $sendeds = $this->internal_sended->list($_SESSION['user']['id'], $limit, $page); $this->render('sended/list', ['sendeds' => $sendeds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($sendeds)]); } @@ -65,6 +67,18 @@ namespace controllers\publics; $ids = $_GET['ids'] ?? []; foreach ($ids as $id) { + $sended = $this->internal_sended->get($id); + if (!$sended) + { + continue; + } + + $is_owner = (bool) $this->internal_phone->get_by_number_and_user($sended['origin'], $_SESSION['user']['id']); + if (!$is_owner) + { + continue; + } + $this->internal_sended->delete($id); } diff --git a/models/Received.php b/models/Received.php index 36989e4..e79685b 100755 --- a/models/Received.php +++ b/models/Received.php @@ -30,26 +30,23 @@ namespace models; /** * 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 + * @param int $id_user : User id + * @param int $limit : Max results to return + * @param int $offset : Number of results to ignore */ - public function list_for_destinations($allowed_destinations, $limit, $offset) + public function list_for_user($id_user, $limit, $offset) { $limit = (int) $limit; $offset = (int) $offset; $query = ' SELECT * FROM received - WHERE destination '; + WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user) + LIMIT ' . $limit . ' OFFSET ' . $offset; - //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']; + $params = [ + 'id_user' => $id_user, + ]; return $this->_run_query($query, $params); } diff --git a/models/Sended.php b/models/Sended.php index b2e5a61..8f06634 100755 --- a/models/Sended.php +++ b/models/Sended.php @@ -31,14 +31,26 @@ namespace models; } /** - * Retourne une liste de sendedes sous forme d'un tableau. - * - * @param int $limit : Nombre de résultat maximum à retourner - * @param int $offset : Nombre de résultat à ingnorer + * Return a list of sms where destination in array allowed_destinations + * @param int $id_user : User id + * @param int $limit : Max results to return + * @param int $offset : Number of results to ignore */ - public function list($limit, $offset) + public function list_for_user($id_user, $limit, $offset) { - return $this->_select('sended', [], null, false, $limit, $offset); + $limit = (int) $limit; + $offset = (int) $offset; + + $query = ' + SELECT * FROM sended + WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user) + LIMIT ' . $limit . ' OFFSET ' . $offset; + + $params = [ + 'id_user' => $id_user, + ]; + + return $this->_run_query($query, $params); } /** diff --git a/templates/sended/list.php b/templates/sended/list.php index db0553b..b236965 100755 --- a/templates/sended/list.php +++ b/templates/sended/list.php @@ -43,7 +43,8 @@ # - Numéro + De + À Message Date Statut @@ -56,6 +57,7 @@ s($sended['id']); ?> + s($sended['origin']); ?> s($sended['destination']); ?> s($sended['text']); ?> s($sended['at']); ?>