CommonHelper.php 22 KB

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