FormatHelper.php 41 KB

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