Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
Plugin.inc.php
1 <?php
2 
47 // Define the well-known file name for filter configuration data.
48 define('PLUGIN_FILTER_DATAFILE', 'filterConfig.xml');
49 
50 class Plugin {
52  var $pluginPath;
53 
56 
58  var $request;
59 
63  function Plugin() {
64  }
65 
66  /*
67  * Public Plugin API (Registration and Initialization)
68  */
84  function register($category, $path) {
85  $this->pluginPath = $path;
86  $this->pluginCategory = $category;
87  if ($this->getInstallSchemaFile()) {
88  HookRegistry::register ('Installer::postInstall', array($this, 'updateSchema'));
89  }
90  if ($this->getInstallSitePluginSettingsFile()) {
91  HookRegistry::register ('Installer::postInstall', array($this, 'installSiteSettings'));
92  }
94  HookRegistry::register ('Installer::postInstall', array($this, 'installEmailTemplates'));
95  }
96  if ($this->getInstallEmailTemplateDataFile()) {
97  HookRegistry::register ('Installer::postInstall', array($this, 'installEmailTemplateData'));
98  HookRegistry::register ('PKPLocale::installLocale', array($this, 'installLocale'));
99  }
100  if ($this->getInstallDataFile()) {
101  HookRegistry::register ('Installer::postInstall', array($this, 'installData'));
102  }
104  HookRegistry::register ($this->_getContextSpecificInstallationHook(), array($this, 'installContextSpecificSettings'));
105  }
106  HookRegistry::register ('Installer::postInstall', array($this, 'installFilters'));
107  return true;
108  }
109 
114  //
115  // Plugin Display
116  //
117 
125  function getName() {
126  assert(false);
127  }
128 
134  function getDisplayName() {
135  assert(false);
136  }
137 
143  function getDescription() {
144  assert(false);
145  }
146 
147  //
148  // Plugin Behavior and Management
149  //
150 
158  function getSeq() {
159  return 0;
160  }
161 
167  function isSitePlugin() {
168  return false;
169  }
170 
178  function getManagementVerbs() {
179  return array();
180  }
181 
196  function manage($verb, $args, &$message, &$messageParams, &$pluginModalContent = null) {
197  return false;
198  }
199 
207  function getHideManagement() {
208  return false;
209  }
210 
211  //
212  // Plugin Installation
213  //
214 
221  function getInstallSchemaFile() {
222  return null;
223  }
224 
231  function getInstallDataFile() {
232  return null;
233  }
234 
243  return null;
244  }
245 
256  return null;
257  }
258 
265  function getInstallEmailTemplatesFile() {
266  return null;
267  }
268 
276  return null;
277  }
278 
289  function getInstallFilterConfigFiles() {
290  // Construct the well-known filter configuration file names.
291  $filterConfigFile = $this->getPluginPath().'/filter/'.PLUGIN_FILTER_DATAFILE;
292  $filterConfigFiles = array(
293  './lib/pkp/'.$filterConfigFile,
294  './'.$filterConfigFile
295  );
296  return $filterConfigFiles;
297  }
299  /*
300  * Protected helper methods (can be used by custom plugins but
301  * should not be overridden by custom plugins)
302  */
307  function getCategory() {
308  return $this->pluginCategory;
309  }
310 
315  function getPluginPath() {
317  }
318 
324  function getTemplatePath($inCore = false) {
325  $basePath = Core::getBaseDir();
326  if ($inCore) {
327  $basePath = $basePath . DIRECTORY_SEPARATOR . PKP_LIB_PATH;
328  }
329  return "file:$basePath" . DIRECTORY_SEPARATOR . $this->getPluginPath() . DIRECTORY_SEPARATOR;
330  }
331 
338  function addLocaleData($locale = null) {
339  if ($locale == '') $locale = AppLocale::getLocale();
340  $localeFilenames = $this->getLocaleFilename($locale);
341  if ($localeFilenames) {
342  if (is_scalar($localeFilenames)) $localeFilenames = array($localeFilenames);
343  foreach($localeFilenames as $localeFilename) {
344  AppLocale::registerLocaleFile($locale, $localeFilename);
345  }
346  return true;
347  }
348  return false;
349  }
350 
357  function getSetting($contextId, $name) {
358  return $this->getContextSpecificSetting(array($contextId), $name);
359  }
360 
369  function updateSetting($contextId, $name, $value, $type = null) {
370  $this->updateContextSpecificSetting(array($contextId), $name, $value, $type);
371  }
378  function getContextSpecificSetting($context, $name) {
379  if (!defined('RUNNING_UPGRADE') && !Config::getVar('general', 'installed')) return null;
380 
381  // Check that the context has the correct depth
382  $application = PKPApplication::getApplication();
383  assert(is_array($context) && $application->getContextDepth() == count($context));
384 
385  // Construct the argument list and call the plug-in settings DAO
386  $arguments = $context;
387  $arguments[] = $this->getName();
388  $arguments[] = $name;
389  $pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
390  return call_user_func_array(array(&$pluginSettingsDao, 'getSetting'), $arguments);
391  }
392 
401  function updateContextSpecificSetting($context, $name, $value, $type = null) {
402  // Check that the context has the correct depth
403  $application = PKPApplication::getApplication();
404  assert(is_array($context) && $application->getContextDepth() == count($context));
405 
406  // Construct the argument list and call the plug-in settings DAO
407  $arguments = $context;
408  $arguments[] = $this->getName();
409  $arguments[] = $name;
410  $arguments[] = $value;
411  $arguments[] = $type;
412  $pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
413  call_user_func_array(array(&$pluginSettingsDao, 'updateSetting'), $arguments);
414  }
415 
421  function import($class) {
422  require_once($this->getPluginPath() . '/' . str_replace('.', '/', $class) . '.inc.php');
423  }
424 
425  /*
426  * Protected helper methods (for internal use only, should not
427  * be used by custom plug-ins)
428  *
429  * NB: These methods may change without notice in the future!
430  */
443  function getSettingMainContext() {
444  $application = PKPApplication::getApplication();
445  $contextDepth = $application->getContextDepth();
446 
447  $settingContext = array();
448  if ($contextDepth > 0) {
449  if ($this->isSitePlugin()) {
450  $mainContext = null;
451  } else {
452  $request = $application->getRequest();
453  $router = $request->getRouter();
454  // Try to identify the main context (e.g. journal, conference, press),
455  // will be null if none found.
456  $mainContext = $router->getContext($request, 1);
457  }
458 
459  // Create the context for the setting if found
460  if ($mainContext) $settingContext[] = $mainContext->getId();
461  $settingContext = array_pad($settingContext, $contextDepth, CONTEXT_ID_NONE);
462  }
463  return $settingContext;
464  }
465 
473  function getLocaleFilename($locale) {
474  $masterLocale = MASTER_LOCALE;
475  $baseLocaleFilename = $this->getPluginPath() . "/locale/$locale/locale.xml";
476  $baseMasterLocaleFilename = $this->getPluginPath() . "/locale/$masterLocale/locale.xml";
477  $libPkpFilename = "lib/pkp/$baseLocaleFilename";
478  $masterLibPkpFilename = "lib/pkp/$baseMasterLocaleFilename";
479  $filenames = array();
480  if (file_exists($baseMasterLocaleFilename)) $filenames[] = $baseLocaleFilename;
481  if (file_exists($masterLibPkpFilename)) $filenames[] = $libPkpFilename;
482  return $filenames;
483  }
484 
492  function installData($hookName, $args) {
493  $installer =& $args[0];
494  $result =& $args[1];
495 
496  // Treat single and multiple data files uniformly.
497  $dataFiles = $this->getInstallDataFile();
498  if (is_scalar($dataFiles)) $dataFiles = array($dataFiles);
499 
500  // Install all data files.
501  foreach($dataFiles as $dataFile) {
502  $sql = $installer->dataXMLParser->parseData($dataFile);
503  if ($sql) {
504  $result = $installer->executeSQL($sql);
505  } else {
506  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_INSTALLER);
507  $installer->setError(INSTALLER_ERROR_DB, str_replace('{$file}', $this->getInstallDataFile(), __('installer.installParseDBFileError')));
508  $result = false;
509  }
510  if (!$result) return false;
511  }
512  return false;
513  }
514 
522  function installSiteSettings($hookName, $args) {
523  // All contexts are set to zero for site-wide plug-in settings
524  $application = PKPApplication::getApplication();
525  $contextDepth = $application->getContextDepth();
526  if ($contextDepth >0) {
527  $arguments = array_fill(0, $contextDepth, 0);
528  } else {
529  $arguments = array();
530  }
531  $arguments[] = $this->getName();
532  $arguments[] = $this->getInstallSitePluginSettingsFile();
533  $pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
534  call_user_func_array(array(&$pluginSettingsDao, 'installSettings'), $arguments);
535 
536  return false;
537  }
538 
547  function installContextSpecificSettings($hookName, $args) {
548  // Only applications that have at least one context can
549  // install context specific settings.
550  $application = PKPApplication::getApplication();
551  $contextDepth = $application->getContextDepth();
552  if ($contextDepth > 0) {
553  $context =& $args[1];
554 
555  // Make sure that this is really a new context
556  $isNewContext = isset($args[3]) ? $args[3] : true;
557  if (!$isNewContext) return false;
558 
559  // Install context specific settings
560  $pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
561  switch ($contextDepth) {
562  case 1:
563  $pluginSettingsDao->installSettings($context->getId(), $this->getName(), $this->getContextSpecificPluginSettingsFile());
564  break;
565 
566  case 2:
567  $pluginSettingsDao->installSettings($context->getId(), 0, $this->getName(), $this->getContextSpecificPluginSettingsFile());
568  break;
569 
570  default:
571  // No application can have a context depth > 2
572  assert(false);
573  }
574  }
575  return false;
576  }
577 
585  function installEmailTemplates($hookName, $args) {
586  $installer =& $args[0]; /* @var $installer Installer */
587  $result =& $args[1];
588 
589  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
590  $sql = $emailTemplateDao->installEmailTemplates($this->getInstallEmailTemplatesFile(), true, null, true);
591 
592  if ($sql === false) {
593  // The template file seems to be invalid.
594  $installer->setError(INSTALLER_ERROR_DB, str_replace('{$file}', $this->getInstallDataFile(), __('installer.installParseEmailTemplatesFileError')));
595  $result = false;
596  } else {
597  // Are there any yet uninstalled email templates?
598  assert(is_array($sql));
599  if (!empty($sql)) {
600  // Install templates.
601  $result = $installer->executeSQL($sql);
602  }
603  }
604  return false;
605  }
606 
614  function installEmailTemplateData($hookName, $args) {
615  $installer =& $args[0];
616  $result =& $args[1];
617 
618  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO');
619  foreach ($installer->installedLocales as $locale) {
620  $filename = str_replace('{$installedLocale}', $locale, $this->getInstallEmailTemplateDataFile());
621  if (!file_exists($filename)) continue;
622  $sql = $emailTemplateDao->installEmailTemplateData($filename, true);
623  if ($sql) {
624  $result = $installer->executeSQL($sql);
625  } else {
626  $installer->setError(INSTALLER_ERROR_DB, str_replace('{$file}', $this->getInstallDataFile(), __('installer.installParseEmailTemplatesFileError')));
627  $result = false;
628  }
629  }
630  return false;
631  }
632 
640  function installLocale($hookName, $args) {
641  $locale =& $args[0];
642  $filename = str_replace('{$installedLocale}', $locale, $this->getInstallEmailTemplateDataFile());
643  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO');
644  $emailTemplateDao->installEmailTemplateData($filename);
645  return false;
646  }
647 
653  function installFilters($hookName, $args) {
654  $installer =& $args[0]; /* @var $installer Installer */
655  $result =& $args[1]; /* @var $result boolean */
656 
657  // Get the filter configuration file name(s).
658  $filterConfigFiles = $this->getInstallFilterConfigFiles();
659  if (is_scalar($filterConfigFiles)) $filterConfigFiles = array($filterConfigFiles);
660 
661  // Run through the config file positions and see
662  // whether one of these exists and needs to be installed.
663  foreach($filterConfigFiles as $filterConfigFile) {
664  // Is there a filter configuration?
665  if (!file_exists($filterConfigFile)) continue;
666 
667  // Install the filter configuration.
668  $result = $installer->installFilterConfig($filterConfigFile);
669  if (!$result) {
670  // The filter configuration file seems to be invalid.
671  $installer->setError(INSTALLER_ERROR_DB, str_replace('{$file}', $filterConfigFile, __('installer.installParseFilterConfigFileError')));
672  }
673  }
674 
675  // Do not stop installation.
676  return false;
677  }
678 
687  function updateSchema($hookName, $args) {
688  $installer =& $args[0];
689  $result =& $args[1];
690 
691  $schemaXMLParser = new adoSchema($installer->dbconn);
692  $dict =& $schemaXMLParser->dict;
693  $dict->SetCharSet($installer->dbconn->charSet);
694  $sql = $schemaXMLParser->parseSchema($this->getInstallSchemaFile());
695  if ($sql) {
696  $result = $installer->executeSQL($sql);
697  } else {
698  $installer->setError(INSTALLER_ERROR_DB, str_replace('{$file}', $this->getInstallSchemaFile(), __('installer.installParseDBFileError')));
699  $result = false;
700  }
701  return false;
702  }
703 
711  function smartyPluginUrl($params, &$smarty) {
712  $path = array($this->getCategory(), $this->getName());
713  if (is_array($params['path'])) {
714  $params['path'] = array_merge($path, $params['path']);
715  } elseif (!empty($params['path'])) {
716  $params['path'] = array_merge($path, array($params['path']));
717  } else {
718  $params['path'] = $path;
719  }
720  return $smarty->smartyUrl($params, $smarty);
721  }
722 
728  function getCurrentVersion() {
729  $versionDao = DAORegistry::getDAO('VersionDAO');
730  $pluginPath = $this->getPluginPath();
731  $product = basename($pluginPath);
732  $category = basename(dirname($pluginPath));
733  $installedPlugin = $versionDao->getCurrentVersion('plugins.'.$category, $product, true);
734 
735  if ($installedPlugin) {
736  return $installedPlugin;
737  } else {
738  return false;
739  }
740  }
741 
746  function &getRequest() {
747  if (!$this->request) {
748  $this->request =& Registry::get('request');
749  }
750  return $this->request;
751  }
752 
753  /*
754  * Private helper methods
755  */
762  $application = PKPApplication::getApplication();
763 
764  if ($application->getContextDepth() == 0) return null;
765 
766  $contextList = $application->getContextList();
767  return ucfirst(array_shift($contextList)).'SiteSettingsForm::execute';
768  }
769 
778  function getManagementVerbLinkAction($request, $verb) {
779  return null;
780  }
781 }
782 
783 ?>
static & getDAO($name, $dbconn=null)
& getRequest()
Definition: Plugin.inc.php:755
smartyPluginUrl($params, &$smarty)
Definition: Plugin.inc.php:720
getInstallEmailTemplatesFile()
Definition: Plugin.inc.php:274
getManagementVerbLinkAction($request, $verb)
Definition: Plugin.inc.php:787
getContextSpecificPluginSettingsFile()
Definition: Plugin.inc.php:264
installSiteSettings($hookName, $args)
Definition: Plugin.inc.php:531
static requireComponents()
getInstallSitePluginSettingsFile()
Definition: Plugin.inc.php:251
getHideManagement()
Definition: Plugin.inc.php:216
getInstallSchemaFile()
Definition: Plugin.inc.php:230
getDisplayName()
Definition: Plugin.inc.php:143
_getContextSpecificInstallationHook()
Definition: Plugin.inc.php:770
static getLocale()
Plugin()
Definition: Plugin.inc.php:72
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
getInstallFilterConfigFiles()
Definition: Plugin.inc.php:298
isSitePlugin()
Definition: Plugin.inc.php:176
getCurrentVersion()
Definition: Plugin.inc.php:737
installEmailTemplateData($hookName, $args)
Definition: Plugin.inc.php:623
Abstract class for plugins.
Definition: Plugin.inc.php:50
manage($verb, $args, &$message, &$messageParams, &$pluginModalContent=null)
Definition: Plugin.inc.php:205
getManagementVerbs()
Definition: Plugin.inc.php:187
getPluginPath()
Definition: Plugin.inc.php:324
getSettingMainContext()
Definition: Plugin.inc.php:452
installFilters($hookName, $args)
Definition: Plugin.inc.php:662
getSetting($contextId, $name)
Definition: Plugin.inc.php:366
$pluginCategory
Definition: Plugin.inc.php:61
installContextSpecificSettings($hookName, $args)
Definition: Plugin.inc.php:556
addLocaleData($locale=null)
Definition: Plugin.inc.php:347
getContextSpecificSetting($context, $name)
Definition: Plugin.inc.php:387
installData($hookName, $args)
Definition: Plugin.inc.php:501
getInstallEmailTemplateDataFile()
Definition: Plugin.inc.php:284
static & get($key, $createIfEmpty=false, $createWithDefault=null)
getInstallDataFile()
Definition: Plugin.inc.php:240
installLocale($hookName, $args)
Definition: Plugin.inc.php:649
static register($hookName, $callback)
static getBaseDir()
Definition: Core.inc.php:37
updateSchema($hookName, $args)
Definition: Plugin.inc.php:696
installEmailTemplates($hookName, $args)
Definition: Plugin.inc.php:594
getCategory()
Definition: Plugin.inc.php:316
updateSetting($contextId, $name, $value, $type=null)
Definition: Plugin.inc.php:378
updateContextSpecificSetting($context, $name, $value, $type=null)
Definition: Plugin.inc.php:410
getTemplatePath($inCore=false)
Definition: Plugin.inc.php:333
getLocaleFilename($locale)
Definition: Plugin.inc.php:482
getDescription()
Definition: Plugin.inc.php:152