MyHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. App::uses('Helper', 'View');
  3. class MyHelper extends Helper {
  4. public function __construct($View = null, $settings = array()) {
  5. if (class_exists('Packages')) {
  6. Packages::initialize($this, __CLASS__);
  7. }
  8. parent::__construct($View, $settings);
  9. }
  10. # deprecated in 2.0?
  11. public function initHelpers($additionalHelpers = array()) {
  12. if (!empty($this->helpers)) {
  13. $this->loadHelpers(array_merge($this->helpers, $additionalHelpers));
  14. }
  15. }
  16. /**
  17. * manually
  18. */
  19. public function loadHelpers($helpers = array(), $callbacks = false) {
  20. foreach ((array)$helpers as $helper => $config) {
  21. if (is_int($helper)) {
  22. $helper = $config;
  23. $config = array();
  24. }
  25. list($plugin, $helperName) = pluginSplit($helper);
  26. if (isset($this->{$helperName})) {
  27. continue;
  28. }
  29. App::import('Helper', $helper);
  30. $helperFullName = $helperName.'Helper';
  31. $this->{$helperName} = new $helperFullName($this->_View, (array)$config);
  32. if ($callbacks) {
  33. if (method_exists($helper, 'beforeRender')) {
  34. $this->{$helperName}->beforeRender();
  35. }
  36. }
  37. }
  38. }
  39. //TODO
  40. /**
  41. * problems: what if inside plugin webroot? not easy to do...
  42. */
  43. public function imageIfExists($path, $options = array(), $default = '---') {
  44. if (startsWith($path, '/')) {
  45. /*
  46. $completePath = Router::url($path);
  47. //echo (returns(file_exists($completePath)));
  48. //die($completePath);
  49. # problem with plugin files!!! needs "webroot" added after plugin name
  50. if (!file_exists($completePath)) {
  51. return $default;
  52. }
  53. */
  54. } else {
  55. $completePath = Router::url($path);
  56. }
  57. if (!empty($completePath) && !file_exists($completePath)) {
  58. return $default;
  59. }
  60. return $this->image($path, $options);
  61. }
  62. /**
  63. * display image tag from blob content
  64. * enhancement for HtmlHelper
  65. * @param binary $content
  66. * @param array $options
  67. * @return string $html imageTag
  68. * 2010-11-22 ms
  69. */
  70. public function imageFromBlob($content, $options = array()) {
  71. $text = 'data:image/png;base64,' . base64_encode($content);
  72. $image = sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
  73. return $image;
  74. }
  75. /**
  76. * HTML Helper extension for HTML5 time
  77. * The time element represents either a time on a 24 hour clock,
  78. * or a precise date in the proleptic Gregorian calendar,
  79. * optionally with a time and a time-zone offset.
  80. *
  81. * @param $content string
  82. * @param $options array
  83. * 'format' STRING: Use the specified TimeHelper method (or format()). FALSE: Generate the datetime. NULL: Do nothing.
  84. * 'datetime' STRING: If 'format' is STRING use as the formatting string. FALSE: Don't generate attribute
  85. *
  86. * //TODO: fixme
  87. * 2011-07-17 ms
  88. */
  89. public function time($content, $options = array()) {
  90. if (!isset($this->tags['time'])) {
  91. $this->tags['time'] = '<time%s>%s</time>';
  92. }
  93. $options = array_merge(array(
  94. 'datetime' => '%Y-%m-%d %T',
  95. 'pubdate' => false,
  96. 'format' => '%Y-%m-%d %T',
  97. ), $options);
  98. if ($options['format'] !== null) {
  99. App::import('helper', 'Time');
  100. $t = new TimeHelper(new View(null));
  101. }
  102. if ($options['format']) {
  103. if (method_exists($t, $options['format'])) {
  104. $content = $t->$options['format']($content);
  105. } else {
  106. $content = $t->i18nFormat($content, $options['format']);
  107. }
  108. $options['datetime'] = $t->i18nFormat(strtotime($content), $options['datetime']);
  109. } elseif ($options['format'] === false && $options['datetime']) {
  110. $options['datetime'] = $t->i18nFormat(strtotime($content), $options['datetime']);
  111. }
  112. if ($options['pubdate'])
  113. $pubdate = true;
  114. unset($options['format']);
  115. unset($options['pubdate']);
  116. $attributes = $this->_parseAttributes($options, array(0), ' ', '');
  117. if (isset($pubdate))
  118. $attributes .= ' pubdate';
  119. return sprintf($this->tags['time'], $attributes, $content);
  120. }
  121. # for convienience function Html::defaultLink()
  122. protected $linkDefaults = null;
  123. /*
  124. # only one prefix at a time
  125. public function url($url, $full = false) {
  126. if (is_array($url)) {
  127. $url['lang'] = 'deu';
  128. }
  129. return parent::url($url, $full);
  130. }
  131. */
  132. /**
  133. * convenience function for normal links
  134. * useful for layout links and links inside elements etc
  135. * @params same as Html::link($title, $url, $options, $confirmMessage)
  136. * 2010-01-23 ms
  137. */
  138. public function defaultLink($title, $url=null, $options=array(), $confirmMessage=false) {
  139. if ($this->linkDefaults === null) {
  140. if (!class_exists('CommonComponent')) {
  141. App::import('Component', 'Tools.Common');
  142. }
  143. $this->linkDefaults = CommonComponent::defaultUrlParams();
  144. }
  145. if (!defined('PREFIX_ADMIN')) {
  146. define('PREFIX_ADMIN', 'admin');
  147. }
  148. if ($url !== null && is_array($url)) {
  149. $url = array_merge($this->linkDefaults, $url);
  150. if (!empty($url[PREFIX_ADMIN])) {
  151. $options['rel'] = 'nofollow';
  152. }
  153. } elseif (is_array($title)) {
  154. $title = array_merge($this->linkDefaults, $title);
  155. if (!empty($title[PREFIX_ADMIN])) {
  156. $options['rel'] = 'nofollow';
  157. }
  158. }
  159. //$this->log($url, '404');
  160. return $this->link($title, $url, $options, $confirmMessage);
  161. }
  162. /**
  163. * convenience function for normal urls
  164. * useful for layout urls and urls inside elements etc
  165. * @params same as Html::url($url, $full)
  166. * 2010-01-23 ms
  167. */
  168. public function defaultUrl($url = null, $full = false) {
  169. if ($this->linkDefaults === null) {
  170. if (!class_exists('CommonComponent')) {
  171. App::import('Component', 'Tools.Common');
  172. }
  173. $this->linkDefaults = CommonComponent::defaultUrlParams();
  174. }
  175. if ($url !== null && is_array($url)) {
  176. $url = array_merge($this->linkDefaults, $url);
  177. }
  178. return $this->url($url, $full);
  179. }
  180. public $urlHere = null;
  181. /**
  182. * Small Helper Function to retrieve CORRECT $this->here (as it should be) - CAKE BUG !? -> this is a fix
  183. * 2009-01-06 ms
  184. */
  185. public function here() {
  186. if (empty($this->urlHere) && isset($_GET['url'])) {
  187. $this->urlHere = $_GET['url'];
  188. if (strpos($this->urlHere, '/') !== 0) {
  189. $this->urlHere = '/'.$this->urlHere;
  190. }
  191. }
  192. return $this->urlHere;
  193. }
  194. /**
  195. * enhancement to htmlHelper which allows the crumbs protected array
  196. * to be cleared so that more than one set of crumbs can be generated in the same view.
  197. *
  198. * @return void
  199. * 2009-08-05 ms
  200. */
  201. public function resetCrumbs() {
  202. $this->_crumbs = array();
  203. }
  204. /** deprecated */
  205. /**
  206. * @deprecated
  207. */
  208. public function nl2p($text, $options = array(), $enforceMaxLength = true) {
  209. $pS = $this->Html->tag('p', null, $options);
  210. $pE = '</p>';
  211. if (!empty($text)) {
  212. // Max length auto line break, if enabled
  213. if ($enforceMaxLength) {
  214. $maxLength = null;
  215. if (isset($options['maxLength'])) {
  216. $maxLength = (int)$options['maxLength'];
  217. }
  218. $text = $this->maxLength($text, $maxLength);
  219. }
  220. // Replace double newlines with <p>
  221. $text = $pS . preg_replace('#(\r?\n) {2,}(\s+)?#u', $pE . $pS, $text) . $pE;
  222. // Replace single newlines with <br>
  223. $text = preg_replace('#\r?\n#u', BR, $text);
  224. // Add newlines to sourcecode for sourcode readability
  225. $text = preg_replace(
  226. array(
  227. '#' . $pE . '#u', // Matches $pE (like </p>)
  228. '#' . BR . '#u', // Matches $br (like <br />)
  229. ),
  230. array(
  231. $pE . "\n",
  232. BR . "\n",
  233. ),
  234. $text);
  235. }
  236. return $text;
  237. }
  238. }