Compare commits

..

No commits in common. "8e43c53498c4dbeca7630b8cd15a5a6342b02288" and "170a00e30a8194fa697f6db720f47f0ee7d27fbf" have entirely different histories.

33 changed files with 144 additions and 302 deletions

1
.gitignore vendored
View File

@ -3,7 +3,6 @@
.php_cs.cache .php_cs.cache
.credentials .credentials
.credentials* .credentials*
.vscode/
vendor/ vendor/
scripts/ scripts/
composer.lock composer.lock

View File

@ -40,7 +40,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
private $sender; private $sender;
/** /**
* Octopush SMS type. * Octopush SMS type
*/ */
private $sms_type; private $sms_type;
@ -49,6 +49,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
*/ */
private $api_url = 'https://api.octopush.com/v1/public'; private $api_url = 'https://api.octopush.com/v1/public';
/** /**
* Adapter constructor, called when instanciated by RaspiSMS. * Adapter constructor, called when instanciated by RaspiSMS.
* *
@ -63,7 +64,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
$this->api_key = $this->data['api_key']; $this->api_key = $this->data['api_key'];
$this->sms_type = self::SMS_TYPE_LOWCOST; $this->sms_type = self::SMS_TYPE_LOWCOST;
if (($this->data['sms_type'] ?? false) && 'premium' === $this->data['sms_type']) if (($this->data['sms_type'] ?? false) && $this->data['sms_type'] === 'premium')
{ {
$this->sms_type = self::SMS_TYPE_PREMIUM; $this->sms_type = self::SMS_TYPE_PREMIUM;
} }
@ -244,11 +245,12 @@ class OctopushShortcodeAdapter implements AdapterInterface
} }
else else
{ {
$data['with_replies'] = 'True'; $data['with_replies'] = "True";
} }
$data = json_encode($data); $data = json_encode($data);
$endpoint = $this->api_url . '/sms-campaign/send'; $endpoint = $this->api_url . '/sms-campaign/send';
$curl = curl_init(); $curl = curl_init();
@ -348,7 +350,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
$http_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); $http_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);
if (200 !== $http_code) if ($http_code !== 200)
{ {
return false; return false;
} }
@ -367,6 +369,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
header('Content-Encoding: none'); header('Content-Encoding: none');
header('Content-Length: 0'); header('Content-Length: 0');
$input = file_get_contents('php://input'); $input = file_get_contents('php://input');
$content = json_decode($input, true); $content = json_decode($input, true);
if (null === $content) if (null === $content)
@ -382,6 +385,7 @@ class OctopushShortcodeAdapter implements AdapterInterface
return false; return false;
} }
switch ($status) switch ($status)
{ {
case 'DELIVERED': case 'DELIVERED':

View File

@ -35,7 +35,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
private $api_key; private $api_key;
/** /**
* Octopush SMS type. * Octopush SMS type
*/ */
private $sms_type; private $sms_type;
@ -45,10 +45,11 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
private $api_url = 'https://api.octopush.com/v1/public'; private $api_url = 'https://api.octopush.com/v1/public';
/** /**
* Octopush phone number. * Octopush phone number
*/ */
private $number; private $number;
/** /**
* Adapter constructor, called when instanciated by RaspiSMS. * Adapter constructor, called when instanciated by RaspiSMS.
* *
@ -64,7 +65,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
$this->number = $this->data['number']; $this->number = $this->data['number'];
$this->sms_type = self::SMS_TYPE_LOWCOST; $this->sms_type = self::SMS_TYPE_LOWCOST;
if (($this->data['sms_type'] ?? false) && 'premium' === $this->data['sms_type']) if (($this->data['sms_type'] ?? false) && $this->data['sms_type'] === 'premium')
{ {
$this->sms_type = self::SMS_TYPE_PREMIUM; $this->sms_type = self::SMS_TYPE_PREMIUM;
} }
@ -117,6 +118,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
Envoi de SMS avec un numéro virtuel en utilisant <a target="_blank" href="https://www.octopush.com/">Octopush</a>. Pour trouver vos clés API Octopush <a target="_blank" href="' . $credentials_url . '">cliquez ici.</a><br/> Envoi de SMS avec un numéro virtuel en utilisant <a target="_blank" href="https://www.octopush.com/">Octopush</a>. Pour trouver vos clés API Octopush <a target="_blank" href="' . $credentials_url . '">cliquez ici.</a><br/>
Pour plus d\'information sur l\'utilisation de ce téléphone, reportez-vous à <a href="https://documentation.raspisms.fr/users/adapters/octopush_virtual_number.html" target="_blank">la documentation sur les téléphones "Octopush Numéro Virtuel".</a> Pour plus d\'information sur l\'utilisation de ce téléphone, reportez-vous à <a href="https://documentation.raspisms.fr/users/adapters/octopush_virtual_number.html" target="_blank">la documentation sur les téléphones "Octopush Numéro Virtuel".</a>
'; ';
} }
/** /**
@ -235,11 +237,12 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
'sms_type' => $this->sms_type, 'sms_type' => $this->sms_type,
'purpose' => 'alert', 'purpose' => 'alert',
'sender' => $this->number, 'sender' => $this->number,
'with_replies' => 'True', 'with_replies' => "True",
]; ];
$data = json_encode($data); $data = json_encode($data);
$endpoint = $this->api_url . '/sms-campaign/send'; $endpoint = $this->api_url . '/sms-campaign/send';
$curl = curl_init(); $curl = curl_init();
@ -340,7 +343,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
$http_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); $http_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);
if (200 !== $http_code) if ($http_code !== 200)
{ {
return false; return false;
} }
@ -359,6 +362,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
header('Content-Encoding: none'); header('Content-Encoding: none');
header('Content-Length: 0'); header('Content-Length: 0');
$input = file_get_contents('php://input'); $input = file_get_contents('php://input');
$content = json_decode($input, true); $content = json_decode($input, true);
if (null === $content) if (null === $content)
@ -374,6 +378,7 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
return false; return false;
} }
switch ($status) switch ($status)
{ {
case 'DELIVERED': case 'DELIVERED':

View File

@ -136,7 +136,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Call protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Call($this->bdd); $this->model = $this->model ?? new \models\Call($this->bdd);

View File

@ -150,7 +150,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Command protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Command($this->bdd); $this->model = $this->model ?? new \models\Command($this->bdd);

View File

@ -134,7 +134,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\ConditionalGroup protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd); $this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);

View File

@ -15,23 +15,6 @@ namespace controllers\internals;
{ {
protected $model; protected $model;
/**
* @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
*
* @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)
{
return $this->get_model()->datatable_list_for_user($id_user, $limit, $offset, $search, $search_columns, $order_column, $order_desc, $count);
}
/** /**
* Return a contact for a user by a number. * Return a contact for a user by a number.
* *
@ -368,7 +351,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Contact protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Contact($this->bdd); $this->model = $this->model ?? new \models\Contact($this->bdd);

View File

@ -74,7 +74,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Event protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Event($this->bdd); $this->model = $this->model ?? new \models\Event($this->bdd);

View File

@ -130,7 +130,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Group protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Group($this->bdd); $this->model = $this->model ?? new \models\Group($this->bdd);

View File

@ -313,7 +313,7 @@ class Media extends StandardController
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Media protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Media($this->bdd); $this->model = $this->model ?? new \models\Media($this->bdd);

View File

@ -166,7 +166,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Phone protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Phone($this->bdd); $this->model = $this->model ?? new \models\Phone($this->bdd);

View File

@ -288,7 +288,7 @@ class Quota extends StandardController
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Quota protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Quota($this->bdd); $this->model = $this->model ?? new \models\Quota($this->bdd);

View File

@ -15,26 +15,6 @@ 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 $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. * Return the list of unread messages for a user.
* *
@ -375,7 +355,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Received protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Received($this->bdd); $this->model = $this->model ?? new \models\Received($this->bdd);

View File

@ -544,7 +544,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Scheduled protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Scheduled($this->bdd); $this->model = $this->model ?? new \models\Scheduled($this->bdd);

View File

@ -16,16 +16,15 @@ namespace controllers\internals;
protected $model; protected $model;
/** /**
* Return the list of entries for a user.
*
* @param int $id_user : User id * @param int $id_user : User id
* @param ?int $limit : Number of entry to return * @param ?int $limit : Number of entry to return
* @param ?int $offset : Number of entry to avoid * @param ?int $offset : Number of entry to avoid
* @param ?string $search : String to search for * @param ?string $search : String to search for
* @param ?array $search_columns : List of columns to search on * @param ?array $orders : How to order results
* @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 : Entries list * @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) 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)
{ {
@ -306,7 +305,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Sended protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Sended($this->bdd); $this->model = $this->model ?? new \models\Sended($this->bdd);

View File

@ -89,7 +89,7 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Setting protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Setting($this->bdd); $this->model = $this->model ?? new \models\Setting($this->bdd);

View File

@ -64,25 +64,24 @@ namespace controllers\internals;
return $this->get_model()->get_by_number_for_user($id_user, $number); return $this->get_model()->get_by_number_for_user($id_user, $number);
} }
/**
* Parse a string to check if its a SMS stop.
*
* @param string $str : The string to check
*
* @return bool : true if sms stop, false else
*/
public function check_for_stop(string $str)
{
return 'stop' == trim(mb_strtolower($str));
}
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\SmsStop protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\SmsStop($this->bdd); $this->model = $this->model ?? new \models\SmsStop($this->bdd);
return $this->model; return $this->model;
} }
/**
* Parse a string to check if its a SMS stop
*
* @param string $str : The string to check
* @return bool : true if sms stop, false else
*/
public function check_for_stop (string $str)
{
return trim(mb_strtolower($str)) == 'stop';
}
} }

