FormatHelper.php 41 KB

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