MyHelper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. App::uses('Helper', 'View');
  3. App::uses('Router', 'Routing');
  4. App::uses('UrlCacheManager', 'Tools.Routing');
  5. /**
  6. * Helper enhancements for Cake2
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  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 boolean $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. */
  74. public function imageFromBlob($content, $options = array()) {
  75. $text = 'data:image/png;base64,' . base64_encode($content);
  76. $image = sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
  77. return $image;
  78. }
  79. /**
  80. * HTML Helper extension for HTML5 time
  81. * The time element represents either a time on a 24 hour clock,
  82. * or a precise date in the proleptic Gregorian calendar,
  83. * optionally with a time and a time-zone offset.
  84. *
  85. * @param $content string
  86. * @param $options array
  87. * - 'format' STRING: Use the specified TimeHelper method (or format()). FALSE: Generate the datetime. NULL: Do nothing.
  88. * - 'datetime' STRING: If 'format' is STRING use as the formatting string. FALSE: Don't generate attribute
  89. *
  90. * //TODO: fixme
  91. */
  92. public function time($content, $options = array()) {
  93. if (!isset($this->tags['time'])) {
  94. $this->tags['time'] = '<time%s>%s</time>';
  95. }
  96. $options = array_merge(array(
  97. 'datetime' => '%Y-%m-%d %T',
  98. 'pubdate' => false,
  99. 'format' => '%Y-%m-%d %T',
  100. ), $options);
  101. if ($options['format'] !== null) {
  102. App::uses('TimeHelper', 'View/Helper');
  103. $TimeHelper = new TimeHelper($this->_View);
  104. }
  105. if ($options['format']) {
  106. if (method_exists($t, $options['format'])) {
  107. $content = $TimeHelper->$options['format']($content);
  108. } else {
  109. $content = $TimeHelper->i18nFormat($content, $options['format']);
  110. }
  111. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  112. } elseif ($options['format'] === false && $options['datetime']) {
  113. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  114. }
  115. if ($options['pubdate']) {
  116. $pubdate = true;
  117. }
  118. unset($options['format']);
  119. unset($options['pubdate']);
  120. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  121. if (isset($pubdate)) {
  122. $attributes .= ' pubdate';
  123. }
  124. return sprintf($this->tags['time'], $attributes, $content);
  125. }
  126. /**
  127. * For convienience function Html::defaultLink().
  128. *
  129. * @var array
  130. */
  131. protected $_linkDefaults = null;
  132. /**
  133. * Keep named and query params for pagination/filter after edit etc
  134. *
  135. * @params same as Html::link($title, $url, $options, $confirmMessage)
  136. * @return string Link
  137. */
  138. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  139. if (is_array($url)) {
  140. $url += $this->params['named'];
  141. }
  142. return $this->link($title, $url, $options, $confirmMessage);
  143. }
  144. /**
  145. * Keep named and query params for pagination/filter after edit etc
  146. *
  147. * @params same as Html::url($url, $options, $escape)
  148. * @return string Link
  149. */
  150. public function completeUrl($url = null, $full = false, $escape = true) {
  151. if (is_array($url)) {
  152. $url += $this->params['named'];
  153. }
  154. return $this->url($url, $options, $escape);
  155. }
  156. /**
  157. * Convenience function for normal links
  158. * useful for layout links and links inside elements etc
  159. *
  160. * @params same as Html::link($title, $url, $options, $confirmMessage)
  161. * @return string Link
  162. */
  163. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  164. if ($this->_linkDefaults === null) {
  165. if (!class_exists('CommonComponent')) {
  166. App::uses('CommonComponent', 'Tools.Controller/Component');
  167. }
  168. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  169. }
  170. if (!defined('PREFIX_ADMIN')) {
  171. define('PREFIX_ADMIN', 'admin');
  172. }
  173. if ($url !== null && is_array($url)) {
  174. $url = array_merge($this->_linkDefaults, $url);
  175. if (!empty($url[PREFIX_ADMIN])) {
  176. $options['rel'] = 'nofollow';
  177. }
  178. } elseif (is_array($title)) {
  179. $title = array_merge($this->_linkDefaults, $title);
  180. if (!empty($title[PREFIX_ADMIN])) {
  181. $options['rel'] = 'nofollow';
  182. }
  183. }
  184. //$this->log($url, '404');
  185. return $this->link($title, $url, $options, $confirmMessage);
  186. }
  187. /**
  188. * Convenience function for normal urls
  189. * useful for layout urls and urls inside elements etc
  190. * @params same as Html::url($url, $full)
  191. */
  192. public function defaultUrl($url = null, $full = false) {
  193. if ($this->_linkDefaults === null) {
  194. if (!class_exists('CommonComponent')) {
  195. App::uses('CommonComponent', 'Tools.Controller/Component');
  196. }
  197. $this->_linkDefaults = CommonComponent::defaultUrlParams();
  198. }
  199. if ($url !== null && is_array($url)) {
  200. $url = array_merge($this->_linkDefaults, $url);
  201. }
  202. return $this->url($url, $full);
  203. }
  204. public $urlHere = null;
  205. /**
  206. * Enhancement to htmlHelper which allows the crumbs protected array
  207. * to be cleared so that more than one set of crumbs can be generated in the same view.
  208. *
  209. * @return void
  210. */
  211. public function resetCrumbs() {
  212. $this->_crumbs = array();
  213. }
  214. /**
  215. * This function is responsible for setting up the Url cache before the application starts generating urls in views
  216. *
  217. * @return void
  218. */
  219. public function beforeRender($viewFile) {
  220. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
  221. return;
  222. }
  223. # todo: maybe lazy load with HtmlHelper::url()?
  224. UrlCacheManager::init($this->_View);
  225. Configure::write('UrlCache.runtime.beforeRender', true);
  226. }
  227. /**
  228. * This method will store the current generated urls into a persistent cache for next use
  229. *
  230. * @return void
  231. */
  232. public function afterLayout($layoutFile) {
  233. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  234. return;
  235. }
  236. UrlCacheManager::finalize();
  237. Configure::write('UrlCache.runtime.afterLayout', true);
  238. }
  239. /**
  240. * Intercepts the parent url function to first look if the cache was already generated for the same params
  241. *
  242. * @param mixed $url url to generate using cakephp array syntax
  243. * @param boolean|array $full whether to generate a full url or not (http scheme). As array: full, escape.
  244. * @return string
  245. * @see Helper::url()
  246. */
  247. public function url($url = null, $full = false) {
  248. if (is_array($full)) {
  249. $escape = isset($full['ecape']) ? $full['escape'] : true;
  250. $full = isset($full['full']) ? $full['full'] : false;
  251. } else {
  252. $escape = true;
  253. }
  254. if (Configure::read('UrlCache.active')) {
  255. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  256. return $cachedUrl;
  257. }
  258. }
  259. if (!$escape) {
  260. $routerUrl = Router::url($url, $full);
  261. } else {
  262. $routerUrl = parent::url($url, $full);
  263. }
  264. if (Configure::read('UrlCache.active')) {
  265. UrlCacheManager::set($routerUrl);
  266. }
  267. return $routerUrl;
  268. }
  269. }