From 38d350dfc2dc6ce69a9a0dfc19a14610c92996d1 Mon Sep 17 00:00:00 2001
From: osaajani <>
Date: Sat, 18 Feb 2023 16:39:07 +0100
Subject: [PATCH 01/10] start add support for phone status + improve edit of
phone for hidden phones
---
adapters/AdapterInterface.php | 14 +++++
adapters/BenchmarkAdapter.php | 18 +++++++
adapters/GammuAdapter.php | 18 +++++++
adapters/KannelAdapter.php | 18 +++++++
adapters/OctopushShortcodeAdapter.php | 18 +++++++
adapters/OctopushVirtualNumberAdapter.php | 18 +++++++
adapters/OvhSmsShortcodeAdapter.php | 18 +++++++
adapters/OvhSmsVirtualNumberAdapter.php | 18 +++++++
adapters/TestAdapter.php | 18 +++++++
adapters/TwilioVirtualNumberAdapter.php | 18 +++++++
controllers/internals/Phone.php | 13 +++++
controllers/publics/Api.php | 42 ++++++++++++++-
controllers/publics/Phone.php | 54 ++++++++++++++++++-
.../20230218113648_add_status_phone.php | 38 +++++++++++++
models/Phone.php | 4 ++
routes.php | 4 ++
templates/group/list.php | 2 +-
templates/phone/edit.php | 6 ++-
templates/phone/list.php | 24 ++++++++-
19 files changed, 357 insertions(+), 6 deletions(-)
create mode 100644 db/migrations/20230218113648_add_status_phone.php
diff --git a/adapters/AdapterInterface.php b/adapters/AdapterInterface.php
index c967bb2..5de6a5c 100644
--- a/adapters/AdapterInterface.php
+++ b/adapters/AdapterInterface.php
@@ -79,6 +79,11 @@ interface AdapterInterface
*/
public static function meta_support_read(): bool;
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool;
+
/**
* Does the implemented service support reception callback.
*/
@@ -152,6 +157,15 @@ interface AdapterInterface
*/
public function test(): bool;
+
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string;
+
+
/**
* Method called on reception of a status update notification for a SMS.
*
diff --git a/adapters/BenchmarkAdapter.php b/adapters/BenchmarkAdapter.php
index 493ad6a..5d03ce3 100644
--- a/adapters/BenchmarkAdapter.php
+++ b/adapters/BenchmarkAdapter.php
@@ -111,6 +111,14 @@ namespace adapters;
return false;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -226,6 +234,16 @@ namespace adapters;
return [];
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public static function status_change_callback()
{
return null;
diff --git a/adapters/GammuAdapter.php b/adapters/GammuAdapter.php
index da42b49..f1c15e4 100644
--- a/adapters/GammuAdapter.php
+++ b/adapters/GammuAdapter.php
@@ -121,6 +121,14 @@ namespace adapters;
return true;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -301,6 +309,16 @@ namespace adapters;
return $response;
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
//Always return true as we cannot test because we would be needing a root account
diff --git a/adapters/KannelAdapter.php b/adapters/KannelAdapter.php
index 4d63802..19782d3 100644
--- a/adapters/KannelAdapter.php
+++ b/adapters/KannelAdapter.php
@@ -209,6 +209,14 @@ class KannelAdapter implements AdapterInterface
return false;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -354,6 +362,16 @@ class KannelAdapter implements AdapterInterface
return [];
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/adapters/OctopushShortcodeAdapter.php b/adapters/OctopushShortcodeAdapter.php
index 2c70b97..59283a5 100644
--- a/adapters/OctopushShortcodeAdapter.php
+++ b/adapters/OctopushShortcodeAdapter.php
@@ -174,6 +174,14 @@ class OctopushShortcodeAdapter implements AdapterInterface
return false;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -325,6 +333,16 @@ class OctopushShortcodeAdapter implements AdapterInterface
return [];
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/adapters/OctopushVirtualNumberAdapter.php b/adapters/OctopushVirtualNumberAdapter.php
index 0096f72..f9238a6 100644
--- a/adapters/OctopushVirtualNumberAdapter.php
+++ b/adapters/OctopushVirtualNumberAdapter.php
@@ -173,6 +173,14 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
return false;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -317,6 +325,16 @@ class OctopushVirtualNumberAdapter implements AdapterInterface
return [];
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/adapters/OvhSmsShortcodeAdapter.php b/adapters/OvhSmsShortcodeAdapter.php
index 91a28a3..cf16795 100644
--- a/adapters/OvhSmsShortcodeAdapter.php
+++ b/adapters/OvhSmsShortcodeAdapter.php
@@ -162,6 +162,14 @@ namespace adapters;
return true;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -327,6 +335,16 @@ namespace adapters;
}
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/adapters/OvhSmsVirtualNumberAdapter.php b/adapters/OvhSmsVirtualNumberAdapter.php
index b1078f9..cfc1b2a 100644
--- a/adapters/OvhSmsVirtualNumberAdapter.php
+++ b/adapters/OvhSmsVirtualNumberAdapter.php
@@ -166,6 +166,14 @@ namespace adapters;
return true;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -317,6 +325,16 @@ namespace adapters;
}
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/adapters/TestAdapter.php b/adapters/TestAdapter.php
index 048e81c..2ad3c48 100644
--- a/adapters/TestAdapter.php
+++ b/adapters/TestAdapter.php
@@ -116,6 +116,14 @@ namespace adapters;
return true;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -287,6 +295,16 @@ namespace adapters;
}
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
return true;
diff --git a/adapters/TwilioVirtualNumberAdapter.php b/adapters/TwilioVirtualNumberAdapter.php
index 5ad0cc9..a17722c 100644
--- a/adapters/TwilioVirtualNumberAdapter.php
+++ b/adapters/TwilioVirtualNumberAdapter.php
@@ -160,6 +160,14 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
return true;
}
+ /**
+ * Does the implemented service support updating phone status.
+ */
+ public static function meta_support_phone_status(): bool
+ {
+ return false;
+ }
+
/**
* Does the implemented service support flash smss.
*/
@@ -295,6 +303,16 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
}
}
+ /**
+ * Method called to verify phone status
+ *
+ * @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
+ */
+ public function check_phone_status(): string
+ {
+ return \models\Phone::STATUS_AVAILABLE;
+ }
+
public function test(): bool
{
try
diff --git a/controllers/internals/Phone.php b/controllers/internals/Phone.php
index 9bdeac7..a2cebf5 100644
--- a/controllers/internals/Phone.php
+++ b/controllers/internals/Phone.php
@@ -236,6 +236,19 @@ namespace controllers\internals;
return true;
}
+ /**
+ * Update a phone status.
+ *
+ * @param int $id : Phone id
+ * @param string $status : The new status of the phone
+ *
+ * @return bool : false on error, true on success
+ */
+ public function update_status(int $id, string $status) : bool
+ {
+ return (bool) $this->get_model()->update($id, ['status' => $status]);
+ }
+
/**
* Get the model for the Controller.
*/
diff --git a/controllers/publics/Api.php b/controllers/publics/Api.php
index 7ee0d36..c5547d6 100644
--- a/controllers/publics/Api.php
+++ b/controllers/publics/Api.php
@@ -710,7 +710,7 @@ namespace controllers\publics;
$priority = $_POST['priority'] ?? $phone['priority'];
$priority = max(((int) $priority), 0);
$adapter = $_POST['adapter'] ?? $phone['adapter'];
- $adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : json_decode($phone['adapter_data']);
+ $adapter_data = !empty($_POST['adapter_data']) ? $_POST['adapter_data'] : json_decode($phone['adapter_data'], true);
$adapter_data = is_array($adapter_data) ? $adapter_data : [$adapter_data];
$limits = $_POST['limits'] ?? $limits;
$limits = is_array($limits) ? $limits : [$limits];
@@ -890,4 +890,44 @@ namespace controllers\publics;
return $this->json($return);
}
+
+ /**
+ * Trigger re-checking of a phone status
+ *
+ * @param int $id : Id of phone to re-check status
+ */
+ public function post_update_phone_status ($id)
+ {
+ $return = self::DEFAULT_RETURN;
+
+ $phone = $this->internal_phone->get_for_user($this->user['id'], $id);
+ if (!$phone)
+ {
+ $return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];
+ $return['message'] = self::ERROR_MESSAGES['CANNOT_UPDATE'];
+ $this->auto_http_code(false);
+
+ return $this->json($return);
+ }
+
+ //Check adapter is working correctly with thoses names and data
+ $adapter_classname = $phone['adapter'];
+ if (!call_user_func([$adapter_classname, 'meta_support_phone_status']))
+ {
+ $return['error'] = self::ERROR_CODES['CANNOT_UPDATE'];
+ $return['message'] = self::ERROR_MESSAGES['CANNOT_UPDATE'];
+ $this->auto_http_code(false);
+
+ return $this->json($return);
+ }
+
+ $adapter_instance = new $adapter_classname($phone['adapter_data']);
+ $new_status = $adapter_instance->check_phone_status();
+
+ $status_update = $this->internal_phone->update_status($id, $new_status);
+ $return['response'] = $new_status;
+ $this->auto_http_code(true);
+
+ return $this->json($return);
+ }
}
diff --git a/controllers/publics/Phone.php b/controllers/publics/Phone.php
index 7451288..80c5912 100644
--- a/controllers/publics/Phone.php
+++ b/controllers/publics/Phone.php
@@ -88,6 +88,11 @@ class Phone extends \descartes\Controller
{
$phone['callback_end_call'] = \descartes\Router::url('Callback', 'end_call', ['id_phone' => $phone['id']], ['api_key' => $api_key]);
}
+
+ if ($adapter['meta_support_phone_status'])
+ {
+ $phone['support_phone_status'] = true;
+ }
}
header('Content-Type: application/json');
@@ -425,7 +430,14 @@ class Phone extends \descartes\Controller
continue;
}
- if ($find_adapter['meta_hidden'])
+ $current_phone = $this->internal_phone->get_for_user($id_user, $id_phone);
+ if (!$current_phone)
+ {
+ continue;
+ }
+
+ // We can only use an hidden adapter if it was already the adapter we was using
+ if ($find_adapter['meta_hidden'] && $adapter != $current_phone['adapter'])
{
continue;
}
@@ -499,4 +511,44 @@ class Phone extends \descartes\Controller
return $this->redirect(\descartes\Router::url('Phone', 'list'));
}
+
+
+ /**
+ * Re-check phone status
+ * @param array int $_GET['ids'] : ids of phones we want to update status
+ * @param $csrf : CSRF token
+ */
+ public function update_status ($csrf)
+ {
+ if (!$this->verify_csrf($csrf))
+ {
+ \FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
+
+ return $this->redirect(\descartes\Router::url('Phone', 'add'));
+ }
+
+ $ids = $_GET['ids'] ?? [];
+ $id_user = $_SESSION['user']['id'];
+
+ foreach ($ids as $id)
+ {
+ $phone = $this->internal_phone->get_for_user($id_user, $id);
+
+ //Check adapter is working correctly with thoses names and data
+ $adapter_classname = $phone['adapter'];
+ if (!call_user_func([$adapter_classname, 'meta_support_phone_status']))
+ {
+ continue;
+ }
+
+ $adapter_instance = new $adapter_classname($phone['adapter_data']);
+ $new_status = $adapter_instance->check_phone_status();
+
+ $status_update = $this->internal_phone->update_status($id, $new_status);
+ }
+
+ \FlashMessage\FlashMessage::push('success', 'Les status des téléphones ont bien été mis à jour.');
+
+ return $this->redirect(\descartes\Router::url('Phone', 'list'));
+ }
}
diff --git a/db/migrations/20230218113648_add_status_phone.php b/db/migrations/20230218113648_add_status_phone.php
new file mode 100644
index 0000000..97c397e
--- /dev/null
+++ b/db/migrations/20230218113648_add_status_phone.php
@@ -0,0 +1,38 @@
+table('phone');
+ $table->addColumn('status', 'enum', ['values' => ['available', 'unavailable', 'no_credit'], 'default' => 'available']);
+ $table->update();
+ }
+}
diff --git a/models/Phone.php b/models/Phone.php
index 4579b22..41fde4d 100644
--- a/models/Phone.php
+++ b/models/Phone.php
@@ -14,6 +14,10 @@ namespace models;
class Phone extends StandardModel
{
+ const STATUS_AVAILABLE = 'available';
+ const STATUS_UNAVAILABLE = 'unavailable';
+ const STATUS_NO_CREDIT = 'no_credit';
+
/**
* Return all phones that belongs to active users
*
diff --git a/routes.php b/routes.php
index a5e87cd..1de71f8 100644
--- a/routes.php
+++ b/routes.php
@@ -164,6 +164,7 @@
'delete' => '/phone/delete/{csrf}/',
'edit' => '/phone/edit/',
'update' => '/phone/update/{csrf}/',
+ 'update_status' => '/phone/update_status/{csrf}/'
],
'Call' => [
@@ -206,6 +207,9 @@
'post_update_phone' => [
'/api/phone/{id}/',
],
+ 'post_update_phone_status' => [
+ '/api/phone/{id}/status/',
+ ],
'delete_phone' => [
'/api/phone/{id}/',
],
diff --git a/templates/group/list.php b/templates/group/list.php
index 5da0452..4af8d06 100644
--- a/templates/group/list.php
+++ b/templates/group/list.php
@@ -111,7 +111,7 @@ jQuery(document).ready(function ()
{
data: '_',
render: function (data, type, row, meta) {
- return '';
+ return '';
},
},
{
diff --git a/templates/phone/edit.php b/templates/phone/edit.php
index a1b2446..c27d24a 100644
--- a/templates/phone/edit.php
+++ b/templates/phone/edit.php
@@ -65,13 +65,15 @@