MyHelper.php 8.5 KB

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