/
Module.php
113 lines (97 loc) · 3.63 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Copyright (c)2014-2014 heiglandreas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category
* @author Andreas Heigl<andreas@heigl.org>
* @copyright ©2014-2014 Andreas Heigl
* @license http://www.opesource.org/licenses/mit-license.php MIT-License
* @version 0.0
* @since 28.06.14
* @link https://github.com/heiglandreas/
*/
namespace OrgHeiglPiwik;
use Zend\EventManager\StaticEventManager;
use Zend\Mvc\MvcEvent;
use Zend\View\ViewEvent;
use Zend\View\View;
class Module
{
protected $template = <<<EOT
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//%%server%%/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', %%site_id%%]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
EOT;
protected $serviceManager = null;
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $e)
{
$this->serviceManager = $e->getApplication()->getServiceManager();
}
public function init()
{
// Attach Event to EventManager
$events = StaticEventManager::getInstance ();
// Add event of authentication before dispatch
$events->attach('Zend\View\View', ViewEvent::EVENT_RENDERER_POST, array(
$this,
'addPiwikCode'
), 110 );
}
/**
* Include the PIWIK-Tracking-code into every page.
*
* @param $event
*/
public function addPiwikCode(ViewEvent $event)
{
$model = $event->getModel();
if (! $model instanceof \Zend\View\Model\ViewModel) {
return;
}
// Return if this is a subrenderer. Therefore we only render once!
$options = $model->getOptions();
if (array_key_exists('has_parent', $options) && $options['has_parent']) {
return;
}
$renderer = $event->getRenderer();
if (! $renderer instanceof \Zend\View\Renderer\PhpRenderer) {
return;
}
$config = $this->serviceManager->get('config');
$piwikConfig = $config['orgHeiglPiwik'];
$code = str_replace(array_map(function($e){
return '%%' . $e . '%%';
}, array_keys($piwikConfig)), array_values($piwikConfig), $this->template);
$renderer->headScript()->appendScript('//<![CDATA[' . "\n" . $code . "\n" . '//]]>');
return $renderer;
}
}