FormatHelper.php 41 KB

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