PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/metadata/nlm/NlmCitationSchemaCitationOutputFormatFilter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 124 lines | 47 code | 17 blank | 60 comment | 6 complexity | 48e78ebe59e5592b834f3381959c6e7c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/nlm/NlmCitationSchemaCitationOutputFormatFilter.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class NlmCitationSchemaCitationOutputFormatFilter
  9. * @ingroup classes_metadata_nlm
  10. *
  11. * @brief Abstract base class for all filters that transform
  12. * NLM citation metadata descriptions into citation output formats
  13. * via smarty template.
  14. */
  15. // $Id$
  16. import('filter.Filter');
  17. class NlmCitationSchemaCitationOutputFormatFilter extends Filter {
  18. /** @var PKPRequest */
  19. var $_request;
  20. /**
  21. * Constructor
  22. * @param $request PKPRequest
  23. */
  24. function NlmCitationSchemaCitationOutputFormatFilter(&$request) {
  25. parent::Filter();
  26. // Load additional translations
  27. $locale = AppLocale::getLocale();
  28. $basePath = $this->getBasePath();
  29. $localeFile = $basePath.DIRECTORY_SEPARATOR.'locale'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.'locale.xml';
  30. AppLocale::registerLocaleFile($locale, $localeFile);
  31. // Save the request for later use
  32. $this->_request =& $request;
  33. }
  34. //
  35. // Abstract template methods to be implemented by subclasses
  36. //
  37. /**
  38. * Return the base path of the citation filter
  39. * @return string
  40. */
  41. function getBasePath() {
  42. assert(false);
  43. }
  44. //
  45. // Implement template methods from Filter
  46. //
  47. /**
  48. * @see Filter::supports()
  49. * @param $input mixed
  50. * @param $output mixed
  51. * @return boolean
  52. */
  53. function supports(&$input, &$output) {
  54. // Check the input type
  55. if (!is_a($input, 'MetadataDescription')) return false;
  56. $metadataSchema =& $input->getMetadataSchema();
  57. if ($metadataSchema->getName() != 'nlm-3.0-element-citation') return false;
  58. // Check the output type
  59. if (is_null($output)) return true;
  60. return is_string($output);
  61. }
  62. /**
  63. * @see Filter::process()
  64. * @param $input MetadataDescription NLM citation description
  65. * @return string formatted citation output
  66. */
  67. function &process(&$input) {
  68. // Initialize view
  69. $locale = AppLocale::getLocale();
  70. $templateMgr =& TemplateManager::getManager($this->_request);
  71. // Add the filter's directory as additional template dir so that
  72. // citation output format templates can include sub-templates in
  73. // the same folder.
  74. $templateMgr->template_dir[] = $this->getBasePath();
  75. // Loop over the statements in the schema and add them
  76. // to the template
  77. $propertyNames =& $input->getPropertyNames();
  78. foreach($propertyNames as $propertyName) {
  79. $templateVariable = $input->getNamespacedPropertyId($propertyName);
  80. if ($input->hasProperty($propertyName)) {
  81. $propertyLocale = $input->getProperty($propertyName)->getTranslated() ? $locale : null;
  82. $templateMgr->assign_by_ref($templateVariable, $input->getStatement($propertyName, $propertyLocale));
  83. } else {
  84. // Delete potential leftovers from previous calls
  85. $templateMgr->clear_assign($templateVariable);
  86. }
  87. }
  88. // Let the template engine render the citation
  89. $templateName = $this->_getCitationTemplate();
  90. $output = $templateMgr->fetch($templateName);
  91. // Remove the additional template dir
  92. array_pop($templateMgr->template_dir);
  93. return $output;
  94. }
  95. //
  96. // Private helper methods
  97. //
  98. /**
  99. * Get the citation template
  100. * @return string
  101. */
  102. function _getCitationTemplate() {
  103. $basePath = $this->getBasePath();
  104. return 'file:'.$basePath.DIRECTORY_SEPARATOR.'nlm-citation.tpl';
  105. }
  106. }
  107. ?>