View File

@ -137,8 +137,6 @@ namespace controllers\internals;
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*
* @return \models\StandardModel
*/ */
abstract protected function get_model(); abstract protected function get_model(): \descartes\Model;
} }

View File

@ -146,7 +146,7 @@ class Webhook extends StandardController
/** /**
* Get the model for the Controller. * Get the model for the Controller.
*/ */
protected function get_model(): \models\Webhook protected function get_model(): \descartes\Model
{ {
$this->model = $this->model ?? new \models\Webhook($this->bdd); $this->model = $this->model ?? new \models\Webhook($this->bdd);

View File

@ -206,8 +206,7 @@ namespace controllers\publics;
} }
/** /**
* Allow to stop impersonating a user. * Allow to stop impersonating a user
*
* @param mixed $csrf * @param mixed $csrf
*/ */
public function stop_impersonate() public function stop_impersonate()
@ -224,7 +223,6 @@ namespace controllers\publics;
$_SESSION = $old_session; $_SESSION = $old_session;
\FlashMessage\FlashMessage::push('success', 'Vous n\'incarnez plus l\'utilisateur ' . $user_email . '.'); \FlashMessage\FlashMessage::push('success', 'Vous n\'incarnez plus l\'utilisateur ' . $user_email . '.');
return $this->redirect(\descartes\Router::url('Dashboard', 'show')); return $this->redirect(\descartes\Router::url('Dashboard', 'show'));
} }
} }

