00001 <?php
00002
00020
00021
00022
00023
00024 define('SMARTY_DIR', Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'smarty' . DIRECTORY_SEPARATOR);
00025
00026 require_once('smarty/Smarty.class.php');
00027 require_once('smarty/plugins/modifier.escape.php');
00028
00029 import('search.ArticleSearch');
00030
00031 define('CACHEABILITY_NO_CACHE', 'no-cache');
00032 define('CACHEABILITY_NO_STORE', 'no-store');
00033 define('CACHEABILITY_PUBLIC', 'public');
00034 define('CACHEABILITY_MUST_REVALIDATE', 'must-revalidate');
00035 define('CACHEABILITY_PROXY_REVALIDATE', 'proxy-revalidate');
00036
00037 class TemplateManager extends Smarty {
00039 var $styleSheets;
00040
00043 var $initialized;
00044
00046 var $cacheability;
00047
00052 function TemplateManager() {
00053 parent::Smarty();
00054
00055 import('file.PublicFileManager');
00056 import('cache.CacheManager');
00057
00058
00059 $baseDir = Core::getBaseDir();
00060 $cachePath = CacheManager::getFileCachePath();
00061 $this->template_dir = $baseDir . DIRECTORY_SEPARATOR . 'templates';
00062 $this->compile_dir = $cachePath . DIRECTORY_SEPARATOR . 't_compile';
00063 $this->config_dir = $cachePath . DIRECTORY_SEPARATOR . 't_config';
00064 $this->cache_dir = $cachePath . DIRECTORY_SEPARATOR . 't_cache';
00065
00066
00067 $this->styleSheets = array();
00068 $this->assign_by_ref('stylesheets', $this->styleSheets);
00069 $this->cacheability = CACHEABILITY_NO_STORE;
00070
00071 $this->assign('defaultCharset', Config::getVar('i18n', 'client_charset'));
00072 $this->assign('baseUrl', Request::getBaseUrl());
00073 $this->assign('pageTitle', 'common.openJournalSystems');
00074 $this->assign('requestedPage', Request::getRequestedPage());
00075 $this->assign('currentUrl', Request::getCompleteUrl());
00076 $this->assign('dateFormatTrunc', Config::getVar('general', 'date_format_trunc'));
00077 $this->assign('dateFormatShort', Config::getVar('general', 'date_format_short'));
00078 $this->assign('dateFormatLong', Config::getVar('general', 'date_format_long'));
00079 $this->assign('datetimeFormatShort', Config::getVar('general', 'datetime_format_short'));
00080 $this->assign('datetimeFormatLong', Config::getVar('general', 'datetime_format_long'));
00081
00082
00083 $this->assign('implicitAuth', Config::getVar('security', 'implicit_auth'));
00084
00085 $locale = Locale::getLocale();
00086 $this->assign('currentLocale', $locale);
00087
00088 if (!defined('SESSION_DISABLE_INIT')) {
00089
00090
00091 $this->assign('isUserLoggedIn', Validation::isLoggedIn());
00092
00093 $journal = &Request::getJournal();
00094 $site = &Request::getSite();
00095
00096 $versionDAO = &DAORegistry::getDAO('VersionDAO');
00097 $currentVersion = $versionDAO->getCurrentVersion();
00098 $this->assign('currentVersionString', $currentVersion->getVersionString());
00099
00100 $siteFilesDir = Request::getBaseUrl() . '/' . PublicFileManager::getSiteFilesPath();
00101 $this->assign('sitePublicFilesDir', $siteFilesDir);
00102 $this->assign('publicFilesDir', $siteFilesDir);
00103
00104 $siteStyleFilename = PublicFileManager::getSiteFilesPath() . '/' . $site->getSiteStyleFilename();
00105 if (file_exists($siteStyleFilename)) $this->addStyleSheet(Request::getBaseUrl() . '/' . $siteStyleFilename);
00106
00107 if (isset($journal)) {
00108 $this->assign_by_ref('currentJournal', $journal);
00109 $journalTitle = $journal->getJournalTitle();
00110 $this->assign('siteTitle', $journalTitle);
00111 $this->assign('publicFilesDir', Request::getBaseUrl() . '/' . PublicFileManager::getJournalFilesPath($journal->getJournalId()));
00112
00113 $this->assign('primaryLocale', $journal->getPrimaryLocale());
00114 $this->assign('alternateLocales', $journal->getSetting('alternateLocales'));
00115
00116
00117 $navMenuItems = &$journal->getLocalizedSetting('navItems');
00118 $this->assign_by_ref('navMenuItems', $navMenuItems);
00119
00120
00121 $this->assign('displayPageHeaderTitle', $journal->getJournalPageHeaderTitle());
00122 $this->assign('displayPageHeaderLogo', $journal->getJournalPageHeaderLogo());
00123 $this->assign('alternatePageHeader', $journal->getLocalizedSetting('journalPageHeader'));
00124 $this->assign('metaSearchDescription', $journal->getLocalizedSetting('searchDescription'));
00125 $this->assign('metaSearchKeywords', $journal->getLocalizedSetting('searchKeywords'));
00126 $this->assign('metaCustomHeaders', $journal->getLocalizedSetting('customHeaders'));
00127 $this->assign('numPageLinks', $journal->getSetting('numPageLinks'));
00128 $this->assign('itemsPerPage', $journal->getSetting('itemsPerPage'));
00129 $this->assign('enableAnnouncements', $journal->getSetting('enableAnnouncements'));
00130
00131
00132 $themePluginPath = $journal->getSetting('journalTheme');
00133 if (!empty($themePluginPath)) {
00134
00135 $themePlugin =& PluginRegistry::loadPlugin('themes', $themePluginPath);
00136 if ($themePlugin) $themePlugin->activate($this);
00137 }
00138
00139
00140 $journalStyleSheet = $journal->getSetting('journalStyleSheet');
00141 if ($journalStyleSheet) {
00142 $this->addStyleSheet(Request::getBaseUrl() . '/' . PublicFileManager::getJournalFilesPath($journal->getJournalId()) . '/' . $journalStyleSheet['uploadName']);
00143 }
00144
00145 import('payment.ojs.OJSPaymentManager');
00146 $paymentManager =& OJSPaymentManager::getManager();
00147 $this->assign('journalPaymentsEnabled', $paymentManager->isConfigured());
00148
00149 $this->assign('pageFooter', $journal->getLocalizedSetting('journalPageFooter'));
00150 } else {
00151
00152 $this->assign('displayPageHeaderTitle', $site->getSitePageHeaderTitle());
00153
00154 $this->assign('siteTitle', $site->getSiteTitle());
00155 $this->assign('itemsPerPage', Config::getVar('interface', 'items_per_page'));
00156 $this->assign('numPageLinks', Config::getVar('interface', 'page_links'));
00157 }
00158
00159 if (!$site->getJournalRedirect()) {
00160 $this->assign('hasOtherJournals', true);
00161 }
00162 }
00163
00164
00165 if (($localeStyleSheet = Locale::getLocaleStyleSheet($locale)) != null) $this->addStyleSheet(Request::getBaseUrl() . '/' . $localeStyleSheet);
00166
00167
00168 $this->register_modifier('translate', array('Locale', 'translate'));
00169 $this->register_modifier('strip_unsafe_html', array('String', 'stripUnsafeHtml'));
00170 $this->register_modifier('String_substr', array('String', 'substr'));
00171 $this->register_modifier('to_array', array(&$this, 'smartyToArray'));
00172 $this->register_modifier('escape', array(&$this, 'smartyEscape'));
00173 $this->register_modifier('explode', array(&$this, 'smartyExplode'));
00174 $this->register_modifier('assign', array(&$this, 'smartyAssign'));
00175 $this->register_function('translate', array(&$this, 'smartyTranslate'));
00176 $this->register_function('flush', array(&$this, 'smartyFlush'));
00177 $this->register_function('call_hook', array(&$this, 'smartyCallHook'));
00178 $this->register_function('html_options_translate', array(&$this, 'smartyHtmlOptionsTranslate'));
00179 $this->register_block('iterate', array(&$this, 'smartyIterate'));
00180 $this->register_function('call_progress_function', array(&$this, 'smartyCallProgressFunction'));
00181 $this->register_function('page_links', array(&$this, 'smartyPageLinks'));
00182 $this->register_function('page_info', array(&$this, 'smartyPageInfo'));
00183 $this->register_function('get_help_id', array(&$this, 'smartyGetHelpId'));
00184 $this->register_function('icon', array(&$this, 'smartyIcon'));
00185 $this->register_function('help_topic', array(&$this, 'smartyHelpTopic'));
00186 $this->register_function('get_debug_info', array(&$this, 'smartyGetDebugInfo'));
00187 $this->register_function('assign_mailto', array(&$this, 'smartyAssignMailto'));
00188 $this->register_function('display_template', array(&$this, 'smartyDisplayTemplate'));
00189
00190 $this->register_function('url', array(&$this, 'smartyUrl'));
00191
00192 $this->initialized = false;
00193 }
00194
00199 function setCacheability($cacheability = CACHEABILITY_PUBLIC) {
00200 $this->cacheability = $cacheability;
00201 }
00202
00203 function initialize() {
00204
00205
00206
00207
00208 $plugins =& PluginRegistry::loadCategory('blocks');
00209
00210 $this->initialized = true;
00211 }
00212
00213 function addStyleSheet($url) {
00214 array_push($this->styleSheets, $url);
00215 }
00216
00220 function display($template, $sendContentType = 'text/html', $hookName = 'TemplateManager::display') {
00221 if (!$this->initialized) {
00222 $this->initialize();
00223 }
00224
00225 $charset = Config::getVar('i18n', 'client_charset');
00226
00227
00228
00229
00230
00231 $output = null;
00232 if (!HookRegistry::call($hookName, array(&$this, &$template, &$sendContentType, &$charset, &$output))) {
00233
00234 if ($hookName == 'TemplateManager::display') {
00235
00236
00237
00238
00239
00240 header('Content-Type: ' . $sendContentType . '; charset=' . $charset);
00241
00242
00243 header('Cache-Control: ' . $this->cacheability);
00244 }
00245
00246
00247 parent::display($template);
00248 } else {
00249
00250 echo $output;
00251 }
00252 }
00253
00259 function smartyDisplayTemplate($params, &$smarty) {
00260 $templateMgr =& TemplateManager::getManager();
00261
00262 if (isset($params['template'])) {
00263 $templateMgr->display($params['template'], "", $params['hookname']);
00264 }
00265 }
00266
00267
00271 function clearTemplateCache() {
00272 $this->clear_compiled_tpl();
00273 $this->clear_all_cache();
00274 }
00275
00280 function &getManager() {
00281 static $instance;
00282
00283 if (!isset($instance)) {
00284 $instance = new TemplateManager();
00285 }
00286 return $instance;
00287 }
00288
00289
00290
00291
00292
00304 function smartyTranslate($params, &$smarty) {
00305 if (isset($params) && !empty($params)) {
00306 if (!isset($params['key'])) return Locale::translate('');
00307
00308 $key = $params['key'];
00309 unset($params['key']);
00310 if (isset($params['params'])) {
00311 $paramsArray = $params['params'];
00312 unset($params['params']);
00313 $params = array_merge($params, $paramsArray);
00314 }
00315 return Locale::translate($key, $params);
00316 }
00317 }
00318
00324 function smartyAssignMailto($params, &$smarty) {
00325 if (isset($params['var']) && isset($params['address'])) {
00326
00327
00328 $address = $params['address'];
00329 $address_encode = '';
00330 for ($x=0; $x < strlen($address); $x++) {
00331 if(preg_match('!\w!',$address[$x])) {
00332 $address_encode .= '%' . bin2hex($address[$x]);
00333 } else {
00334 $address_encode .= $address[$x];
00335 }
00336 }
00337 $text_encode = '';
00338 for ($x=0; $x < strlen($text); $x++) {
00339 $text_encode .= '&#x' . bin2hex($text[$x]).';';
00340 }
00341
00342 $mailto = "mailto:";
00343 $smarty->assign($params['var'], $mailto . $address_encode);
00344 }
00345 }
00346
00355 function smartyHtmlOptionsTranslate($params, &$smarty) {
00356 if (isset($params['options'])) {
00357 if (isset($params['translateValues'])) {
00358
00359 $newOptions = array();
00360 foreach ($params['options'] as $k => $v) {
00361 $newOptions[Locale::translate($k)] = Locale::translate($v);
00362 }
00363 $params['options'] = $newOptions;
00364 } else {
00365
00366 $params['options'] = array_map(array('Locale', 'translate'), $params['options']);
00367 }
00368 }
00369
00370 if (isset($params['output'])) {
00371 $params['output'] = array_map(array('Locale', 'translate'), $params['output']);
00372 }
00373
00374 if (isset($params['values']) && isset($params['translateValues'])) {
00375 $params['values'] = array_map(array('Locale', 'translate'), $params['values']);
00376 }
00377
00378 require_once($this->_get_plugin_filepath('function','html_options'));
00379 return smarty_function_html_options($params, $smarty);
00380 }
00381
00390 function smartyIterate($params, $content, &$smarty, &$repeat) {
00391 $iterator = &$smarty->get_template_vars($params['from']);
00392
00393 if (isset($params['key'])) {
00394 if (empty($content)) $smarty->assign($params['key'], 1);
00395 else $smarty->assign($params['key'], $smarty->get_template_vars($params['key'])+1);
00396 }
00397
00398
00399 if (!$iterator || $iterator->eof()) {
00400 if (!$repeat) return $content;
00401 $repeat = false;
00402 return '';
00403 }
00404
00405 $repeat = true;
00406
00407 if (isset($params['key'])) {
00408 list($key, $value) = $iterator->nextWithKey();
00409 $smarty->assign_by_ref($params['item'], $value);
00410 $smarty->assign_by_ref($params['key'], $key);
00411 } else {
00412 $smarty->assign_by_ref($params['item'], $iterator->next());
00413 }
00414 return $content;
00415 }
00416
00426 function smartyGetHelpId($params, &$smarty) {
00427 $help =& Help::getHelp();
00428 if (isset($params) && !empty($params)) {
00429 if (isset($params['key'])) {
00430 $key = $params['key'];
00431 unset($params['key']);
00432 $translatedKey = $help->translate($key);
00433 } else {
00434 $translatedKey = $help->translate('');
00435 }
00436
00437 if ($params['url'] == "true") {
00438 return Request::url(null, 'help', 'view', explode('/', $translatedKey));
00439 } else {
00440 return $translatedKey;
00441 }
00442 }
00443 }
00444
00453 function smartyHelpTopic($params, &$smarty) {
00454 $help =& Help::getHelp();
00455 if (isset($params) && !empty($params)) {
00456 $translatedKey = isset($params['key']) ? $help->translate($params['key']) : $help->translate('');
00457 $link = Request::url(null, 'help', 'view', explode('/', $translatedKey));
00458 $text = isset($params['text']) ? $params['text'] : '';
00459 return "<a href=\"$link\">$text</a>";
00460 }
00461 }
00462
00470 function smartyIcon($params, &$smarty) {
00471 if (isset($params) && !empty($params)) {
00472 $iconHtml = '';
00473 if (isset($params['name'])) {
00474
00475 $disabled = (isset($params['disabled']) && !empty($params['disabled']));
00476 if (!isset($params['path'])) $params['path'] = 'templates/images/icons/';
00477 $iconHtml = '<img src="' . $smarty->get_template_vars('baseUrl') . '/' . $params['path'];
00478 $iconHtml .= $params['name'] . ($disabled ? '_disabled' : '') . '.gif" alt="';
00479
00480
00481 if (isset($params['alt'])) {
00482 $iconHtml .= $params['alt'];
00483 } else {
00484 $iconHtml .= Locale::translate('icon.'.$params['name'].'.alt');
00485 }
00486 $iconHtml .= '" ';
00487
00488
00489 if (isset($params['onclick'])) {
00490 $iconHtml .= 'onclick="' . $params['onclick'] . '" ';
00491 }
00492
00493
00494 $iconHtml .= '/>';
00495
00496
00497 if (!$disabled && isset($params['url'])) {
00498 $iconHtml = '<a href="' . $params['url'] . '" class="icon">' . $iconHtml . '</a>';
00499 }
00500 }
00501 return $iconHtml;
00502 }
00503 }
00504
00511 function smartyPageInfo($params, &$smarty) {
00512 $iterator = $params['iterator'];
00513
00514 $itemsPerPage = $smarty->get_template_vars('itemsPerPage');
00515 if (!is_numeric($itemsPerPage)) $itemsPerPage=25;
00516
00517 $page = $iterator->getPage();
00518 $pageCount = $iterator->getPageCount();
00519 $itemTotal = $iterator->getCount();
00520
00521 if ($pageCount<1) return '';
00522
00523 return Locale::translate('navigation.items', array(
00524 'from' => (($page - 1) * $itemsPerPage) + 1,
00525 'to' => min($itemTotal, $page * $itemsPerPage),
00526 'total' => $itemTotal
00527 ));
00528 }
00529
00535 function smartyFlush($params, &$smarty) {
00536 $smarty->flush();
00537 }
00538
00539 function flush() {
00540 while (ob_get_level()) {
00541 ob_end_flush();
00542 }
00543 flush();
00544 }
00545
00549 function smartyCallHook($params, &$smarty) {
00550 $output = null;
00551 HookRegistry::call($params['name'], array(&$params, &$smarty, &$output));
00552 return $output;
00553 }
00554
00558 function smartyGetDebugInfo($params, &$smarty) {
00559 if (Config::getVar('debug', 'show_stats')) {
00560 $smarty->assign('enableDebugStats', true);
00561 $smarty->assign('debugExecutionTime', Core::microtime() - Registry::get('system.debug.startTime'));
00562 $dbconn = &DBConnection::getInstance();
00563 $smarty->assign('debugNumDatabaseQueries', $dbconn->getNumQueries());
00564 $smarty->assign_by_ref('debugNotes', Registry::get('system.debug.notes'));
00565 }
00566
00567 }
00568
00572 function smartyUrl($params, &$smarty) {
00573
00574
00575
00576 $paramList = array('journal', 'page', 'op', 'path', 'anchor', 'escape');
00577 foreach ($paramList as $param) {
00578 if (isset($params[$param])) {
00579 $$param = $params[$param];
00580 unset($params[$param]);
00581 } else {
00582 $$param = null;
00583 }
00584 }
00585
00586 return Request::url($journal, $page, $op, $path, $params, $anchor, !isset($escape) || $escape);
00587 }
00588
00589 function setProgressFunction($progressFunction) {
00590 Registry::set('progressFunctionCallback', $progressFunction);
00591 }
00592
00593 function smartyCallProgressFunction($params, &$smarty) {
00594 $progressFunctionCallback =& Registry::get('progressFunctionCallback');
00595 if ($progressFunctionCallback) {
00596 call_user_func($progressFunctionCallback);
00597 }
00598 }
00599
00600 function updateProgressBar($progress, $total) {
00601 static $lastPercent;
00602 $percent = round($progress * 100 / $total);
00603 if (!isset($lastPercent) || $lastPercent != $percent) {
00604 for($i=1; $i <= $percent-$lastPercent; $i++) {
00605 echo '<img src="' . Request::getBaseUrl() . '/templates/images/progbar.gif" width="5" height="15">';
00606 }
00607 }
00608 $lastPercent = $percent;
00609
00610 $templateMgr =& TemplateManager::getManager();
00611 $templateMgr->flush();
00612 }
00613
00624 function smartyPageLinks($params, &$smarty) {
00625 $iterator = $params['iterator'];
00626 $name = $params['name'];
00627 if (isset($params['params']) && is_array($params['params'])) {
00628 $extraParams = $params['params'];
00629 unset($params['params']);
00630 $params = array_merge($params, $extraParams);
00631 }
00632 if (isset($params['anchor'])) {
00633 $anchor = $params['anchor'];
00634 unset($params['anchor']);
00635 } else {
00636 $anchor = null;
00637 }
00638 if (isset($params['all_extra'])) {
00639 $allExtra = ' ' . $params['all_extra'];
00640 unset($params['all_extra']);
00641 } else {
00642 $allExtra = '';
00643 }
00644
00645 unset($params['iterator']);
00646 unset($params['name']);
00647
00648 $numPageLinks = $smarty->get_template_vars('numPageLinks');
00649 if (!is_numeric($numPageLinks)) $numPageLinks=10;
00650
00651 $page = $iterator->getPage();
00652 $pageCount = $iterator->getPageCount();
00653 $itemTotal = $iterator->getCount();
00654
00655 $pageBase = max($page - floor($numPageLinks / 2), 1);
00656 $paramName = $name . 'Page';
00657
00658 if ($pageCount<=1) return '';
00659
00660 $value = '';
00661
00662 if ($page>1) {
00663 $params[$paramName] = 1;
00664 $value .= '<a href="' . Request::url(null, null, null, Request::getRequestedArgs(), $params, $anchor) . '"' . $allExtra . '><<</a> ';
00665 $params[$paramName] = $page - 1;
00666 $value .= '<a href="' . Request::url(null, null, null, Request::getRequestedArgs(), $params, $anchor) . '"' . $allExtra . '><</a> ';
00667 }
00668
00669 for ($i=$pageBase; $i<min($pageBase+$numPageLinks, $pageCount+1); $i++) {
00670 if ($i == $page) {
00671 $value .= "<strong>$i</strong> ";
00672 } else {
00673 $params[$paramName] = $i;
00674 $value .= '<a href="' . Request::url(null, null, null, Request::getRequestedArgs(), $params, $anchor) . '"' . $allExtra . '>' . $i . '</a> ';
00675 }
00676 }
00677 if ($page < $pageCount) {
00678 $params[$paramName] = $page + 1;
00679 $value .= '<a href="' . Request::url(null, null, null, Request::getRequestedArgs(), $params, $anchor) . '"' . $allExtra . '>></a> ';
00680 $params[$paramName] = $pageCount;
00681 $value .= '<a href="' . Request::url(null, null, null, Request::getRequestedArgs(), $params, $anchor) . '"' . $allExtra . '>>></a> ';
00682 }
00683
00684 return $value;
00685 }
00686
00690 function smartyToArray() {
00691 return func_get_args();
00692 }
00693
00698 function smartyEscape($string, $esc_type = 'html', $char_set = null) {
00699 if ($char_set === null) $char_set = LOCALE_ENCODING;
00700 switch ($esc_type) {
00701 case 'jsparam':
00702
00703
00704
00705
00706
00707 $value = smarty_modifier_escape($string, 'html', $char_set);
00708 return str_replace(''', '\\\'', $value);
00709 default:
00710 return smarty_modifier_escape($string, $esc_type, $char_set);
00711 }
00712 }
00713
00717 function smartyExplode($string, $separator) {
00718 return explode($separator, $string);
00719 }
00720
00724 function smartyAssign($value, $varName, $passThru = false) {
00725 if (isset($varName)) {
00726
00727
00728
00729 $templateMgr =& TemplateManager::getManager();
00730 $templateMgr->assign($varName, $value);
00731 }
00732 if ($passThru) return $value;
00733 }
00734 }
00735
00736 ?>