CommonHelper.php 19 KB

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