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

View File

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

View File

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

View File

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

View File

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

View File

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