HtmlExtHelper.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. App::uses('HtmlHelper', 'View/Helper');
  3. /**
  4. * HtmlExt Helper
  5. *
  6. * Provides additional functionality for HtmlHelper.
  7. * Use with aliasing to map it back to $this->Html attribute.
  8. *
  9. * @author Mark Scherer
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class HtmlExtHelper extends HtmlHelper {
  13. /**
  14. * For convenience functions Html::defaultLink() and defaultUrl().
  15. *
  16. * @var array
  17. */
  18. protected $_linkDefaults = null;
  19. /**
  20. * Display image tag from blob content.
  21. * Enhancement for HtmlHelper. Defaults to png image
  22. *
  23. * Options:
  24. * - type: png, gif, jpg, ...
  25. *
  26. * @param binary $content
  27. * @param array $options
  28. * @return string html imageTag
  29. */
  30. public function imageFromBlob($content, $options = array()) {
  31. $options += array('type' => 'png');
  32. $mimeType = 'image/' . $options['type'];
  33. $text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
  34. return sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
  35. }
  36. /**
  37. * HTML Helper extension for HTML5 time
  38. * The time element represents either a time on a 24 hour clock,
  39. * or a precise date in the proleptic Gregorian calendar,
  40. * optionally with a time and a time-zone offset.
  41. *
  42. * Options:
  43. * - 'format' STRING: Use the specified TimeHelper method (or format()).
  44. * FALSE: Generate the datetime. NULL: Do nothing.
  45. * - 'datetime' STRING: If 'format' is STRING use as the formatting string.
  46. * FALSE: Don't generate attribute
  47. *
  48. * @param $content string Time
  49. * @param $options array Options
  50. * @return string HTML time tag.
  51. */
  52. public function time($content, $options = array()) {
  53. if (!isset($this->tags['time'])) {
  54. $this->tags['time'] = '<time%s>%s</time>';
  55. }
  56. $options = array_merge(array(
  57. 'datetime' => '%Y-%m-%d %T',
  58. 'pubdate' => false,
  59. 'format' => '%Y-%m-%d %T',
  60. ), $options);
  61. if ($options['format'] !== null) {
  62. if (!isset($this->Time)) {
  63. App::uses('TimeHelper', 'View/Helper');
  64. $this->Time = new TimeHelper($this->_View);
  65. }
  66. }
  67. if ($options['format']) {
  68. if (method_exists($this->Time, $options['format'])) {
  69. $content = $this->Time->$options['format']($content);
  70. } else {
  71. $content = $this->Time->i18nFormat($content, $options['format']);
  72. }
  73. $options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
  74. } elseif ($options['format'] === false && $options['datetime']) {
  75. $options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
  76. }
  77. if ($options['pubdate']) {
  78. $pubdate = true;
  79. }
  80. unset($options['format']);
  81. unset($options['pubdate']);
  82. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  83. if (isset($pubdate)) {
  84. $attributes .= ' pubdate';
  85. }
  86. return sprintf($this->tags['time'], $attributes, $content);
  87. }
  88. /**
  89. * Keep named and query params for pagination/filter after edit etc.
  90. *
  91. * @params same as Html::link($title, $url, $options, $confirmMessage)
  92. * @return string Link
  93. */
  94. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  95. // Named are deprecated
  96. if (is_array($url)) {
  97. $url += $this->params['named'];
  98. }
  99. if (is_array($url)) {
  100. if (!isset($url['?'])) {
  101. $url['?'] = array();
  102. }
  103. $url['?'] += $this->request->query;
  104. }
  105. return $this->link($title, $url, $options, $confirmMessage);
  106. }
  107. /**
  108. * Keep named and query params for pagination/filter after edit etc.
  109. *
  110. * @params same as Html::url($url, $options, $escape)
  111. * @return string Link
  112. */
  113. public function completeUrl($url = null, $full = false, $escape = true) {
  114. // Named are deprecated
  115. if (is_array($url)) {
  116. $url += $this->params['named'];
  117. }
  118. if (is_array($url)) {
  119. if (!isset($url['?'])) {
  120. $url['?'] = array();
  121. }
  122. $url['?'] += $this->request->query;
  123. }
  124. return $this->url($url, $options, $escape);
  125. }
  126. /**
  127. * Convenience function for normal links.
  128. * Useful for layout links and links inside elements etc if you don't want to
  129. * verbosely reset all parts of it (prefix, plugin, ...).
  130. *
  131. * @params same as Html::link($title, $url, $options, $confirmMessage)
  132. * @return string HTML Link
  133. */
  134. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  135. if ($this->_linkDefaults === null) {
  136. if (!class_exists('CommonComponent')) {
  137. App::uses('CommonComponent', 'Tools.Controller/Component');
  138. }
  139. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  140. }
  141. if (!defined('PREFIX_ADMIN')) {
  142. define('PREFIX_ADMIN', 'admin');
  143. }
  144. if ($url !== null && is_array($url)) {
  145. $url = array_merge($this->_linkDefaults, $url);
  146. if (!empty($url[PREFIX_ADMIN])) {
  147. $options['rel'] = 'nofollow';
  148. }
  149. } elseif (is_array($title)) {
  150. $title = array_merge($this->_linkDefaults, $title);
  151. if (!empty($title[PREFIX_ADMIN])) {
  152. $options['rel'] = 'nofollow';
  153. }
  154. }
  155. //$this->log($url, '404');
  156. return $this->link($title, $url, $options, $confirmMessage);
  157. }
  158. /**
  159. * Convenience function for normal urls.
  160. * Useful for layout links and links inside elements etc if you don't want to
  161. * verbosely reset all parts of it (prefix, plugin, ...).
  162. *
  163. * @params same as Html::url($url, $full)
  164. * @return string URL
  165. */
  166. public function defaultUrl($url = null, $full = false) {
  167. if ($this->_linkDefaults === null) {
  168. if (!class_exists('CommonComponent')) {
  169. App::uses('CommonComponent', 'Tools.Controller/Component');
  170. }
  171. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  172. }
  173. if ($url !== null && is_array($url)) {
  174. $url = array_merge($this->_linkDefaults, $url);
  175. }
  176. return $this->url($url, $full);
  177. }
  178. /**
  179. * Enhancement to htmlHelper which allows the crumbs protected array
  180. * to be cleared so that more than one set of crumbs can be generated in the same view.
  181. *
  182. * @return void
  183. */
  184. public function resetCrumbs() {
  185. $this->_crumbs = array();
  186. }
  187. }