FormatHelper.php 41 KB

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