Use correct phone for response to discussion

This commit is contained in:
osaajani 2020-01-11 17:27:07 +01:00
parent c4bc7d94c1
commit aee9fc847f
7 changed files with 104 additions and 6 deletions

View File

@ -250,7 +250,7 @@
$uid = $_GET['id'] ?? false;
$dlr = $_GET['dlr'] ?? false;
if ($uid === false || $dlr ==== false)
if ($uid === false || $dlr === false)
{
return false;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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';

View File

@ -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] ?? []);
}
}

View File

@ -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] ?? []);
}
}

View File

@ -38,7 +38,10 @@
<div class="discussion-message message-input">
<form class="send-message-discussion" action="<?php $this->s(\descartes\Router::url('Discussion', 'send', ['csrf' => $_SESSION['csrf']])); ?>" method="POST">
<textarea name="text" placeholder="Envoyer un message..."></textarea>
<input type="hidden" name="numbers[]" value="<?php $this->s($number); ?>" />
<input type="hidden" name="destination" value="<?php $this->s($number); ?>" />
<?php if ($response_number ) { ?>
<input type="hidden" name="origin" value="<?php $this->s($response_number); ?>" />
<?php } ?>
<button class="btn" ><span class="fa fa-fw fa-send-o"></span> Envoyer</button>
</form>
</div>