HtmlExtHelper.php 6.5 KB

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