MyHelper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. # for convienience function Html::defaultLink()
  129. protected $linkDefaults = null;
  130. /*
  131. # only one prefix at a time
  132. public function url($url, $full = false) {
  133. if (is_array($url)) {
  134. $url['lang'] = 'deu';
  135. }
  136. return parent::url($url, $full);
  137. }
  138. */
  139. /**
  140. * keep named and query params for pagination/filter after edit etc
  141. *
  142. * @params same as Html::link($title, $url, $options, $confirmMessage)
  143. * @return string Link
  144. * 2012-12-03 ms
  145. */
  146. public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
  147. if (is_array($url)) {
  148. $url += $this->params['named'];
  149. }
  150. return $this->link($title, $url, $options, $confirmMessage);
  151. }
  152. /**
  153. * keep named and query params for pagination/filter after edit etc
  154. *
  155. * @params same as Html::url($url, $options, $escape)
  156. * @return string Link
  157. * 2012-12-03 ms
  158. */
  159. public function completeUrl($url = null, $full = false, $escape = true) {
  160. if (is_array($url)) {
  161. $url += $this->params['named'];
  162. }
  163. return $this->url($url, $options, $escape);
  164. }
  165. /**
  166. * convenience function for normal links
  167. * useful for layout links and links inside elements etc
  168. *
  169. * @params same as Html::link($title, $url, $options, $confirmMessage)
  170. * @return string Link
  171. * 2010-01-23 ms
  172. */
  173. public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
  174. if ($this->linkDefaults === null) {
  175. if (!class_exists('CommonComponent')) {
  176. App::uses('CommonComponent', 'Tools.Controller/Component');
  177. }
  178. $this->linkDefaults = CommonComponent::defaultUrlParams();
  179. }
  180. if (!defined('PREFIX_ADMIN')) {
  181. define('PREFIX_ADMIN', 'admin');
  182. }
  183. if ($url !== null && is_array($url)) {
  184. $url = array_merge($this->linkDefaults, $url);
  185. if (!empty($url[PREFIX_ADMIN])) {
  186. $options['rel'] = 'nofollow';
  187. }
  188. } elseif (is_array($title)) {
  189. $title = array_merge($this->linkDefaults, $title);
  190. if (!empty($title[PREFIX_ADMIN])) {
  191. $options['rel'] = 'nofollow';
  192. }
  193. }
  194. //$this->log($url, '404');
  195. return $this->link($title, $url, $options, $confirmMessage);
  196. }
  197. /**
  198. * convenience function for normal urls
  199. * useful for layout urls and urls inside elements etc
  200. * @params same as Html::url($url, $full)
  201. * 2010-01-23 ms
  202. */
  203. public function defaultUrl($url = null, $full = false) {
  204. if ($this->linkDefaults === null) {
  205. if (!class_exists('CommonComponent')) {
  206. App::uses('CommonComponent', 'Tools.Controller/Component');
  207. }
  208. $this->linkDefaults = CommonComponent::defaultUrlParams();
  209. }
  210. if ($url !== null && is_array($url)) {
  211. $url = array_merge($this->linkDefaults, $url);
  212. }
  213. return $this->url($url, $full);
  214. }
  215. public $urlHere = null;
  216. /**
  217. * enhancement to htmlHelper which allows the crumbs protected array
  218. * to be cleared so that more than one set of crumbs can be generated in the same view.
  219. *
  220. * @return void
  221. * 2009-08-05 ms
  222. */
  223. public function resetCrumbs() {
  224. $this->_crumbs = array();
  225. }
  226. /**
  227. * This function is responsible for setting up the Url cache before the application starts generating urls in views
  228. *
  229. * @return void
  230. */
  231. public function beforeRender($viewFile) {
  232. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
  233. return;
  234. }
  235. # todo: maybe lazy load with HtmlHelper::url()?
  236. UrlCacheManager::init($this->_View);
  237. Configure::write('UrlCache.runtime.beforeRender', true);
  238. }
  239. /**
  240. * This method will store the current generated urls into a persistent cache for next use
  241. *
  242. * @return void
  243. */
  244. public function afterLayout($layoutFile) {
  245. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  246. return;
  247. }
  248. UrlCacheManager::finalize();
  249. Configure::write('UrlCache.runtime.afterLayout', true);
  250. }
  251. /**
  252. * Intercepts the parent url function to first look if the cache was already generated for the same params
  253. *
  254. * @param mixed $url url to generate using cakephp array syntax
  255. * @param boolean $full wheter to generate a full url or not (http scheme)
  256. * @return string
  257. * @see Helper::url()
  258. */
  259. public function url($url = null, $full = false, $escape = true) {
  260. if (Configure::read('UrlCache.active')) {
  261. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  262. return $cachedUrl;
  263. }
  264. }
  265. $routerUrl = parent::url($url, $full, $escape);
  266. if (Configure::read('UrlCache.active')) {
  267. UrlCacheManager::set($routerUrl);
  268. }
  269. return $routerUrl;
  270. }
  271. }