View File

@ -164,4 +164,5 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Connect', 'login')); return $this->redirect(\descartes\Router::url('Connect', 'login'));
} }
} }

View File

@ -45,43 +45,19 @@ namespace controllers\publics;
return $this->render('contact/list'); return $this->render('contact/list');
} }
/** /**
* Return contacts as json. * Return contacts as json.
*/ */
public function list_json() public function list_json()
{ {
$draw = (int) ($_GET['draw'] ?? false); $entities = $this->internal_contact->list_for_user($_SESSION['user']['id']);
$columns = [
0 => 'name',
1 => 'number',
2 => 'created_at',
3 => 'updated_at',
];
$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_contact->datatable_list_for_user($_SESSION['user']['id'], $limit, $offset, $search, $columns, $order_column, $order_desc);
$count_entities = $this->internal_contact->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['number_formatted'] = \controllers\internals\Tool::phone_link($entity['number']); $entity['number_formatted'] = \controllers\internals\Tool::phone_link($entity['number']);
} }
$records_total = $this->internal_contact->count_for_user($_SESSION['user']['id']);
header('Content-Type: application/json'); header('Content-Type: application/json');
echo json_encode([ echo json_encode(['data' => $entities]);
'draw' => $draw,
'recordsTotal' => $records_total,
'recordsFiltered' => $count_entities,
'data' => $entities,
]);
} }
/** /**

View File

@ -47,31 +47,11 @@ namespace controllers\publics;
} }
/** /**
* Return receiveds as json. * Return received as json.
*
* @param bool $unread : Should we only search for unread messages
*/ */
public function list_json(bool $unread = false) public function list_json()
{ {
$draw = (int) ($_GET['draw'] ?? false); $entities = $this->internal_received->list_for_user($_SESSION['user']['id']);
$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) foreach ($entities as &$entity)
{ {
$entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']); $entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']);
@ -81,15 +61,8 @@ namespace controllers\publics;
} }
} }
$records_total = $this->internal_received->count_for_user($_SESSION['user']['id']);
header('Content-Type: application/json'); header('Content-Type: application/json');
echo json_encode([ echo json_encode(['data' => $entities]);
'draw' => $draw,
'recordsTotal' => $records_total,
'recordsFiltered' => $count_entities,
'data' => $entities,
]);
} }
/** /**
@ -100,6 +73,25 @@ namespace controllers\publics;
$this->render('received/list', ['is_unread' => true]); $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. * Mark messages as.
* *

View File

@ -53,7 +53,8 @@ namespace controllers\publics;
*/ */
public function list_json() public function list_json()
{ {
$draw = (int) ($_GET['draw'] ?? false);
$draw = (int)($_GET['draw'] ?? false);
$columns = [ $columns = [
0 => 'phone_name', 0 => 'phone_name',

View File

@ -410,13 +410,13 @@ class User extends \descartes\Controller
return $this->redirect(\descartes\Router::url('User', 'list')); return $this->redirect(\descartes\Router::url('User', 'list'));
} }
/** /**
* Allow an admin to impersonate a user. * Allow an admin to impersonate a user
*
* @param mixed $csrf * @param mixed $csrf
* @param array int $_GET['user_ids'] : Ids of users to impersonate, the array should actually contain one id only, we keep use of array for simpler compatibility in UI * @param array int $_GET['user_ids'] : Ids of users to impersonate, the array should actually contain one id only, we keep use of array for simpler compatibility in UI
*/ */
public function impersonate($csrf) public function impersonate ($csrf)
{ {
if (!$this->verify_csrf($csrf)) if (!$this->verify_csrf($csrf))
{ {
@ -425,7 +425,7 @@ class User extends \descartes\Controller
return $this->redirect(\descartes\Router::url('User', 'list')); return $this->redirect(\descartes\Router::url('User', 'list'));
} }
if (1 != count($_GET['user_ids'])) if (count($_GET['user_ids']) != 1)
{ {
\FlashMessage\FlashMessage::push('danger', 'Vous devez séléctionner un et un seul utilisateur à incarner !'); \FlashMessage\FlashMessage::push('danger', 'Vous devez séléctionner un et un seul utilisateur à incarner !');
@ -470,7 +470,7 @@ class User extends \descartes\Controller
]; ];
\FlashMessage\FlashMessage::push('success', 'Vous incarnez désormais l\'utilisateur ' . $user['email'] . '.'); \FlashMessage\FlashMessage::push('success', 'Vous incarnez désormais l\'utilisateur ' . $user['email'] . '.');
return $this->redirect(\descartes\Router::url('Dashboard', 'show')); return $this->redirect(\descartes\Router::url('Dashboard', 'show'));
} }
} }

