GoogleAnalyticsPlugin.inc.php 6.85 KB
Newer Older
michael's avatar
michael committed
1
2
3
<?php

/**
asmecher's avatar
asmecher committed
4
 * @file GoogleAnalyticsPlugin.inc.php
michael's avatar
michael committed
5
 *
asmecher's avatar
asmecher committed
6
 * Copyright (c) 2003-2008 John Willinsky
michael's avatar
michael committed
7
8
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
asmecher's avatar
asmecher committed
9
 * @class GoogleAnalyticsPlugin
asmecher's avatar
asmecher committed
10
 * @ingroup plugins_generic_googleAnalytics
michael's avatar
michael committed
11
 *
asmecher's avatar
asmecher committed
12
 * @brief Google Analytics plugin class
michael's avatar
michael committed
13
14
 */

asmecher's avatar
asmecher committed
15
16
17
// $Id$


michael's avatar
michael committed
18
19
20
21
22
23
import('classes.plugins.GenericPlugin');

class GoogleAnalyticsPlugin extends GenericPlugin {

	/**
	 * Called as a plugin is registered to the registry
asmecher's avatar
asmecher committed
24
	 * @param $category String Name of category plugin was registered to
michael's avatar
michael committed
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	 * @return boolean True iff plugin initialized successfully; if false,
	 * 	the plugin will not be registered.
	 */
	function register($category, $path) {
		$success = parent::register($category, $path);
		if (!Config::getVar('general', 'installed')) return false;
		$this->addLocaleData();
		if ($success) {
			// Insert Google Analytics page tag to common footer  
			HookRegistry::register('Templates::Common::Footer::PageFooter', array($this, 'insertFooter'));

			// Insert Google Analytics page tag to article footer
			HookRegistry::register('Templates::Article::Footer::PageFooter', array($this, 'insertFooter'));

			// Insert Google Analytics page tag to article interstitial footer
			HookRegistry::register('Templates::Article::Interstitial::PageFooter', array($this, 'insertFooter'));

			// Insert Google Analytics page tag to article pdf interstitial footer
			HookRegistry::register('Templates::Article::PdfInterstitial::PageFooter', array($this, 'insertFooter'));

			// Insert Google Analytics page tag to reading tools footer
			HookRegistry::register('Templates::Rt::Footer::PageFooter', array($this, 'insertFooter'));

			// Insert Google Analytics page tag to help footer
			HookRegistry::register('Templates::Help::Footer::PageFooter', array($this, 'insertFooter'));
		}
		return $success;
	}

	/**
	 * Get the name of this plugin. The name must be unique within
	 * its category, and should be suitable for part of a filename
	 * (ie short, no spaces, and no dependencies on cases being unique).
	 * @return String name of plugin
	 */
	function getName() {
		return 'GoogleAnalyticsPlugin';
	}

	function getDisplayName() {
		return Locale::translate('plugins.generic.googleAnalytics.displayName');
	}

	function getDescription() {
		return Locale::translate('plugins.generic.googleAnalytics.description');
	}

	/**
	 * Extend the {url ...} smarty to support this plugin.
	 */
	function smartyPluginUrl($params, &$smarty) {
		$path = array($this->getCategory(), $this->getName());
		if (is_array($params['path'])) {
			$params['path'] = array_merge($path, $params['path']);
		} elseif (!empty($params['path'])) {
			$params['path'] = array_merge($path, array($params['path']));
		} else {
			$params['path'] = $path;
		}

		if (!empty($params['id'])) {
			$params['path'] = array_merge($params['path'], array($params['id']));
			unset($params['id']);
		}
		return $smarty->smartyUrl($params, $smarty);
	}

	/**
	 * Set the page's breadcrumbs, given the plugin's tree of items
	 * to append.
	 * @param $subclass boolean
	 */
	function setBreadcrumbs($isSubclass = false) {
		$templateMgr = &TemplateManager::getManager();
		$pageCrumbs = array(
			array(
				Request::url(null, 'user'),
				'navigation.user'
			),
			array(
				Request::url(null, 'manager'),
				'user.role.manager'
			)
		);
		if ($isSubclass) $pageCrumbs[] = array(
			Request::url(null, 'manager', 'plugins'),
			'manager.plugins'
		);

		$templateMgr->assign('pageHierarchy', $pageCrumbs);
	}

