From 5ebfa7f1c38612de218e1f3deed52caade6fc29c Mon Sep 17 00:00:00 2001 From: eug-L Date: Thu, 23 Jan 2025 12:34:02 +0800 Subject: [PATCH 01/17] rename setVisibility to setOptions & add override current settings --- .gitignore | 1 + .../admin/AdminTawktoController.php | 86 ++++++++++++------- .../admin/tawkto/helpers/view/view.tpl | 4 +- .../admin/AdminTawktoController.php | 84 +++++++++++------- .../admin/tawkto/helpers/view/view.tpl | 4 +- 5 files changed, 115 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index a05346c..81b95e7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ docker/bin .phpcs.cache node_modules/ .php_cs_fixer.cache +.aider* diff --git a/prestashop1.7/controllers/admin/AdminTawktoController.php b/prestashop1.7/controllers/admin/AdminTawktoController.php index e52b218..f028f41 100644 --- a/prestashop1.7/controllers/admin/AdminTawktoController.php +++ b/prestashop1.7/controllers/admin/AdminTawktoController.php @@ -221,8 +221,35 @@ public function ajaxProcessRemoveWidget() * * @return void */ - public function ajaxProcessSetVisibility() + public function ajaxProcessSetOptions() { + $key = TawkTo::TAWKTO_WIDGET_OPTS; + + // Apply selected options + $jsonOpts = $this->processSetOptions(Tools::getValue('options')); + + // Override current options/fallback if not selected + $currentOpts = Configuration::get($key); + if (!empty($currentOpts)) { + $currentOpts = json_decode($currentOpts, true); + $jsonOpts = array_merge($currentOpts, $jsonOpts); + } + + Configuration::updateValue($key, json_encode($jsonOpts)); + + die(json_encode(['success' => true])); + } + + /** + * Process options + * + * @param string $options Selected options + * + * @return array + */ + private function processSetOptions(string $options): array + { + // default options $jsonOpts = [ 'always_display' => false, @@ -241,38 +268,35 @@ public function ajaxProcessSetVisibility() 'enable_visitor_recognition' => false, ]; - $options = Tools::getValue('options'); - if (!empty($options)) { - $options = explode('&', $options); - foreach ($options as $post) { - [$column, $value] = explode('=', $post); - switch ($column) { - case 'hide_oncustom': - case 'show_oncustom': - // replace newlines and returns with comma, and convert to array for - // saving - $value = urldecode($value); - $value = str_ireplace(["\r\n", "\r", "\n"], ',', $value); - if (!empty($value)) { - $value = explode(',', $value); - $jsonOpts[$column] = json_encode($value); - } - break; - - case 'show_onfrontpage': - case 'show_oncategory': - case 'show_onproduct': - case 'always_display': - case 'enable_visitor_recognition': - $jsonOpts[$column] = ($value == 1); - break; - } - } + if (empty($options)) { + return $jsonOpts; } - $key = TawkTo::TAWKTO_WIDGET_OPTS; - Configuration::updateValue($key, json_encode($jsonOpts)); + $options = explode('&', $options); + foreach ($options as $post) { + [$column, $value] = explode('=', $post); + switch ($column) { + case 'hide_oncustom': + case 'show_oncustom': + // replace newlines and returns with comma, and convert to array for saving + $value = urldecode($value); + $value = str_ireplace(["\r\n", "\r", "\n"], ',', $value); + if (!empty($value)) { + $value = explode(',', $value); + $jsonOpts[$column] = json_encode($value); + } + break; + + case 'show_onfrontpage': + case 'show_oncategory': + case 'show_onproduct': + case 'always_display': + case 'enable_visitor_recognition': + $jsonOpts[$column] = ($value == 1); + break; + } + } - die(Tools::jsonEncode(['success' => true])); + return $jsonOpts; } } diff --git a/prestashop1.7/views/templates/admin/tawkto/helpers/view/view.tpl b/prestashop1.7/views/templates/admin/tawkto/helpers/view/view.tpl index 981ca48..2b0d856 100644 --- a/prestashop1.7/views/templates/admin/tawkto/helpers/view/view.tpl +++ b/prestashop1.7/views/templates/admin/tawkto/helpers/view/view.tpl @@ -207,7 +207,7 @@
-
Visibility Settings
+
Visibility Options
-
Visibility Settings
+
Visibility Options