/core/lib/helpers/html_helper.php

https://github.com/djalmaaraujo/pian.in · PHP · 278 lines · 158 code · 4 blank · 116 comment · 17 complexity · 333e6cc7bbe91e4ec7515435460b34f2 MD5 · raw file

  1. <?php
  2. /**
  3. * Geração automática dos elementos HTML de acordo com os dados passados.
  4. *
  5. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  6. * @copyright Copyright 2008-2009, Spaghetti* Framework (http://spaghettiphp.org/)
  7. *
  8. */
  9. class HtmlHelper extends Helper {
  10. /**
  11. * Mantém uma referência à view que chamou o helper.
  12. */
  13. protected $view;
  14. public function __construct(&$view) {
  15. $this->view =& $view;
  16. $this->view->stylesForLayout = "";
  17. $this->view->scriptsForLayout = "";
  18. }
  19. /**
  20. * Cria HTML para tags de abertura.
  21. *
  22. * @param string $tag Tag a ser criada
  23. * @param string $attr Atributos da tag
  24. * @param boolean $empty Verdadeiro para criar uma tag vazia
  25. * @return string Tag HTML
  26. */
  27. public function openTag($tag, $attr = array(), $empty = false) {
  28. $html = "<{$tag}";
  29. $attr = $this->attr($attr);
  30. if(!empty($attr)):
  31. $html .= " $attr";
  32. endif;
  33. $html .= ($empty ? " /" : "") . ">";
  34. return $html;
  35. }
  36. /**
  37. * Cria HTML para tag de fechamento.
  38. *
  39. * @param string $tag Tag a ser fechada
  40. * @return string Tag HMTL fechada
  41. */
  42. public function closeTag($tag) {
  43. return "</{$tag}>";
  44. }
  45. /**
  46. * Cria HTML para tags de abertura e fechamento contendo algum conteúdo.
  47. *
  48. * @param string $tag Tag a ser criada
  49. * @param string $content Conteúdo entre as tags inseridas
  50. * @param array $attr Atributos da tag
  51. * @param boolean $empty Verdadeiro para criar uma tag vazia
  52. * @return string Tag HTML com o seu conteúdo
  53. */
  54. public function tag($tag, $content = "", $attr = array(), $empty = false) {
  55. $html = $this->openTag($tag, $attr, $empty);
  56. if(!$empty):
  57. $html .= "{$content}" . $this->closeTag($tag);
  58. endif;
  59. return $html;
  60. }
  61. /**
  62. * Prepara atributos para utilização em tags HTML.
  63. *
  64. * @param array $attr Atributos a serem preparados
  65. * @return string Atributos para preenchimento da tag
  66. */
  67. public function attr($attr) {
  68. $attributes = array();
  69. foreach($attr as $name => $value):
  70. if($value === true):
  71. $value = $name;
  72. elseif($value === false):
  73. continue;
  74. endif;
  75. $attributes []= $name . '="' . $value . '"';
  76. endforeach;
  77. return join(" ", $attributes);
  78. }
  79. /**
  80. * Cria um link para ser utilizado na aplicação.
  81. *
  82. * @param string $text Conteúdo para o link
  83. * @param string $url URL relativa à raiz da aplicação
  84. * @param array $attr Atributos da tag
  85. * @param boolean $full Verdadeiro para gerar uma URL completa
  86. * @return string Link HTML
  87. */
  88. public function link($text, $url = null, $attr = array(), $full = false) {
  89. if(is_null($url)):
  90. $url = $text;
  91. endif;
  92. $attr["href"] = Mapper::url($url, $full);
  93. return $this->output($this->tag("a", $text, $attr));
  94. }
  95. /**
  96. * Cria um elemento de imagem para ser na aplicação.
  97. *
  98. * @param string $src Caminho da imagem
  99. * @param array $attr Atributos da tag
  100. * @param boolean $full Verdadeiro para gerar uma URL completa
  101. * @return string HTML da imagem a ser inserida
  102. */
  103. public function image($src, $attr = array(), $full = false) {
  104. $attr = array_merge(
  105. array(
  106. "alt" => "",
  107. "title" => isset($attr["alt"]) ? $attr["alt"] : ""
  108. ),
  109. $attr
  110. );
  111. if(!$this->external($src)):
  112. $src = Mapper::url("/images/" . $src, $full);
  113. endif;
  114. $attr["src"] = $src;
  115. return $this->output($this->tag("img", null, $attr, true));
  116. }
  117. /**
  118. * Short description.
  119. *
  120. * @param string $src
  121. * @param string $url
  122. * @param array $img_attr
  123. * @param array $attr
  124. * @param boolean $full
  125. * @return string
  126. */
  127. public function imagelink($src, $url, $img_attr = array(), $attr = array(), $full = false) {
  128. return $this->link($this->image($src, $img_attr, $full), $url, $attr, $full);
  129. }
  130. /**
  131. * Cria elementos de folha de estilho para serem usados no HTML.
  132. *
  133. * @param string $href Caminho da folha de estilo a ser inserida no HTML
  134. * @param array $attr Atributos da tag
  135. * @param boolean $inline Verdadeiro para imprimir a folha de estilo inline
  136. * @param boolean $full Verdadeiro para gerar uma URL completa
  137. * @return string Elemento de folha de estilo a ser utilizado
  138. */
  139. public function stylesheet($href = "", $attr = array(), $inline = true, $full = false) {
  140. if(is_array($href)):
  141. $output = "";
  142. foreach($href as $tag):
  143. $output .= $this->stylesheet($tag, $attr, true, $full) . PHP_EOL;
  144. endforeach;
  145. else:
  146. if(!$this->external($href)):
  147. $href = Mapper::url("/styles/" . $this->extension($href, "css"), $full);
  148. endif;
  149. $attr = array_merge(
  150. array(
  151. "href" => $href,
  152. "rel" => "stylesheet",
  153. "type" => "text/css"
  154. ),
  155. $attr
  156. );
  157. $output = $this->output($this->tag("link", null, $attr, true));
  158. endif;
  159. if($inline):
  160. return $output;
  161. else:
  162. $this->view->stylesForLayout .= $output;
  163. return true;
  164. endif;
  165. }
  166. /**
  167. * Cria um elemento de script para ser usado no HTML.
  168. *
  169. * @param string $src Caminho do script a ser inseido no HTML
  170. * @param array $attr Atributos da tag
  171. * @param boolean $inline Verdadeiro para imprimir o script inline
  172. * @param boolean $full Verdadeiro para gerar uma URL completa
  173. * @return string Elemento de script a ser utilizado
  174. */
  175. public function script($src = "", $attr = array(), $inline = true, $full = false) {
  176. if(is_array($src)):
  177. $output = "";
  178. foreach($src as $tag):
  179. $output .= $this->script($tag, $attr, true, $full) . PHP_EOL;
  180. endforeach;
  181. else:
  182. if(!$this->external($src)):
  183. $src = Mapper::url("/scripts/" . $this->extension($src, "js"), $full);
  184. endif;
  185. $attr = array_merge(
  186. array(
  187. "src" => $src,
  188. "type" => "text/javascript"
  189. ),
  190. $attr
  191. );
  192. $output = $this->output($this->tag("script", null, $attr));
  193. endif;
  194. if($inline):
  195. return $output;
  196. else:
  197. $this->view->scriptsForLayout .= $output;
  198. return true;
  199. endif;
  200. }
  201. /*
  202. * Cria uma lista a partir de um array.
  203. *
  204. * @param array $list Array com conjunto de elementos da lista
  205. * @return string
  206. */
  207. public function nestedList($list, $attr = array(), $type = "ul") {
  208. $content = "";
  209. foreach($list as $k => $li):
  210. if(is_array($li)):
  211. $li = $this->nestedList($li, array(), $type);
  212. if(!is_numeric($k)):
  213. $li = $k . $li;
  214. endif;
  215. endif;
  216. $content .= $this->tag("li", $li) . PHP_EOL;
  217. endforeach;
  218. return $this->tag($type, $content, $attr);
  219. }
  220. /**
  221. * Cria uma tag DIV.
  222. *
  223. * @param string $content Conteúdo da tag
  224. * @param array $attr Atributos da tag
  225. * @return string Tag DIV
  226. */
  227. public function div($content, $attr = array()) {
  228. if(!is_array($attr)):
  229. $attr = array("class" => $attr);
  230. endif;
  231. return $this->output($this->tag("div", $content, $attr));
  232. }
  233. /**
  234. * Adiciona uma meta tag para definir o charset da página.
  235. *
  236. * @param string $charset Charset a ser utilizado
  237. * @return string Tag META
  238. */
  239. public function charset($charset = null) {
  240. if(is_null($charset)):
  241. $charset = Config::read("appEncoding");
  242. endif;
  243. $attr = array(
  244. "http-equiv" => "Content-type",
  245. "content" => "text/html; charset={$charset}"
  246. );
  247. return $this->output($this->tag("meta", null, $attr));
  248. }
  249. /**
  250. * Verifica se uma URL é externa.
  251. *
  252. * @param string $url URL a ser verificada
  253. * @return boolean Verdadeiro se a URL for externa
  254. */
  255. public function external($url) {
  256. return preg_match("/^[a-z]+:/", $url);
  257. }
  258. /**
  259. * Adiciona uma extensão a um arquivo caso ela não exista.
  260. *
  261. * @param string $file Nome do arquivo
  262. * @param string $extension Extensão a ser adicionada
  263. * @return string Novo nome do arquivo
  264. */
  265. public function extension($file, $extension) {
  266. if(strpos($file, "?") === false):
  267. if(strpos($file, "." . $extension) === false):
  268. $file .= "." . $extension;
  269. endif;
  270. endif;
  271. return $file;
  272. }
  273. }
  274. ?>