PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/site/helpers/ExecuteHelperMarkup.php

https://code.google.com/p/ontowiki/
PHP | 112 lines | 52 code | 15 blank | 45 comment | 0 complexity | 79e6a99e99eaaf74eb92d31c63d2d9e1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  4. *
  5. * @copyright Copyright (c) 2011, {@link http://aksw.org AKSW}
  6. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  7. */
  8. /**
  9. * OntoWiki ExecuteHelperMarkup view helper
  10. *
  11. * takes a string and replaces occurrences of helper markup with its results
  12. * helper markup is defined as:
  13. * {{helpername p1="v1" p2="v2 ...}} and {{helpername v1}}
  14. *
  15. * @todo distinguish between helpers and markup-helpers (actions)
  16. *
  17. * @category OntoWiki
  18. * @package OntoWiki_extensions_components_site
  19. */
  20. class Site_View_Helper_ExecuteHelperMarkup extends Zend_View_Helper_Abstract
  21. {
  22. /*
  23. * current view, injected with setView from Zend
  24. */
  25. public $view;
  26. /*
  27. * the short helper markup pattern, allows linking of qnames in text
  28. * without any syntax
  29. */
  30. //public $shortPattern = '/(?\'prefix\'[^a-zA-Z"])(?\'value\'[a-zA-Z]+\:[a-zA-Z]+)/';
  31. public $shortPattern = '/\[\[(?\'value\'[a-zA-Z]+\:[a-zA-Z]+)\]\]/';
  32. /*
  33. * the outer helper markup pattern
  34. * note: http://stackoverflow.com/questions/1435254/ - now way to identify
  35. * key/value pairs at once, so this is done by a second pattern
  36. */
  37. public $helperPattern = '/{{(?\'helper\'[a-zA-Z]+)(?\'attributes\'( [a-zA-Z]+\=\"[^"]+\")*)}}/';
  38. /*
  39. * identifies key value pairs in the attribute part
  40. */
  41. public $keyValuePattern = '/((?\'key\'[a-zA-Z]+)\=\"(?\'value\'[^"]+)\")/';
  42. /*
  43. * view setter (dev zone article: http://devzone.zend.com/article/3412)
  44. */
  45. public function setView(Zend_View_Interface $view)
  46. {
  47. $this->view = $view;
  48. }
  49. private function replaceShortHelper($matches)
  50. {
  51. $value = $matches['value'];
  52. // the prefix is to avoid replacement of qname values in full helper tags
  53. //$prefix = $matches['prefix'];
  54. return '{{link uri="'.$value.'" text="'.$value.'"}}';
  55. }
  56. private function executeFullHelper($matches)
  57. {
  58. $tag = $matches[0];
  59. $helper = strtolower($matches['helper']);
  60. $attributes = trim($matches['attributes']);
  61. // split the attributes part of the helper markup and fill it to the
  62. // options array
  63. preg_match_all ($this->keyValuePattern, $attributes, $matches, PREG_SET_ORDER);
  64. $options = array();
  65. foreach ($matches as $i => $match) {
  66. $key = $match['key'];
  67. $value = $match['value'];
  68. $options[$key] = $value;
  69. }
  70. // pre-set some standard values
  71. $options['selectedResource'] = OntoWiki::getInstance()->selectedResource;
  72. // return the output of the helper or its error message
  73. try {
  74. return $this->view->$helper($options);
  75. } catch (Exception $e) {
  76. $message = htmlspecialchars($e->getMessage(), ENT_NOQUOTES);
  77. $message = str_replace ('"', '', $message);
  78. $message = str_replace ('\'', '', $message);
  79. return $this->returnError($message . " ($tag)");
  80. }
  81. }
  82. private function returnError($message = null)
  83. {
  84. return "<span title='$message'>{{helper error}}</span>";
  85. }
  86. public function executeHelperMarkup($text = null)
  87. {
  88. $this->text = (string) $text;
  89. // replace shortened link tags with full helper tags
  90. $callback = array( &$this, 'replaceShortHelper');
  91. $this->text = preg_replace_callback ($this->shortPattern, $callback , $this->text);
  92. // execute full helper tags
  93. $callback = array( &$this, 'executeFullHelper');
  94. $this->text = preg_replace_callback ($this->helperPattern, $callback , $this->text);
  95. return $this->text;
  96. }
  97. }