RenderHtml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 3101Xh46ZTWQeEz3g4PSAoa6vVIYLDxdCINMbD+OZGY=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2016 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.8
  11. */
  12. /**
  13. * processes an input text and renders it as html
  14. * parses code that has the following format:
  15. * <%=action:{action}.{controller}.{module}%>
  16. * => Helper::Action(action, controller, module)
  17. * <%=url:{param-key},{param-value};{param-key},{param-value}...
  18. * => Helper::Url - array of params
  19. * <%=href:{uri}%>
  20. * => Helper::Url - string uri param
  21. */
  22. namespace Cube\View\Helper;
  23. class RenderHtml extends AbstractHelper
  24. {
  25. /**
  26. *
  27. * output formatted string
  28. *
  29. * @param string $string
  30. * @param bool $parseCode
  31. * @param bool $script
  32. *
  33. * @return string
  34. */
  35. public function renderHtml($string, $parseCode = false, $script = true)
  36. {
  37. $output = str_ireplace(
  38. array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&nbsp;'), array('&', "'", '"', '<', '>', ' '), $string);
  39. if ($parseCode) {
  40. if (preg_match_all('#<%=action:(.+)%>#', $output, $m)) {
  41. $params = array();
  42. foreach ($m[1] as $key => $matches) {
  43. $array = explode('.', $matches);
  44. $action = (isset($array[0])) ? $array[0] : null;
  45. $controller = (isset($array[1])) ? $array[1] : null;
  46. $module = (isset($array[2])) ? $array[2] : null;
  47. $vars = (isset($array[3])) ? explode(';', $array[3]) : null;
  48. foreach ((array)$vars as $var) {
  49. list ($k, $v) = explode(',', $var);
  50. if (!empty($k)) {
  51. $params[$k] = $v;
  52. }
  53. }
  54. $replace = $this->getView()->action($action, $controller, $module, $params);
  55. $output = str_replace($m[0][$key], $replace, $output);
  56. }
  57. }
  58. // url helper
  59. if (preg_match_all('#<%=url:(.+)%>#', $output, $m)) {
  60. foreach ($m[1] as $key => $matches) {
  61. $array = explode(';', $matches);
  62. $params = array();
  63. foreach ($array as $row) {
  64. list($k, $v) = explode(',', $row);
  65. if (!empty($v)) {
  66. $params[$k] = $v;
  67. }
  68. }
  69. $replace = $this->getView()->url($params);
  70. $output = str_replace($m[0][$key], $replace, $output);
  71. }
  72. }
  73. // url helper with path string
  74. if (preg_match_all('#<%=href:([a-zA-Z0-9\/\-\_]+)%>#', $output, $m)) {
  75. foreach ($m[1] as $key => $href) {
  76. $replace = $this->getView()->url($href);
  77. $output = str_replace($m[0][$key], $replace, $output);
  78. }
  79. }
  80. }
  81. return ($script === true) ?
  82. $output : preg_replace('#<script(.*?)>(.*?)</script>#is', '', $output);
  83. }
  84. }