move received listing to serverside processing

This commit is contained in:
osaajani 2021-07-16 22:53:33 +02:00
parent 170a00e30a
commit 651c428ed7
6 changed files with 116 additions and 57 deletions

View File

@ -15,6 +15,26 @@ namespace controllers\internals;
{
protected $model;
/**
* Return the list of entries for a user.
*
* @param int $id_user : User id
* @param ?int $limit : Number of entry to return
* @param ?int $offset : Number of entry to avoid
* @param ?string $search : String to search for
* @param ?array $search_columns : List of columns to search on
* @param ?string $order_column : Name of the column to order by
* @param bool $order_desc : Should result be ordered DESC, if false order ASC
* @param bool $count : Should the query only count results
* @param bool $unread : Should only unread messages be returned
*
* @return array : Entrys list
*/
public function datatable_list_for_user(int $id_user, ?int $limit = null, ?int $offset = null, ?string $search = null, ?array $search_columns = [], ?string $order_column = null, bool $order_desc = false, bool $count = false, bool $unread = false)
{
return $this->get_model()->datatable_list_for_user($id_user, $limit, $offset, $search, $search_columns, $order_column, $order_desc, $count, $unread);
}
/**
* Return the list of unread messages for a user.
*
@ -355,7 +375,7 @@ namespace controllers\internals;
/**
* Get the model for the Controller.
*/
protected function get_model(): \descartes\Model
protected function get_model(): \models\Received
{
$this->model = $this->model ?? new \models\Received($this->bdd);

View File

@ -16,15 +16,17 @@ namespace controllers\internals;
protected $model;
/**
* Return the list of entries for a user.
*
*
* @param int $id_user : User id
* @param ?int $limit : Number of entry to return
* @param ?int $offset : Number of entry to avoid
* @param ?string $search : String to search for
* @param ?array $orders : How to order results
* @param ?array $search_columns : List of columns to search on
* @param ?string $order_column : Name of the column to order by
* @param bool $order_desc : Should result be ordered DESC, if false order ASC
* @param bool $count : Should the query only count results
*
* @return array : Entrys list
* @return array : Entries list
*/
public function datatable_list_for_user(int $id_user, ?int $limit = null, ?int $offset = null, ?string $search = null, ?array $search_columns = [], ?string $order_column = null, bool $order_desc = false, $count = false)
{
@ -305,7 +307,7 @@ namespace controllers\internals;
/**
* Get the model for the Controller.
*/
protected function get_model(): \descartes\Model
protected function get_model(): \models\Sended
{
$this->model = $this->model ?? new \models\Sended($this->bdd);

View File

@ -47,11 +47,31 @@ namespace controllers\publics;
}
/**
* Return received as json.
* Return receiveds as json.
*
* @param bool $unread : Should we only search for unread messages
*/
public function list_json()
public function list_json(bool $unread = false)
{
$entities = $this->internal_received->list_for_user($_SESSION['user']['id']);
$draw = (int)($_GET['draw'] ?? false);
$columns = [
0 => 'searchable_origin',
1 => 'phone_name',
2 => 'text',
3 => 'at',
4 => 'status',
5 => 'command',
];
$search = $_GET['search']['value'] ?? null;
$order_column = $columns[$_GET['order'][0]['column']] ?? null;
$order_desc = ($_GET['order'][0]['dir'] ?? 'asc') == 'desc' ? true : false;
$offset = (int) ($_GET['start'] ?? 0);
$limit = (int) ($_GET['length'] ?? 25);
$entities = $this->internal_received->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, false, $unread);
$count_entities = $this->internal_received->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, true, $unread);
foreach ($entities as &$entity)
{
$entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']);
@ -61,8 +81,15 @@ namespace controllers\publics;
}
}
$records_total = $this->internal_received->count_for_user($_SESSION['user']['id']);
header('Content-Type: application/json');
echo json_encode(['data' => $entities]);
echo json_encode([
'draw' => $draw,
'recordsTotal' => $records_total,
'recordsFiltered' => $count_entities,
'data' => $entities,
]);
}
/**
@ -73,25 +100,6 @@ namespace controllers\publics;
$this->render('received/list', ['is_unread' => true]);
}
/**
* Return unred received as json.
*/
public function list_unread_json()
{
$entities = $this->internal_received->list_unread_for_user($_SESSION['user']['id']);
foreach ($entities as &$entity)
{
$entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']);
if ($entity['mms'])
{
$entity['medias'] = $this->internal_media->gets_for_received($entity['id']);
}
}
header('Content-Type: application/json');
echo json_encode(['data' => $entities]);
}
/**
* Mark messages as.
*

View File

@ -20,29 +20,60 @@ namespace models;
const STATUS_READ = 'read';
/**
* Return a list of received messages for a user.
* Add a column contact_name and phone_name when available.
* @param int $id_user : User id
* @param ?int $limit : Number of entry to return
* @param ?int $offset : Number of entry to avoid
* @param ?string $search : String to search for
* @param ?array $search_columns : List of columns to search on
* @param ?string $order_column : Name of the column to order by
* @param bool $order_desc : Should result be ordered DESC, if false order ASC
* @param bool $count : Should the query only count results
* @param bool $unread : Should only unread messages be returned
*
* @param int $id_user : user id
* @param ?int $limit : Number of entry to return or null
* @param ?int $offset : Number of entry to ignore or null
*
* @return array
* @return array : Entrys list
*/
public function list_for_user(int $id_user, $limit, $offset)
public function datatable_list_for_user(int $id_user, ?int $limit = null, ?int $offset = null, ?string $search = null, ?array $search_columns = [], ?string $order_column = null, bool $order_desc = false, bool $count = false, bool $unread = false)
{
$query = '
SELECT received.*, contact.name as contact_name, phone.name as phone_name
FROM received
LEFT JOIN contact
ON contact.number = received.origin
AND contact.id_user = received.id_user
LEFT JOIN phone
ON phone.id = received.id_phone
WHERE received.id_user = :id_user
';
$params = [
'id_user' => $id_user,
];
if (null !== $limit)
$query = $count ? 'SELECT COUNT(*) as nb' : 'SELECT * ';
$query .= '
FROM (
SELECT received.*, contact.name as contact_name, phone.name as phone_name, IF(contact.name IS NULL, received.origin, CONCAT(received.origin, " (", contact.name, ")")) as searchable_origin
FROM received
LEFT JOIN contact
ON contact.number = received.origin
AND contact.id_user = received.id_user
LEFT JOIN phone
ON phone.id = received.id_phone
WHERE received.id_user = :id_user
' . ($unread ? ' AND received.status = \'unread\'' : '') . '
) as results
';
if ($search && $search_columns)
{
$like_search = '%' . str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $search) . '%';
$params[':like_search'] = $like_search;
$query .= ' WHERE (0';
foreach ($search_columns as $column)
{
$query .= ' OR ' . $column . ' LIKE :like_search';
}
$query .= ')';
}
if ($order_column)
{
$query .= ' ORDER BY ' . $order_column . ($order_desc ? ' DESC' : ' ASC');
}
if (null !== $limit && !$count)
{
$limit = (int) $limit;
@ -54,11 +85,7 @@ namespace models;
}
}
$params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
return ($count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params)) ;
}
/**

View File

@ -93,9 +93,11 @@
'Received' => [
'list' => '/received/',
'list_json' => '/received/json/',
'list_json' => [
'/received/json/',
'/received/json/{unread}/',
],
'list_unread' => '/unread/',
'list_unread_json' => '/unread/json/',
'mark_as' => '/mark/{status}/{csrf}/',
'delete' => '/received/delete/{csrf}/',
'popup' => '/received/popup/',

View File

@ -87,9 +87,9 @@ jQuery(document).ready(function ()
'targets': 'checkcolumn',
'orderable': false,
}],
"serverSide": true,
"ajax": {
'url': '<?php echo $is_unread ? \descartes\Router::url('Received', 'list_unread_json') : \descartes\Router::url('Received', 'list_json'); ?>',
'url': '<?php echo $is_unread ? \descartes\Router::url('Received', 'list_json', ['unread' => true]) : \descartes\Router::url('Received', 'list_json'); ?>',
'dataSrc': 'data',
},
"columns" : [