From aee9fc847f02090b7e3c158ecfdfa40928d086c7 Mon Sep 17 00:00:00 2001 From: osaajani Date: Sat, 11 Jan 2020 17:27:07 +0100 Subject: [PATCH] Use correct phone for response to discussion --- adapters/OvhSmsAdapter.php | 2 +- controllers/internals/Received.php | 12 ++++++++++++ controllers/internals/Sended.php | 12 ++++++++++++ controllers/publics/Discussion.php | 24 ++++++++++++++++++++---- models/Received.php | 27 +++++++++++++++++++++++++++ models/Sended.php | 28 ++++++++++++++++++++++++++++ templates/discussion/show.php | 5 ++++- 7 files changed, 104 insertions(+), 6 deletions(-) diff --git a/adapters/OvhSmsAdapter.php b/adapters/OvhSmsAdapter.php index 96f14e4..0a007c3 100755 --- a/adapters/OvhSmsAdapter.php +++ b/adapters/OvhSmsAdapter.php @@ -250,7 +250,7 @@ $uid = $_GET['id'] ?? false; $dlr = $_GET['dlr'] ?? false; - if ($uid === false || $dlr ==== false) + if ($uid === false || $dlr === false) { return false; } diff --git a/controllers/internals/Received.php b/controllers/internals/Received.php index fee5c70..e732137 100755 --- a/controllers/internals/Received.php +++ b/controllers/internals/Received.php @@ -213,4 +213,16 @@ namespace controllers\internals; { return $this->get_model()->get_since_by_date_for_origin_and_user($id_user, $date, $origin); } + + + /** + * Find destination of last received message for an origin and user + * @param int $id_user : User id + * @param string $origin : Origin number + * @return array + */ + public function get_last_for_origin_and_user (int $id_user, string $origin) + { + return $this->get_model()->get_last_for_origin_and_user($id_user, $origin); + } } diff --git a/controllers/internals/Sended.php b/controllers/internals/Sended.php index 9b9ff8e..e2351fc 100755 --- a/controllers/internals/Sended.php +++ b/controllers/internals/Sended.php @@ -172,4 +172,16 @@ namespace controllers\internals; return $return; } + + + /** + * Find last sended message for a destination and user + * @param int $id_user : User id + * @param string $destination : Destination number + * @return array + */ + public function get_last_for_destination_and_user (int $id_user, string $destination) + { + return $this->get_model()->get_last_for_destination_and_user($id_user, $destination); + } } diff --git a/controllers/publics/Discussion.php b/controllers/publics/Discussion.php index 90c0e06..f985116 100755 --- a/controllers/publics/Discussion.php +++ b/controllers/publics/Discussion.php @@ -70,9 +70,15 @@ namespace controllers\publics; { $contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $number); + $last_sended = $this->internal_sended->get_last_for_destination_and_user($_SESSION['user']['id'], $number); + $last_received = $this->internal_received->get_last_for_origin_and_user($_SESSION['user']['id'], $number); + + $response_number = ($last_received['destination'] ?? $last_sended['origin'] ?? false); + $this->render('discussion/show', [ 'number' => $number, 'contact' => $contact, + 'response_number' => $response_number, ]); } @@ -148,7 +154,8 @@ namespace controllers\publics; * * @param string $csrf : Le jeton csrf * @param string $_POST['text'] : Le contenu du Sms - * @param string $_POST['numbers'] : Un tableau avec le numero des gens auxquel envoyer le sms + * @param string $_POST['destination'] : Number to send sms to + * @param string $_POST['origin'] : Number to send sms with * * @return string : json string Le statut de l'envoi */ @@ -172,9 +179,10 @@ namespace controllers\publics; $id_user = $_SESSION['user']['id']; $at = $now; $text = $_POST['text'] ?? ''; - $numbers = $_POST['numbers'] ?? false; + $destination = $_POST['destination'] ?? false; + $origin = $_POST['origin'] ?? false; - if (!$numbers) + if (!$destination) { $return['success'] = false; $return['message'] = 'Vous devez renseigner un numéro valide'; @@ -183,7 +191,15 @@ namespace controllers\publics; return false; } - if (!$this->internal_scheduled->create($id_user, $at, $text, null, false, $numbers)) + if (!$origin) + { + $origin = null; + } + + //Destinations must be an array of number + $destinations = [$destination]; + + if (!$this->internal_scheduled->create($id_user, $at, $text, $origin, false, $destinations)) { $return['success'] = false; $return['message'] = 'Impossible de créer le Sms'; diff --git a/models/Received.php b/models/Received.php index 153c587..bddb0f3 100755 --- a/models/Received.php +++ b/models/Received.php @@ -388,4 +388,31 @@ namespace models; return $this->_run_query($query, $params); } + + /** + * Find destination of last received message for an origin and user + * @param int $id_user : User id + * @param string $origin : Origin number + * @return array + */ + public function get_last_for_origin_and_user (int $id_user, string $origin) + { + $query = " + SELECT * + FROM received + WHERE origin = :origin + AND destination IN (SELECT number FROM phone WHERE id_user = :id_user) + ORDER BY at DESC + LIMIT 0,1 + "; + + $params = [ + 'origin' => $origin, + 'id_user' => $id_user, + ]; + + $result = $this->_run_query($query, $params); + + return ($result[0] ?? []); + } } diff --git a/models/Sended.php b/models/Sended.php index 01e5b52..caaa286 100755 --- a/models/Sended.php +++ b/models/Sended.php @@ -300,5 +300,33 @@ namespace models; return $this->_run_query($query, $params); } + + + /** + * Find last sended message for a destination and user + * @param int $id_user : User id + * @param string $destination : Destination number + * @return array + */ + public function get_last_for_destination_and_user (int $id_user, string $destination) + { + $query = " + SELECT * + FROM sended + WHERE destination = :destination + AND origin IN (SELECT number FROM phone WHERE id_user = :id_user) + ORDER BY at DESC + LIMIT 0,1 + "; + + $params = [ + 'destination' => $destination, + 'id_user' => $id_user, + ]; + + $result = $this->_run_query($query, $params); + + return ($result[0] ?? []); + } } diff --git a/templates/discussion/show.php b/templates/discussion/show.php index 87a9fc4..f0a24b6 100755 --- a/templates/discussion/show.php +++ b/templates/discussion/show.php @@ -38,7 +38,10 @@
- + + + +