Update received to use phone_id instead of destination

This commit is contained in:
osaajani 2020-03-31 01:53:07 +02:00
parent 78abbef26e
commit 2ae2baa6c4
7 changed files with 61 additions and 52 deletions

View file

@ -33,23 +33,23 @@ namespace controllers\internals;
* Create a received.
*
* @param $id_user : Id of user to create received for
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return bool : false on error, new received id else
*/
public function create(int $id_user, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false): bool
{
$received = [
'id_user' => $id_user,
'id_phone' => $id_phone,
'at' => $at,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
'command' => $command,
];
@ -57,34 +57,6 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($received);
}
/**
* Update a received for a user.
*
* @param int $id_user : user id
* @param int $id_received : received id
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return bool : false on error, true on success
*/
public function update_for_user(int $id_user, int $id_received, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
{
$received = [
'at' => $at,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
'command' => $command,
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Update a received message for a user to mark the message as read.
*
@ -218,7 +190,7 @@ namespace controllers\internals;
}
/**
* Find destination of last received message for an origin and user.
* Find last received message for an origin and user.
*
* @param int $id_user : User id
* @param string $origin : Origin number

View file

@ -54,12 +54,20 @@ namespace controllers\publics;
$this->internal_received->mark_as_read_for_user($_SESSION['user']['id'], $received['id']);
}
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
if ($received['id_phone'] !== null)
{
continue;
$phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $received['id_phone']);
if ($phone)
{
$receiveds[$key]['phone_name'] = $phone['name'];
}
}
$receiveds[$key]['origin'] = $contact['name'] . ' (' . $received['origin'] . ')';
$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']);
if ($contact)
{
$receiveds[$key]['contact'] = $contact['name'];
}
}
$this->render('received/list', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($receiveds)]);
@ -80,12 +88,20 @@ namespace controllers\publics;
{
$this->internal_received->mark_as_read_for_user($_SESSION['user']['id'], $received['id']);
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
if ($received['id_phone'] !== null)
{
continue;
$phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $received['id_phone']);
if ($phone)
{
$receiveds[$key]['phone_name'] = $phone['name'];
}
}
$receiveds[$key]['origin'] = $contact['name'] . ' (' . $received['origin'] . ')';
$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']);
if ($contact)
{
$receiveds[$key]['contact'] = $contact['name'];
}
}
$this->render('received/list_unread', ['receiveds' => $receiveds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($receiveds)]);

View file

@ -30,6 +30,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);
$this->internal_contact = new \controllers\internals\Contact($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -45,20 +46,22 @@ namespace controllers\publics;
$limit = 25;
$sendeds = $this->internal_sended->list_for_user($_SESSION['user']['id'], $limit, $page);
foreach ($sendeds as &$sended)
foreach ($sendeds as $key => $sended)
{
if ($sended['id_phone'] === null)
if ($sended['id_phone'] !== null)
{
continue;
$phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $sended['id_phone']);
if ($phone)
{
$sendeds[$key]['phone_name'] = $phone['name'];
}
}
$phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $sended['id_phone']);
if (!$phone)
$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $sended['destination']);
if ($contact)
{
continue;
$sendeds[$key]['contact'] = $contact['name'];
}
$sended['phone_name'] = $phone['name'];
}
$this->render('sended/list', ['sendeds' => $sendeds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($sendeds)]);