HtmlExtHelper.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $defaults = array(
  57. 'datetime' => '%Y-%m-%d %T',
  58. 'pubdate' => false,
  59. 'format' => '%Y-%m-%d %T',
  60. );
  61. $options += $defaults;
  62. if ($options['format'] !== null) {
  63. if (!isset($this->Time)) {
  64. App::uses('TimeHelper', 'View/Helper');
  65. $this->Time = new TimeHelper($this->_View);
  66. }
  67. }
  68. if ($options['format']) {
  69. if (method_exists($this->Time, $options['format'])) {
  70. $content = $this->Time->$options['format']($content);
  71. } else {
  72. $content = $this->Time->i18nFormat($content, $options['format']);
  73. }
  74. $options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
  75. } elseif ($options['format'] === false && $options['datetime']) {
  76. $options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
  77. }
  78. if ($options['pubdate']) {
  79. $pubdate = true;
  80. }
  81. unset($options['format']);
  82. unset($options['pubdate']);
  83. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  84. if (isset($pubdate)) {
  85. $attributes .= ' pubdate';
  86. }
  87. return sprintf($this->tags['time'], $attributes, $content);
  88. }
  89. /**
  90. * HtmlExtHelper::link()
  91. *
  92. * Shim option
  93. * - confirm: replaces 4th param
  94. *
  95. * @see HtmlHelper::link for details
  96. *
  97. * @return string
  98. */
  99. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  100. if (isset($options['confirm'])) {
  101. $confirmMessage = $options['confirm'];
  102. unset($options['confirm']);
  103. }
  104. return parent::link($title, $url, $options, $confirmMessage);
  105. }
  106. /**
  107. * Keep named and query params for pagination/filter after edit etc.
  108. *
  109. * @params same as Html::link($title, $url, $options, $confirmMessage)
  110. * @return string Link
  111. */
  112. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  113. if (is_array($url)) {
  114. // Named are deprecated
  115. $url += $this->params['named'];
  116. // Add query strings
  117. if (!isset($url['?'])) {
  118. $url['?'] = array();
  119. }
  120. $url['?'] += $this->request->query;
  121. }
  122. return $this->link($title, $url, $options, $confirmMessage);
  123. }
  124. /**
  125. * Keep named and query params for pagination/filter after edit etc.
  126. *
  127. * @params same as Html::url($url, $options, $escape)
  128. * @return string Link
  129. */
  130. public function completeUrl($url = null, $full = false, $escape = true) {
  131. if (is_array($url)) {
  132. // Named are deprecated
  133. $url += $this->params['named'];
  134. // Add query strings
  135. if (!isset($url['?'])) {
  136. $url['?'] = array();
  137. }
  138. $url['?'] += $this->request->query;
  139. }
  140. return $this->url($url, $full, $escape);
  141. }
  142. /**
  143. * Convenience function for normal links.
  144. * Useful for layout links and links inside elements etc if you don't want to
  145. * verbosely reset all parts of it (prefix, plugin, ...).
  146. *
  147. * @params same as Html::link($title, $url, $options, $confirmMessage)
  148. * @return string HTML Link
  149. */
  150. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  151. if ($this->_linkDefaults === null) {
  152. if (!class_exists('CommonComponent')) {
  153. App::uses('CommonComponent', 'Tools.Controller/Component');
  154. }
  155. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  156. }
  157. if (!defined('PREFIX_ADMIN')) {
  158. define('PREFIX_ADMIN', 'admin');
  159. }
  160. if ($url !== null && is_array($url)) {
  161. $url = array_merge($this->_linkDefaults, $url);
  162. if (!empty($url[PREFIX_ADMIN])) {
  163. $options['rel'] = 'nofollow';
  164. }
  165. } elseif (is_array($title)) {
  166. $title = array_merge($this->_linkDefaults, $title);
  167. if (!empty($title[PREFIX_ADMIN])) {
  168. $options['rel'] = 'nofollow';
  169. }
  170. }
  171. return $this->link($title, $url, $options, $confirmMessage);
  172. }
  173. /**
  174. * Convenience function for normal urls.
  175. * Useful for layout links and links inside elements etc if you don't want to
  176. * verbosely reset all parts of it (prefix, plugin, ...).
  177. *
  178. * @params same as Html::url($url, $full)
  179. * @return string URL
  180. */
  181. public function defaultUrl($url = null, $full = false) {
  182. if ($this->_linkDefaults === null) {
  183. if (!class_exists('CommonComponent')) {
  184. App::uses('CommonComponent', 'Tools.Controller/Component');
  185. }
  186. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  187. }
  188. if ($url !== null && is_array($url)) {
  189. $url = array_merge($this->_linkDefaults, $url);
  190. }
  191. return $this->url($url, $full);
  192. }
  193. /**
  194. * Enhancement to htmlHelper which allows the crumbs protected array
  195. * to be cleared so that more than one set of crumbs can be generated in the same view.
  196. *
  197. * @return void
  198. */
  199. public function resetCrumbs() {
  200. $this->_crumbs = array();
  201. }
  202. }