00001 <?php
00002
00015
00016
00017
00018 define('COUNTER_UID_VAR', 'CounterPlugin_UID');
00019 import('classes.plugins.GenericPlugin');
00020
00021 class CounterPlugin extends GenericPlugin {
00028 function register($category, $path) {
00029 $isEnabled = $this->getSetting(0, 'enabled');
00030 $success = parent::register($category, $path);
00031 if ($success && $isEnabled === true) {
00032
00033 HookRegistry::register ('Templates::Admin::Index::AdminFunctions', array(&$this, 'displayMenuOption'));
00034 HookRegistry::register ('Templates::Manager::Index::ManagementPages', array(&$this, 'displayMenuOption'));
00035 HookRegistry::register ('LoadHandler', array(&$this, 'handleRequest'));
00036 HookRegistry::register ('TemplateManager::display', array(&$this, 'logRequest'));
00037
00038 $this->import('LogEntryDAO');
00039 $logEntryDao =& new LogEntryDAO();
00040 DAORegistry::registerDAO('LogEntryDAO', $logEntryDao);
00041 }
00042 return $success;
00043 }
00044
00050 function getName() {
00051 return 'CounterPlugin';
00052 }
00053
00054 function getDisplayName() {
00055 $this->addLocaleData();
00056 return Locale::translate('plugins.generic.counter');
00057 }
00058
00059 function getDescription() {
00060 $this->addLocaleData();
00061 return Locale::translate('plugins.generic.counter.description');
00062 }
00063
00064 function displayMenuOption($hookName, $args) {
00065 if (!Validation::isSiteAdmin()) return false;
00066
00067 $params =& $args[0];
00068 $smarty =& $args[1];
00069 $output =& $args[2];
00070
00071 $this->addLocaleData();
00072 $output .= '<li>» <a href="' . Request::url(null, 'counter') . '">' . Locale::translate('plugins.generic.counter') . '</a></li>';
00073 return false;
00074 }
00075
00082 function logRequest($hookName, $args) {
00083 $templateManager =& $args[0];
00084 $template =& $args[1];
00085
00086 $site =& Request::getSite();
00087 $journal =& Request::getJournal();
00088 $session =& Request::getSession();
00089
00090 if (!$journal) return false;
00091
00092 if (($logUser = $session->getSessionVar(COUNTER_UID_VAR))=='') {
00093 $logUser = Core::getCurrentDate() . '_' . $session->getId();
00094 $session->setSessionVar(COUNTER_UID_VAR, $logUser);
00095 }
00096
00097 switch ($template) {
00098 case 'article/article.tpl':
00099 case 'article/interstitial.tpl':
00100 case 'article/pdfInterstitial.tpl':
00101
00102 $article = $templateManager->get_template_vars('article');
00103 $galley = $templateManager->get_template_vars('galley');
00104
00105
00106
00107 if (!$galley) return false;
00108
00109 $logEntry =& new LogEntry();
00110 $logEntry->setSite($site->getTitle($site->getPrimaryLocale()));
00111 $logEntry->setJournal($journal->getTitle($journal->getPrimaryLocale()));
00112 $logEntry->setJournalUrl(Request::url(null, 'index'));
00113 $logEntry->setPrintIssn($journal->getSetting('printIssn'));
00114 $logEntry->setOnlineIssn($journal->getSetting('onlineIssn'));
00115 $logEntry->setPublisher($journal->getLocalizedSetting('publisherInstitution'));
00116 $logEntry->setUser($logUser);
00117 if ($galley->isHTMLGalley()) $logEntry->setType(LOG_ENTRY_TYPE_HTML_ARTICLE);
00118 elseif ($galley->isPdfGalley()) $logEntry->setType(LOG_ENTRY_TYPE_PDF_ARTICLE);
00119 else $logEntry->setType(LOG_ENTRY_TYPE_OTHER_ARTICLE);
00120 $logEntry->setValue($article->getArticleTitle());
00121 $logEntryDao =& DAORegistry::getDAO('LogEntryDAO');
00122 $logEntryDao->addEntry($logEntry);
00123 break;
00124 case 'search/searchResults.tpl':
00125
00126 $logEntry =& new LogEntry();
00127 $article = $templateManager->get_template_vars('article');
00128 $logEntry->setSite($site->getTitle($site->getPrimaryLocale()));
00129 $logEntry->setJournal($journal->getTitle($journal->getPrimaryLocale()));
00130 $logEntry->setJournalUrl(Request::url(null, 'index'));
00131 $logEntry->setUser($logUser);
00132 $logEntry->setType(LOG_ENTRY_TYPE_SEARCH);
00133 $logEntry->setValue(Request::getUserVar('query'));
00134 $logEntryDao =& DAORegistry::getDAO('LogEntryDAO');
00135 $logEntryDao->addEntry($logEntry);
00136 break;
00137 }
00138
00139 return false;
00140 }
00141
00142 function handleRequest($hookName, $args) {
00143 $page =& $args[0];
00144 $op =& $args[1];
00145 $sourceFile =& $args[2];
00146
00147
00148 if ($page === 'counter') {
00149 $this->addLocaleData();
00150 $this->import('CounterHandler');
00151 Registry::set('plugin', $this);
00152 define('HANDLER_CLASS', 'CounterHandler');
00153 return true;
00154 }
00155
00156 return false;
00157 }
00158
00159 function isSitePlugin() {
00160 return true;
00161 }
00162
00163 function getManagementVerbs() {
00164 $this->addLocaleData();
00165 $isEnabled = $this->getSetting(0, 'enabled');
00166
00167 $verbs = array();
00168
00169
00170 if (!Validation::isSiteAdmin()) return $verbs;
00171
00172 if ($isEnabled) $verbs[] = array(
00173 'counter',
00174 Locale::translate('plugins.generic.counter')
00175 );
00176 $verbs[] = array(
00177 ($isEnabled?'disable':'enable'),
00178 Locale::translate($isEnabled?'manager.plugins.disable':'manager.plugins.enable')
00179 );
00180 return $verbs;
00181 }
00182
00183 function manage($verb, $args) {
00184
00185
00186 if (!Validation::isSiteAdmin()) return false;
00187
00188 $isEnabled = $this->getSetting(0, 'enabled');
00189 $this->addLocaleData();
00190 switch ($verb) {
00191 case 'enable':
00192 $this->updateSetting(0, 'enabled', true);
00193 break;
00194 case 'disable':
00195 $this->updateSetting(0, 'enabled', false);
00196 break;
00197 case 'counter':
00198 if ($isEnabled) Request::redirect(null, 'counter');
00199 }
00200 return false;
00201 }
00202 }
00203
00204 ?>