MyHelper.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. App::uses('Helper', 'View');
  3. App::uses('UrlCacheManager', 'Tools.Routing');
  4. /**
  5. * Helper enhancements for Cake2
  6. *
  7. * @author Mark Scherer
  8. * @license MIT
  9. * 2012-02-27 ms
  10. */
  11. class MyHelper extends Helper {
  12. public function __construct($View = null, $settings = array()) {
  13. if (class_exists('Packages')) {
  14. Packages::initialize($this, __CLASS__);
  15. }
  16. parent::__construct($View, $settings);
  17. }
  18. /**
  19. * Manually load helpers
  20. * @param array $helpers (either strings, or [string => array(config...)])
  21. * @param bool $callbacks - trigger missed callbacks
  22. * @return void
  23. */
  24. public function loadHelpers($helpers = array(), $callbacks = false) {
  25. foreach ((array)$helpers as $helper => $config) {
  26. if (is_int($helper)) {
  27. $helper = $config;
  28. $config = array();
  29. }
  30. list($plugin, $helperName) = pluginSplit($helper, true);
  31. if (isset($this->{$helperName})) {
  32. continue;
  33. }
  34. App::uses($helperName . 'Helper', $plugin . 'View/Helper');
  35. $helperFullName = $helperName . 'Helper';
  36. $this->{$helperName} = new $helperFullName($this->_View, (array)$config);
  37. if ($callbacks) {
  38. if (method_exists($helper, 'beforeRender')) {
  39. $this->{$helperName}->beforeRender();
  40. }
  41. }
  42. }
  43. }
  44. //TODO
  45. /**
  46. * problems: what if inside plugin webroot? not easy to do...
  47. */
  48. public function imageIfExists($path, $options = array(), $default = '---') {
  49. if (startsWith($path, '/')) {
  50. /*
  51. $completePath = Router::url($path);
  52. //echo (returns(file_exists($completePath)));
  53. //die($completePath);
  54. # problem with plugin files!!! needs "webroot" added after plugin name
  55. if (!file_exists($completePath)) {
  56. return $default;
  57. }
  58. */
  59. } else {
  60. $completePath = Router::url($path);
  61. }
  62. if (!empty($completePath) && !file_exists($completePath)) {
  63. return $default;
  64. }
  65. return $this->image($path, $options);
  66. }
  67. /**
  68. * display image tag from blob content
  69. * enhancement for HtmlHelper
  70. * @param binary $content
  71. * @param array $options
  72. * @return string $html imageTag
  73. * 2010-11-22 ms
  74. */
  75. public function imageFromBlob($content, $options = array()) {
  76. $text = 'data:image/png;base64,' . base64_encode($content);
  77. $image = sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
  78. return $image;
  79. }
  80. /**
  81. * HTML Helper extension for HTML5 time
  82. * The time element represents either a time on a 24 hour clock,
  83. * or a precise date in the proleptic Gregorian calendar,
  84. * optionally with a time and a time-zone offset.
  85. *
  86. * @param $content string
  87. * @param $options array
  88. * - 'format' STRING: Use the specified TimeHelper method (or format()). FALSE: Generate the datetime. NULL: Do nothing.
  89. * - 'datetime' STRING: If 'format' is STRING use as the formatting string. FALSE: Don't generate attribute
  90. *
  91. * //TODO: fixme
  92. * 2011-07-17 ms
  93. */
  94. public function time($content, $options = array()) {
  95. if (!isset($this->tags['time'])) {
  96. $this->tags['time'] = '<time%s>%s</time>';
  97. }
  98. $options = array_merge(array(
  99. 'datetime' => '%Y-%m-%d %T',
  100. 'pubdate' => false,
  101. 'format' => '%Y-%m-%d %T',
  102. ), $options);
  103. if ($options['format'] !== null) {
  104. App::uses('TimeHelper', 'View/Helper');
  105. $TimeHelper = new TimeHelper($this->_View);
  106. }
  107. if ($options['format']) {
  108. if (method_exists($t, $options['format'])) {
  109. $content = $TimeHelper->$options['format']($content);
  110. } else {
  111. $content = $TimeHelper->i18nFormat($content, $options['format']);
  112. }
  113. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  114. } elseif ($options['format'] === false && $options['datetime']) {
  115. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  116. }
  117. if ($options['pubdate']) {
  118. $pubdate = true;
  119. }
  120. unset($options['format']);
  121. unset($options['pubdate']);
  122. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  123. if (isset($pubdate)) {
  124. $attributes .= ' pubdate';
  125. }
  126. return sprintf($this->tags['time'], $attributes, $content);
  127. }
  128. /**
  129. * For convienience function Html::defaultLink().
  130. *
  131. * @var array
  132. */
  133. protected $_linkDefaults = null;
  134. /**
  135. * keep named and query params for pagination/filter after edit etc
  136. *
  137. * @params same as Html::link($title, $url, $options, $confirmMessage)
  138. * @return string Link
  139. * 2012-12-03 ms
  140. */
  141. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  142. if (is_array($url)) {
  143. $url += $this->params['named'];
  144. }
  145. return $this->link($title, $url, $options, $confirmMessage);
  146. }
  147. /**
  148. * keep named and query params for pagination/filter after edit etc
  149. *
  150. * @params same as Html::url($url, $options, $escape)
  151. * @return string Link
  152. * 2012-12-03 ms
  153. */
  154. public function completeUrl($url = null, $full = false, $escape = true) {
  155. if (is_array($url)) {
  156. $url += $this->params['named'];
  157. }
  158. return $this->url($url, $options, $escape);
  159. }
  160. /**
  161. * convenience function for normal links
  162. * useful for layout links and links inside elements etc
  163. *
  164. * @params same as Html::link($title, $url, $options, $confirmMessage)
  165. * @return string Link
  166. * 2010-01-23 ms
  167. */
  168. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  169. if ($this->_linkDefaults === null) {
  170. if (!class_exists('CommonComponent')) {
  171. App::uses('CommonComponent', 'Tools.Controller/Component');
  172. }
  173. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  174. }
  175. if (!defined('PREFIX_ADMIN')) {
  176. define('PREFIX_ADMIN', 'admin');
  177. }
  178. if ($url !== null && is_array($url)) {
  179. $url = array_merge($this->_linkDefaults, $url);
  180. if (!empty($url[PREFIX_ADMIN])) {
  181. $options['rel'] = 'nofollow';
  182. }
  183. } elseif (is_array($title)) {
  184. $title = array_merge($this->_linkDefaults, $title);
  185. if (!empty($title[PREFIX_ADMIN])) {
  186. $options['rel'] = 'nofollow';
  187. }
  188. }
  189. //$this->log($url, '404');
  190. return $this->link($title, $url, $options, $confirmMessage);
  191. }
  192. /**
  193. * convenience function for normal urls
  194. * useful for layout urls and urls inside elements etc
  195. * @params same as Html::url($url, $full)
  196. * 2010-01-23 ms
  197. */
  198. public function defaultUrl($url = null, $full = false) {
  199. if ($this->_linkDefaults === null) {
  200. if (!class_exists('CommonComponent')) {
  201. App::uses('CommonComponent', 'Tools.Controller/Component');
  202. }
  203. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  204. }
  205. if ($url !== null && is_array($url)) {
  206. $url = array_merge($this->_linkDefaults, $url);
  207. }
  208. return $this->url($url, $full);
  209. }
  210. public $urlHere = null;
  211. /**
  212. * enhancement to htmlHelper which allows the crumbs protected array
  213. * to be cleared so that more than one set of crumbs can be generated in the same view.
  214. *
  215. * @return void
  216. * 2009-08-05 ms
  217. */
  218. public function resetCrumbs() {
  219. $this->_crumbs = array();
  220. }
  221. /**
  222. * This function is responsible for setting up the Url cache before the application starts generating urls in views
  223. *
  224. * @return void
  225. */
  226. public function beforeRender($viewFile) {
  227. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
  228. return;
  229. }
  230. # todo: maybe lazy load with HtmlHelper::url()?
  231. UrlCacheManager::init($this->_View);
  232. Configure::write('UrlCache.runtime.beforeRender', true);
  233. }
  234. /**
  235. * This method will store the current generated urls into a persistent cache for next use
  236. *
  237. * @return void
  238. */
  239. public function afterLayout($layoutFile) {
  240. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  241. return;
  242. }
  243. UrlCacheManager::finalize();
  244. Configure::write('UrlCache.runtime.afterLayout', true);
  245. }
  246. /**
  247. * Intercepts the parent url function to first look if the cache was already generated for the same params
  248. *
  249. * @param mixed $url url to generate using cakephp array syntax
  250. * @param boolean $full wheter to generate a full url or not (http scheme)
  251. * @return string
  252. * @see Helper::url()
  253. */
  254. public function url($url = null, $full = false, $escape = true) {
  255. if (Configure::read('UrlCache.active')) {
  256. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  257. return $cachedUrl;
  258. }
  259. }
  260. $routerUrl = parent::url($url, $full, $escape);
  261. if (Configure::read('UrlCache.active')) {
  262. UrlCacheManager::set($routerUrl);
  263. }
  264. return $routerUrl;
  265. }
  266. }