|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tag Manager |
|
4
|
|
|
* Copyright (c) Webmatch GmbH |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace WbmTagManager\Subscriber\Frontend; |
|
18
|
|
|
|
|
19
|
|
|
use Enlight\Event\SubscriberInterface; |
|
20
|
|
|
use WbmTagManager\Services\TagManagerVariables; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class PostDispatch |
|
24
|
|
|
*/ |
|
25
|
|
|
class PostDispatch implements SubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var TagManagerVariables |
|
29
|
|
|
*/ |
|
30
|
|
|
private $variables; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Shopware_Components_Config |
|
34
|
|
|
*/ |
|
35
|
|
|
private $config; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
TagManagerVariables $variables, |
|
39
|
|
|
\Shopware_Components_Config $config |
|
40
|
|
|
) { |
|
41
|
|
|
$this->variables = $variables; |
|
42
|
|
|
$this->config = $config; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getSubscribedEvents() |
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatch', |
|
52
|
|
|
'Enlight_Controller_Action_PostDispatch_Widgets' => 'onPostDispatch', |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param \Enlight_Controller_ActionEventArgs $args |
|
58
|
|
|
*/ |
|
59
|
|
|
public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args) |
|
60
|
|
|
{ |
|
61
|
|
|
if ( |
|
62
|
|
|
!$this->config->getByNamespace('WbmTagManager', 'wbmTagManagerActive') || |
|
63
|
|
|
empty($this->config->getByNamespace('WbmTagManager', 'wbmTagManagerContainer')) |
|
64
|
|
|
) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$controller = $args->getSubject(); |
|
69
|
|
|
$request = $controller->Request(); |
|
70
|
|
|
|
|
71
|
|
|
$module = $oModule = join('_', [ |
|
72
|
|
|
strtolower($request->getModuleName()), |
|
73
|
|
|
strtolower($request->getControllerName()), |
|
74
|
|
|
strtolower($request->getActionName()), |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
if ($module == 'frontend_checkout_ajaxcart') { |
|
78
|
|
|
$module = 'frontend_checkout_' . strtolower($request->getParam('action')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$search = [ |
|
82
|
|
|
'widgets_listing_ajaxlisting', |
|
83
|
|
|
'widgets_listing_listingcount', |
|
84
|
|
|
'frontend_checkout_ajaxcart', |
|
85
|
|
|
'frontend_checkout_ajax_add_article', |
|
86
|
|
|
'frontend_checkout_ajax_delete_article', |
|
87
|
|
|
]; |
|
88
|
|
|
$replace = [ |
|
89
|
|
|
'frontend_listing_index', |
|
90
|
|
|
'frontend_listing_index', |
|
91
|
|
|
'frontend_checkout_cart', |
|
92
|
|
|
'frontend_checkout_ajaxaddarticlecart', |
|
93
|
|
|
'frontend_checkout_ajaxdeletearticlecart', |
|
94
|
|
|
]; |
|
95
|
|
|
$module = str_replace($search, $replace, $module); |
|
96
|
|
|
|
|
97
|
|
|
$this->variables->setViewVariables($controller->View()->getAssign()); |
|
98
|
|
|
$this->variables->render($module); |
|
99
|
|
|
|
|
100
|
|
|
// Since SW 5.3 the generic listingCountAction is used for paginated listings. |
|
101
|
|
|
// Get the response json body, decode it, prepend the dataLayer to the listing key |
|
102
|
|
|
// and set json encoded markup as response body. |
|
103
|
|
|
if ($oModule == 'widgets_listing_listingcount') { |
|
104
|
|
|
/** @var \Enlight_Controller_Response_ResponseHttp $response */ |
|
105
|
|
|
$response = $controller->Response(); |
|
106
|
|
|
$data = json_decode($response->getBody(), true); |
|
107
|
|
|
|
|
108
|
|
|
if (isset($data['listing'])) { |
|
109
|
|
|
if ($this->variables->getVariables()) { |
|
110
|
|
|
$data['listing'] = $this->variables->prependDataLayer( |
|
111
|
|
|
$data['listing'], |
|
112
|
|
|
$this->config->getByNamespace('WbmTagManager', 'wbmTagManagerJsonPrettyPrint') |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$response->setBody(json_encode($data)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|