CommonHelper.php 19 KB

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