From c63f3bebba5732791a94f450cf6fc5e179228188 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Wed, 24 Mar 2021 00:22:17 +0100 Subject: [PATCH] add page for calls --- adapters/TestAdapter.php | 4 +- controllers/publics/Call.php | 93 +++++++++++++++++ controllers/publics/Phone.php | 48 ++------- models/Call.php | 42 ++++++++ routes.php | 12 ++- templates/call/list.php | 145 +++++++++++++++++++++++++++ templates/conditional_group/list.php | 2 +- templates/incs/nav.php | 5 +- templates/phone/list.php | 17 ++++ 9 files changed, 323 insertions(+), 45 deletions(-) create mode 100644 controllers/publics/Call.php create mode 100644 templates/call/list.php diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php index 8a318a4..4ddb9c6 100644 --- a/adapters/TestAdapter.php +++ b/adapters/TestAdapter.php @@ -149,12 +149,12 @@ namespace adapters; public static function meta_support_inbound_call_callback(): bool { - return false; + return true; } public static function meta_support_end_call_callback(): bool { - return false; + return true; } public function send(string $destination, string $text, bool $flash = false, bool $mms = false, array $medias = []) : array diff --git a/controllers/publics/Call.php b/controllers/publics/Call.php new file mode 100644 index 0000000..fb05bc2 --- /dev/null +++ b/controllers/publics/Call.php @@ -0,0 +1,93 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +namespace controllers\publics; + + /** + * Page des calls. + */ + class Call extends \descartes\Controller + { + private $internal_call; + + /** + * Cette fonction est appelée avant toute les autres : + * Elle vérifie que l'utilisateur est bien connecté. + * + * @return void; + */ + public function __construct() + { + $bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD); + + $this->internal_call = new \controllers\internals\Call($bdd); + + \controllers\internals\Tool::verifyconnect(); + } + + /** + * Page for showing calls list + */ + public function list() + { + $this->render('call/list'); + } + + /** + * Return calls list as json. + */ + public function list_json() + { + $entities = $this->internal_call->list_for_user($_SESSION['user']['id']); + foreach ($entities as &$entity) + { + switch ($entity['direction']) + { + case \models\Call::DIRECTION_INBOUND : + $entity['origin_formatted'] = \controllers\internals\Tool::phone_link($entity['origin']); + break; + + case \models\Call::DIRECTION_OUTBOUND : + $entity['destination_formatted'] = \controllers\internals\Tool::phone_link($entity['destination']); + break; + } + } + + header('Content-Type: application/json'); + echo json_encode(['data' => $entities]); + } + + /** + * Delete a list of calls + * + * @param array int $_GET['ids'] : Ids of calls to delete + * @param string $csrf : csrf token + * + * @return boolean; + */ + public function delete(string $csrf) + { + if (!$this->verify_csrf($csrf)) + { + \FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !'); + + return $this->redirect(\descartes\Router::url('Call', 'list')); + } + + $ids = $_GET['ids'] ?? []; + foreach ($ids as $id) + { + $this->internal_call->delete_for_user($_SESSION['user']['id'], $id); + } + + return $this->redirect(\descartes\Router::url('Call', 'list')); + } + } diff --git a/controllers/publics/Phone.php b/controllers/publics/Phone.php index 329572a..81d6249 100644 --- a/controllers/publics/Phone.php +++ b/controllers/publics/Phone.php @@ -33,43 +33,7 @@ class Phone extends \descartes\Controller */ public function list() { - $id_user = $_SESSION['user']['id']; - $api_key = $_SESSION['user']['api_key']; - $phones = $this->internal_phone->list_for_user($id_user); - - $adapters = []; - $adapters = $this->internal_adapter->list_adapters(); - foreach ($adapters as $key => $adapter) - { - unset($adapters[$key]); - $adapters[$adapter['meta_classname']] = $adapter; - } - - foreach ($phones as &$phone) - { - $adapter = $adapters[$phone['adapter']] ?? false; - - if (!$adapter) - { - $phone['adapter'] = 'Inconnu'; - - continue; - } - - $phone['adapter'] = $adapter['meta_name']; - - if ($adapter['meta_support_reception']) - { - $phone['callback_reception'] = \descartes\Router::url('Callback', 'reception', ['adapter_uid' => $adapter['meta_uid'], 'id_phone' => $phone['id']], ['api_key' => $api_key]); - } - - if ($adapter['meta_support_status_change']) - { - $phone['callback_status'] = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => $adapter['meta_uid']], ['api_key' => $api_key]); - } - } - - $this->render('phone/list', ['phones' => $phones]); + $this->render('phone/list'); } /** @@ -111,6 +75,16 @@ class Phone extends \descartes\Controller { $phone['callback_status'] = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => $adapter['meta_uid']], ['api_key' => $api_key]); } + + if ($adapter['meta_support_inbound_call_callback']) + { + $phone['callback_inbound_call'] = \descartes\Router::url('Callback', 'inbound_call', ['id_phone' => $phone['id']], ['api_key' => $api_key]); + } + + if ($adapter['meta_support_end_call_callback']) + { + $phone['callback_end_call'] = \descartes\Router::url('Callback', 'end_call', ['id_phone' => $phone['id']], ['api_key' => $api_key]); + } } header('Content-Type: application/json'); diff --git a/models/Call.php b/models/Call.php index dd3e8a9..e8e08ce 100644 --- a/models/Call.php +++ b/models/Call.php @@ -18,6 +18,48 @@ namespace models; { const DIRECTION_INBOUND = 'inbound'; const DIRECTION_OUTBOUND = 'outbound'; + + /** + * Return a list of call 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 list_for_user(int $id_user, $limit, $offset) + { + $query = ' + SELECT `call`.*, contact.name as contact_name, phone.name as phone_name + FROM `call` + LEFT JOIN contact + ON contact.number = `call`.destination + OR contact.number = `call`.origin + LEFT JOIN phone + ON phone.id = `call`.id_phone + WHERE `call`.id_user = :id_user + '; + + if (null !== $limit) + { + $limit = (int) $limit; + + $query .= ' LIMIT ' . $limit; + if (null !== $offset) + { + $offset = (int) $offset; + $query .= ' OFFSET ' . $offset; + } + } + + $params = [ + 'id_user' => $id_user, + ]; + + return $this->_run_query($query, $params); + } /** * Get a call for a user by his phone and uid diff --git a/routes.php b/routes.php index bf66579..0384728 100644 --- a/routes.php +++ b/routes.php @@ -58,10 +58,7 @@ ], 'Event' => [ - 'list' => [ - '/event/', - '/event/p/{page}/', - ], + 'list' => '/event/', 'list_json' => '/event/json/', 'delete' => '/event/delete/{csrf}/', ], @@ -155,6 +152,12 @@ 'delete' => '/phone/delete/{csrf}/', ], + 'Call' => [ + 'list' => '/call/', + 'list_json' => '/call/json/', + 'delete' => '/call/delete/{csrf}/', + ], + 'Webhook' => [ 'list' => '/webhook/', 'list_json' => '/webhook/json/', @@ -169,6 +172,7 @@ 'update_sended_status' => '/callback/status/{adapter_uid}/', 'reception' => '/callback/reception/{adapter_uid}/{id_phone}/', 'inbound_call' => '/callback/inbound_call/{id_phone}/', + 'end_call' => '/callback/end_call/{id_phone}/', ], 'Api' => [ diff --git a/templates/call/list.php b/templates/call/list.php new file mode 100644 index 0000000..7fed56a --- /dev/null +++ b/templates/call/list.php @@ -0,0 +1,145 @@ +render('incs/head', ['title' => 'Appels - Show All']) +?> +
+render('incs/nav', ['page' => 'calls']) +?> +
+
+ +
+
+

+ Dashboard Appels +

+ +
+
+ + +
+
+
+
+

Liste des appels

+
+
+
+
+ + + + + + + + + + + + +
OrigineDestinataireDébut de l'appelFin de l'appelDirection
+
+
+
+ Action pour la séléction : + +
+
+
+
+
+
+
+
+
+
+ +render('incs/footer'); diff --git a/templates/conditional_group/list.php b/templates/conditional_group/list.php index ecb104e..b8ce2ad 100644 --- a/templates/conditional_group/list.php +++ b/templates/conditional_group/list.php @@ -1,7 +1,7 @@ render('incs/head', ['title' => 'ConditionalGroupes Conditionnels - Show All']) + $this->render('incs/head', ['title' => 'Groupes Conditionnels - Show All']) ?>
  • Logs -