MyHelper.php 8.5 KB

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