MyHelper.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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::uses('TimeHelper', 'View/Helper');
  102. $TimeHelper = new TimeHelper($this->_View);
  103. }
  104. if ($options['format']) {
  105. if (method_exists($t, $options['format'])) {
  106. $content = $TimeHelper->$options['format']($content);
  107. } else {
  108. $content = $TimeHelper->i18nFormat($content, $options['format']);
  109. }
  110. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  111. } elseif ($options['format'] === false && $options['datetime']) {
  112. $options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
  113. }
  114. if ($options['pubdate']) {
  115. $pubdate = true;
  116. }
  117. unset($options['format']);
  118. unset($options['pubdate']);
  119. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  120. if (isset($pubdate)) {
  121. $attributes .= ' pubdate';
  122. }
  123. return sprintf($this->tags['time'], $attributes, $content);
  124. }
  125. # for convienience function Html::defaultLink()
  126. protected $linkDefaults = null;
  127. /*
  128. # only one prefix at a time
  129. public function url($url, $full = false) {
  130. if (is_array($url)) {
  131. $url['lang'] = 'deu';
  132. }
  133. return parent::url($url, $full);
  134. }
  135. */
  136. /**
  137. * convenience function for normal links
  138. * useful for layout links and links inside elements etc
  139. * @params same as Html::link($title, $url, $options, $confirmMessage)
  140. * 2010-01-23 ms
  141. */
  142. public function defaultLink($title, $url=null, $options=array(), $confirmMessage=false) {
  143. if ($this->linkDefaults === null) {
  144. if (!class_exists('CommonComponent')) {
  145. App::import('Component', 'Tools.Common');
  146. }
  147. $this->linkDefaults = CommonComponent::defaultUrlParams();
  148. }
  149. if (!defined('PREFIX_ADMIN')) {
  150. define('PREFIX_ADMIN', 'admin');
  151. }
  152. if ($url !== null && is_array($url)) {
  153. $url = array_merge($this->linkDefaults, $url);
  154. if (!empty($url[PREFIX_ADMIN])) {
  155. $options['rel'] = 'nofollow';
  156. }
  157. } elseif (is_array($title)) {
  158. $title = array_merge($this->linkDefaults, $title);
  159. if (!empty($title[PREFIX_ADMIN])) {
  160. $options['rel'] = 'nofollow';
  161. }
  162. }
  163. //$this->log($url, '404');
  164. return $this->link($title, $url, $options, $confirmMessage);
  165. }
  166. /**
  167. * convenience function for normal urls
  168. * useful for layout urls and urls inside elements etc
  169. * @params same as Html::url($url, $full)
  170. * 2010-01-23 ms
  171. */
  172. public function defaultUrl($url = null, $full = false) {
  173. if ($this->linkDefaults === null) {
  174. if (!class_exists('CommonComponent')) {
  175. App::import('Component', 'Tools.Common');
  176. }
  177. $this->linkDefaults = CommonComponent::defaultUrlParams();
  178. }
  179. if ($url !== null && is_array($url)) {
  180. $url = array_merge($this->linkDefaults, $url);
  181. }
  182. return $this->url($url, $full);
  183. }
  184. public $urlHere = null;
  185. /**
  186. * Small Helper Function to retrieve CORRECT $this->here (as it should be) - CAKE BUG !? -> this is a fix
  187. * 2009-01-06 ms
  188. */
  189. public function here() {
  190. if (empty($this->urlHere) && isset($_GET['url'])) {
  191. $this->urlHere = $_GET['url'];
  192. if (strpos($this->urlHere, '/') !== 0) {
  193. $this->urlHere = '/'.$this->urlHere;
  194. }
  195. }
  196. return $this->urlHere;
  197. }
  198. /**
  199. * enhancement to htmlHelper which allows the crumbs protected array
  200. * to be cleared so that more than one set of crumbs can be generated in the same view.
  201. *
  202. * @return void
  203. * 2009-08-05 ms
  204. */
  205. public function resetCrumbs() {
  206. $this->_crumbs = array();
  207. }
  208. /** deprecated */
  209. /**
  210. * @deprecated
  211. */
  212. public function nl2p($text, $options = array(), $enforceMaxLength = true) {
  213. $pS = $this->Html->tag('p', null, $options);
  214. $pE = '</p>';
  215. if (!empty($text)) {
  216. // Max length auto line break, if enabled
  217. if ($enforceMaxLength) {
  218. $maxLength = null;
  219. if (isset($options['maxLength'])) {
  220. $maxLength = (int)$options['maxLength'];
  221. }
  222. $text = $this->maxLength($text, $maxLength);
  223. }
  224. // Replace double newlines with <p>
  225. $text = $pS . preg_replace('#(\r?\n) {2,}(\s+)?#u', $pE . $pS, $text) . $pE;
  226. // Replace single newlines with <br>
  227. $text = preg_replace('#\r?\n#u', BR, $text);
  228. // Add newlines to sourcecode for sourcode readability
  229. $text = preg_replace(
  230. array(
  231. '#' . $pE . '#u', // Matches $pE (like </p>)
  232. '#' . BR . '#u', // Matches $br (like <br />)
  233. ),
  234. array(
  235. $pE . "\n",
  236. BR . "\n",
  237. ),
  238. $text);
  239. }
  240. return $text;
  241. }
  242. /**
  243. * This function is responsible for setting up the Url cache before the application starts generating urls in views
  244. *
  245. * @return void
  246. */
  247. public function beforeRender($viewFile) {
  248. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
  249. return;
  250. }
  251. # todo: maybe lazy load with HtmlHelper::url()?
  252. UrlCacheManager::init($this->_View);
  253. Configure::write('UrlCache.runtime.beforeRender', true);
  254. }
  255. /**
  256. * This method will store the current generated urls into a persistent cache for next use
  257. *
  258. * @return void
  259. */
  260. public function afterLayout($layoutFile = null) {
  261. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  262. return;
  263. }
  264. UrlCacheManager::finalize();
  265. Configure::write('UrlCache.runtime.afterLayout', true);
  266. }
  267. /**
  268. * Intercepts the parent url function to first look if the cache was already generated for the same params
  269. *
  270. * @param mixed $url url to generate using cakephp array syntax
  271. * @param boolean $full wheter to generate a full url or not (http scheme)
  272. * @return string
  273. * @see Helper::url()
  274. */
  275. public function url($url = null, $full = false, $escape = true) {
  276. if (Configure::read('UrlCache.active')) {
  277. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  278. return $cachedUrl;
  279. }
  280. }
  281. $routerUrl = parent::url($url, $full, $escape);
  282. if (Configure::read('UrlCache.active')) {
  283. UrlCacheManager::set($routerUrl);
  284. }
  285. return $routerUrl;
  286. }
  287. }