From 67439317c466936873b883576964a32bad9a3465 Mon Sep 17 00:00:00 2001 From: "Fedor A. Fetisov" Date: Thu, 18 Mar 2021 02:38:19 +0300 Subject: [PATCH] Make possible to switch tracking for each type of button (also, fix #239) --- .../Button/Generator/AbstractGenerator.php | 52 ++++++++++++++++++ .../Mibew/Button/Generator/ImageGenerator.php | 53 +------------------ .../Generator/OperatorCodeGenerator.php | 4 ++ .../Mibew/Button/Generator/TextGenerator.php | 4 ++ .../Mibew/Controller/ButtonCodeController.php | 6 ++- .../server_side/button_code.handlebars | 15 ++++-- .../server_side/button_code.handlebars | 15 ++++-- 7 files changed, 90 insertions(+), 59 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php index d770d0c9..6732724f 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php @@ -24,7 +24,9 @@ use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface; use Mibew\EventDispatcher\EventDispatcher; use Mibew\EventDispatcher\Events; use Mibew\Routing\Generator\SecureUrlGeneratorInterface as RouteUrlGeneratorInterface; +use Mibew\Settings; use Mibew\Style\ChatStyle; +use Mibew\Style\InvitationStyle; /** * Contains base button generation functionality. @@ -246,4 +248,54 @@ abstract class AbstractGenerator implements GeneratorInterface return $fragment; } + + /** + * Generates HTML markup for Mibew Messenger Widget. + * + * @return \Canteen\HTML5\Fragment + */ + protected function getWidgetCode() + { + $widget_data = array(); + + // Get actual invitation style instance + $style_name = $this->getOption('invitation_style') + ? $this->getOption('invitation_style') + : InvitationStyle::getCurrentStyle(); + $style = new InvitationStyle($style_name); + + // URL of file with additional CSS rules for invitation popup + $widget_data['inviteStyle'] = $this->generateAssetUrl( + $style->getFilesPath() . '/invite.css' + ); + + // Time between requests to the server in milliseconds + $widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') * 1000; + + // URL for requests + $widget_data['requestURL'] = $this->generateUrl('widget_gateway'); + + // Locale for invitation + $widget_data['locale'] = $this->getOption('locale'); + + // Name of the cookie to track user. It is used if third-party cookie + // blocked + $widget_data['visitorCookieName'] = VISITOR_COOKIE_NAME; + + $markup = HTML5\html('fragment'); + $markup->addChild(HTML5\html('div#mibew-invitation')); + $markup->addChild( + HTML5\html('script')->setAttributes(array( + 'type' => 'text/javascript', + 'src' => $this->generateAssetUrl('js/compiled/widget.js'), + )) + ); + $markup->addChild( + HTML5\html('script') + ->setAttribute('type', 'text/javascript') + ->addChild('Mibew.Widget.init(' . json_encode($widget_data) . ')') + ); + + return $markup; + } } diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php index 1cea43b2..b77049db 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php @@ -21,7 +21,6 @@ namespace Mibew\Button\Generator; use Canteen\HTML5; use Mibew\Settings; -use Mibew\Style\InvitationStyle; /** * Generates an Image button. @@ -57,61 +56,11 @@ class ImageGenerator extends TextGenerator $button = HTML5\html('fragment'); $button->addChild(HTML5\html('comment', 'mibew button')); $button->addChild($this->getPopupLink($image)); - if (Settings::get('enabletracking')) { + if (Settings::get('enabletracking') && !$this->getOption('disable_tracking')) { $button->addChild($this->getWidgetCode()); } $button->addChild(HTML5\html('comment', '/ mibew button')); return $button; } - - /** - * Generates HTML markup for Mibew Messenger Widget. - * - * @return \Canteen\HTML5\Fragment - */ - protected function getWidgetCode() - { - $widget_data = array(); - - // Get actual invitation style instance - $style_name = $this->getOption('invitation_style') - ? $this->getOption('invitation_style') - : InvitationStyle::getCurrentStyle(); - $style = new InvitationStyle($style_name); - - // URL of file with additional CSS rules for invitation popup - $widget_data['inviteStyle'] = $this->generateAssetUrl( - $style->getFilesPath() . '/invite.css' - ); - - // Time between requests to the server in milliseconds - $widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') * 1000; - - // URL for requests - $widget_data['requestURL'] = $this->generateUrl('widget_gateway'); - - // Locale for invitation - $widget_data['locale'] = $this->getOption('locale'); - - // Name of the cookie to track user. It is used if third-party cookie - // blocked - $widget_data['visitorCookieName'] = VISITOR_COOKIE_NAME; - - $markup = HTML5\html('fragment'); - $markup->addChild(HTML5\html('div#mibew-invitation')); - $markup->addChild( - HTML5\html('script')->setAttributes(array( - 'type' => 'text/javascript', - 'src' => $this->generateAssetUrl('js/compiled/widget.js'), - )) - ); - $markup->addChild( - HTML5\html('script') - ->setAttribute('type', 'text/javascript') - ->addChild('Mibew.Widget.init(' . json_encode($widget_data) . ')') - ); - - return $markup; - } } diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php index be090660..4402906d 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php @@ -20,6 +20,7 @@ namespace Mibew\Button\Generator; use Canteen\HTML5; +use Mibew\Settings; /** * Generates an Operator's Code field. @@ -57,6 +58,9 @@ class OperatorCodeGenerator extends AbstractGenerator $button->addChild($form); $button->addChild($this->getPopup()); $button->addChild(HTML5\html('comment', '/ mibew operator code field')); + if (Settings::get('enabletracking') && !$this->getOption('disable_tracking')) { + $button->addChild($this->getWidgetCode()); + } return $button; } diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php index 027a807a..3a549541 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php @@ -20,6 +20,7 @@ namespace Mibew\Button\Generator; use Canteen\HTML5; +use Mibew\Settings; /** * Generates a Text button. @@ -65,6 +66,9 @@ class TextGenerator extends AbstractGenerator $fragment = HTML5\html('fragment'); $fragment->addChild($link); $fragment->addChild($this->getPopup()); + if (Settings::get('enabletracking') && !$this->getOption('disable_tracking')) { + $fragment->addChild($this->getWidgetCode()); + } return $fragment; } diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php index 3ec08dd1..fe9a2b30 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php @@ -84,6 +84,8 @@ class ButtonCodeController extends AbstractController $mod_security = $request->query->get('modsecurity') == 'on'; $force_windows = $request->query->get('forcewindows') == 'on'; + $disable_tracking = $request->query->get('disabletracking') == 'on'; + $code_type = $request->query->get('codetype', 'button'); if (!in_array($code_type, array('button', 'operator_code', 'text_link'))) { throw new BadRequestException('Wrong value of "codetype" param.'); @@ -103,6 +105,8 @@ class ButtonCodeController extends AbstractController 'force_secure' => $force_secure, 'mod_security' => $mod_security, 'prefer_iframe' => !$force_windows, + 'invitation_style' => $invitation_style, + 'disable_tracking' => $disable_tracking ); if ($operator_code) { @@ -127,7 +131,6 @@ class ButtonCodeController extends AbstractController // Set generator-specific options $button_generator->setOption('image', $image); - $button_generator->setOption('invitation_style', $invitation_style); } else { // Make sure locale exists if (!$lang || !in_array($lang, $locales_list)) { @@ -172,6 +175,7 @@ class ButtonCodeController extends AbstractController $page['formmodsecurity'] = $mod_security; $page['formcodetype'] = $code_type; $page['formforcewindows'] = $force_windows; + $page['formdisabletracking'] = $disable_tracking; $page['enabletracking'] = Settings::get('enabletracking'); $page['operator_code'] = $operator_code; diff --git a/src/mibew/styles/pages/dark/templates_src/server_side/button_code.handlebars b/src/mibew/styles/pages/dark/templates_src/server_side/button_code.handlebars index 48eb2516..e0948a9c 100644 --- a/src/mibew/styles/pages/dark/templates_src/server_side/button_code.handlebars +++ b/src/mibew/styles/pages/dark/templates_src/server_side/button_code.handlebars @@ -70,8 +70,18 @@ +
+ {{/if}} - {{#if enabletracking}} + {{#if enabletracking}} +
+ +
+ +
+
+ + {{#unless formdisabletracking}}
@@ -82,8 +92,7 @@
- {{/if}} - + {{/unless}}
{{/if}} diff --git a/src/mibew/styles/pages/default/templates_src/server_side/button_code.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/button_code.handlebars index 48eb2516..e0948a9c 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/button_code.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/button_code.handlebars @@ -70,8 +70,18 @@ +
+ {{/if}} - {{#if enabletracking}} + {{#if enabletracking}} +
+ +
+ +
+
+ + {{#unless formdisabletracking}}
@@ -82,8 +92,7 @@
- {{/if}} - + {{/unless}}
{{/if}}