ReferralPlugin.inc.php 5.33 KB
Newer Older
asmecher's avatar
asmecher committed
1
2
3
4
5
<?php

/**
 * @file plugins/generic/referral/ReferralPlugin.inc.php
 *
asmecher's avatar
asmecher committed
6
 * Copyright (c) 2003-2010 John Willinsky
asmecher's avatar
asmecher committed
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
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class ReferralPlugin
 * @ingroup plugins_generic_referral
 *
 * @brief Referral plugin to track and maintain potential references to published articles
 */

// $Id$


import('classes.plugins.GenericPlugin');

class ReferralPlugin extends GenericPlugin {
	/**
	 * Register the plugin, if enabled; note that this plugin
	 * runs under both Journal and Site contexts.
	 * @param $category string
	 * @param $path string
	 * @return boolean
	 */
	function register($category, $path) {
		if (parent::register($category, $path)) {
			if ($this->getEnabled()) {
				HookRegistry::register ('TemplateManager::display', array(&$this, 'handleTemplateDisplay'));
				HookRegistry::register ('LoadHandler', array(&$this, 'handleLoadHandler'));
				$this->import('Referral');
				$this->import('ReferralDAO');
				$referralDao = new ReferralDAO();
				DAORegistry::registerDAO('ReferralDAO', $referralDao);
			}
			return true;
		}
		return false;
	}

	function handleLoadHandler($hookName, $args) {
		$page =& $args[0];
		$op =& $args[1];
		$sourceFile =& $args[2];

		if ($page === 'referral') {
			$this->import('ReferralHandler');
			Registry::set('plugin', $this);
			define('HANDLER_CLASS', 'ReferralHandler');
			return true;
		}

		return false;
	}

58
	function handleAuthorTemplateInclude($hookName, $args) {
asmecher's avatar
asmecher committed
59
60
61
62
63
64
65
		$templateMgr =& $args[0];
		$params =& $args[1];
		if (!isset($params['smarty_include_tpl_file'])) return false;
		switch ($params['smarty_include_tpl_file']) {
			case 'common/footer.tpl':
				$referralDao =& DAORegistry::getDAO('ReferralDAO');
				$user =& Request::getUser();
66
				$rangeInfo =& Handler::getRangeInfo('referrals');
asmecher's avatar
asmecher committed
67
68
69
				$referralFilter = (int) Request::getUserVar('referralFilter');
				if ($referralFilter == 0) $referralFilter = null;

70
				$referrals =& $referralDao->getReferralsByUserId($user->getId(), $referralFilter, $rangeInfo);
71

asmecher's avatar
asmecher committed
72
73
				$templateMgr->assign('referrals', $referrals);
				$templateMgr->assign('referralFilter', $referralFilter);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
				$templateMgr->display($this->getTemplatePath() . 'authorReferrals.tpl', 'text/html', 'ReferralPlugin::addAuthorReferralContent');
				break;
		}
		return false;
	}

	function handleReaderTemplateInclude($hookName, $args) {
		$templateMgr =& $args[0];
		$params =& $args[1];
		if (!isset($params['smarty_include_tpl_file'])) return false;
		switch ($params['smarty_include_tpl_file']) {
			case 'article/comments.tpl':
				$referralDao =& DAORegistry::getDAO('ReferralDAO');
				$article = $templateMgr->get_template_vars('article');
88
				$referrals =& $referralDao->getPublishedReferralsForArticle($article->getId());
89

90
91
				$templateMgr->assign('referrals', $referrals);
				$templateMgr->display($this->getTemplatePath() . 'readerReferrals.tpl', 'text/html', 'ReferralPlugin::addReaderReferralContent');
asmecher's avatar
asmecher committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
				break;
		}
		return false;
	}

	/**
	 * Hook callback: Handle requests.
	 */
	function handleTemplateDisplay($hookName, $args) {
		$templateMgr =& $args[0];
		$template =& $args[1];

		switch ($template) {
			case 'article/article.tpl':
106
				HookRegistry::register ('TemplateManager::include', array(&$this, 'handleReaderTemplateInclude'));
asmecher's avatar
asmecher committed
107
108
			case 'article/interstitial.tpl':
			case 'article/pdfInterstitial.tpl':
asmecher's avatar
asmecher committed
109
			case 'article/view.tpl':
asmecher's avatar
asmecher committed
110
111
112
113
114
115
				$this->logArticleRequest($templateMgr);
				break;
			case 'author/index.tpl':
				// Slightly convoluted: register a hook to
				// display the administration options at the
				// end of the normal content
116
				HookRegistry::register ('TemplateManager::include', array(&$this, 'handleAuthorTemplateInclude'));
asmecher's avatar
asmecher committed
117
118
119
120
121
122
123
124
				break;
		}
		return false;
	}

	function logArticleRequest(&$templateMgr) {
		$article = $templateMgr->get_template_vars('article');
		if (!$article) return false;
125
		$articleId = $article->getId();
asmecher's avatar
asmecher committed
126

127
		$referrer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null;
asmecher's avatar
asmecher committed
128
129

		// Check if referrer is empty or is the local journal
130
		if (empty($referrer) || strpos($referrer, Request::getIndexUrl()) !== false) return false;
asmecher's avatar
asmecher committed
131
132
133
134

		$referralDao =& DAORegistry::getDAO('ReferralDAO');
		if ($referralDao->referralExistsByUrl($articleId, $referrer)) {
			// It exists -- increment the count
135
			$referralDao->incrementReferralCount($article->getId(), $referrer);
asmecher's avatar
asmecher committed
136
137
138
		} else {
			// It's a new referral -- log it.
			$referral = new Referral();
139
			$referral->setArticleId($article->getId());
asmecher's avatar
asmecher committed
140
141
142
143
144
145
146
147
148
149
150
151
152
			$referral->setLinkCount(1);
			$referral->setUrl($referrer);
			$referral->setStatus(REFERRAL_STATUS_NEW);
			$referral->setDateAdded(Core::getCurrentDate());
			$referralDao->insertReferral($referral);
		}
	}

	/**
	 * Get the name of the settings file to be installed on new journal
	 * creation.
	 * @return string
	 */
153
	function getContextSpecificPluginSettingsFile() {
asmecher's avatar
asmecher committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		return $this->getPluginPath() . '/settings.xml';
	}

	/**
	 * Get the display name of this plugin
	 * @return string
	 */
	function getDisplayName() {
		return Locale::translate('plugins.generic.referral.name');
	}

	/**
	 * Get the description of this plugin
	 * @return string
	 */
	function getDescription() {
		return Locale::translate('plugins.generic.referral.description');
	}

173
174
175
176
177
178
	/**
	 * Get the filename of the ADODB schema for this plugin.
	 */
	function getInstallSchemaFile() {
		return $this->getPluginPath() . '/' . 'schema.xml';
	}
asmecher's avatar
asmecher committed
179
180
181
}

?>