Make possible to switch tracking for each type of button (also, fix #239)

This commit is contained in:
Fedor A. Fetisov 2021-03-18 02:38:19 +03:00
parent 5e90541918
commit 67439317c4
7 changed files with 90 additions and 59 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -70,8 +70,18 @@
</select>
</div>
</div>
<br clear="all"/>
{{/if}}
{{#if enabletracking}}
{{#if enabletracking}}
<div class="field-in-row">
<label for="disable-tracking" class="field-label">{{l10n "Disable tracking"}}</label>
<div class="field-value-no-description">
<input id="disable-tracking" type="checkbox" name="disabletracking" value="on"{{#if formdisabletracking}} checked="checked"{{/if}} onchange="this.form.submit();"/>
</div>
</div>
{{#unless formdisabletracking}}
<div class="field-in-row">
<label for="invitation-style" class="field-label">{{l10n "Invitation style"}}</label>
<div class="field-value-no-description">
@ -82,8 +92,7 @@
</select>
</div>
</div>
{{/if}}
{{/unless}}
<br clear="all"/>
{{/if}}

View File

@ -70,8 +70,18 @@
</select>
</div>
</div>
<br clear="all"/>
{{/if}}
{{#if enabletracking}}
{{#if enabletracking}}
<div class="field-in-row">
<label for="disable-tracking" class="field-label">{{l10n "Disable tracking"}}</label>
<div class="field-value-no-description">
<input id="disable-tracking" type="checkbox" name="disabletracking" value="on"{{#if formdisabletracking}} checked="checked"{{/if}} onchange="this.form.submit();"/>
</div>
</div>
{{#unless formdisabletracking}}
<div class="field-in-row">
<label for="invitation-style" class="field-label">{{l10n "Invitation style"}}</label>
<div class="field-value-no-description">
@ -82,8 +92,7 @@
</select>
</div>
</div>
{{/if}}
{{/unless}}
<br clear="all"/>
{{/if}}