mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-21 00:46:27 +02:00
Change sended list to use server side datatable
This commit is contained in:
parent
30386f8caf
commit
170a00e30a
4 changed files with 85 additions and 21 deletions
|
@ -30,20 +30,47 @@ namespace models;
|
|||
*
|
||||
* @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 = '
|
||||
SELECT sended.*, contact.name as contact_name, phone.name as phone_name
|
||||
FROM sended
|
||||
LEFT JOIN contact
|
||||
ON contact.number = sended.destination
|
||||
AND contact.id_user = sended.id_user
|
||||
LEFT JOIN phone
|
||||
ON phone.id = sended.id_phone
|
||||
WHERE sended.id_user = :id_user
|
||||
';
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
if (null !== $limit)
|
||||
$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
|
||||
LEFT JOIN contact
|
||||
ON contact.number = sended.destination
|
||||
AND contact.id_user = sended.id_user
|
||||
LEFT JOIN phone
|
||||
ON phone.id = sended.id_phone
|
||||
WHERE sended.id_user = :id_user
|
||||
) 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;
|
||||
|
||||
|
@ -55,11 +82,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)) ;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue