Change sended list to use server side datatable
This commit is contained in:
parent
30386f8caf
commit
170a00e30a
|
@ -15,6 +15,22 @@ namespace controllers\internals;
|
||||||
{
|
{
|
||||||
protected $model;
|
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
|
||||||
|
*
|
||||||
|
* @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, $count = false)
|
||||||
|
{
|
||||||
|
return $this->get_model()->datatable_list_for_user($id_user, $limit, $offset, $search, $search_columns, $order_column, $order_desc, $count);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a sended.
|
* Create a sended.
|
||||||
*
|
*
|
||||||
|
|
|
@ -53,7 +53,25 @@ namespace controllers\publics;
|
||||||
*/
|
*/
|
||||||
public function list_json()
|
public function list_json()
|
||||||
{
|
{
|
||||||
$entities = $this->internal_sended->list_for_user($_SESSION['user']['id']);
|
|
||||||
|
$draw = (int)($_GET['draw'] ?? false);
|
||||||
|
|
||||||
|
$columns = [
|
||||||
|
0 => 'phone_name',
|
||||||
|
1 => 'searchable_destination',
|
||||||
|
2 => 'text',
|
||||||
|
3 => 'at',
|
||||||
|
4 => 'status',
|
||||||
|
];
|
||||||
|
|
||||||
|
$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_sended->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc);
|
||||||
|
$count_entities = $this->internal_sended->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc, true);
|
||||||
foreach ($entities as &$entity)
|
foreach ($entities as &$entity)
|
||||||
{
|
{
|
||||||
$entity['destination_formatted'] = \controllers\internals\Tool::phone_link($entity['destination']);
|
$entity['destination_formatted'] = \controllers\internals\Tool::phone_link($entity['destination']);
|
||||||
|
@ -63,8 +81,15 @@ namespace controllers\publics;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$records_total = $this->internal_sended->count_for_user($_SESSION['user']['id']);
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode(['data' => $entities]);
|
echo json_encode([
|
||||||
|
'draw' => $draw,
|
||||||
|
'recordsTotal' => $records_total,
|
||||||
|
'recordsFiltered' => $count_entities,
|
||||||
|
'data' => $entities,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,10 +30,16 @@ namespace models;
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
$query = '
|
$params = [
|
||||||
SELECT sended.*, contact.name as contact_name, phone.name as phone_name
|
'id_user' => $id_user,
|
||||||
|
];
|
||||||
|
|
||||||
|
$query = $count ? 'SELECT COUNT(*) as nb' : 'SELECT * ';
|
||||||
|
$query .= '
|
||||||
|
FROM (
|
||||||
|
SELECT sended.*, contact.name as contact_name, phone.name as phone_name, IF(contact.name IS NULL, sended.destination, CONCAT(sended.destination, " (", contact.name, ")")) as searchable_destination
|
||||||
FROM sended
|
FROM sended
|
||||||
LEFT JOIN contact
|
LEFT JOIN contact
|
||||||
ON contact.number = sended.destination
|
ON contact.number = sended.destination
|
||||||
|
@ -41,9 +47,30 @@ namespace models;
|
||||||
LEFT JOIN phone
|
LEFT JOIN phone
|
||||||
ON phone.id = sended.id_phone
|
ON phone.id = sended.id_phone
|
||||||
WHERE sended.id_user = :id_user
|
WHERE sended.id_user = :id_user
|
||||||
|
) as results
|
||||||
';
|
';
|
||||||
|
|
||||||
if (null !== $limit)
|
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;
|
$limit = (int) $limit;
|
||||||
|
|
||||||
|
@ -55,11 +82,7 @@ namespace models;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = [
|
return ($count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params)) ;
|
||||||
'id_user' => $id_user,
|
|
||||||
];
|
|
||||||
|
|
||||||
return $this->_run_query($query, $params);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -69,7 +69,7 @@ jQuery(document).ready(function ()
|
||||||
{
|
{
|
||||||
jQuery('.datatable').DataTable({
|
jQuery('.datatable').DataTable({
|
||||||
"pageLength": 25,
|
"pageLength": 25,
|
||||||
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000, "All"]],
|
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000]],
|
||||||
"language": {
|
"language": {
|
||||||
"url": HTTP_PWD + "/assets/js/datatables/french.json",
|
"url": HTTP_PWD + "/assets/js/datatables/french.json",
|
||||||
},
|
},
|
||||||
|
@ -77,7 +77,7 @@ jQuery(document).ready(function ()
|
||||||
'targets': 'checkcolumn',
|
'targets': 'checkcolumn',
|
||||||
'orderable': false,
|
'orderable': false,
|
||||||
}],
|
}],
|
||||||
|
"serverSide": true,
|
||||||
"ajax": {
|
"ajax": {
|
||||||
'url': '<?php echo \descartes\Router::url('Sended', 'list_json'); ?>',
|
'url': '<?php echo \descartes\Router::url('Sended', 'list_json'); ?>',
|
||||||
'dataSrc': 'data',
|
'dataSrc': 'data',
|
||||||
|
|
Loading…
Reference in New Issue