View File

@ -13,65 +13,6 @@ namespace models;
class Contact extends StandardModel class Contact extends StandardModel
{ {
/**
* Return a list of sended 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 or null
* @param ?int $offset : Number of entry to ignore or null
*
* @return array
*/
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)
{
$params = [
'id_user' => $id_user,
];
$query = $count ? 'SELECT COUNT(*) as nb' : 'SELECT * ';
$query .= '
FROM (
SELECT * FROM contact
WHERE 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;
$query .= ' LIMIT ' . $limit;
if (null !== $offset)
{
$offset = (int) $offset;
$query .= ' OFFSET ' . $offset;
}
}
return $count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params);
}
/** /**
* Return a contact by his number for a user. * Return a contact by his number for a user.
* *

View File

@ -20,28 +20,19 @@ namespace models;
const STATUS_READ = 'read'; const STATUS_READ = 'read';
/** /**
* @param int $id_user : User id * Return a list of received messages for a user.
* @param ?int $limit : Number of entry to return * Add a column contact_name and phone_name when available.
* @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 * @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
*/ */
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) public function list_for_user(int $id_user, $limit, $offset)
{ {
$params = [ $query = '
'id_user' => $id_user, SELECT received.*, contact.name as contact_name, phone.name as phone_name
];
$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 FROM received
LEFT JOIN contact LEFT JOIN contact
ON contact.number = received.origin ON contact.number = received.origin
@ -49,31 +40,9 @@ namespace models;
LEFT JOIN phone LEFT JOIN phone
ON phone.id = received.id_phone ON phone.id = received.id_phone
WHERE received.id_user = :id_user WHERE received.id_user = :id_user
' . ($unread ? ' AND received.status = \'unread\'' : '') . ' ';
) as results
';
if ($search && $search_columns) if (null !== $limit)
{
$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;
@ -85,7 +54,11 @@ namespace models;
} }
} }
return $count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params); $params = [
'id_user' => $id_user,
];
return $this->_run_query($query, $params);
} }
/** /**

View File

@ -48,7 +48,7 @@ namespace models;
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 ) as results
'; ';
if ($search && $search_columns) if ($search && $search_columns)
{ {
@ -82,7 +82,7 @@ namespace models;
} }
} }
return $count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params); return ($count ? $this->_run_query($query, $params)[0]['nb'] ?? 0 : $this->_run_query($query, $params)) ;
} }
/** /**

View File

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

View File

@ -186,12 +186,11 @@ jQuery(document).ready(function()
"language": { "language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json", "url": HTTP_PWD + "/assets/js/datatables/french.json",
}, },
"orderMulti": false,
"columnDefs": [{ "columnDefs": [{
'targets': 'checkcolumn', 'targets': 'checkcolumn',
'orderable': false, 'orderable': false,
}], }],
"serverSide": true,
"ajax": { "ajax": {
'url': '<?php echo \descartes\Router::url('Contact', 'list_json'); ?>', 'url': '<?php echo \descartes\Router::url('Contact', 'list_json'); ?>',
'dataSrc': 'data', 'dataSrc': 'data',

View File

@ -83,14 +83,13 @@ jQuery(document).ready(function ()
"language": { "language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json", "url": HTTP_PWD + "/assets/js/datatables/french.json",
}, },
"orderMulti": false,
"columnDefs": [{ "columnDefs": [{
'targets': 'checkcolumn', 'targets': 'checkcolumn',
'orderable': false, 'orderable': false,
}], }],
"serverSide": true,
"ajax": { "ajax": {
'url': '<?php echo $is_unread ? \descartes\Router::url('Received', 'list_json', ['unread' => true]) : \descartes\Router::url('Received', 'list_json'); ?>', 'url': '<?php echo $is_unread ? \descartes\Router::url('Received', 'list_unread_json') : \descartes\Router::url('Received', 'list_json'); ?>',
'dataSrc': 'data', 'dataSrc': 'data',
}, },
"columns" : [ "columns" : [

View File

@ -73,7 +73,6 @@ jQuery(document).ready(function ()
"language": { "language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json", "url": HTTP_PWD + "/assets/js/datatables/french.json",
}, },
"orderMulti": false,
"columnDefs": [{ "columnDefs": [{
'targets': 'checkcolumn', 'targets': 'checkcolumn',
'orderable': false, 'orderable': false,

View File

@ -40,7 +40,7 @@
<thead> <thead>
<tr> <tr>
<th>Numéro</th> <th>Numéro</th>
<?php if ($_SESSION['user']['admin'] ?? false) { ?> <?php if ($_SESSION['user']['admin']) { ?>
<th class="checkcolumn"><input type="checkbox" id="check-all"/></th> <th class="checkcolumn"><input type="checkbox" id="check-all"/></th>
<?php } ?> <?php } ?>
</tr> </tr>
@ -50,7 +50,7 @@
</table> </table>
</div> </div>
<div> <div>
<?php if ($_SESSION['user']['admin'] ?? false) { ?> <?php if ($_SESSION['user']['admin']) { ?>
<div class="text-right col-xs-12 no-padding"> <div class="text-right col-xs-12 no-padding">
<strong>Action pour la séléction :</strong> <strong>Action pour la séléction :</strong>
<button class="btn btn-default btn-confirm" type="submit" formaction="<?php echo \descartes\Router::url('SmsStop', 'delete', ['csrf' => $_SESSION['csrf']]); ?>"><span class="fa fa-trash-o"></span> Supprimer</button> <button class="btn btn-default btn-confirm" type="submit" formaction="<?php echo \descartes\Router::url('SmsStop', 'delete', ['csrf' => $_SESSION['csrf']]); ?>"><span class="fa fa-trash-o"></span> Supprimer</button>
@ -74,11 +74,11 @@ jQuery(document).ready(function ()
"language": { "language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json", "url": HTTP_PWD + "/assets/js/datatables/french.json",
}, },
"orderMulti": false,
"columnDefs": [{ "columnDefs": [{
'targets': 'checkcolumn', 'targets': 'checkcolumn',
'orderable': false, 'orderable': false,
}], }],
"ajax": { "ajax": {
'url': '<?php echo \descartes\Router::url('SmsStop', 'list_json'); ?>', 'url': '<?php echo \descartes\Router::url('SmsStop', 'list_json'); ?>',
'dataSrc': 'data', 'dataSrc': 'data',
@ -86,15 +86,13 @@ jQuery(document).ready(function ()
"columns" : [ "columns" : [
{ {
data: 'number_formatted', data: 'number_formatted',
} },
<?php if ($_SESSION['user']['admin'] ?? false) { ?> {
,{
data: 'id', data: 'id',
render: function (data, type, row, meta) { render: function (data, type, row, meta) {
return '<input name="ids[]" type="checkbox" value="' + data + '">'; return '<input name="ids[]" type="checkbox" value="' + data + '">';
}, },
}, },
<?php } ?>
], ],
"deferRender": true "deferRender": true
}); });