/flavor/classes/views.class.php

https://github.com/cjuarez/Come-en-Colima · PHP · 147 lines · 114 code · 16 blank · 17 comment · 26 complexity · a587f05395d665f58c4afcc95a613a4c MD5 · raw file

  1. <?php
  2. /* ===========================
  3. FlavorPHP - because php should have a better taste
  4. homepage: http://www.flavorphp.com/
  5. git repository: https://github.com/Axloters/FlavorPHP
  6. FlavorPHP is a free software licensed under the MIT license
  7. Copyright (C) 2008 by Pedro Santana <contacto at pedrosantana dot mx>
  8. Team:
  9. Pedro Santana
  10. Victor Bracco
  11. Victor de la Rocha
  12. Jorge Condomí
  13. Aaron Munguia
  14. =========================== */
  15. ?>
  16. <?php
  17. class views {
  18. protected $vars = array();
  19. protected $layout = "default";
  20. protected $registry;
  21. protected $html;
  22. protected $ajax;
  23. protected $path;
  24. protected $session;
  25. protected $cookie;
  26. protected $l10n;
  27. public function __construct() {
  28. $this->registry = registry::getInstance();
  29. $this->path = $this->registry["path"];
  30. $this->html = html::getInstance();
  31. $this->session = session::getInstance();
  32. $this->cookie = cookie::getInstance();
  33. $this->ajax = new ajax();
  34. $this->l10n = l10n::getInstance();
  35. }
  36. public function __set($name, $value){
  37. if (isset($this->vars[$name]) == true) {
  38. throw new Exception("Unable to set view '".$name."'. Already set.");
  39. return false;
  40. }
  41. $this->vars[$name] = $value;
  42. return true;
  43. }
  44. public function remove($name) {
  45. unset($this->vars[$name]);
  46. return true;
  47. }
  48. public function setlayout($name) {
  49. $this->layout = $name;
  50. }
  51. public function renderElement($name) {
  52. echo $this->fetch($name, "element");
  53. }
  54. public function fetch($name, $type = NULL) {
  55. if ($type == "element") {
  56. $path = Absolute_Path.APPDIR.DIRSEP."views".DIRSEP."elements".DIRSEP.$name.".php";
  57. $errorMsg = "The <strong>element</strong> '<em>".$name."</em>' does not exist.";
  58. } elseif ($type == "layout") {
  59. $path = Absolute_Path.APPDIR.DIRSEP."views".DIRSEP."layouts".DIRSEP.$this->layout.".php";
  60. $errorMsg = "The <strong>layout</strong> '<em>".$this->layout."</em>' does not exist.";
  61. } else {
  62. $route = explode(".", $name);
  63. $path = Absolute_Path.APPDIR.DIRSEP."views".DIRSEP.$route[0].DIRSEP.$route[1].".php";
  64. $errorMsg = "The <strong>view</strong> '<em>".$name."</em>' does not exist.";
  65. }
  66. if (file_exists($path) == false) {
  67. throw new Exception("FlavorPHP error: ". $errorMsg);
  68. return false;
  69. }
  70. foreach ($this->vars as $key => $value) {
  71. $$key = $value;
  72. }
  73. ob_start();
  74. include ($path);
  75. $contents = ob_get_contents();
  76. ob_end_clean();
  77. return $contents;
  78. }
  79. public function fixNewlinesForCleanHtml($fixthistext) {
  80. $fixthistext_array = explode("\n", $fixthistext);
  81. foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue) {
  82. if (!preg_match("/^(\s)*$/", $unfixedtextvalue)) {
  83. $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
  84. $fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
  85. }
  86. }
  87. return implode("\n", $fixedtext_array);
  88. }
  89. public function cleanHtmlCode($uncleanhtml) {
  90. $indent = " ";
  91. $fixed_uncleanhtml = $this->fixNewlinesForCleanHtml($uncleanhtml);
  92. $uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
  93. $indentlevel = 0;
  94. foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml) {
  95. $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
  96. $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
  97. $replaceindent = "";
  98. for ($o = 0; $o < $indentlevel; $o++) {
  99. $replaceindent .= $indent;
  100. }
  101. if (preg_match("/<(.+)\/>/", $currentuncleanhtml)) {
  102. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  103. } else if (preg_match("/<!(.*)>/", $currentuncleanhtml)) {
  104. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  105. } else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml)) {
  106. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  107. } else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml)) {
  108. $indentlevel--;
  109. $replaceindent = "";
  110. for ($o = 0; $o < $indentlevel; $o++) {
  111. $replaceindent .= $indent;
  112. }
  113. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  114. } else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml)) {
  115. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  116. $indentlevel++;
  117. $replaceindent = "";
  118. for ($o = 0; $o < $indentlevel; $o++) {
  119. $replaceindent .= $indent;
  120. }
  121. } else {
  122. $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
  123. }
  124. }
  125. return implode("\n", $cleanhtml_array);
  126. }
  127. }