FormatHelper.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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, ['title' => false]) . '&nbsp;' . __d('tools', 'prev' . $name), $url, ['escape' => false, 'title' => $neighbors['prev'][$titleAlias][$titleField]]);
  128. } else {
  129. $ret .= $this->cIcon(ICON_PREV_DISABLED, ['title' => __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, ['title' => false]) . '&nbsp;' . __d('tools', 'next' . $name), $url, ['escape' => false, 'title' => $neighbors['next'][$titleAlias][$titleField]]);
  138. } else {
  139. $ret .= $this->cIcon(ICON_NEXT_DISABLED, ['title' => __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. * Params $on, $text are deprecated
  615. *
  616. * @param int|bool $value Value
  617. * @param array $options
  618. * - on (defaults to 1/true)
  619. * - onTitle
  620. * - offTitle
  621. * @param array $attributes
  622. * - title, ...
  623. * @return string HTML icon Yes/No
  624. */
  625. public function yesNo($value, $options = [], $attributes = [], $on = 1, $text = false) {
  626. $defaults = [
  627. 'on' => 1,
  628. 'onTitle' => __d('tools', 'Yes'),
  629. 'offTitle' => __d('tools', 'No'),
  630. 'text' => false
  631. ];
  632. if (!is_array($options)) {
  633. $onTitle = $options ?: null;
  634. $options = [
  635. 'on' => $on,
  636. 'text' => $text,
  637. ];
  638. if ($onTitle) {
  639. $options['onTitle'] = $onTitle;
  640. }
  641. trigger_error('Deprecated, use array syntax', E_USER_DEPRECATED);
  642. }
  643. if (!is_array($attributes)) {
  644. $options['offTitle'] = $attributes;
  645. }
  646. $options += $defaults;
  647. $sbez = ['0' => @substr($options['offTitle'], 0, 1), '1' => @substr($options['onTitle'], 0, 1)];
  648. $bez = ['0' => $options['offTitle'], '1' => $options['onTitle']];
  649. if ($value == $options['on']) {
  650. $icon = ICON_YES;
  651. $value = 1;
  652. } else {
  653. $icon = ICON_NO;
  654. $value = 0;
  655. }
  656. if ($options['text'] !== false) {
  657. return $bez[$value];
  658. }
  659. $options = ['title' => ($options['onTitle'] === false ? '' : $bez[$value]), 'alt' => $sbez[$value], 'class' => 'icon'];
  660. if ($this->settings['fontIcons']) {
  661. return $this->cIcon($icon, ['title' => $options['title']]);
  662. }
  663. return $this->Html->image('icons/' . $icon, $options);
  664. }
  665. /**
  666. * Get URL of a png img of a website (16x16 pixel).
  667. *
  668. * @param string domain
  669. * @return string
  670. */
  671. public function siteIconUrl($domain) {
  672. if (strpos($domain, 'http') === 0) {
  673. // Strip protocol
  674. $pieces = parse_url($domain);
  675. $domain = $pieces['host'];
  676. }
  677. return 'http://www.google.com/s2/favicons?domain=' . $domain;
  678. }
  679. /**
  680. * Display a png img of a website (16x16 pixel)
  681. * if not available, will return a fallback image (a globe)
  682. *
  683. * @param string $domain (preferably without protocol, e.g. "www.site.com")
  684. * @param array $options
  685. * @return string
  686. */
  687. public function siteIcon($domain, $options = []) {
  688. $url = $this->siteIconUrl($domain);
  689. $options['width'] = 16;
  690. $options['height'] = 16;
  691. if (!isset($options['alt'])) {
  692. $options['alt'] = $domain;
  693. }
  694. if (!isset($options['title'])) {
  695. $options['title'] = $domain;
  696. }
  697. return $this->Html->image($url, $options);
  698. }
  699. /**
  700. * Display text as image
  701. *
  702. * @param string $text
  703. * @param array $options (for generation):
  704. * - inline, font, size, background (optional)
  705. * @param array $tagAttributes (for image)
  706. * @return string result - as image
  707. * @deprecated Must be a different helper in the future
  708. */
  709. public function textAsImage($text, $options = [], $tagAttributes = []) {
  710. /*
  711. $image = new Imagick();
  712. //$image->newImage(218, 46, new ImagickPixel('white'));
  713. $image->setImageCompression(10); // Keine Auswirkung auf Dicke
  714. $draw = new ImagickDraw();
  715. $draw->setFont($font);
  716. $draw->setFontSize(22.0); // Keine Auswirkung auf Dicke
  717. $draw->setFontWeight(100); // 0-999 Keine Auswirkung auf Dicke
  718. $draw->annotation(5, 20, $text);
  719. $image->drawImage($draw);
  720. $image->setImageResolution(1200, 1200); // Keine Auswirkung auf Dicke
  721. $image->setImageFormat('gif');
  722. $image->writeImage(TMP.'x.gif');
  723. $image->trim($mw,0);
  724. */
  725. $defaults = ['alt' => $text];
  726. $tagAttributes += $defaults;
  727. return $this->_textAsImage($text, $options, $tagAttributes);
  728. }
  729. /**
  730. * @param string $text
  731. * @param array $options
  732. * @param array $attr
  733. * @return string htmlImage tag (or empty string on failure)
  734. * @deprecated Must be a different helper in the future
  735. */
  736. public function _textAsImage($text, $options = [], $attr = []) {
  737. $defaults = ['inline' => true, 'font' => FILES . 'linotype.ttf', 'size' => 18, 'color' => '#7A7166'];
  738. $options += $defaults;
  739. if ($options['inline']) { // Inline base 64 encoded
  740. $folder = CACHE . 'imagick';
  741. } else {
  742. $folder = WWW_ROOT . 'img' . DS . 'content' . DS . 'imagick';
  743. }
  744. $file = sha1($text . serialize($options)) . '.' . ($options['inline'] || !empty($options['background']) ? 'png' : 'gif');
  745. if (!file_exists($folder)) {
  746. mkdir($folder, 0755);
  747. }
  748. if (!file_exists($folder . DS . $file)) {
  749. $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;
  750. exec($command, $a, $r);
  751. if ($r !== 0) {
  752. return '';
  753. }
  754. }
  755. if ($options['inline']) {
  756. $res = file_get_contents($folder . DS . $file);
  757. $out = $this->Html->imageFromBlob($res, $attr);
  758. } else {
  759. $out = $this->Html->image($this->Html->url('/img/content/imagick/', true) . $file, $attr);
  760. }
  761. return $out;
  762. }
  763. /**
  764. * Display a disabled link tag
  765. *
  766. * @param string $text
  767. * @param array $options
  768. * @return string
  769. */
  770. public function disabledLink($text, $options = []) {
  771. $defaults = ['class' => 'disabledLink', 'title' => __d('tools', 'notAvailable')];
  772. $options += $defaults;
  773. return $this->Html->tag('span', $text, $options);
  774. }
  775. /**
  776. * Generates a pagination count: #1 etc for each pagination record
  777. * respects order (ASC/DESC)
  778. *
  779. * @param array $paginator
  780. * @param int $count (current post count on this page)
  781. * @param string $dir (ASC/DESC)
  782. * @return int
  783. */
  784. public function absolutePaginateCount(array $paginator, $count, $dir = null) {
  785. if ($dir === null) {
  786. $dir = 'ASC';
  787. }
  788. $currentPage = $paginator['page'];
  789. $pageCount = $paginator['pageCount'];
  790. $totalCount = $paginator['count'];
  791. $limit = $paginator['limit'];
  792. $step = 1; //$paginator['step'];
  793. //pr($paginator);
  794. if ($dir === 'DESC') {
  795. $currentCount = $count + ($pageCount - $currentPage) * $limit * $step;
  796. if ($currentPage != $pageCount && $pageCount > 1) {
  797. $currentCount -= $pageCount * $limit * $step - $totalCount;
  798. }
  799. } else {
  800. $currentCount = $count + ($currentPage - 1) * $limit * $step;
  801. }
  802. return $currentCount;
  803. }
  804. /**
  805. * @param float $progress
  806. * @param array $options:
  807. * - min, max
  808. * - steps
  809. * - decimals (how precise should the result be displayed)
  810. * @return string HTML
  811. * @deprecated Try to use font icons or move to own helper
  812. */
  813. public function progressBar($progress, $options = [], $htmlOptions = []) {
  814. $defaults = [
  815. 'min' => 0,
  816. 'max' => 100,
  817. 'steps' => 15,
  818. 'decimals' => 1 // TODO: rename to places!!!
  819. ];
  820. $options += $defaults;
  821. $current = (((float)$progress / $options['max']) - $options['min']);
  822. $percent = $current * 100;
  823. $current *= $options['steps'];
  824. $options['progress'] = number_format($current, $options['decimals'], null, '');
  825. $params = Router::queryString($options, [], true);
  826. $htmlDefaults = [
  827. 'title' => $this->Numeric->format($percent, $options['decimals']) . ' ' . __d('tools', 'Percent'),
  828. 'class' => 'help'];
  829. $htmlDefaults['alt'] = $htmlDefaults['title'];
  830. $htmlOptions += $htmlDefaults;
  831. //return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions);
  832. return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' .
  833. $htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />';
  834. }
  835. /**
  836. * FormatHelper::tip()
  837. *
  838. * @param mixed $type
  839. * @param mixed $file
  840. * @param mixed $title
  841. * @param mixed $icon
  842. * @return string
  843. * @deprecated Try to use font icons or move to own helper
  844. */
  845. public function tip($type, $file, $title, $icon) {
  846. return $this->cIcon($icon, ['title' => $title], ['class' => 'tip-' . $type . ' tip' . ucfirst($type) . ' hand', 'rel' => $file]);
  847. }
  848. /**
  849. * FormatHelper::tipHelp()
  850. *
  851. * @param mixed $file
  852. * @return string
  853. * @deprecated Try to use font icons or move to own helper
  854. */
  855. public function tipHelp($file) {
  856. return $this->tip('help', $file, __d('tools', 'Help'), ICON_HELP);
  857. }
  858. /**
  859. * Fixes utf8 problems of native php str_pad function
  860. * //TODO: move to textext helper?
  861. *
  862. * @param string $input
  863. * @param int $padLength
  864. * @param string $padString
  865. * @param mixed $padType
  866. * @return string input
  867. */
  868. public function pad($input, $padLength, $padString, $padType = STR_PAD_RIGHT) {
  869. $length = mb_strlen($input);
  870. if ($padLength - $length > 0) {
  871. switch ($padType) {
  872. case STR_PAD_LEFT:
  873. $input = str_repeat($padString, $padLength - $length) . $input;
  874. break;
  875. case STR_PAD_RIGHT:
  876. $input .= str_repeat($padString, $padLength - $length);
  877. break;
  878. }
  879. }
  880. return $input;
  881. }
  882. /**
  883. * Display traffic light for status etc
  884. *
  885. * @return void
  886. * @deprecated Try to use font icons or move to own helper
  887. */
  888. public function statusLight($color = null, $title = null, $alt = null, $options = []) {
  889. $icons = [
  890. 'green', 'yellow', 'red', 'blue'
  891. /*
  892. 'red' => array(
  893. 'title'=>'',
  894. 'alt'=>''
  895. ),
  896. */
  897. ];
  898. $icon = (in_array($color, $icons) ? $color : 'blank');
  899. $defaults = ['title' => (!empty($title) ? $title : ucfirst(__d('tools', 'color' . ucfirst($color)))), 'alt' => (!empty($alt) ? $alt :
  900. __d('tools', 'color' . ucfirst($color))), 'class' => 'icon help'];
  901. $options += $defaults;
  902. return $this->Html->image('icons/status_light_' . $icon . '.gif', $options);
  903. }
  904. /**
  905. * FormatHelper::onlineIcon()
  906. *
  907. * @param mixed $modified
  908. * @param mixed $options
  909. * @return string
  910. * @deprecated Try to use font icons or move to own helper
  911. */
  912. public function onlineIcon($modified = null, $options = []) {
  913. // from low (off) to high (on)
  914. $icons = ['healthbar0.gif', 'healthbar1.gif', 'healthbar1b.gif', 'healthbar2.gif', 'healthbar3.gif', 'healthbar4.gif', 'healthbar5.gif'];
  915. // default = offline
  916. $res = $icons[0]; // inactive
  917. $time = strtotime($modified);
  918. $timeAgo = time() - $time; // in seconds
  919. if ($timeAgo < 180) { // 3min // active
  920. $res = $icons[6];
  921. } elseif ($timeAgo < 360) { // 6min
  922. $res = $icons[5];
  923. } elseif ($timeAgo < 540) { // 9min
  924. $res = $icons[4];
  925. } elseif ($timeAgo < 720) { // 12min
  926. $res = $icons[3];
  927. } elseif ($timeAgo < 900) { // 15min
  928. $res = $icons[2];
  929. } elseif ($timeAgo < 1080) { // 18min
  930. $res = $icons[1];
  931. }
  932. return $this->Html->image('misc/' . $res, ['style' => 'width: 60px; height: 16px']);
  933. }
  934. /**
  935. * Returns red colored if not ok
  936. *
  937. * @param string $value
  938. * @param $okValue
  939. * @return string Value in HTML tags
  940. */
  941. public function warning($value, $ok = false) {
  942. if (!$ok) {
  943. return $this->ok($value, false);
  944. }
  945. return $value;
  946. }
  947. /**
  948. * Returns green on ok, red otherwise
  949. *
  950. * @todo Remove inline css and make classes better: green=>ok red=>not-ok
  951. *
  952. * @param mixed $currentValue
  953. * @param bool $ok: true/false (defaults to false)
  954. * //@param string $comparizonType
  955. * //@param mixed $okValue
  956. * @return string newValue nicely formatted/colored
  957. */
  958. public function ok($value, $ok = false) {
  959. if ($ok) {
  960. $value = '<span class="green" style="color:green">' . $value . '</span>';
  961. } else {
  962. $value = '<span class="red" style="color:red">' . $value . '</span>';
  963. }
  964. return $value;
  965. }
  966. /**
  967. * test@test.de becomes t..t@t..t.de
  968. *
  969. * @param string $email: valid(!) email address
  970. * @return string
  971. */
  972. public static function hideEmail($mail) {
  973. $mailParts = explode('@', $mail, 2);
  974. $domainParts = explode('.', $mailParts[1], 2);
  975. $user = mb_substr($mailParts[0], 0, 1) . '..' . mb_substr($mailParts[0], -1, 1);
  976. $domain = mb_substr($domainParts[0], 0, 1) . '..' . mb_substr($domainParts[0], -1, 1) . '.' . $domainParts[1];
  977. return $user . '@' . $domain;
  978. }
  979. /**
  980. * (Intelligent) Shortening of a text string
  981. *
  982. * @param STRING textstring
  983. * @param int chars = max-length
  984. * For options array:
  985. * @param bool strict (default: FALSE = intelligent shortening, cutting only between whole words)
  986. * @param STRING ending (default: '...' no leading whitespace)
  987. * @param bool remain_lf (default: false = \n to ' ')
  988. * Note: ONLY If intelligent:
  989. * - the word supposed to be cut is removed completely (instead of remaining as last one)
  990. * - Looses line breaks (for textarea content to work with this)!
  991. * @deprecated use truncate instead
  992. */
  993. public function shortenText($textstring, $chars, $options = []) {
  994. $chars++; // add +1 for correct cut
  995. $needsEnding = false;
  996. #Options
  997. $strict = false;
  998. $ending = CHAR_HELLIP; //'...';
  999. $remainLf = false; // not implemented: choose if LF transformed to ' '
  1000. $class = 'help';
  1001. $escape = true;
  1002. $title = '';
  1003. if (!empty($options) && is_array($options)) {
  1004. if (!empty($options['strict']) && ($options['strict'] === true || $options['strict'] === false)) {
  1005. $strict = $options['strict'];
  1006. }
  1007. if (!empty($options['remain_lf']) && ($options['remain_lf'] === true || $options['remain_lf'] === false)) {
  1008. $remainLf = $options['remain_lf'];
  1009. }
  1010. if (isset($options['title'])) {
  1011. $title = $options['title'];
  1012. if ($options['title'] === true) {
  1013. $title = $textstring;
  1014. }
  1015. }
  1016. if (isset($options['class']) && $options['class'] === false) {
  1017. $class = '';
  1018. }
  1019. if (isset($options['ending'])) {
  1020. $ending = (string)$options['ending'];
  1021. }
  1022. if (isset($options['escape'])) {
  1023. $escape = (bool)$options['escape'];
  1024. }
  1025. }
  1026. $textstring = trim($textstring);
  1027. // cut only between whole words
  1028. if ($strict !== true) {
  1029. $completeWordText = $textstring . ' ';
  1030. // transform line breaks to whitespaces (for textarea content etc.)
  1031. $completeWordTextLf = str_replace(LF, ' ', $completeWordText);
  1032. $completeWordText = $completeWordTextLf;
  1033. $completeWordText = substr($completeWordTextLf, 0, $chars);
  1034. // round the text to the previous entire word instead of cutting off part way through a word
  1035. $completeWordText = substr($completeWordText, 0, strrpos($completeWordText, ' '));
  1036. }
  1037. $textEnding = '';
  1038. if ($strict !== true && strlen($completeWordText) > 1) {
  1039. $text = trim($completeWordText);
  1040. // add ending only if result is shorter then original
  1041. if (strlen($text) < strlen(trim($completeWordTextLf))) {
  1042. $textEnding = ' ' . $ending; // additional whitespace as there is a new word added
  1043. }
  1044. } else {
  1045. $text = trim(substr($textstring, 0, $chars));
  1046. // add ending only if result is shorter then original
  1047. if (strlen($text) < strlen($textstring)) {
  1048. $textEnding = $ending;
  1049. }
  1050. }
  1051. if ($escape) {
  1052. $text = h($text);
  1053. $title = h($title);
  1054. }
  1055. $text .= $textEnding;
  1056. #TitleIfTooLong
  1057. if (!empty($title)) {
  1058. $text = '<span ' . (!empty($class) ? 'class="' . $class . '" ' : '') . 'title="' . $title . '">' . $text . '</span>';
  1059. }
  1060. return $text;
  1061. }
  1062. /**
  1063. * Useful for displaying tabbed (code) content when the default of 8 spaces
  1064. * inside <pre> is too much. This converts it to spaces for better output.
  1065. *
  1066. * Inspired by the tab2space function found at:
  1067. * @see http://aidan.dotgeek.org/lib/?file=function.tab2space.php
  1068. * @param string $text
  1069. * @param int $spaces
  1070. * @return string
  1071. */
  1072. public function tab2space($text, $spaces = 4) {
  1073. $spaces = str_repeat(" ", $spaces);
  1074. $text = preg_split("/\r\n|\r|\n/", trim($text));
  1075. $wordLengths = [];
  1076. $wArray = [];
  1077. // Store word lengths
  1078. foreach ($text as $line) {
  1079. $words = preg_split("/(\t+)/", $line, -1, PREG_SPLIT_DELIM_CAPTURE);
  1080. foreach (array_keys($words) as $i) {
  1081. $strlen = strlen($words[$i]);
  1082. $add = isset($wordLengths[$i]) && ($wordLengths[$i] < $strlen);
  1083. if ($add || !isset($wordLengths[$i])) {
  1084. $wordLengths[$i] = $strlen;
  1085. }
  1086. }
  1087. $wArray[] = $words;
  1088. }
  1089. $text = '';
  1090. // Apply padding when appropriate and rebuild the string
  1091. foreach (array_keys($wArray) as $i) {
  1092. foreach (array_keys($wArray[$i]) as $ii) {
  1093. if (preg_match("/^\t+$/", $wArray[$i][$ii])) {
  1094. $wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii], "\t");
  1095. } else {
  1096. $wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii]);
  1097. }
  1098. }
  1099. $text .= str_replace("\t", $spaces, implode("", $wArray[$i])) . "\n";
  1100. }
  1101. return $text;
  1102. }
  1103. /**
  1104. * Word Censoring Function
  1105. *
  1106. * Supply a string and an array of disallowed words and any
  1107. * matched words will be converted to #### or to the replacement
  1108. * word you've submitted.
  1109. *
  1110. * @todo Move to Text Helper etc.
  1111. *
  1112. * @param string the text string
  1113. * @param string the array of censoered words
  1114. * @param string the optional replacement value
  1115. * @return string
  1116. */
  1117. public function wordCensor($str, $censored, $replacement = null) {
  1118. if (empty($censored)) {
  1119. return $str;
  1120. }
  1121. $str = ' ' . $str . ' ';
  1122. // \w, \b and a few others do not match on a unicode character
  1123. // set for performance reasons. As a result words like ..ber
  1124. // will not match on a word boundary. Instead, we'll assume that
  1125. // a bad word will be bookended by any of these characters.
  1126. $delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
  1127. foreach ($censored as $badword) {
  1128. if ($replacement !== null) {
  1129. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
  1130. } else {
  1131. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",
  1132. $str);
  1133. }
  1134. }
  1135. return trim($str);
  1136. }
  1137. /**
  1138. * Translate a result array into a HTML table
  1139. *
  1140. * @todo Move to Text Helper etc.
  1141. *
  1142. * @author Aidan Lister <aidan@php.net>
  1143. * @version 1.3.2
  1144. * @link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
  1145. * @param array $array The result (numericaly keyed, associative inner) array.
  1146. * @param bool $recursive Recursively generate tables for multi-dimensional arrays
  1147. * @param string $null String to output for blank cells
  1148. */
  1149. public function array2table($array, $options = []) {
  1150. $defaults = [
  1151. 'null' => '&nbsp;',
  1152. 'recursive' => false,
  1153. 'heading' => true,
  1154. 'escape' => true
  1155. ];
  1156. $options += $defaults;
  1157. // Sanity check
  1158. if (empty($array) || !is_array($array)) {
  1159. return false;
  1160. }
  1161. if (!isset($array[0]) || !is_array($array[0])) {
  1162. $array = [$array];
  1163. }
  1164. // Start the table
  1165. $table = "<table>\n";
  1166. if ($options['heading']) {
  1167. // The header
  1168. $table .= "\t<tr>";
  1169. // Take the keys from the first row as the headings
  1170. foreach (array_keys($array[0]) as $heading) {
  1171. $table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
  1172. }
  1173. $table .= "</tr>\n";
  1174. }
  1175. // The body
  1176. foreach ($array as $row) {
  1177. $table .= "\t<tr>";
  1178. foreach ($row as $cell) {
  1179. $table .= '<td>';
  1180. // Cast objects
  1181. if (is_object($cell)) {
  1182. $cell = (array)$cell;
  1183. }
  1184. if ($options['recursive'] && is_array($cell) && !empty($cell)) {
  1185. // Recursive mode
  1186. $table .= "\n" . static::array2table($cell, $options) . "\n";
  1187. } else {
  1188. $table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h($cell) : $cell) : $options['null'];
  1189. }
  1190. $table .= '</td>';
  1191. }
  1192. $table .= "</tr>\n";
  1193. }
  1194. $table .= '</table>';
  1195. return $table;
  1196. }
  1197. public $icons = [
  1198. 'up' => [
  1199. 'pic' => ICON_UP,
  1200. 'title' => 'Up',
  1201. ],
  1202. 'down' => [
  1203. 'pic' => ICON_DOWN,
  1204. 'title' => 'Down',
  1205. ],
  1206. 'edit' => [
  1207. 'pic' => ICON_EDIT,
  1208. 'title' => 'Edit',
  1209. ],
  1210. 'view' => [
  1211. 'pic' => ICON_VIEW,
  1212. 'title' => 'View',
  1213. ],
  1214. 'delete' => [
  1215. 'pic' => ICON_DELETE,
  1216. 'title' => 'Delete',
  1217. ],
  1218. 'reset' => [
  1219. 'pic' => ICON_RESET,
  1220. 'title' => 'Reset',
  1221. ],
  1222. 'help' => [
  1223. 'pic' => ICON_HELP,
  1224. 'title' => 'Help',
  1225. ],
  1226. 'loader' => [
  1227. 'pic' => 'loader.white.gif',
  1228. 'title' => 'Loading...',
  1229. ],
  1230. 'loader-alt' => [
  1231. 'pic' => 'loader.black.gif',
  1232. 'title' => 'Loading...',
  1233. ],
  1234. 'details' => [
  1235. 'pic' => ICON_DETAILS,
  1236. 'title' => 'Details',
  1237. ],
  1238. 'use' => [
  1239. 'pic' => ICON_USE,
  1240. 'title' => 'Use',
  1241. ],
  1242. 'yes' => [
  1243. 'pic' => ICON_YES,
  1244. 'title' => 'Yes',
  1245. ],
  1246. 'no' => [
  1247. 'pic' => ICON_NO,
  1248. 'title' => 'No',
  1249. ],
  1250. // deprecated from here down
  1251. 'close' => [
  1252. 'pic' => ICON_CLOCK,
  1253. 'title' => 'Close',
  1254. ],
  1255. 'reply' => [
  1256. 'pic' => ICON_REPLY,
  1257. 'title' => 'Reply',
  1258. ],
  1259. 'time' => [
  1260. 'pic' => ICON_CLOCK,
  1261. 'title' => 'Time',
  1262. ],
  1263. 'check' => [
  1264. 'pic' => ICON_CHECK,
  1265. 'title' => 'Check',
  1266. ],
  1267. 'role' => [
  1268. 'pic' => ICON_ROLE,
  1269. 'title' => 'Role',
  1270. ],
  1271. 'add' => [
  1272. 'pic' => ICON_ADD,
  1273. 'title' => 'Add',
  1274. ],
  1275. 'remove' => [
  1276. 'pic' => ICON_REMOVE,
  1277. 'title' => 'Remove',
  1278. ],
  1279. 'email' => [
  1280. 'pic' => ICON_EMAIL,
  1281. 'title' => 'Email',
  1282. ],
  1283. 'options' => [
  1284. 'pic' => ICON_SETTINGS,
  1285. 'title' => 'Options',
  1286. ],
  1287. 'lock' => [
  1288. 'pic' => ICON_LOCK,
  1289. 'title' => 'Locked',
  1290. ],
  1291. 'warning' => [
  1292. 'pic' => ICON_WARNING,
  1293. 'title' => 'Warning',
  1294. ],
  1295. 'genderUnknown' => [
  1296. 'pic' => 'gender_icon.gif',
  1297. 'title' => 'genderUnknown',
  1298. ],
  1299. 'genderMale' => [
  1300. 'pic' => 'gender_icon_m.gif',
  1301. 'title' => 'genderMale',
  1302. ],
  1303. 'genderFemale' => [
  1304. 'pic' => 'gender_icon_f.gif',
  1305. 'title' => 'genderFemale',
  1306. ],
  1307. ];
  1308. }
  1309. // Default icons
  1310. if (!defined('ICON_UP')) {
  1311. define('ICON_UP', 'up.gif');
  1312. }
  1313. if (!defined('ICON_DOWN')) {
  1314. define('ICON_DOWN', 'down.gif');
  1315. }
  1316. if (!defined('ICON_EDIT')) {
  1317. define('ICON_EDIT', 'edit.gif');
  1318. }
  1319. if (!defined('ICON_VIEW')) {
  1320. define('ICON_VIEW', 'see.gif');
  1321. }
  1322. if (!defined('ICON_DELETE')) {
  1323. define('ICON_DELETE', 'delete.gif');
  1324. }
  1325. if (!defined('ICON_DETAILS')) {
  1326. define('ICON_DETAILS', 'loupe.gif');
  1327. }
  1328. if (!defined('ICON_OPTIONS')) {
  1329. define('ICON_OPTIONS', 'options.gif');
  1330. }
  1331. if (!defined('ICON_SETTINGS')) {
  1332. define('ICON_SETTINGS', 'options.gif');
  1333. }
  1334. if (!defined('ICON_USE')) {
  1335. define('ICON_USE', 'use.gif');
  1336. }
  1337. if (!defined('ICON_CLOSE')) {
  1338. define('ICON_CLOSE', 'close.gif');
  1339. }
  1340. if (!defined('ICON_REPLY')) {
  1341. define('ICON_REPLY', 'reply.gif');
  1342. }
  1343. if (!defined('ICON_RESET')) {
  1344. define('ICON_RESET', 'reset.gif');
  1345. }
  1346. if (!defined('ICON_HELP')) {
  1347. define('ICON_HELP', 'help.gif');
  1348. }
  1349. if (!defined('ICON_YES')) {
  1350. define('ICON_YES', 'yes.gif');
  1351. }
  1352. if (!defined('ICON_NO')) {
  1353. define('ICON_NO', 'no.gif');
  1354. }
  1355. if (!defined('ICON_CLOCK')) {
  1356. define('ICON_CLOCK', 'clock.gif');
  1357. }
  1358. if (!defined('ICON_CHECK')) {
  1359. define('ICON_CHECK', 'check.gif');
  1360. }
  1361. if (!defined('ICON_ROLE')) {
  1362. define('ICON_ROLE', 'role.gif');
  1363. }
  1364. if (!defined('ICON_ADD')) {
  1365. define('ICON_ADD', 'add.gif');
  1366. }
  1367. if (!defined('ICON_REMOVE')) {
  1368. define('ICON_REMOVE', 'remove.gif');
  1369. }
  1370. if (!defined('ICON_EMAIL')) {
  1371. define('ICON_EMAIL', 'email.gif');
  1372. }
  1373. if (!defined('ICON_LOCK')) {
  1374. define('ICON_LOCK', 'lock.gif');
  1375. }
  1376. if (!defined('ICON_WARNING')) {
  1377. define('ICON_WARNING', 'warning.png');
  1378. }
  1379. if (!defined('ICON_MAP')) {
  1380. define('ICON_MAP', 'map.gif');
  1381. }
  1382. if (!defined('ICON_PREV')) {
  1383. define('ICON_PREV', 'prev.png');
  1384. }
  1385. if (!defined('ICON_NEXT')) {
  1386. define('ICON_NEXT', 'next.png');
  1387. }
  1388. if (!defined('ICON_NEXT_DISABLED')) {
  1389. define('ICON_NEXT_DISABLED', 'nav_forward_grey.png');
  1390. }
  1391. if (!defined('ICON_PREV_DISABLED')) {
  1392. define('ICON_PREV_DISABLED', 'nav_back_grey.png');
  1393. }