CommonHelper.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. if ($type == 'language') {
  164. return sprintf($tags['meta'], 'language', ' content="'.$value.'"');
  165. } elseif ($type == 'pragma') {
  166. return sprintf($tags['meta'], 'pragma', ' content="'.$value.'"');
  167. } elseif ($type == 'expires') {
  168. return sprintf($tags['meta'], 'expires', ' content="'.$value.'"');
  169. } elseif ($type == 'cache-control') {
  170. return sprintf($tags['meta'], 'cache-control', ' content="'.$value.'"');
  171. } elseif ($type == 'refresh') {
  172. return sprintf($tags['meta'], 'refresh', ' content="'.$value.'"');
  173. }
  174. return '';
  175. }
  176. /**
  177. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  178. * => x is in webroot/
  179. * => y is in plugins/tools/webroot/
  180. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  181. * @return string $htmlMarkup
  182. * 2011-03-23 ms
  183. */
  184. public function css($files = array(), $rel = null, $options = array()) {
  185. $files = (array)$files;
  186. $pieces = array();
  187. foreach ($files as $file) {
  188. $pieces[] = 'file='.$file;
  189. }
  190. if ($v = Configure::read('Config.layout_v')) {
  191. $pieces[] = 'v='.$v;
  192. }
  193. $string = implode('&', $pieces);
  194. return $this->Html->css('/css.php?'.$string, $rel, $options);
  195. }
  196. /**
  197. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  198. * => x is in webroot/
  199. * => y is in plugins/tools/webroot/
  200. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  201. * @return string $htmlMarkup
  202. * 2011-03-23 ms
  203. */
  204. public function script($files = array(), $options = array()) {
  205. $files = (array)$files;
  206. foreach ($files as $file) {
  207. $pieces[] = 'file='.$file;
  208. }
  209. if ($v = Configure::read('Config.layout_v')) {
  210. $pieces[] = 'v='.$v;
  211. }
  212. $string = implode('&', $pieces);
  213. return $this->Html->script('/js.php?'.$string, $options);
  214. }
  215. /**
  216. * special css tag generator with the option to add '?...' to the link (for caching prevention)
  217. * IN USAGE
  218. * needs manual adjustment, but still better than the core one!
  219. * @example needs Asset.cssversion => xyz (going up with counter)
  220. * @return string $htmlMarkup
  221. * 2008-12-08 ms
  222. */
  223. public function cssDyn($path, $rel = null, $htmlAttributes = array(), $return = true) {
  224. $v = (int)Configure::read('Asset.version');
  225. return $this->Html->css($path.'.css?'.$v, $rel, $htmlAttributes, $return);
  226. }
  227. /**
  228. * NOT IN USAGE
  229. * but better than the core one!
  230. * @example needs Asset.timestamp => force
  231. * @return string $htmlMarkup
  232. * 2008-12-08 ms
  233. */
  234. public function cssAuto($path, $rel = null, $htmlAttributes = array(), $return = true) {
  235. define('COMPRESS_CSS',true);
  236. $time = date('YmdHis', filemtime(APP . 'webroot' . DS . CSS_URL . $path . '.css'));
  237. $url = "{$this->request->webroot}" . (COMPRESS_CSS ? 'c' : '') . CSS_URL . $this->themeWeb . $path . ".css?" . $time;
  238. return $url;
  239. }
  240. /*** Content Stuff ***/
  241. /**
  242. * still necessary?
  243. * @param array $fields
  244. * @return string $html
  245. */
  246. public function displayErrors($fields = array()) {
  247. $res = '';
  248. if (!empty($this->validationErrors)) {
  249. if ($fields === null) { # catch ALL
  250. foreach ($this->validationErrors as $alias => $error) {
  251. list($alias, $fieldname) = explode('.', $error);
  252. $this->validationErrors[$alias][$fieldname];
  253. }
  254. } elseif (!empty($fields)) {
  255. foreach ($fields as $field) {
  256. list($alias, $fieldname) = explode('.', $field);
  257. if (!empty($this->validationErrors[$alias][$fieldname])) {
  258. $res .= $this->_renderError($this->validationErrors[$alias][$fieldname]);
  259. }
  260. }
  261. }
  262. /*
  263. if (!empty($catched)) {
  264. foreach ($catched as $c) {
  265. }
  266. }
  267. */
  268. }
  269. return $res;
  270. }
  271. protected function _renderError($error, $escape = true) {
  272. if ($escape !== false) {
  273. $error = h($error);
  274. }
  275. return '<div class="error-message">'.$error.'</div>';
  276. }
  277. /**
  278. * Alternates between two or more strings.
  279. *
  280. * echo CommonHelper::alternate('one', 'two'); // "one"
  281. * echo CommonHelper::alternate('one', 'two'); // "two"
  282. * echo CommonHelper::alternate('one', 'two'); // "one"
  283. *
  284. * Note that using multiple iterations of different strings may produce
  285. * unexpected results.
  286. * TODO: move to booststrap!!!
  287. *
  288. * @param string strings to alternate between
  289. * @return string
  290. */
  291. public static function alternate() {
  292. static $i;
  293. if (func_num_args() === 0) {
  294. $i = 0;
  295. return '';
  296. }
  297. $args = func_get_args();
  298. return $args[($i++ % count($args))];
  299. }
  300. /**
  301. * check if session works due to allowed cookies
  302. *
  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. * 2009-06-29 ms
  317. */
  318. public function sessionCheckAlert() {
  319. if (!$this->sessionCheck()) {
  320. return '<div class="cookieWarning">'.__('Please enable cookies').'</div>';
  321. }
  322. return '';
  323. }
  324. /**
  325. * //TODO: move boostrap
  326. * auto-pluralizing a word using the Inflection class
  327. * @param string $s = the string to be pl.
  328. * @param int $c = the count
  329. * @return $string "member" or "members" OR "Mitglied"/"Mitglieder" if autoTranslate TRUE
  330. * 2009-07-23 ms
  331. */
  332. public function asp($s, $c, $autoTranslate = false) {
  333. if ((int)$c !== 1) {
  334. $p = Inflector::pluralize($s);
  335. } else {
  336. $p = null; # no pluralization necessary
  337. }
  338. return $this->sp($s, $p, $c, $autoTranslate);
  339. }
  340. /**
  341. * //TODO: move boostrap
  342. * manual pluralizing a word using the Inflection class
  343. *
  344. * @param string $singular
  345. * @param string $plural
  346. * @param int $count
  347. * @return string $result
  348. * 2009-07-23 ms
  349. */
  350. public function sp($s, $p, $c, $autoTranslate = false) {
  351. if ((int)$c !== 1) {
  352. $ret = $p;
  353. } else {
  354. $ret = $s;
  355. }
  356. if ($autoTranslate) {
  357. $ret = __($ret);
  358. }
  359. return $ret;
  360. }
  361. /**
  362. * Show FlashMessages
  363. * @param boolean unsorted true/false [default:FALSE = sorted by priority]
  364. * TODO: export div wrapping method (for static messaging on a page)
  365. * TODO: sorting
  366. * 2010-11-22 ms
  367. */
  368. public function flash($unsorted = false, $backwardsComp = true) {
  369. // Get the messages from the session
  370. $messages = (array)$this->Session->read('messages');
  371. $cMessages = (array)Configure::read('messages');
  372. if (!empty($cMessages)) {
  373. $messages = (array)Set::merge($messages, $cMessages);
  374. }
  375. $html='';
  376. if (!empty($messages)) {
  377. $html = '<div class="flashMessages">';
  378. if ($unsorted !== true) {
  379. // Add a div for each message using the type as the class
  380. foreach ($messages as $type => $msgs) {
  381. foreach ((array)$msgs as $msg) {
  382. $html .= $this->_message($msg, $type);
  383. }
  384. }
  385. } else {
  386. foreach ($messages as $type) {
  387. //
  388. }
  389. }
  390. $html .= '</div>';
  391. if (method_exists($this->Session, 'delete')) {
  392. $this->Session->delete('messages');
  393. } else {
  394. CakeSession::delete('messages');
  395. }
  396. }
  397. return $html;
  398. }
  399. /**
  400. * output a single flashMessage
  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 bool $success
  424. * 2011-05-25 ms
  425. */
  426. public function transientFlashMessage($msg, $class = null) {
  427. return CommonComponent::transientFlashMessage($msg, $class);
  428. }
  429. /**
  430. * TODO: move into TextExt?
  431. * escape text with some more automagic
  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. if ($.browser.msie) {
  518. var version = $.browser.version.substring(0,1);
  519. }
  520. */
  521. }
  522. /**
  523. * in noscript tags:
  524. * - link which should not be followed by bots!
  525. * - "pseudo"image which triggers log
  526. * 2009-12-28 ms
  527. */
  528. public function honeypot($noFollowUrl, $noscriptUrl = array()) {
  529. $res = '<div class="invisible" style="display:none"><noscript>';
  530. $res .= $this->Html->defaultLink('Email', $noFollowUrl, array('rel'=>'nofollow'));
  531. if (!empty($noscriptUrl)) {
  532. $res .= BR.$this->Html->image($this->Html->defaultUrl($noscriptUrl, true)); //$this->Html->link($noscriptUrl);
  533. }
  534. $res .= '</noscript></div>';
  535. return $res;
  536. }
  537. /*** Stats ***/
  538. /**
  539. * print js-visit-stats-link to layout
  540. * uses Piwik open source statistics framework
  541. * 2009-04-15 ms
  542. */
  543. public function visitStats($viewPath = null) {
  544. $res = '';
  545. if (!defined('HTTP_HOST_LIVESERVER')) {
  546. return '';
  547. }
  548. if (HTTP_HOST == HTTP_HOST_LIVESERVER && (int)Configure::read('Config.tracking') === 1) {
  549. $trackingUrl = Configure::read('Config.tracking_url');
  550. if (empty($trackingUrl)) {
  551. $trackingUrl = 'visit_stats';
  552. }
  553. $error = false;
  554. if (!empty($viewPath) && $viewPath == 'errors') {
  555. $error = true;
  556. }
  557. $res .= '
  558. <script type="text/javascript">
  559. var pkBaseURL = (("https:" == document.location.protocol) ? "https://'.HTTP_HOST.'/'.$trackingUrl.'/" : "http://'.HTTP_HOST.'/'.$trackingUrl.'/");
  560. document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));
  561. </script>
  562. <script type="text/javascript">
  563. try {
  564. var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
  565. piwikTracker.trackPageView();
  566. piwikTracker.enableLinkTracking();
  567. '.($error?'piwikTracker.setDocumentTitle(\'404/URL = \'+encodeURIComponent(document.location.pathname+document.location.search) + \'/From = \' + encodeURIComponent(document.referrer));':'').'
  568. } catch( err ) {}
  569. </script>
  570. <noscript><p>'.$this->visitStatsImg().'</p></noscript>
  571. ';
  572. }
  573. return $res;
  574. }
  575. /**
  576. * non js browsers
  577. * 2009-09-07 ms
  578. */
  579. public function visitStatsImg($trackingUrl = null) {
  580. if (empty($trackingUrl)) {
  581. $trackingUrl = Configure::read('Config.tracking_url');
  582. }
  583. if (empty($trackingUrl)) {
  584. $trackingUrl = 'visit_stats';
  585. }
  586. return '<img src="'.Router::url('/').$trackingUrl.'/piwik.php?idsite=1" style="border:0" alt=""/>';
  587. }
  588. /*** deprecated ***/
  589. /**
  590. * SINGLE ROLES function
  591. * currently: isRole('admin'), isRole('user')
  592. *
  593. * @deprecated - use Auth class instead
  594. * 2009-07-06 ms
  595. */
  596. public function isRole($role) {
  597. $sessionRole = $this->Session->read('Auth.User.role_id');
  598. $roles = array(
  599. ROLE_USER => 'user',
  600. ROLE_ADMIN => 'admin',
  601. ROLE_SUPERADMIN => 'superadmin',
  602. ROLE_GUEST => 'guest',
  603. );
  604. if (!empty($roles[$sessionRole]) && $role = $roles[$sessionRole]) {
  605. return true;
  606. }
  607. return false;
  608. }
  609. /**
  610. * Checks if a role is in the current users session
  611. *
  612. * @param necessary right(s) as array - or a single one as string possible
  613. * Note: all of them need to be in the user roles to return true by default
  614. * @deprecated - use Auth class instead
  615. */
  616. public function roleNames($sessionRoles = null) {
  617. $tmp = array();
  618. if ($sessionRoles === null) {
  619. $sessionRoles = $this->Session->read('Auth.User.Role');
  620. }
  621. $roles = Cache::read('User.Role');
  622. if (empty($roles) || !is_array($roles)) {
  623. $Role = ClassRegistry::init('Role');
  624. /*
  625. if ($Role->useDbConfig == 'test_suite') {
  626. return array();
  627. }
  628. */
  629. $roles = $Role->getActive('list');
  630. Cache::write('User.Role', $roles);
  631. }
  632. //$roleKeys = Set::combine($roles, '/Role/id','/Role/name'); // on find(all)
  633. if (!empty($sessionRoles)) {
  634. if (is_array($sessionRoles)) {
  635. foreach ($sessionRoles as $sessionRole) {
  636. if (!$sessionRole) {
  637. continue;
  638. }
  639. if (array_key_exists((int)$sessionRole, $roles)) {
  640. $tmp[$sessionRole] = $roles[(int)$sessionRole];
  641. }
  642. }
  643. } else {
  644. if (array_key_exists($sessionRoles, $roles)) {
  645. $tmp[$sessionRoles] = $roles[$sessionRoles];
  646. }
  647. }
  648. }
  649. return $tmp;
  650. }
  651. /**
  652. * Display Roles separated by Commas
  653. * @deprecated - use Auth class instead
  654. * 2009-07-17 ms
  655. */
  656. public function displayRoles($sessionRoles = null, $placeHolder = '---') {
  657. $roles = $this->roleNames($sessionRoles);
  658. if (!empty($roles)) {
  659. return implode(', ', $roles);
  660. }
  661. return $placeHolder;
  662. }
  663. /**
  664. * takes int / array(int) and finds the role name to it
  665. * @return array roles
  666. */
  667. public function roleNamesTranslated($value) {
  668. if (empty($value)) { return array(); }
  669. $ret = array();
  670. $translate = (array)Configure::read('Role');
  671. if (is_array($value)) {
  672. foreach ($value as $k => $v) {
  673. $ret[$v] = __($translate[$v]);
  674. }
  675. } else {
  676. $ret[$value] = __($translate[$value]);
  677. }
  678. return $ret;
  679. }
  680. /**
  681. * @deprecated
  682. */
  683. public function showDebug() {
  684. $output = '';
  685. $groupout = '';
  686. foreach (debugTab::$content as $group => $debug) {
  687. if (is_int($group)) {
  688. $output .= '<div class="common-debug">';
  689. $output .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong>';
  690. $output .= ' (line <strong>' . $debug['line'] . '</strong>)</span>';
  691. if ($debug['display'])
  692. $debug['display'] = 'block';
  693. else
  694. $debug['display'] = 'none';
  695. $output .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  696. $output .= h($debug['debug']);
  697. $output .= "\n</pre>\n</div>";
  698. }
  699. }
  700. foreach (debugTab::$groups as $group => $data) {
  701. $groupout .= '<div class="common-debug">';
  702. $groupout .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('div').slideToggle('fast');\"><strong>" . h($group) . '</strong></span>';
  703. foreach ($data as $debug) {
  704. $groupout .= "<div style=\"display:none\"><br/><span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong></span>';
  705. $groupout .= ' (line <strong>' . h($debug['line']) . '</strong>)</span>';
  706. if ($debug['display'])
  707. $debug['display'] = 'block';
  708. else
  709. $debug['display'] = 'none';
  710. $groupout .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  711. $groupout .= h($debug['debug']);
  712. $groupout .= "\n</pre>\n</div>";
  713. }
  714. $groupout .= "\n</div>";
  715. }
  716. return $groupout . $output;
  717. }
  718. /**
  719. * Creates an HTML link.
  720. *
  721. * If $url starts with "http://" this is treated as an external link. Else,
  722. * it is treated as a path to controller/action and parsed with the
  723. * HtmlHelper::url() method.
  724. *
  725. * If the $url is empty, $title is used instead.
  726. *
  727. * @param string $title The content to be wrapped by <a> tags.
  728. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  729. * @param array $htmlAttributes Array of HTML attributes.
  730. * @param string $confirmMessage JavaScript confirmation message.
  731. * @param boolean $escapeTitle Whether or not $title should be HTML escaped.
  732. * @return string An <a /> element.
  733. * @deprecated?
  734. * // core-hack! $rel = null | !!!!!!!!! Somehow causes trouble with routing functionality of this helper function... careful!
  735. */
  736. public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $rel = null) {
  737. if ($url !== null) {
  738. /** core-hack $rel (relative to current position/routing) **/
  739. if ($rel === true || !is_array($url)) {
  740. // leave it as it is
  741. } else {
  742. $defaultArray = array('admin'=>false, 'prefix'=>0);
  743. $url = array_merge($defaultArray, $url);
  744. }
  745. /** core-hack END **/
  746. return $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
  747. }
  748. }
  749. }