CommonHelper.php 19 KB

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