CommonHelper.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * All site-wide necessary stuff for the view layer
  5. */
  6. class CommonHelper extends AppHelper {
  7. public $helpers = array ('Session', 'Html');
  8. public $packages = array(
  9. 'Tools.Jquery' //Used by showDebug
  10. );
  11. /*** Layout Stuff ***/
  12. /**
  13. * Convenience function for clean ROBOTS allowance
  14. *
  15. * @param string $type - private/public
  16. * @return string HTML
  17. * 2008-12-08 ms
  18. */
  19. public function metaRobots($type = null) {
  20. if ($type === null && ($meta = Configure::read('Config.robots')) !== null) {
  21. $type = $meta;
  22. }
  23. $content = array();
  24. if ($type === 'public') {
  25. $this->privatePage = false;
  26. $content['robots']= array('index', 'follow', 'noarchive');
  27. } else {
  28. $this->privatePage = true;
  29. $content['robots']= array('noindex', 'nofollow', 'noarchive');
  30. }
  31. $return = '<meta name="robots" content="' . implode(',', $content['robots']) . '" />';
  32. return $return;
  33. }
  34. /**
  35. * convinience function for clean meta name tags
  36. * @param STRING $name: author, date, generator, revisit-after, language
  37. * @param MIXED $content: if array, it will be seperated by commas
  38. * @return string $htmlMarkup
  39. * 2009-07-07 ms
  40. */
  41. public function metaName($name = null, $content = null) {
  42. if (empty($name) || empty($content)) {
  43. return '';
  44. }
  45. if (!is_array($content)) {
  46. $content = (array)$content;
  47. }
  48. $return = '<meta name="' . $name . '" content="' . implode(', ', $content) . '" />';
  49. return $return;
  50. }
  51. /**
  52. * @param string $content
  53. * @param string $language (iso2: de, en-us, ...)
  54. * @param array $additionalOptions
  55. * @return string $htmlMarkup
  56. */
  57. public function metaDescription($content, $language = null, $options = array()) {
  58. if (!empty($language)) {
  59. $options['lang'] = mb_strtolower($language);
  60. } elseif ($language !== false) {
  61. $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
  62. }
  63. return $this->Html->meta('description', $content, $options);
  64. }
  65. /**
  66. * @param string|array $keywords
  67. * @param string $language (iso2: de, en-us, ...)
  68. * @param bool $escape
  69. * @return string $htmlMarkup
  70. */
  71. public function metaKeywords($keywords = null, $language = null, $escape = true) {
  72. if ($keywords === null) {
  73. $keywords = Configure::read('Config.keywords');
  74. }
  75. if (is_array($keywords)) {
  76. $keywords = implode(', ', $keywords);
  77. }
  78. if ($escape) {
  79. $keywords = h($keywords);
  80. }
  81. if (!empty($language)) {
  82. $options['lang'] = mb_strtolower($language);
  83. } elseif ($language !== false) {
  84. $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
  85. }
  86. return $this->Html->meta('keywords', $keywords, $options);
  87. }
  88. /**
  89. * convinience function for "canonical" SEO links
  90. *
  91. * @return string $htmlMarkup
  92. * 2010-03-03 ms
  93. */
  94. public function metaCanonical($url = null, $full = false) {
  95. $canonical = $this->Html->url($url, $full);
  96. //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
  97. return '<link rel="canonical" href="' . $canonical . '" />';
  98. }
  99. /**
  100. * convinience function for "alternate" SEO links
  101. * @param mixed $url
  102. * @param mixed $lang (lang(iso2) or array of langs)
  103. * lang: language (in ISO 6391-1 format) + optionally the region (in ISO 3166-1 Alpha 2 format)
  104. * - de
  105. * - de-ch
  106. * etc
  107. * @return string $htmlMarkup
  108. * 2011-12-12 ms
  109. */
  110. public function metaAlternate($url, $lang, $full = false) {
  111. $canonical = $this->Html->url($url, $full);
  112. //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
  113. $lang = (array)$lang;
  114. $res = array();
  115. foreach ($lang as $language => $countries) {
  116. if (is_numeric($language)) {
  117. $language = '';
  118. } else {
  119. $language .= '-';
  120. }
  121. $countries = (array)$countries;
  122. foreach ($countries as $country) {
  123. $l = $language . $country;
  124. $options = array('rel' => 'alternate', 'hreflang' => $l, 'type' => null, 'title' => null);
  125. $res[] = $this->Html->meta('alternate', $url, $options).PHP_EOL;
  126. }
  127. }
  128. return implode('', $res);
  129. }
  130. /**
  131. * convinience function for META Tags
  132. * @param STRING type
  133. * @param STRING content
  134. * @return string $htmlMarkup
  135. * 2008-12-08 ms
  136. */
  137. public function metaRss($url = null, $title = null) {
  138. $tags = array(
  139. 'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
  140. );
  141. $content = array();
  142. if (empty($url)) { return ''; }
  143. if (empty($title)) {
  144. $title = 'Diesen Feed abonnieren';
  145. }
  146. return sprintf($tags['meta'], $title, $this->url($url));
  147. }
  148. /**
  149. * convinience function for META Tags
  150. * @param STRING type
  151. * @param STRING content
  152. * @return string $htmlMarkup
  153. * 2008-12-08 ms
  154. */
  155. public function metaEquiv($type = null, $value = null, $escape = true) {
  156. $tags = array(
  157. 'meta' => '<meta http-equiv="%s"%s />',
  158. );
  159. if (empty($value)) {
  160. return '';
  161. }
  162. if ($escape) {
  163. $value = h($value);
  164. }
  165. return sprintf($tags['meta'], $type, ' content="'.$value.'"');
  166. }
  167. /**
  168. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  169. * => x is in webroot/
  170. * => y is in plugins/tools/webroot/
  171. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  172. * @return string $htmlMarkup
  173. * 2011-03-23 ms
  174. */
  175. public function css($files = array(), $rel = null, $options = array()) {
  176. $files = (array)$files;
  177. $pieces = array();
  178. foreach ($files as $file) {
  179. $pieces[] = 'file='.$file;
  180. }
  181. if ($v = Configure::read('Config.layout_v')) {
  182. $pieces[] = 'v='.$v;
  183. }
  184. $string = implode('&', $pieces);
  185. return $this->Html->css('/css.php?'.$string, $rel, $options);
  186. }
  187. /**
  188. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  189. * => x is in webroot/
  190. * => y is in plugins/tools/webroot/
  191. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  192. * @return string $htmlMarkup
  193. * 2011-03-23 ms
  194. */
  195. public function script($files = array(), $options = array()) {
  196. $files = (array)$files;
  197. foreach ($files as $file) {
  198. $pieces[] = 'file='.$file;
  199. }
  200. if ($v = Configure::read('Config.layout_v')) {
  201. $pieces[] = 'v='.$v;
  202. }
  203. $string = implode('&', $pieces);
  204. return $this->Html->script('/js.php?'.$string, $options);
  205. }
  206. /**
  207. * special css tag generator with the option to add '?...' to the link (for caching prevention)
  208. * IN USAGE
  209. * needs manual adjustment, but still better than the core one!
  210. * @example needs Asset.cssversion => xyz (going up with counter)
  211. * @return string $htmlMarkup
  212. * 2008-12-08 ms
  213. */
  214. public function cssDyn($path, $rel = null, $htmlAttributes = array(), $return = true) {
  215. $v = (int)Configure::read('Asset.version');
  216. return $this->Html->css($path.'.css?'.$v, $rel, $htmlAttributes, $return);
  217. }
  218. /**
  219. * NOT IN USAGE
  220. * but better than the core one!
  221. * @example needs Asset.timestamp => force
  222. * @return string $htmlMarkup
  223. * 2008-12-08 ms
  224. */
  225. public function cssAuto($path, $rel = null, $htmlAttributes = array(), $return = true) {
  226. define('COMPRESS_CSS',true);
  227. $time = date('YmdHis', filemtime(APP . 'webroot' . DS . CSS_URL . $path . '.css'));
  228. $url = "{$this->request->webroot}" . (COMPRESS_CSS ? 'c' : '') . CSS_URL . $this->themeWeb . $path . ".css?" . $time;
  229. return $url;
  230. }
  231. /*** Content Stuff ***/
  232. /**
  233. * still necessary?
  234. * @param array $fields
  235. * @return string $html
  236. */
  237. public function displayErrors($fields = array()) {
  238. $res = '';
  239. if (!empty($this->validationErrors)) {
  240. if ($fields === null) { # catch ALL
  241. foreach ($this->validationErrors as $alias => $error) {
  242. list($alias, $fieldname) = explode('.', $error);
  243. $this->validationErrors[$alias][$fieldname];
  244. }
  245. } elseif (!empty($fields)) {
  246. foreach ($fields as $field) {
  247. list($alias, $fieldname) = explode('.', $field);
  248. if (!empty($this->validationErrors[$alias][$fieldname])) {
  249. $res .= $this->_renderError($this->validationErrors[$alias][$fieldname]);
  250. }
  251. }
  252. }
  253. /*
  254. if (!empty($catched)) {
  255. foreach ($catched as $c) {
  256. }
  257. }
  258. */
  259. }
  260. return $res;
  261. }
  262. protected function _renderError($error, $escape = true) {
  263. if ($escape !== false) {
  264. $error = h($error);
  265. }
  266. return '<div class="error-message">'.$error.'</div>';
  267. }
  268. /**
  269. * Alternates between two or more strings.
  270. *
  271. * echo CommonHelper::alternate('one', 'two'); // "one"
  272. * echo CommonHelper::alternate('one', 'two'); // "two"
  273. * echo CommonHelper::alternate('one', 'two'); // "one"
  274. *
  275. * Note that using multiple iterations of different strings may produce
  276. * unexpected results.
  277. * TODO: move to booststrap!!!
  278. *
  279. * @param string strings to alternate between
  280. * @return string
  281. */
  282. public static function alternate() {
  283. static $i;
  284. if (func_num_args() === 0) {
  285. $i = 0;
  286. return '';
  287. }
  288. $args = func_get_args();
  289. return $args[($i++ % count($args))];
  290. }
  291. /**
  292. * check if session works due to allowed cookies
  293. *
  294. * 2009-06-29 ms
  295. */
  296. public function sessionCheck() {
  297. return !CommonComponent::cookiesDisabled();
  298. /*
  299. if (!empty($_COOKIE) && !empty($_COOKIE[Configure::read('Session.cookie')])) {
  300. return true;
  301. }
  302. return false;
  303. */
  304. }
  305. /**
  306. * display warning if cookies are disallowed (and session won't work)
  307. * 2009-06-29 ms
  308. */
  309. public function sessionCheckAlert() {
  310. if (!$this->sessionCheck()) {
  311. return '<div class="cookieWarning">'.__('Please enable cookies').'</div>';
  312. }
  313. return '';
  314. }
  315. /**
  316. * //TODO: move boostrap
  317. * auto-pluralizing a word using the Inflection class
  318. * @param string $s = the string to be pl.
  319. * @param int $c = the count
  320. * @return $string "member" or "members" OR "Mitglied"/"Mitglieder" if autoTranslate TRUE
  321. * 2009-07-23 ms
  322. */
  323. public function asp($s, $c, $autoTranslate = false) {
  324. if ((int)$c !== 1) {
  325. $p = Inflector::pluralize($s);
  326. } else {
  327. $p = null; # no pluralization necessary
  328. }
  329. return $this->sp($s, $p, $c, $autoTranslate);
  330. }
  331. /**
  332. * //TODO: move boostrap
  333. * manual pluralizing a word using the Inflection class
  334. *
  335. * @param string $singular
  336. * @param string $plural
  337. * @param int $count
  338. * @return string $result
  339. * 2009-07-23 ms
  340. */
  341. public function sp($s, $p, $c, $autoTranslate = false) {
  342. if ((int)$c !== 1) {
  343. $ret = $p;
  344. } else {
  345. $ret = $s;
  346. }
  347. if ($autoTranslate) {
  348. $ret = __($ret);
  349. }
  350. return $ret;
  351. }
  352. /**
  353. * Show FlashMessages
  354. * @param boolean unsorted true/false [default:FALSE = sorted by priority]
  355. * TODO: export div wrapping method (for static messaging on a page)
  356. * TODO: sorting
  357. * 2010-11-22 ms
  358. */
  359. public function flash($unsorted = false, $backwardsComp = true) {
  360. // Get the messages from the session
  361. $messages = (array)$this->Session->read('messages');
  362. $cMessages = (array)Configure::read('messages');
  363. if (!empty($cMessages)) {
  364. $messages = (array)Set::merge($messages, $cMessages);
  365. }
  366. $html='';
  367. if (!empty($messages)) {
  368. $html = '<div class="flashMessages">';
  369. if ($unsorted !== true) {
  370. // Add a div for each message using the type as the class
  371. foreach ($messages as $type => $msgs) {
  372. foreach ((array)$msgs as $msg) {
  373. $html .= $this->_message($msg, $type);
  374. }
  375. }
  376. } else {
  377. foreach ($messages as $type) {
  378. //
  379. }
  380. }
  381. $html .= '</div>';
  382. if (method_exists($this->Session, 'delete')) {
  383. $this->Session->delete('messages');
  384. } else {
  385. CakeSession::delete('messages');
  386. }
  387. }
  388. return $html;
  389. }
  390. /**
  391. * output a single flashMessage
  392. * 2010-11-22 ms
  393. */
  394. public function flashMessage($msg, $type = 'info', $escape = true) {
  395. $html = '<div class="flashMessages">';
  396. if ($escape) {
  397. $msg = h($msg);
  398. }
  399. $html .= $this->_message($msg, $type);
  400. $html .= '</div>';
  401. return $html;
  402. }
  403. protected function _message($msg, $type) {
  404. if (!empty($msg)) {
  405. return '<div class="message'.(!empty($type) ? ' '.$type : '').'">'.$msg.'</div>';
  406. }
  407. return '';
  408. }
  409. /**
  410. * add a message on the fly
  411. *
  412. * @param string $msg
  413. * @param string $class
  414. * @return bool $success
  415. * 2011-05-25 ms
  416. */
  417. public function transientFlashMessage($msg, $class = null) {
  418. return CommonComponent::transientFlashMessage($msg, $class);
  419. }
  420. /**
  421. * TODO: move into TextExt?
  422. * escape text with some more automagic
  423. *
  424. * @param string $text
  425. * @param array $options
  426. * @return string $processedText
  427. * - nl2br: true/false (defaults to true)
  428. * - escape: false prevents h() and space transformation (defaults to true)
  429. * - tabsToSpaces: int (defaults to 4)
  430. * 2010-11-20 ms
  431. */
  432. public function esc($text, $options = array()) {
  433. if (!isset($options['escape']) || $options['escape'] !== false) {
  434. //$text = str_replace(' ', '&nbsp;', h($text));
  435. $text = h($text);
  436. # try to fix indends made out of spaces
  437. $text = explode(NL, $text);
  438. foreach ($text as $key => $t) {
  439. $i = 0;
  440. while (!empty($t[$i]) && $t[$i] === ' ') {
  441. $i++;
  442. }
  443. if ($i > 0) {
  444. $t = str_repeat('&nbsp;', $i) . substr($t, $i);
  445. $text[$key] = $t;
  446. }
  447. }
  448. $text = implode(NL, $text);
  449. $esc = true;
  450. }
  451. if (!isset($options['nl2br']) || $options['nl2br'] !== false) {
  452. $text = nl2br($text);
  453. }
  454. if (!isset($options['tabsToSpaces'])) {
  455. $options['tabsToSpaces'] = 4;
  456. }
  457. if (!empty($options['tabsToSpaces'])) {
  458. $text = str_replace(TB, str_repeat(!empty($esc) ? '&nbsp;' : ' ', $options['tabsToSpaces']), $text);
  459. }
  460. return $text;
  461. }
  462. /**
  463. * prevents site being opened/included by others/websites inside frames
  464. * 2009-01-08 ms
  465. */
  466. public function framebuster() {
  467. return $this->Html->scriptBlock('
  468. if (top!=self) top.location.ref=self.location.href;
  469. ');
  470. }
  471. /**
  472. * currenctly only alerts on IE6/IE7
  473. * options
  474. * - engine (js, jquery)
  475. * - escape
  476. * needs the id element to be a present (div) container in the layout
  477. * 2009-12-26 ms
  478. */
  479. public function browserAlert($id, $message, $options = array()) {
  480. $engine = 'js';
  481. if (!isset($options['escape']) || $options['escape'] !== false) {
  482. $message = h($message);
  483. }
  484. return $this->Html->scriptBlock('
  485. // Returns the version of Internet Explorer or a -1
  486. function getInternetExplorerVersion() {
  487. var rv = -1; // Return value assumes failure.
  488. if (navigator.appName === "Microsoft Internet Explorer") {
  489. var ua = navigator.userAgent;
  490. var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  491. if (re.exec(ua) != null)
  492. rv = parseFloat( RegExp.$1 );
  493. }
  494. return rv;
  495. }
  496. if ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1) || typeof document.body.style.maxHeight == "undefined") {
  497. document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
  498. }
  499. /*
  500. jQuery(document).ready(function() {
  501. if ($.browser.msie && $.browser.version.substring(0,1) < 8) {
  502. document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
  503. }
  504. });
  505. */
  506. ');
  507. /*
  508. if ($.browser.msie) {
  509. var version = $.browser.version.substring(0,1);
  510. }
  511. */
  512. }
  513. /**
  514. * in noscript tags:
  515. * - link which should not be followed by bots!
  516. * - "pseudo"image which triggers log
  517. * 2009-12-28 ms
  518. */
  519. public function honeypot($noFollowUrl, $noscriptUrl = array()) {
  520. $res = '<div class="invisible" style="display:none"><noscript>';
  521. $res .= $this->Html->defaultLink('Email', $noFollowUrl, array('rel' => 'nofollow'));
  522. if (!empty($noscriptUrl)) {
  523. $res .= BR.$this->Html->image($this->Html->defaultUrl($noscriptUrl, true)); //$this->Html->link($noscriptUrl);
  524. }
  525. $res .= '</noscript></div>';
  526. return $res;
  527. }
  528. /*** Stats ***/
  529. /**
  530. * print js-visit-stats-link to layout
  531. * uses Piwik open source statistics framework
  532. * 2009-04-15 ms
  533. */
  534. public function visitStats($viewPath = null) {
  535. $res = '';
  536. if (!defined('HTTP_HOST_LIVESERVER')) {
  537. return '';
  538. }
  539. if (HTTP_HOST == HTTP_HOST_LIVESERVER && (int)Configure::read('Config.tracking') === 1) {
  540. $trackingUrl = Configure::read('Config.tracking_url');
  541. if (empty($trackingUrl)) {
  542. $trackingUrl = 'visit_stats';
  543. }
  544. $error = false;
  545. if (!empty($viewPath) && $viewPath === 'errors') {
  546. $error = true;
  547. }
  548. $res .= '
  549. <script type="text/javascript">
  550. var pkBaseURL = (("https:" == document.location.protocol) ? "https://'.HTTP_HOST.'/'.$trackingUrl.'/" : "http://'.HTTP_HOST.'/'.$trackingUrl.'/");
  551. document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));
  552. </script>
  553. <script type="text/javascript">
  554. try {
  555. var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
  556. piwikTracker.trackPageView();
  557. piwikTracker.enableLinkTracking();
  558. '.($error?'piwikTracker.setDocumentTitle(\'404/URL = \'+encodeURIComponent(document.location.pathname+document.location.search) + \'/From = \' + encodeURIComponent(document.referrer));':'').'
  559. } catch( err ) {}
  560. </script>
  561. <noscript><p>'.$this->visitStatsImg().'</p></noscript>
  562. ';
  563. }
  564. return $res;
  565. }
  566. /**
  567. * non js browsers
  568. * 2009-09-07 ms
  569. */
  570. public function visitStatsImg($trackingUrl = null) {
  571. if (empty($trackingUrl)) {
  572. $trackingUrl = Configure::read('Config.tracking_url');
  573. }
  574. if (empty($trackingUrl)) {
  575. $trackingUrl = 'visit_stats';
  576. }
  577. return '<img src="'.Router::url('/').$trackingUrl.'/piwik.php?idsite=1" style="border:0" alt=""/>';
  578. }
  579. /*** deprecated ***/
  580. /**
  581. * SINGLE ROLES function
  582. * currently: isRole('admin'), isRole('user')
  583. *
  584. * @deprecated - use Auth class instead
  585. * 2009-07-06 ms
  586. */
  587. public function isRole($role) {
  588. $sessionRole = $this->Session->read('Auth.User.role_id');
  589. $roles = array(
  590. ROLE_USER => 'user',
  591. ROLE_ADMIN => 'admin',
  592. ROLE_SUPERADMIN => 'superadmin',
  593. ROLE_GUEST => 'guest',
  594. );
  595. if (!empty($roles[$sessionRole]) && $role = $roles[$sessionRole]) {
  596. return true;
  597. }
  598. return false;
  599. }
  600. /**
  601. * Checks if a role is in the current users session
  602. *
  603. * @param necessary right(s) as array - or a single one as string possible
  604. * Note: all of them need to be in the user roles to return true by default
  605. * @deprecated - use Auth class instead
  606. */
  607. public function roleNames($sessionRoles = null) {
  608. $tmp = array();
  609. if ($sessionRoles === null) {
  610. $sessionRoles = $this->Session->read('Auth.User.Role');
  611. }
  612. $roles = Cache::read('User.Role');
  613. if (empty($roles) || !is_array($roles)) {
  614. $Role = ClassRegistry::init('Role');
  615. /*
  616. if ($Role->useDbConfig === 'test_suite') {
  617. return array();
  618. }
  619. */
  620. $roles = $Role->getActive('list');
  621. Cache::write('User.Role', $roles);
  622. }
  623. //$roleKeys = Set::combine($roles, '/Role/id','/Role/name'); // on find(all)
  624. if (!empty($sessionRoles)) {
  625. if (is_array($sessionRoles)) {
  626. foreach ($sessionRoles as $sessionRole) {
  627. if (!$sessionRole) {
  628. continue;
  629. }
  630. if (array_key_exists((int)$sessionRole, $roles)) {
  631. $tmp[$sessionRole] = $roles[(int)$sessionRole];
  632. }
  633. }
  634. } else {
  635. if (array_key_exists($sessionRoles, $roles)) {
  636. $tmp[$sessionRoles] = $roles[$sessionRoles];
  637. }
  638. }
  639. }
  640. return $tmp;
  641. }
  642. /**
  643. * Display Roles separated by Commas
  644. * @deprecated - use Auth class instead
  645. * 2009-07-17 ms
  646. */
  647. public function displayRoles($sessionRoles = null, $placeHolder = '---') {
  648. $roles = $this->roleNames($sessionRoles);
  649. if (!empty($roles)) {
  650. return implode(', ', $roles);
  651. }
  652. return $placeHolder;
  653. }
  654. /**
  655. * takes int / array(int) and finds the role name to it
  656. * @return array roles
  657. */
  658. public function roleNamesTranslated($value) {
  659. if (empty($value)) { return array(); }
  660. $ret = array();
  661. $translate = (array)Configure::read('Role');
  662. if (is_array($value)) {
  663. foreach ($value as $k => $v) {
  664. $ret[$v] = __($translate[$v]);
  665. }
  666. } else {
  667. $ret[$value] = __($translate[$value]);
  668. }
  669. return $ret;
  670. }
  671. /**
  672. * @deprecated
  673. */
  674. public function showDebug() {
  675. $output = '';
  676. $groupout = '';
  677. foreach (debugTab::$content as $group => $debug) {
  678. if (is_int($group)) {
  679. $output .= '<div class="common-debug">';
  680. $output .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong>';
  681. $output .= ' (line <strong>' . $debug['line'] . '</strong>)</span>';
  682. if ($debug['display'])
  683. $debug['display'] = 'block';
  684. else
  685. $debug['display'] = 'none';
  686. $output .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  687. $output .= h($debug['debug']);
  688. $output .= "\n</pre>\n</div>";
  689. }
  690. }
  691. foreach (debugTab::$groups as $group => $data) {
  692. $groupout .= '<div class="common-debug">';
  693. $groupout .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('div').slideToggle('fast');\"><strong>" . h($group) . '</strong></span>';
  694. foreach ($data as $debug) {
  695. $groupout .= "<div style=\"display:none\"><br/><span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong></span>';
  696. $groupout .= ' (line <strong>' . h($debug['line']) . '</strong>)</span>';
  697. if ($debug['display'])
  698. $debug['display'] = 'block';
  699. else
  700. $debug['display'] = 'none';
  701. $groupout .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  702. $groupout .= h($debug['debug']);
  703. $groupout .= "\n</pre>\n</div>";
  704. }
  705. $groupout .= "\n</div>";
  706. }
  707. return $groupout . $output;
  708. }
  709. /**
  710. * Creates an HTML link.
  711. *
  712. * If $url starts with "http://" this is treated as an external link. Else,
  713. * it is treated as a path to controller/action and parsed with the
  714. * HtmlHelper::url() method.
  715. *
  716. * If the $url is empty, $title is used instead.
  717. *
  718. * @param string $title The content to be wrapped by <a> tags.
  719. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  720. * @param array $htmlAttributes Array of HTML attributes.
  721. * @param string $confirmMessage JavaScript confirmation message.
  722. * @param boolean $escapeTitle Whether or not $title should be HTML escaped.
  723. * @return string An <a /> element.
  724. * @deprecated?
  725. * // core-hack! $rel = null | !!!!!!!!! Somehow causes trouble with routing functionality of this helper function... careful!
  726. */
  727. public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $rel = null) {
  728. if ($url !== null) {
  729. /** core-hack $rel (relative to current position/routing) **/
  730. if ($rel === true || !is_array($url)) {
  731. // leave it as it is
  732. } else {
  733. $defaultArray = array('admin'=>false, 'prefix'=>0);
  734. $url = array_merge($defaultArray, $url);
  735. }
  736. /** core-hack END **/
  737. return $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
  738. }
  739. }
  740. }