HtmlExtHelper.php 5.8 KB

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