Update sended, uniformize received

This commit is contained in:
osaajani 2019-11-12 17:58:07 +01:00
parent 23892f5983
commit 65dacb5302
6 changed files with 67 additions and 43 deletions

View File

@ -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 int $id_user : user id
* @param mixed(int|bool) $nb_entry : Le nombre d'entrées à retourner par page * @param mixed(int|bool) $nb_entry : Number of entry to return
* @param mixed(int|bool) $page : Le numéro de page en cours * @param mixed(int|bool) $page : Pagination, will offset $nb_entry * $page results
* * @return array
* @return array : La liste des receivedes
*/ */
public function list($id_user, $nb_entry = null, $page = null) public function list($id_user, $nb_entry = null, $page = null)
{ {
//Recupération des receivedes return $this->model_received->list_for_user($id_user, $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);
} }

View File

@ -24,17 +24,25 @@ namespace controllers\internals;
} }
/** /**
* Cette fonction retourne une liste des sendedes 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) $nb_entry : Number of entry to return
* @param mixed(int|bool) $page : Le numéro de page en cours * @param mixed(int|bool) $page : Pagination, will offset $nb_entry * $page results
* * @return array
* @return array : La liste des sendedes
*/ */
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_for_user($id_user, $nb_entry, $nb_entry * $page);
return $this->model_sended->list($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);
} }
/** /**

View File

@ -17,6 +17,7 @@ namespace controllers\publics;
class Sended extends \descartes\Controller class Sended extends \descartes\Controller
{ {
private $internal_sended; private $internal_sended;
private $internal_phone;
/** /**
* Cette fonction est appelée avant toute les autres : * 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); $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_sended = new \controllers\internals\Sended($bdd); $this->internal_sended = new \controllers\internals\Sended($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
\controllers\internals\Tool::verifyconnect(); \controllers\internals\Tool::verifyconnect();
} }
@ -41,7 +43,7 @@ namespace controllers\publics;
{ {
$page = (int) $page; $page = (int) $page;
$limit = 25; $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)]); $this->render('sended/list', ['sendeds' => $sendeds, 'page' => $page, 'limit' => $limit, 'nb_results' => \count($sendeds)]);
} }
@ -65,6 +67,18 @@ namespace controllers\publics;
$ids = $_GET['ids'] ?? []; $ids = $_GET['ids'] ?? [];
foreach ($ids as $id) 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); $this->internal_sended->delete($id);
} }

View File

@ -30,26 +30,23 @@ namespace models;
/** /**
* Return a list of sms where destination in array allowed_destinations * Return a list of sms where destination in array allowed_destinations
* @param array $allowed_destinations : Allowed destinations for sms * @param int $id_user : User id
* @param int $limit : Nombre de résultat maximum à retourner * @param int $limit : Max results to return
* @param int $offset : Nombre de résultat à ingnorer * @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; $limit = (int) $limit;
$offset = (int) $offset; $offset = (int) $offset;
$query = ' $query = '
SELECT * FROM received 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 $params = [
$generated_in = $this->_generate_in_from_array($allowed_destinations); 'id_user' => $id_user,
$query .= $generated_in['QUERY']; ];
$query .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
$params = $generated_in['PARAMS'];
return $this->_run_query($query, $params); return $this->_run_query($query, $params);
} }

View File

@ -31,14 +31,26 @@ namespace models;
} }
/** /**
* Retourne une liste de sendedes sous forme d'un tableau. * Return a list of sms where destination in array allowed_destinations
* * @param int $id_user : User id
* @param int $limit : Nombre de résultat maximum à retourner * @param int $limit : Max results to return
* @param int $offset : Nombre de résultat à ingnorer * @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);
} }
/** /**

View File

@ -43,7 +43,8 @@
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>Numéro</th> <th>De</th>
<th>À</th>
<th>Message</th> <th>Message</th>
<th>Date</th> <th>Date</th>
<th>Statut</th> <th>Statut</th>
@ -56,6 +57,7 @@
<?php foreach ($sendeds as $sended) { ?> <?php foreach ($sendeds as $sended) { ?>
<tr> <tr>
<td><?php $this->s($sended['id']); ?></td> <td><?php $this->s($sended['id']); ?></td>
<td><?php $this->s($sended['origin']); ?></td>
<td><?php $this->s($sended['destination']); ?></td> <td><?php $this->s($sended['destination']); ?></td>
<td><?php $this->s($sended['text']); ?></td> <td><?php $this->s($sended['text']); ?></td>
<td><?php $this->s($sended['at']); ?></td> <td><?php $this->s($sended['at']); ?></td>