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 http://opensource.org/licenses/mit-license.php MIT
  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. * Keep named and query params for pagination/filter after edit etc.
  91. *
  92. * @params same as Html::link($title, $url, $options, $confirmMessage)
  93. * @return string Link
  94. */
  95. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  96. if (is_array($url)) {
  97. // Named are deprecated
  98. $url += $this->params['named'];
  99. // Add query strings
  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. if (is_array($url)) {
  115. // Named are deprecated
  116. $url += $this->params['named'];
  117. // Add query strings
  118. if (!isset($url['?'])) {
  119. $url['?'] = array();
  120. }
  121. $url['?'] += $this->request->query;
  122. }
  123. return $this->url($url, $full, $escape);
  124. }
  125. /**
  126. * Convenience function for normal links.
  127. * Useful for layout links and links inside elements etc if you don't want to
  128. * verbosely reset all parts of it (prefix, plugin, ...).
  129. *
  130. * @params same as Html::link($title, $url, $options, $confirmMessage)
  131. * @return string HTML Link
  132. */
  133. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  134. if ($this->_linkDefaults === null) {
  135. if (!class_exists('CommonComponent')) {
  136. App::uses('CommonComponent', 'Tools.Controller/Component');
  137. }
  138. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  139. }
  140. if (!defined('PREFIX_ADMIN')) {
  141. define('PREFIX_ADMIN', 'admin');
  142. }
  143. if ($url !== null && is_array($url)) {
  144. $url = array_merge($this->_linkDefaults, $url);
  145. if (!empty($url[PREFIX_ADMIN])) {
  146. $options['rel'] = 'nofollow';
  147. }
  148. } elseif (is_array($title)) {
  149. $title = array_merge($this->_linkDefaults, $title);
  150. if (!empty($title[PREFIX_ADMIN])) {
  151. $options['rel'] = 'nofollow';
  152. }
  153. }
  154. return $this->link($title, $url, $options, $confirmMessage);
  155. }
  156. /**
  157. * Convenience function for normal urls.
  158. * Useful for layout links and links inside elements etc if you don't want to
  159. * verbosely reset all parts of it (prefix, plugin, ...).
  160. *
  161. * @params same as Html::url($url, $full)
  162. * @return string URL
  163. */
  164. public function defaultUrl($url = null, $full = false) {
  165. if ($this->_linkDefaults === null) {
  166. if (!class_exists('CommonComponent')) {
  167. App::uses('CommonComponent', 'Tools.Controller/Component');
  168. }
  169. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  170. }
  171. if ($url !== null && is_array($url)) {
  172. $url = array_merge($this->_linkDefaults, $url);
  173. }
  174. return $this->url($url, $full);
  175. }
  176. /**
  177. * Enhancement to htmlHelper which allows the crumbs protected array
  178. * to be cleared so that more than one set of crumbs can be generated in the same view.
  179. *
  180. * @return void
  181. */
  182. public function resetCrumbs() {
  183. $this->_crumbs = array();
  184. }
  185. }