plugins/generic/jquery/JQueryPlugin.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00015 // $Id$
00016 
00017 
00018 import('classes.plugins.GenericPlugin');
00019 
00020 define('JQUERY_INSTALL_PATH', 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'jquery');
00021 define('JQUERY_JS_PATH', JQUERY_INSTALL_PATH . DIRECTORY_SEPARATOR . 'jquery.min.js');
00022 
00023 class JQueryPlugin extends GenericPlugin {
00031    function register($category, $path) {
00032       if (parent::register($category, $path)) {
00033          $this->addLocaleData();
00034          if ($this->isJQueryInstalled()) {
00035             HookRegistry::register('TemplateManager::display', array(&$this, 'displayCallback'));
00036             $user =& Request::getUser();
00037             if ($user) HookRegistry::register('Templates::Common::Footer::PageFooter', array(&$this, 'footerCallback'));
00038             $templateMgr =& TemplateManager::getManager();
00039             $templateMgr->addStyleSheet(Request::getBaseUrl() . '/lib/pkp/styles/jqueryUi.css');
00040             $templateMgr->addStyleSheet(Request::getBaseUrl() . '/lib/pkp/styles/jquery.pnotify.default.css');
00041             $templateMgr->addStyleSheet(Request::getBaseUrl() . '/lib/pkp/styles/themes/default/pnotify.css');
00042          }
00043          return true;
00044       }
00045       return false;
00046    }
00047 
00052    function getScriptPath() {
00053       return Request::getBaseUrl() . DIRECTORY_SEPARATOR . JQUERY_JS_PATH;
00054    }
00055 
00062    function getEnabledScripts($page, $op) {
00063       $scripts = array();
00064       switch ("$page/$op") {
00065          case 'admin/conferences':
00066          case 'manager/schedConfs':
00067          case 'manager/groupMembership':
00068          case 'manager/groups':
00069          case 'manager/reviewFormElements':
00070          case 'manager/reviewForms':
00071          case 'manager/sections':
00072          case 'manager/subscriptionTypes':
00073          case 'rtadmin/contexts':
00074          case 'rtadmin/searches':
00075          case 'registrationManager/registrationTypes':
00076             $scripts[] = 'plugins/generic/jquery/scripts/jquery.tablednd_0_5.js';
00077             $scripts[] = 'plugins/generic/jquery/scripts/tablednd.js';
00078             break;
00079       }
00080       $user =& Request::getUser();
00081       if ($user) $scripts[] = 'lib/pkp/js/jquery.pnotify.js';
00082       return $scripts;
00083    }
00084 
00091    function displayCallback($hookName, $args) {
00092       $page = Request::getRequestedPage();
00093       $op = Request::getRequestedOp();
00094       $scripts = JQueryPlugin::getEnabledScripts($page, $op);
00095       if(empty($scripts)) return null;
00096 
00097       $templateManager =& $args[0];
00098       $additionalHeadData = $templateManager->get_template_vars('additionalHeadData');
00099       $baseUrl = $templateManager->get_template_vars('baseUrl');
00100 
00101       $jQueryScript = JQueryPlugin::addScripts($baseUrl, $scripts);
00102 
00103       $templateManager->assign('additionalHeadData', $additionalHeadData."\n".$jQueryScript);
00104    }
00105 
00106    function jsEscape($string) {
00107       return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
00108    }
00109 
00113    function footerCallback($hookName, $args) {
00114       $output =& $args[2];
00115       $notificationDao =& DAORegistry::getDAO('NotificationDAO');
00116       $user =& Request::getUser();
00117       $notificationsMarkup = '';
00118 
00119       $notifications =& $notificationDao->getNotificationsByUserId($user->getId(), NOTIFICATION_LEVEL_TRIVIAL);
00120       while ($notification =& $notifications->next()) {
00121          $notificationTitle = $notification->getTitle();
00122          if ($notification->getIsLocalized() && !empty($notificationTitle)) $notificationTitle = __($notificationTitle);
00123          if (empty($notificationTitle)) $notificationTitle = __('notification.notification');
00124          $notificationsMarkup .= '$.pnotify({pnotify_title: \'' . $this->jsEscape($notificationTitle) . '\', pnotify_text: \'';
00125          if ($notification->getIsLocalized()) $notificationsMarkup .= $this->jsEscape(__($notification->getContents(), array('param' => $notification->getParam())));
00126          else $notificationsMarkup .= $this->jsEscape($notification->getContents());
00127          $notificationsMarkup .= '\', pnotify_addclass: \'' . $this->jsEscape($notification->getStyleClass());
00128          $notificationsMarkup .= '\', pnotify_notice_icon: \'notifyIcon ' . $this->jsEscape($notification->getIconClass());
00129          $notificationsMarkup .= '\'});';
00130          $notificationDao->deleteNotificationById($notification->getId());
00131          unset($notification);
00132       }
00133       if (!empty($notificationsMarkup)) $notificationsMarkup = "<script type=\"text/javascript\">$notificationsMarkup</script>\n";
00134 
00135       $output .= $notificationsMarkup;
00136       return false;
00137    }
00138 
00145    function addScripts($baseUrl, $scripts) {
00146       $scriptOpen = '   <script language="javascript" type="text/javascript" src="';
00147       $scriptClose = '"></script>';
00148       $returner = '';
00149 
00150       foreach ($scripts as $script) {
00151          if(file_exists(Core::getBaseDir() . DIRECTORY_SEPARATOR . $script)) {
00152             $returner .= $scriptOpen . $baseUrl . '/' . $script . $scriptClose . "\n";
00153          }
00154       }
00155       return $returner;
00156    }
00157 
00162    function getName() {
00163       return 'JQueryPlugin';
00164    }
00165 
00170    function getDisplayName() {
00171       return __('plugins.generic.jquery.name');
00172    }
00173 
00178    function getDescription() {
00179       if ($this->isJQueryInstalled()) return __('plugins.generic.jquery.description');
00180       return __('plugins.generic.jquery.descriptionDisabled', array('jQueryPath' => JQUERY_INSTALL_PATH));
00181    }
00182 
00187    function isJQueryInstalled() {
00188       // We may not register jQuery when the application is not yet installed
00189       // as access to the template manager (see register method) requires db access.
00190       return Config::getVar('general', 'installed') && file_exists(JQUERY_JS_PATH);
00191    }
00192 }
00193 
00194 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7