FormatHelper.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. <?php
  2. App::uses('TextHelper', 'View/Helper');
  3. App::uses('StringTemplate', 'Tools.View');
  4. /**
  5. * Format helper with basic html snippets
  6. *
  7. * TODO: make snippets more "css and background image" (instead of inline img links)
  8. *
  9. * @author Mark Scherer
  10. * @license MIT
  11. */
  12. class FormatHelper extends TextHelper {
  13. /**
  14. * Other helpers used by FormHelper
  15. *
  16. * @var array
  17. */
  18. public $helpers = array('Html', 'Tools.Numeric');
  19. public $template;
  20. protected $_defaultConfig = array(
  21. 'fontIcons' => false, // Defaults to false for BC
  22. 'iconNamespace' => 'fa', // Used to be icon
  23. );
  24. public function __construct(View $View, $config = array()) {
  25. $config += $this->_defaultConfig;
  26. if ($config['fontIcons'] === true) {
  27. $config['fontIcons'] = (array)Configure::read('Format.fontIcons');
  28. if ($namespace = Configure::read('Format.iconNamespace')) {
  29. $config['iconNamespace'] = $namespace;
  30. }
  31. }
  32. $templates = array(
  33. 'icon' => '<i class="{{class}}" title="{{title}}" data-placement="bottom" data-toggle="tooltip"></i>',
  34. ) + (array)Configure::read('Format.templates');
  35. if (!isset($this->template)) {
  36. $this->template = new StringTemplate($templates);
  37. }
  38. parent::__construct($View, $config);
  39. }
  40. /**
  41. * jqueryAccess: {id}Pro, {id}Contra
  42. *
  43. * @return string
  44. */
  45. public function thumbs($id, $inactive = false, $inactiveTitle = null) {
  46. $status = 'Active';
  47. $upTitle = __('consentThis');
  48. $downTitle = __('dissentThis');
  49. if ($inactive === true) {
  50. $status = 'Inactive';
  51. $upTitle = $downTitle = !empty($inactiveTitle) ? $inactiveTitle : __('alreadyVoted');
  52. }
  53. if ($this->settings['fontIcons']) {
  54. // TODO: Return proper font icons
  55. // fa-thumbs-down
  56. // fa-thumbs-up
  57. }
  58. $ret = '<div class="thumbsUpDown">';
  59. $ret .= '<div id="' . $id . 'Pro' . $status . '" rel="' . $id . '" class="thumbUp up' . $status . '" title="' . $upTitle . '"></div>';
  60. $ret .= '<div id="' . $id . 'Contra' . $status . '" rel="' . $id . '" class="thumbDown down' . $status . '" title="' . $downTitle . '"></div>';
  61. $ret .= '<br class="clear"/>';
  62. $ret .= '</div>';
  63. return $ret;
  64. }
  65. /**
  66. * Display neighbor quicklinks
  67. *
  68. * @param array $neighbors (containing prev and next)
  69. * @param string $field: just field or Model.field syntax
  70. * @param array $options:
  71. * - name: title name: next{Record} (if none is provided, "record" is used - not translated!)
  72. * - slug: true/false (defaults to false)
  73. * - titleField: field or Model.field
  74. * @return string
  75. */
  76. public function neighbors($neighbors, $field, $options = array()) {
  77. if (mb_strpos($field, '.') !== false) {
  78. $fieldArray = explode('.', $field, 2);
  79. $alias = $fieldArray[0];
  80. $field = $fieldArray[1];
  81. }
  82. if (empty($alias)) {
  83. if (!empty($neighbors['prev'])) {
  84. $modelNames = array_keys($neighbors['prev']);
  85. $alias = $modelNames[0];
  86. } elseif (!empty($neighbors['next'])) {
  87. $modelNames = array_keys($neighbors['next']);
  88. $alias = $modelNames[0];
  89. }
  90. }
  91. if (empty($field)) {
  92. }
  93. $name = 'Record'; // Translation further down!
  94. if (!empty($options['name'])) {
  95. $name = ucfirst($options['name']);
  96. }
  97. $prevSlug = $nextSlug = null;
  98. if (!empty($options['slug'])) {
  99. if (!empty($neighbors['prev'])) {
  100. $prevSlug = Inflector::slug($neighbors['prev'][$alias][$field], '-');
  101. }
  102. if (!empty($neighbors['next'])) {
  103. $nextSlug = Inflector::slug($neighbors['next'][$alias][$field], '-');
  104. }
  105. }
  106. $titleAlias = $alias;
  107. $titleField = $field;
  108. if (!empty($options['titleField'])) {
  109. if (mb_strpos($options['titleField'], '.') !== false) {
  110. $fieldArray = explode('.', $options['titleField'], 2);
  111. $titleAlias = $fieldArray[0];
  112. $titleField = $fieldArray[1];
  113. } else {
  114. $titleField = $options['titleField'];
  115. }
  116. }
  117. if (!isset($options['escape']) || $options['escape'] === false) {
  118. $titleField = h($titleField);
  119. }
  120. $ret = '<div class="next-prev-navi nextPrevNavi">';
  121. if (!empty($neighbors['prev'])) {
  122. $url = array($neighbors['prev'][$alias]['id'], $prevSlug);
  123. if (!empty($options['url'])) {
  124. $url += $options['url'];
  125. }
  126. $ret .= $this->Html->link($this->cIcon(ICON_PREV, false) . '&nbsp;' . __('prev' . $name), $url, array('escape' => false, 'title' => $neighbors['prev'][$titleAlias][$titleField]));
  127. } else {
  128. $ret .= $this->cIcon(ICON_PREV_DISABLED, __('noPrev' . $name)) . '&nbsp;' . __('prev' . $name);
  129. }
  130. $ret .= '&nbsp;&nbsp;';
  131. if (!empty($neighbors['next'])) {
  132. $url = array($neighbors['next'][$alias]['id'], $prevSlug);
  133. if (!empty($options['url'])) {
  134. $url += $options['url'];
  135. }
  136. $ret .= $this->Html->link($this->cIcon(ICON_NEXT, false) . '&nbsp;' . __('next' . $name), $url, array('escape' => false, 'title' => $neighbors['next'][$titleAlias][$titleField]));
  137. } else {
  138. $ret .= $this->cIcon(ICON_NEXT_DISABLED, __('noNext' . $name)) . '&nbsp;' . __('next' . $name);
  139. }
  140. $ret .= '</div>';
  141. return $ret;
  142. }
  143. /**
  144. * Allows icons to be added on the fly
  145. * NOTE: overriding not allowed by default
  146. *
  147. * @return void
  148. * @deprecated Try to use font icons and templates with icon()
  149. */
  150. public function addIcon($name = null, $pic = null, $title = null, $allowOverride = false) {
  151. if ($allowOverride === true || ($allowOverride !== true && !array_key_exists($name, $this->icons))) {
  152. if (!empty($name) && !empty($pic)) {
  153. $this->icons[$name] = array('pic' => strtolower($pic), 'title' => (!empty($title) ? $title : ''));
  154. }
  155. }
  156. }
  157. const GENDER_FEMALE = 2;
  158. const GENDER_MALE = 1;
  159. /**
  160. * Displays gender icon
  161. *
  162. * @return string
  163. */
  164. public function genderIcon($value = null) {
  165. $value = (int)$value;
  166. if ($value == self::GENDER_FEMALE) {
  167. $icon = $this->icon('genderFemale', null, null, null, array('class' => 'gender'));
  168. } elseif ($value == self::GENDER_MALE) {
  169. $icon = $this->icon('genderMale', null, null, null, array('class' => 'gender'));
  170. } else {
  171. $icon = $this->icon('genderUnknown', null, null, null, array('class' => 'gender'));
  172. }
  173. return $icon;
  174. }
  175. /**
  176. * Returns img from customImgFolder
  177. *
  178. * @param string $folder
  179. * @param string $icon
  180. * @param bool $checkExists
  181. * @param array $options (ending [default: gif])
  182. * @return string
  183. * @deprecated Try to use font icons or move functionality into own helper.
  184. */
  185. public function customIcon($folder, $icon = null, $checkExist = false, $options = array(), $attr = array()) {
  186. $attachment = 'default';
  187. $ending = 'gif';
  188. $image = null;
  189. if (!empty($options)) {
  190. if (!empty($options['ending'])) {
  191. $ending = $options['ending'];
  192. }
  193. if (!empty($options['backend'])) {
  194. $attachment = 'backend';
  195. }
  196. }
  197. if (empty($icon)) {
  198. } elseif ($checkExist === true && !file_exists(PATH_CONTENT . $folder . DS . $icon . '.' . $ending)) {
  199. } else {
  200. $image = $icon;
  201. }
  202. if ($image === null) {
  203. return $this->Html->image(IMG_ICONS . 'custom' . '/' . $folder . '_' . $attachment . '.' . $ending, $attr);
  204. }
  205. return $this->Html->image(IMG_CONTENT . $folder . '/' . $image . '.' . $ending, $attr);
  206. }
  207. /**
  208. * @param value
  209. * @param array $options
  210. * - max (3/5, defaults to 5)
  211. * - normal: display an icon for normal as well (defaults to false)
  212. * - map: array (manually map values, if you use 1 based values no need for that)
  213. * - title, alt, ...
  214. * @return string html
  215. * @deprecated Try to use font icons or move functionality into own helper.
  216. */
  217. public function priorityIcon($value, $options = array()) {
  218. $defaults = array(
  219. 'max' => 5,
  220. 'normal' => false,
  221. 'map' => array(),
  222. 'css' => true,
  223. );
  224. $options += $defaults;
  225. extract($options);
  226. $matching = array(
  227. 1 => 'low',
  228. 2 => 'lower',
  229. 3 => 'normal',
  230. 4 => 'higher',
  231. 5 => 'high'
  232. );
  233. if (!empty($map)) {
  234. $value = $map[$value];
  235. }
  236. if (!$normal && $value == ($max + 1) / 2) {
  237. return '';
  238. }
  239. if ($max != 5) {
  240. if ($value == 2) {
  241. $value = 3;
  242. } elseif ($value == 3) {
  243. $value = 5;
  244. }
  245. }
  246. $attr = array(
  247. 'class' => 'prio-' . $matching[$value],
  248. 'title' => __('prio' . ucfirst($matching[$value])),
  249. );
  250. if (!$css) {
  251. $attr['alt'] = $matching[$value];
  252. }
  253. $attr = array_merge($attr, array_diff_key($options, $defaults));
  254. if ($css) {
  255. $html = $this->Html->tag('div', '&nbsp;', $attr);
  256. } else {
  257. $icon = 'priority_' . $matching[$value] . '.gif';
  258. $html = $this->Html->image('icons/' . $icon, $attr);
  259. }
  260. return $html;
  261. }
  262. /**
  263. * Display a font icon (fast and resource-efficient).
  264. * Uses http://fontawesome.io/icons/
  265. *
  266. * Options:
  267. * - size (int|string: 1...5 or large)
  268. * - rotate (integer: 90, 270, ...)
  269. * - spin (booelan: true/false)
  270. * - extra (array: muted, light, dark, border)
  271. * - pull (string: left, right)
  272. *
  273. * @param string|array $icon
  274. * @param array $options
  275. * @return string
  276. */
  277. public function fontIcon($icon, array $options = array(), array $attributes = array()) {
  278. $defaults = array(
  279. 'namespace' => $this->settings['iconNamespace']
  280. );
  281. $options += $defaults;
  282. $icon = (array)$icon;
  283. $class = array();
  284. foreach ($icon as $i) {
  285. $class[] = $options['namespace'] . '-' . $i;
  286. }
  287. if (!empty($options['extra'])) {
  288. foreach ($options['extra'] as $i) {
  289. $class[] = $options['namespace'] . '-' . $i;
  290. }
  291. }
  292. if (!empty($options['size'])) {
  293. $class[] = $options['namespace'] . '-' . ($options['size'] === 'large' ? 'large' : $options['size'] . 'x');
  294. }
  295. if (!empty($options['pull'])) {
  296. $class[] = 'pull-' . $options['pull'];
  297. }
  298. if (!empty($options['rotate'])) {
  299. $class[] = $options['namespace'] . '-rotate-' . (int)$options['rotate'];
  300. }
  301. if (!empty($options['spin'])) {
  302. $class[] = $options['namespace'] . '-spin';
  303. }
  304. return '<i class="' . implode(' ', $class) . '"></i>';
  305. }
  306. /**
  307. * Quick way of printing default icons
  308. *
  309. * @todo refactor to $type, $options, $attributes
  310. *
  311. * @param type
  312. * @param title
  313. * @param alt (set to FALSE if no alt is supposed to be shown)
  314. * @param bool automagic i18n translate [default true = __('xyz')]
  315. * @param options array ('class'=>'','width/height'=>'','onclick=>'') etc
  316. * @return string
  317. */
  318. public function icon($type, $t = null, $a = null, $translate = null, $options = array()) {
  319. if (isset($t) && $t === false) {
  320. $title = '';
  321. } else {
  322. $title = $t;
  323. }
  324. if (isset($a) && $a === false) {
  325. $alt = '';
  326. } else {
  327. $alt = $a;
  328. }
  329. if (!$this->settings['fontIcons'] || !isset($this->settings['fontIcons'][$type])) {
  330. if (array_key_exists($type, $this->icons)) {
  331. $pic = $this->icons[$type]['pic'];
  332. $title = (isset($title) ? $title : $this->icons[$type]['title']);
  333. $alt = (isset($alt) ? $alt : preg_replace('/[^a-zA-Z0-9]/', '', $this->icons[$type]['title']));
  334. if ($translate !== false) {
  335. $title = __($title);
  336. $alt = __($alt);
  337. }
  338. $alt = '[' . $alt . ']';
  339. } else {
  340. $pic = 'pixelspace.gif';
  341. }
  342. $defaults = array('title' => $title, 'alt' => $alt, 'class' => 'icon');
  343. $newOptions = $options + $defaults;
  344. return $this->Html->image('icons/' . $pic, $newOptions);
  345. }
  346. $options['title'] = $title;
  347. $options['translate'] = $translate;
  348. return $this->_fontIcon($type, $options);
  349. }
  350. /**
  351. * Custom Icons
  352. *
  353. * @param string $icon (constant or filename)
  354. * @param array $options:
  355. * - translate, ...
  356. * @param array $attributes:
  357. * - title, alt, ...
  358. * THE REST IS DEPRECATED
  359. * @return string
  360. */
  361. public function cIcon($icon, $t = null, $a = null, $translate = true, $options = array()) {
  362. if (is_array($t)) {
  363. $translate = isset($t['translate']) ? $t['translate'] : true;
  364. $options = (array)$a;
  365. $a = isset($t['alt']) ? $t['alt'] : null; // deprecated
  366. $t = isset($t['title']) ? $t['title'] : null; // deprecated
  367. }
  368. $type = extractPathInfo('filename', $icon);
  369. if (!$this->settings['fontIcons'] || !isset($this->settings['fontIcons'][$type])) {
  370. $title = isset($t) ? $t : ucfirst($type);
  371. $alt = (isset($a) ? $a : Inflector::slug($title, '-'));
  372. if ($translate !== false) {
  373. $title = __($title);
  374. $alt = __($alt);
  375. }
  376. $alt = '[' . $alt . ']';
  377. $defaults = array('title' => $title, 'alt' => $alt, 'class' => 'icon');
  378. $options += $defaults;
  379. if (substr($icon, 0, 1) !== '/') {
  380. $icon = 'icons/' . $icon;
  381. }
  382. return $this->Html->image($icon, $options);
  383. }
  384. $options['title'] = $t;
  385. $options['translate'] = $translate;
  386. return $this->_fontIcon($type, $options);
  387. }
  388. /**
  389. * FormatHelper::_fontIcon()
  390. *
  391. * @param string $type
  392. * @param array $options
  393. * @return string
  394. */
  395. protected function _fontIcon($type, $options) {
  396. $iconType = $this->settings['fontIcons'][$type];
  397. $defaults = array(
  398. 'class' => $iconType . ' ' . $type
  399. );
  400. $options += $defaults;
  401. if (!isset($options['title'])) {
  402. $options['title'] = ucfirst($type);
  403. if ($options['translate'] !== false) {
  404. $options['title'] = __($options['title']);
  405. }
  406. }
  407. return $this->template->format('icon', $options);
  408. }
  409. /**
  410. * Print Star Bar
  411. * //TODO: 0.5 steps!
  412. *
  413. * array $options: steps=1/0.5 [default:1]), show_zero=true/false [default:false], title=false/true [default:false]
  414. * array $attr: string 'title' (both single and span title empty => 'x of x' for span)
  415. * @return string
  416. * @deprecated use RatingHelper::stars() instead
  417. */
  418. public function showStars($current, $max, $options = array(), $attr = array()) {
  419. $res = '---';
  420. if (!empty($options['steps']) && $options['steps'] == 0.5) {
  421. $steps = 0.5;
  422. $current = ((int)(2 * $current) / 2);
  423. } else {
  424. $steps = 1;
  425. $current = (int)$current;
  426. }
  427. $min = (int)$current;
  428. $max = (int)$max;
  429. if ((!empty($current) || (!empty($options['show_zero']) && $current == 0)) && (!empty($max)) && $current <= $max) {
  430. $text = '';
  431. for ($i = 0; $i < $min; $i++) {
  432. $attributes = array('alt' => '#', 'class' => 'full');
  433. if (!empty($options['title'])) { $attributes['title'] = ($i + 1) . '/' . $max; } // ?
  434. $text .= $this->Html->image('icons/star_icon2.gif', $attributes);
  435. }
  436. for ($i = $min; $i < $max; $i++) {
  437. $attributes = array('alt' => '-', 'class' => 'empty');
  438. if (!empty($options['title'])) { $attributes['title'] = ($i + 1) . '/' . $max; } // ?
  439. if ($steps == 0.5 && $current == $i + 0.5) {
  440. $text .= $this->Html->image('icons/star_icon2_half.gif', $attributes);
  441. } else {
  442. $text .= $this->Html->image('icons/star_icon2_empty.gif', $attributes);
  443. }
  444. }
  445. $attributes = array('class' => 'star-bar starBar');
  446. $attributes = array_merge($attributes, $attr);
  447. if (empty($attributes['title']) && empty($options['title'])) {
  448. $attributes['title'] = ($current) . ' ' . __('of') . ' ' . $max;
  449. }
  450. $res = $this->Html->tag('span', $text, $attributes);
  451. //$res='<span title="ss" class="starBar">'.$text.'</span>';
  452. } else {
  453. if ($max > 3) {
  454. for ($i = 0; $i < $max - 3; $i++) {
  455. $res .= '-';
  456. }
  457. }
  458. }
  459. return $res;
  460. }
  461. /**
  462. * Display language flags
  463. *
  464. * @return string HTML
  465. * @deprecated Try to use font icons or move functionality into own helper.
  466. */
  467. public function languageFlags() {
  468. $langs = (array)Configure::read('LanguagesAvailable');
  469. $supportedLangs = array(
  470. 'de' => array('title' => 'Deutsch'),
  471. 'en' => array('title' => 'English'),
  472. 'it' => array('title' => 'Italiano'),
  473. );
  474. $languageChange = __('Language') . ': ';
  475. $languages = array();
  476. foreach ($langs as $lang) {
  477. $languages[$lang] = $supportedLangs[$lang];
  478. }
  479. if ($sLang = (string)CakeSession::read('Config.language')) {
  480. $lang = $sLang;
  481. } else {
  482. $lang = '';
  483. }
  484. $languageChange .= '<span class="country">';
  485. foreach ($languages as $code => $la) {
  486. if ($lang === $code) {
  487. $languageChange .= $this->Html->image('language_flags/' . $code . '.gif', array('alt' => $code, 'title' => $la['title'] . ' (' . __('active') . ')', 'class' => 'country_flag active')) . '';
  488. } else {
  489. $languageChange .= $this->Html->link($this->Html->image('language_flags/' . $code . '.gif', array('alt' => $code, 'title' => $la['title'], 'class' => 'country_flag')), '/lang/' . $code, array('escape' => false)) . '';
  490. }
  491. }
  492. $languageChange .= '</span>'; //.__('(Translation not complete yet)');
  493. return $languageChange;
  494. }
  495. /**
  496. * It is still believed that encoding will stop spam-bots being able to find your email address.
  497. * Nevertheless, encoded email address harvester are on the way (http://www.dreamweaverfever.com/experiments/spam/).
  498. *
  499. * //TODO: move to TextExt?
  500. * Helper Function to Obfuscate Email by inserting a span tag (not more! not very secure on its own...)
  501. * each part of this mail now does not make sense anymore on its own
  502. * (striptags will not work either)
  503. *
  504. * @param string email: necessary (and valid - containing one @)
  505. * @return string
  506. */
  507. public function encodeEmail($mail) {
  508. list($mail1, $mail2) = explode('@', $mail);
  509. $encMail = $this->encodeText($mail1) . '<span>@</span>' . $this->encodeText($mail2);
  510. return $encMail;
  511. }
  512. /**
  513. * //TODO: move to TextExt?
  514. * Obfuscates Email (works without JS!) to avoid spam bots to get it
  515. *
  516. * @param string mail: email to encode
  517. * @param string text: optional (if none is given, email will be text as well)
  518. * @param array attributes: html tag attributes
  519. * @param array params: ?subject=y&body=y to be attached to "mailto:xyz"
  520. * @return string Save string with JS generated link around email (and non JS fallback)
  521. */
  522. public function encodeEmailUrl($mail, $text = null, $params = array(), $attr = array()) {
  523. if (empty($class)) { $class = 'email';}
  524. $defaults = array(
  525. 'title' => __('for use in an external mail client'),
  526. 'class' => 'email',
  527. 'escape' => false
  528. );
  529. if (empty($text)) {
  530. $text = $this->encodeEmail($mail);
  531. }
  532. $encMail = 'mailto:' . $mail;
  533. // additionally there could be a span tag in between: email<span syle="display:none"></span>@web.de
  534. $querystring = '';
  535. foreach ($params as $key => $val) {
  536. if ($querystring) {
  537. $querystring .= "&$key=" . rawurlencode($val);
  538. } else {
  539. $querystring = "?$key=" . rawurlencode($val);
  540. }
  541. }
  542. $attr = array_merge($defaults, $attr);
  543. $xmail = $this->Html->link('', $encMail . $querystring, $attr);
  544. $xmail1 = mb_substr($xmail, 0, count($xmail) - 5);
  545. $xmail2 = mb_substr($xmail, -4, 4);
  546. $len = mb_strlen($xmail1);
  547. $i = 0;
  548. while ($i < $len) {
  549. $c = mt_rand(2, 6);
  550. $par[] = (mb_substr($xmail1, $i, $c));
  551. $i += $c;
  552. }
  553. $join = implode('\'+ \'', $par);
  554. return '<script language=javascript><!--
  555. document.write(\'' . $join . '\');
  556. //--></script>
  557. ' . $text . '
  558. <script language=javascript><!--
  559. document.write(\'' . $xmail2 . '\');
  560. //--></script>';
  561. }
  562. /**
  563. * //TODO: move to TextExt?
  564. * Encodes Piece of Text (without usage of JS!) to avoid spam bots to get it
  565. *
  566. * @param STRING text to encode
  567. * @return string (randomly encoded)
  568. */
  569. public function encodeText($text) {
  570. $encmail = '';
  571. for ($i = 0; $i < mb_strlen($text); $i++) {
  572. $encMod = mt_rand(0, 2);
  573. switch ($encMod) {
  574. case 0: // None
  575. $encmail .= mb_substr($text, $i, 1);
  576. break;
  577. case 1: // Decimal
  578. $encmail .= "&#" . ord(mb_substr($text, $i, 1)) . ';';
  579. break;
  580. case 2: // Hexadecimal
  581. $encmail .= "&#x" . dechex(ord(mb_substr($text, $i, 1))) . ';';
  582. break;
  583. }
  584. }
  585. return $encmail;
  586. }
  587. /**
  588. * Display yes/no symbol.
  589. *
  590. * @todo $on=1, $text=false, $ontitle=false,... => in array(OPTIONS)
  591. *
  592. * @param text: default FALSE; if TRUE, text instead of the image
  593. * @param ontitle: default FALSE; if it is embadded in a link, set to TRUE
  594. * @return image:Yes/No or text:Yes/No
  595. */
  596. public function yesNo($v, $ontitle = null, $offtitle = null, $on = 1, $text = false, $notitle = false) {
  597. $ontitle = (!empty($ontitle) ? $ontitle : __('Yes'));
  598. $offtitle = (!empty($offtitle) ? $offtitle : __('No'));
  599. $sbez = array('0' => @substr($offtitle, 0, 1), '1' => @substr($ontitle, 0, 1));
  600. $bez = array('0' => $offtitle, '1' => $ontitle);
  601. if ($v == $on) {
  602. $icon = ICON_YES;
  603. $value = 1;
  604. } else {
  605. $icon = ICON_NO;
  606. $value = 0;
  607. }
  608. if ($text !== false) {
  609. return $bez[$value];
  610. }
  611. $options = array('title' => ($ontitle === false ? '' : $bez[$value]), 'alt' => $sbez[$value], 'class' => 'icon');
  612. if ($this->settings['fontIcons']) {
  613. return $this->cIcon($icon, $options['title']);
  614. }
  615. return $this->Html->image('icons/' . $icon, $options);
  616. }
  617. /**
  618. * Get URL of a png img of a website (16x16 pixel).
  619. *
  620. * @param string domain
  621. * @return string
  622. */
  623. public function siteIconUrl($domain) {
  624. if (strpos($domain, 'http') === 0) {
  625. // Strip protocol
  626. $pieces = parse_url($domain);
  627. $domain = $pieces['host'];
  628. }
  629. return 'http://www.google.com/s2/favicons?domain=' . $domain;
  630. }
  631. /**
  632. * Display a png img of a website (16x16 pixel)
  633. * if not available, will return a fallback image (a globe)
  634. *
  635. * @param domain (preferably without protocol, e.g. "www.site.com")
  636. * @return string
  637. */
  638. public function siteIcon($domain, $options = array()) {
  639. $url = $this->siteIconUrl($domain);
  640. $options['width'] = 16;
  641. $options['height'] = 16;
  642. if (!isset($options['alt'])) {
  643. $options['alt'] = $domain;
  644. }
  645. if (!isset($options['title'])) {
  646. $options['title'] = $domain;
  647. }
  648. return $this->Html->image($url, $options);
  649. }
  650. /**
  651. * Display text as image
  652. * //TODO: move to own helper
  653. *
  654. * @param string $text
  655. * @param array $options (for generation):
  656. * - inline, font, size, background (optional)
  657. * @param array $tagAttributes (for image)
  658. * @return string result - as image
  659. */
  660. public function textAsImage($text, $options = array(), $attr = array()) {
  661. /*
  662. $image = new Imagick();
  663. //$image->newImage(218, 46, new ImagickPixel('white'));
  664. $image->setImageCompression(10); // Keine Auswirkung auf Dicke
  665. $draw = new ImagickDraw();
  666. $draw->setFont($font);
  667. $draw->setFontSize(22.0); // Keine Auswirkung auf Dicke
  668. $draw->setFontWeight(100); // 0-999 Keine Auswirkung auf Dicke
  669. $draw->annotation(5, 20, $text);
  670. $image->drawImage($draw);
  671. $image->setImageResolution(1200, 1200); // Keine Auswirkung auf Dicke
  672. $image->setImageFormat('gif');
  673. $image->writeImage(TMP.'x.gif');
  674. $image->trim($mw,0);
  675. */
  676. $defaults = array('alt' => $text);
  677. $attr += $defaults;
  678. return $this->_textAsImage($text, $options, $attr);
  679. }
  680. /**
  681. * @return string htmlImage tag (or empty string on failure)
  682. */
  683. public function _textAsImage($text, $options = array(), $attr = array()) {
  684. $defaults = array('inline' => true, 'font' => FILES . 'linotype.ttf', 'size' => 18, 'color' => '#7A7166');
  685. $options += $defaults;
  686. if ($options['inline']) { // Inline base 64 encoded
  687. $folder = CACHE . 'imagick';
  688. } else {
  689. $folder = WWW_ROOT . 'img' . DS . 'content' . DS . 'imagick';
  690. }
  691. $file = sha1($text . serialize($options)) . '.' . ($options['inline'] || !empty($options['background']) ? 'png' : 'gif');
  692. if (!file_exists($folder)) {
  693. mkdir($folder, 0755);
  694. }
  695. if (!file_exists($folder . DS . $file)) {
  696. $command = 'convert -background ' . (!empty($options['background']) ? '"' . $options['background'] . '"' : 'transparent') . ' -font ' . $options['font'] . ' -fill ' . (!empty($options['color']) ? '"' . $options['color'] . '"' : 'transparent') . ' -pointsize ' . $options['size'] . ' label:"' . $text . '" ' . $folder . DS . $file;
  697. exec($command, $a, $r);
  698. if ($r !== 0) {
  699. return '';
  700. }
  701. }
  702. if ($options['inline']) {
  703. $res = file_get_contents($folder . DS . $file);
  704. $out = $this->Html->imageFromBlob($res, $attr);
  705. } else {
  706. $out = $this->Html->image($this->Html->url('/img/content/imagick/', true) . $file, $attr);
  707. }
  708. return $out;
  709. }
  710. /**
  711. * Display a disabled link tag
  712. *
  713. * @param string $text
  714. * @param array $options
  715. * @return string
  716. */
  717. public function disabledLink($text, $options = array()) {
  718. $defaults = array('class' => 'disabledLink', 'title' => __('notAvailable'));
  719. $options += $defaults;
  720. return $this->Html->tag('span', $text, $options);
  721. }
  722. /**
  723. * Generates a pagination count: #1 etc for each pagination record
  724. * respects order (ASC/DESC)
  725. *
  726. * @param array $paginator
  727. * @param int $count (current post count on this page)
  728. * @param string $dir (ASC/DESC)
  729. * @return int
  730. */
  731. public function absolutePaginateCount(array $paginator, $count, $dir = null) {
  732. if ($dir === null) {
  733. $dir = 'ASC';
  734. }
  735. $currentPage = $paginator['page'];
  736. $pageCount = $paginator['pageCount'];
  737. $totalCount = $paginator['count'];
  738. $limit = $paginator['limit'];
  739. $step = 1; //$paginator['step'];
  740. //pr($paginator);
  741. if ($dir === 'DESC') {
  742. $currentCount = $count + ($pageCount - $currentPage) * $limit * $step;
  743. if ($currentPage != $pageCount && $pageCount > 1) {
  744. $currentCount -= $pageCount * $limit * $step - $totalCount;
  745. }
  746. } else {
  747. $currentCount = $count + ($currentPage - 1) * $limit * $step;
  748. }
  749. return $currentCount;
  750. }
  751. /**
  752. * @param float progress
  753. * @param array options:
  754. * - min, max
  755. * - steps
  756. * - decimals (how precise should the result be displayed)
  757. * @return string HTML
  758. * @deprecated Try to use font icons or move to own helper
  759. */
  760. public function progressBar($progress, $options = array(), $htmlOptions = array()) {
  761. $defaults = array(
  762. 'min' => 0,
  763. 'max' => 100,
  764. 'steps' => 15,
  765. 'decimals' => 1 // TODO: rename to places!!!
  766. );
  767. $options += $defaults;
  768. $current = (((float)$progress / $options['max']) - $options['min']);
  769. $percent = $current * 100;
  770. $current *= $options['steps'];
  771. $options['progress'] = number_format($current, $options['decimals'], null, '');
  772. $params = Router::queryString($options, array(), true);
  773. $htmlDefaults = array(
  774. 'title' => $this->Numeric->format($percent, $options['decimals']) . ' ' . __('Percent'),
  775. 'class' => 'help');
  776. $htmlDefaults['alt'] = $htmlDefaults['title'];
  777. $htmlOptions += $htmlDefaults;
  778. //return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions);
  779. return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' .
  780. $htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />';
  781. }
  782. /**
  783. * FormatHelper::tip()
  784. *
  785. * @param mixed $type
  786. * @param mixed $file
  787. * @param mixed $title
  788. * @param mixed $icon
  789. * @return string
  790. * @deprecated Try to use font icons or move to own helper
  791. */
  792. public function tip($type, $file, $title, $icon) {
  793. return $this->cIcon($icon, $title, null, null, array('class' => 'tip-' . $type . ' tip' . ucfirst($type) . ' hand', 'rel' => $file));
  794. }
  795. /**
  796. * FormatHelper::tipHelp()
  797. *
  798. * @param mixed $file
  799. * @return string
  800. * @deprecated Try to use font icons or move to own helper
  801. */
  802. public function tipHelp($file) {
  803. return $this->tip('help', $file, 'Hilfe', ICON_HELP);
  804. }
  805. /**
  806. * Fixes utf8 problems of native php str_pad function
  807. * //TODO: move to textext helper?
  808. *
  809. * @param string $input
  810. * @param int $padLength
  811. * @param string $padString
  812. * @param mixed $padType
  813. * @return string input
  814. */
  815. public function pad($input, $padLength, $padString, $padType = STR_PAD_RIGHT) {
  816. $length = mb_strlen($input);
  817. if ($padLength - $length > 0) {
  818. switch ($padType) {
  819. case STR_PAD_LEFT:
  820. $input = str_repeat($padString, $padLength - $length) . $input;
  821. break;
  822. case STR_PAD_RIGHT:
  823. $input .= str_repeat($padString, $padLength - $length);
  824. break;
  825. }
  826. }
  827. return $input;
  828. }
  829. /**
  830. * Display traffic light for status etc
  831. *
  832. * @return void
  833. * @deprecated Try to use font icons or move to own helper
  834. */
  835. public function statusLight($color = null, $title = null, $alt = null, $options = array()) {
  836. $icons = array(
  837. 'green', 'yellow', 'red', 'blue'
  838. /*
  839. 'red' => array(
  840. 'title'=>'',
  841. 'alt'=>''
  842. ),
  843. */
  844. );
  845. $icon = (in_array($color, $icons) ? $color : 'blank');
  846. $defaults = array('title' => (!empty($title) ? $title : ucfirst(__('color' . ucfirst($color)))), 'alt' => (!empty($alt) ? $alt :
  847. __('color' . ucfirst($color))), 'class' => 'icon help');
  848. $options += $defaults;
  849. return $this->Html->image('icons/status_light_' . $icon . '.gif', $options);
  850. }
  851. /**
  852. * FormatHelper::onlineIcon()
  853. *
  854. * @param mixed $modified
  855. * @param mixed $options
  856. * @return string
  857. * @deprecated Try to use font icons or move to own helper
  858. */
  859. public function onlineIcon($modified = null, $options = array()) {
  860. // from low (off) to high (on)
  861. $icons = array('healthbar0.gif', 'healthbar1.gif', 'healthbar1b.gif', 'healthbar2.gif', 'healthbar3.gif', 'healthbar4.gif', 'healthbar5.gif');
  862. // default = offline
  863. $res = $icons[0]; // inactive
  864. $time = strtotime($modified);
  865. $timeAgo = time() - $time; // in seconds
  866. if ($timeAgo < 180) { // 3min // active
  867. $res = $icons[6];
  868. } elseif ($timeAgo < 360) { // 6min
  869. $res = $icons[5];
  870. } elseif ($timeAgo < 540) { // 9min
  871. $res = $icons[4];
  872. } elseif ($timeAgo < 720) { // 12min
  873. $res = $icons[3];
  874. } elseif ($timeAgo < 900) { // 15min
  875. $res = $icons[2];
  876. } elseif ($timeAgo < 1080) { // 18min
  877. $res = $icons[1];
  878. }
  879. return $this->Html->image('misc/' . $res, array('style' => 'width: 60px; height: 16px'));
  880. }
  881. /**
  882. * Returns red colored if not ok
  883. *
  884. * @param string $value
  885. * @param $okValue
  886. * @return string Value in HTML tags
  887. */
  888. public function warning($value, $ok = false) {
  889. if ($ok) {
  890. return $this->ok($value, false);
  891. }
  892. return $value;
  893. }
  894. /**
  895. * Returns green on ok, red otherwise
  896. *
  897. * @todo Remove inline css and make classes better: green=>ok red=>not-ok
  898. *
  899. * @param mixed $currentValue
  900. * @param bool $ok: true/false (defaults to false)
  901. * //@param string $comparizonType
  902. * //@param mixed $okValue
  903. * @return string newValue nicely formatted/colored
  904. */
  905. public function ok($value, $ok = false) {
  906. if ($ok) {
  907. $value = '<span class="green" style="color:green">' . $value . '</span>';
  908. } else {
  909. $value = '<span class="red" style="color:red">' . $value . '</span>';
  910. }
  911. return $value;
  912. }
  913. /**
  914. * test@test.de becomes t..t@t..t.de
  915. *
  916. * @param string $email: valid(!) email address
  917. * @return string
  918. */
  919. public static function hideEmail($mail) {
  920. $mailParts = explode('@', $mail, 2);
  921. $domainParts = explode('.', $mailParts[1], 2);
  922. $user = mb_substr($mailParts[0], 0, 1) . '..' . mb_substr($mailParts[0], -1, 1);
  923. $domain = mb_substr($domainParts[0], 0, 1) . '..' . mb_substr($domainParts[0], -1, 1) . '.' . $domainParts[1];
  924. return $user . '@' . $domain;
  925. }
  926. /**
  927. * (Intelligent) Shortening of a text string
  928. *
  929. * @param STRING textstring
  930. * @param int chars = max-length
  931. * For options array:
  932. * @param bool strict (default: FALSE = intelligent shortening, cutting only between whole words)
  933. * @param STRING ending (default: '...' no leading whitespace)
  934. * @param bool remain_lf (default: false = \n to ' ')
  935. * Note: ONLY If intelligent:
  936. * - the word supposed to be cut is removed completely (instead of remaining as last one)
  937. * - Looses line breaks (for textarea content to work with this)!
  938. * @deprecated use truncate instead
  939. */
  940. public function shortenText($textstring, $chars, $options = array()) {
  941. $chars++; // add +1 for correct cut
  942. $needsEnding = false;
  943. #Options
  944. $strict = false;
  945. $ending = CHAR_HELLIP; //'...';
  946. $remainLf = false; // not implemented: choose if LF transformed to ' '
  947. $class = 'help';
  948. $escape = true;
  949. $title = '';
  950. if (!empty($options) && is_array($options)) {
  951. if (!empty($options['strict']) && ($options['strict'] === true || $options['strict'] === false)) {
  952. $strict = $options['strict'];
  953. }
  954. if (!empty($options['remain_lf']) && ($options['remain_lf'] === true || $options['remain_lf'] === false)) {
  955. $remainLf = $options['remain_lf'];
  956. }
  957. if (isset($options['title'])) {
  958. $title = $options['title'];
  959. if ($options['title'] === true) {
  960. $title = $textstring;
  961. }
  962. }
  963. if (isset($options['class']) && $options['class'] === false) {
  964. $class = '';
  965. }
  966. if (isset($options['ending'])) {
  967. $ending = (string)$options['ending'];
  968. }
  969. if (isset($options['escape'])) {
  970. $escape = (bool)$options['escape'];
  971. }
  972. }
  973. $textstring = trim($textstring);
  974. // cut only between whole words
  975. if ($strict !== true) {
  976. $completeWordText = $textstring . ' ';
  977. // transform line breaks to whitespaces (for textarea content etc.)
  978. $completeWordTextLf = str_replace(LF, ' ', $completeWordText);
  979. $completeWordText = $completeWordTextLf;
  980. $completeWordText = substr($completeWordTextLf, 0, $chars);
  981. // round the text to the previous entire word instead of cutting off part way through a word
  982. $completeWordText = substr($completeWordText, 0, strrpos($completeWordText, ' '));
  983. }
  984. $textEnding = '';
  985. if ($strict !== true && strlen($completeWordText) > 1) {
  986. $text = trim($completeWordText);
  987. // add ending only if result is shorter then original
  988. if (strlen($text) < strlen(trim($completeWordTextLf))) {
  989. $textEnding = ' ' . $ending; // additional whitespace as there is a new word added
  990. }
  991. } else {
  992. $text = trim(substr($textstring, 0, $chars));
  993. // add ending only if result is shorter then original
  994. if (strlen($text) < strlen($textstring)) {
  995. $textEnding = $ending;
  996. }
  997. }
  998. if ($escape) {
  999. $text = h($text);
  1000. $title = h($title);
  1001. }
  1002. $text .= $textEnding;
  1003. #TitleIfTooLong
  1004. if (!empty($title)) {
  1005. $text = '<span ' . (!empty($class) ? 'class="' . $class . '" ' : '') . 'title="' . $title . '">' . $text . '</span>';
  1006. }
  1007. return $text;
  1008. }
  1009. /**
  1010. * Useful for displaying tabbed (code) content when the default of 8 spaces
  1011. * inside <pre> is too much. This converts it to spaces for better output.
  1012. *
  1013. * Inspired by the tab2space function found at:
  1014. * @see http://aidan.dotgeek.org/lib/?file=function.tab2space.php
  1015. * @param string $text
  1016. * @param int $spaces
  1017. * @return string
  1018. */
  1019. public function tab2space($text, $spaces = 4) {
  1020. $spaces = str_repeat(" ", $spaces);
  1021. $text = preg_split("/\r\n|\r|\n/", trim($text));
  1022. $wordLengths = array();
  1023. $wArray = array();
  1024. // Store word lengths
  1025. foreach ($text as $line) {
  1026. $words = preg_split("/(\t+)/", $line, -1, PREG_SPLIT_DELIM_CAPTURE);
  1027. foreach (array_keys($words) as $i) {
  1028. $strlen = strlen($words[$i]);
  1029. $add = isset($wordLengths[$i]) && ($wordLengths[$i] < $strlen);
  1030. if ($add || !isset($wordLengths[$i])) {
  1031. $wordLengths[$i] = $strlen;
  1032. }
  1033. }
  1034. $wArray[] = $words;
  1035. }
  1036. $text = '';
  1037. // Apply padding when appropriate and rebuild the string
  1038. foreach (array_keys($wArray) as $i) {
  1039. foreach (array_keys($wArray[$i]) as $ii) {
  1040. if (preg_match("/^\t+$/", $wArray[$i][$ii])) {
  1041. $wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii], "\t");
  1042. } else {
  1043. $wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii]);
  1044. }
  1045. }
  1046. $text .= str_replace("\t", $spaces, implode("", $wArray[$i])) . "\n";
  1047. }
  1048. return $text;
  1049. }
  1050. /**
  1051. * Word Censoring Function
  1052. *
  1053. * Supply a string and an array of disallowed words and any
  1054. * matched words will be converted to #### or to the replacement
  1055. * word you've submitted.
  1056. *
  1057. * @todo Move to Text Helper etc.
  1058. *
  1059. * @param string the text string
  1060. * @param string the array of censoered words
  1061. * @param string the optional replacement value
  1062. * @return string
  1063. */
  1064. public function wordCensor($str, $censored, $replacement = null) {
  1065. if (empty($censored)) {
  1066. return $str;
  1067. }
  1068. $str = ' ' . $str . ' ';
  1069. // \w, \b and a few others do not match on a unicode character
  1070. // set for performance reasons. As a result words like ..ber
  1071. // will not match on a word boundary. Instead, we'll assume that
  1072. // a bad word will be bookended by any of these characters.
  1073. $delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
  1074. foreach ($censored as $badword) {
  1075. if ($replacement !== null) {
  1076. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
  1077. } else {
  1078. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",
  1079. $str);
  1080. }
  1081. }
  1082. return trim($str);
  1083. }
  1084. /**
  1085. * Translate a result array into a HTML table
  1086. *
  1087. * @todo Move to Text Helper etc.
  1088. *
  1089. * @author Aidan Lister <aidan@php.net>
  1090. * @version 1.3.2
  1091. * @link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
  1092. * @param array $array The result (numericaly keyed, associative inner) array.
  1093. * @param bool $recursive Recursively generate tables for multi-dimensional arrays
  1094. * @param string $null String to output for blank cells
  1095. */
  1096. public function array2table($array, $options = array()) {
  1097. $defaults = array(
  1098. 'null' => '&nbsp;',
  1099. 'recursive' => false,
  1100. 'heading' => true,
  1101. 'escape' => true
  1102. );
  1103. $options += $defaults;
  1104. // Sanity check
  1105. if (empty($array) || !is_array($array)) {
  1106. return false;
  1107. }
  1108. if (!isset($array[0]) || !is_array($array[0])) {
  1109. $array = array($array);
  1110. }
  1111. // Start the table
  1112. $table = "<table>\n";
  1113. if ($options['heading']) {
  1114. // The header
  1115. $table .= "\t<tr>";
  1116. // Take the keys from the first row as the headings
  1117. foreach (array_keys($array[0]) as $heading) {
  1118. $table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
  1119. }
  1120. $table .= "</tr>\n";
  1121. }
  1122. // The body
  1123. foreach ($array as $row) {
  1124. $table .= "\t<tr>";
  1125. foreach ($row as $cell) {
  1126. $table .= '<td>';
  1127. // Cast objects
  1128. if (is_object($cell)) {
  1129. $cell = (array)$cell;
  1130. }
  1131. if ($options['recursive'] && is_array($cell) && !empty($cell)) {
  1132. // Recursive mode
  1133. $table .= "\n" . self::array2table($cell, $options) . "\n";
  1134. } else {
  1135. $table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h($cell) : $cell) : $options['null'];
  1136. }
  1137. $table .= '</td>';
  1138. }
  1139. $table .= "</tr>\n";
  1140. }
  1141. $table .= '</table>';
  1142. return $table;
  1143. }
  1144. public $icons = array(
  1145. 'up' => array(
  1146. 'pic' => ICON_UP,
  1147. 'title' => 'Up',
  1148. ),
  1149. 'down' => array(
  1150. 'pic' => ICON_DOWN,
  1151. 'title' => 'Down',
  1152. ),
  1153. 'edit' => array(
  1154. 'pic' => ICON_EDIT,
  1155. 'title' => 'Edit',
  1156. ),
  1157. 'view' => array(
  1158. 'pic' => ICON_VIEW,
  1159. 'title' => 'View',
  1160. ),
  1161. 'delete' => array(
  1162. 'pic' => ICON_DELETE,
  1163. 'title' => 'Delete',
  1164. ),
  1165. 'reset' => array(
  1166. 'pic' => ICON_RESET,
  1167. 'title' => 'Reset',
  1168. ),
  1169. 'help' => array(
  1170. 'pic' => ICON_HELP,
  1171. 'title' => 'Help',
  1172. ),
  1173. 'loader' => array(
  1174. 'pic' => 'loader.white.gif',
  1175. 'title' => 'Loading...',
  1176. ),
  1177. 'loader-alt' => array(
  1178. 'pic' => 'loader.black.gif',
  1179. 'title' => 'Loading...',
  1180. ),
  1181. 'details' => array(
  1182. 'pic' => ICON_DETAILS,
  1183. 'title' => 'Details',
  1184. ),
  1185. 'use' => array(
  1186. 'pic' => ICON_USE,
  1187. 'title' => 'Use',
  1188. ),
  1189. 'yes' => array(
  1190. 'pic' => ICON_YES,
  1191. 'title' => 'Yes',
  1192. ),
  1193. 'no' => array(
  1194. 'pic' => ICON_NO,
  1195. 'title' => 'No',
  1196. ),
  1197. // deprecated from here down
  1198. 'close' => array(
  1199. 'pic' => ICON_CLOCK,
  1200. 'title' => 'Close',
  1201. ),
  1202. 'reply' => array(
  1203. 'pic' => ICON_REPLY,
  1204. 'title' => 'Reply',
  1205. ),
  1206. 'time' => array(
  1207. 'pic' => ICON_CLOCK,
  1208. 'title' => 'Time',
  1209. ),
  1210. 'check' => array(
  1211. 'pic' => ICON_CHECK,
  1212. 'title' => 'Check',
  1213. ),
  1214. 'role' => array(
  1215. 'pic' => ICON_ROLE,
  1216. 'title' => 'Role',
  1217. ),
  1218. 'add' => array(
  1219. 'pic' => ICON_ADD,
  1220. 'title' => 'Add',
  1221. ),
  1222. 'remove' => array(
  1223. 'pic' => ICON_REMOVE,
  1224. 'title' => 'Remove',
  1225. ),
  1226. 'email' => array(
  1227. 'pic' => ICON_EMAIL,
  1228. 'title' => 'Email',
  1229. ),
  1230. 'options' => array(
  1231. 'pic' => ICON_SETTINGS,
  1232. 'title' => 'Options',
  1233. ),
  1234. 'lock' => array(
  1235. 'pic' => ICON_LOCK,
  1236. 'title' => 'Locked',
  1237. ),
  1238. 'warning' => array(
  1239. 'pic' => ICON_WARNING,
  1240. 'title' => 'Warning',
  1241. ),
  1242. 'genderUnknown' => array(
  1243. 'pic' => 'gender_icon.gif',
  1244. 'title' => 'genderUnknown',
  1245. ),
  1246. 'genderMale' => array(
  1247. 'pic' => 'gender_icon_m.gif',
  1248. 'title' => 'genderMale',
  1249. ),
  1250. 'genderFemale' => array(
  1251. 'pic' => 'gender_icon_f.gif',
  1252. 'title' => 'genderFemale',
  1253. ),
  1254. );
  1255. }
  1256. // Default icons
  1257. if (!defined('ICON_UP')) {
  1258. define('ICON_UP', 'up.gif');
  1259. }
  1260. if (!defined('ICON_DOWN')) {
  1261. define('ICON_DOWN', 'down.gif');
  1262. }
  1263. if (!defined('ICON_EDIT')) {
  1264. define('ICON_EDIT', 'edit.gif');
  1265. }
  1266. if (!defined('ICON_VIEW')) {
  1267. define('ICON_VIEW', 'see.gif');
  1268. }
  1269. if (!defined('ICON_DELETE')) {
  1270. define('ICON_DELETE', 'delete.gif');
  1271. }
  1272. if (!defined('ICON_DETAILS')) {
  1273. define('ICON_DETAILS', 'loupe.gif');
  1274. }
  1275. if (!defined('ICON_OPTIONS')) {
  1276. define('ICON_OPTIONS', 'options.gif');
  1277. }
  1278. if (!defined('ICON_SETTINGS')) {
  1279. define('ICON_SETTINGS', 'options.gif');
  1280. }
  1281. if (!defined('ICON_USE')) {
  1282. define('ICON_USE', 'use.gif');
  1283. }
  1284. if (!defined('ICON_CLOSE')) {
  1285. define('ICON_CLOSE', 'close.gif');
  1286. }
  1287. if (!defined('ICON_REPLY')) {
  1288. define('ICON_REPLY', 'reply.gif');
  1289. }
  1290. if (!defined('ICON_RESET')) {
  1291. define('ICON_RESET', 'reset.gif');
  1292. }
  1293. if (!defined('ICON_HELP')) {
  1294. define('ICON_HELP', 'help.gif');
  1295. }
  1296. if (!defined('ICON_YES')) {
  1297. define('ICON_YES', 'yes.gif');
  1298. }
  1299. if (!defined('ICON_NO')) {
  1300. define('ICON_NO', 'no.gif');
  1301. }
  1302. if (!defined('ICON_CLOCK')) {
  1303. define('ICON_CLOCK', 'clock.gif');
  1304. }
  1305. if (!defined('ICON_CHECK')) {
  1306. define('ICON_CHECK', 'check.gif');
  1307. }
  1308. if (!defined('ICON_ROLE')) {
  1309. define('ICON_ROLE', 'role.gif');
  1310. }
  1311. if (!defined('ICON_ADD')) {
  1312. define('ICON_ADD', 'add.gif');
  1313. }
  1314. if (!defined('ICON_REMOVE')) {
  1315. define('ICON_REMOVE', 'remove.gif');
  1316. }
  1317. if (!defined('ICON_EMAIL')) {
  1318. define('ICON_EMAIL', 'email.gif');
  1319. }
  1320. if (!defined('ICON_LOCK')) {
  1321. define('ICON_LOCK', 'lock.gif');
  1322. }
  1323. if (!defined('ICON_WARNING')) {
  1324. define('ICON_WARNING', 'warning.png');
  1325. }
  1326. if (!defined('ICON_MAP')) {
  1327. define('ICON_MAP', 'map.gif');
  1328. }