	/**
	 * Display verbs for the management interface.
	 */
	function getManagementVerbs() {
		$verbs = array();
		if ($this->getEnabled()) {
			$verbs[] = array(
				'disable',
				Locale::translate('manager.plugins.disable')
			);
			$verbs[] = array(
				'settings',
				Locale::translate('plugins.generic.googleAnalytics.manager.settings')
			);
		} else {
			$verbs[] = array(
				'enable',
				Locale::translate('manager.plugins.enable')
			);
		}
		return $verbs;
	}

	/**
	 * Determine whether or not this plugin is enabled.
	 */
	function getEnabled() {
		$journal = &Request::getJournal();
		if (!$journal) return false;
		return $this->getSetting($journal->getJournalId(), 'enabled');
	}

	/**
	 * Set the enabled/disabled state of this plugin
	 */
	function setEnabled($enabled) {
		$journal = &Request::getJournal();
		if ($journal) {
			$this->updateSetting($journal->getJournalId(), 'enabled', $enabled ? true : false);
			return true;
		}
		return false;
	}

	/**
	 * Insert Google Analytics page tag to footer
	 */  
	function insertFooter($hookName, $params) {
		if ($this->getEnabled()) {
			$smarty = &$params[1];
			$output = &$params[2];
			$templateMgr = &TemplateManager::getManager();
			$currentJournal = $templateMgr->get_template_vars('currentJournal');

			if (!empty($currentJournal)) {
				$journal = &Request::getJournal();
				$journalId = $journal->getJournalId();
				$googleAnalyticsSiteId = $this->getSetting($journalId, 'googleAnalyticsSiteId');

				if (!empty($googleAnalyticsSiteId)) {
					$templateMgr->assign('googleAnalyticsSiteId', $googleAnalyticsSiteId);
178
179
180
181
182
183
					$trackingCode = $this->getSetting($journalId, 'trackingCode');
					if ($trackingCode == "ga") {
						$output .= $templateMgr->fetch($this->getTemplatePath() . 'pageTagGa.tpl'); 
					} else {
						$output .= $templateMgr->fetch($this->getTemplatePath() . 'pageTagUrchin.tpl'); 
					}
michael's avatar
michael committed
184
185
186
187
188
189
				}
			}
		}
		return false;
	}

190
191
192
193
194
195
196
197
 	/*
 	 * Execute a management verb on this plugin
 	 * @param $verb string
 	 * @param $args array
	 * @param $message string Location for the plugin to put a result msg
 	 * @return boolean
 	 */
	function manage($verb, $args, &$message) {
michael's avatar
michael committed
198
199
200
201
202
203
204
205
		$templateMgr = &TemplateManager::getManager();
		$templateMgr->register_function('plugin_url', array(&$this, 'smartyPluginUrl'));
		$journal = &Request::getJournal();
		$returner = true;

		switch ($verb) {
			case 'enable':
				$this->setEnabled(true);
206
				$message = Locale::translate('plugins.generic.googleAnalytics.enabled');
michael's avatar
michael committed
207
208
209
210
				$returner = false;
				break;
			case 'disable':
				$this->setEnabled(false);
211
				$message = Locale::translate('plugins.generic.googleAnalytics.disabled');
michael's avatar
michael committed
212
213
214
215
216
				$returner = false;
				break;
			case 'settings':
				if ($this->getEnabled()) {
					$this->import('GoogleAnalyticsSettingsForm');
217
218
					// FIXME: Need construction by reference or validation always fails on PHP 4.x
					$form =& new GoogleAnalyticsSettingsForm($this, $journal->getJournalId());
michael's avatar
michael committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
					if (Request::getUserVar('save')) {
						$form->readInputData();
						if ($form->validate()) {
							$form->execute();
							Request::redirect(null, 'manager', 'plugin');
						} else {
							$this->setBreadCrumbs(true);
							$form->display();
						}
					} else {
						$this->setBreadCrumbs(true);
						$form->initData();
						$form->display();
					}
				} else {
					Request::redirect(null, 'manager');
				}
				break;
			default:
				Request::redirect(null, 'manager');
		}
		return $returner;
	}
}
?>