FormatHelper.php 41 KB

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