FormatHelper.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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 $str The text string
  1113. * @param string $censored The array of censoered words
  1114. * @param string|null $replacement 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_callback(
  1132. "/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i",
  1133. function ($x) {
  1134. return $x[1] . str_repeat('#', strlen($x[2])) . $x[3];
  1135. },
  1136. $str
  1137. );
  1138. }
  1139. }
  1140. return trim($str);
  1141. }
  1142. /**
  1143. * Translate a result array into a HTML table
  1144. *
  1145. * @todo Move to Text Helper etc.
  1146. *
  1147. * @author Aidan Lister <aidan@php.net>
  1148. * @version 1.3.2
  1149. * @link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
  1150. * @param array $array The result (numericaly keyed, associative inner) array.
  1151. * @param bool $recursive Recursively generate tables for multi-dimensional arrays
  1152. * @param string $null String to output for blank cells
  1153. */
  1154. public function array2table($array, $options = []) {
  1155. $defaults = [
  1156. 'null' => '&nbsp;',
  1157. 'recursive' => false,
  1158. 'heading' => true,
  1159. 'escape' => true
  1160. ];
  1161. $options += $defaults;
  1162. // Sanity check
  1163. if (empty($array) || !is_array($array)) {
  1164. return false;
  1165. }
  1166. if (!isset($array[0]) || !is_array($array[0])) {
  1167. $array = [$array];
  1168. }
  1169. // Start the table
  1170. $table = "<table>\n";
  1171. if ($options['heading']) {
  1172. // The header
  1173. $table .= "\t<tr>";
  1174. // Take the keys from the first row as the headings
  1175. foreach (array_keys($array[0]) as $heading) {
  1176. $table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
  1177. }
  1178. $table .= "</tr>\n";
  1179. }
  1180. // The body
  1181. foreach ($array as $row) {
  1182. $table .= "\t<tr>";
  1183. foreach ($row as $cell) {
  1184. $table .= '<td>';
  1185. // Cast objects
  1186. if (is_object($cell)) {
  1187. $cell = (array)$cell;
  1188. }
  1189. if ($options['recursive'] && is_array($cell) && !empty($cell)) {
  1190. // Recursive mode
  1191. $table .= "\n" . static::array2table($cell, $options) . "\n";
  1192. } else {
  1193. $table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h($cell) : $cell) : $options['null'];
  1194. }
  1195. $table .= '</td>';
  1196. }
  1197. $table .= "</tr>\n";
  1198. }
  1199. $table .= '</table>';
  1200. return $table;
  1201. }
  1202. public $icons = [
  1203. 'up' => [
  1204. 'pic' => ICON_UP,
  1205. 'title' => 'Up',
  1206. ],
  1207. 'down' => [
  1208. 'pic' => ICON_DOWN,
  1209. 'title' => 'Down',
  1210. ],
  1211. 'edit' => [
  1212. 'pic' => ICON_EDIT,
  1213. 'title' => 'Edit',
  1214. ],
  1215. 'view' => [
  1216. 'pic' => ICON_VIEW,
  1217. 'title' => 'View',
  1218. ],
  1219. 'delete' => [
  1220. 'pic' => ICON_DELETE,
  1221. 'title' => 'Delete',
  1222. ],
  1223. 'reset' => [
  1224. 'pic' => ICON_RESET,
  1225. 'title' => 'Reset',
  1226. ],
  1227. 'help' => [
  1228. 'pic' => ICON_HELP,
  1229. 'title' => 'Help',
  1230. ],
  1231. 'loader' => [
  1232. 'pic' => 'loader.white.gif',
  1233. 'title' => 'Loading...',
  1234. ],
  1235. 'loader-alt' => [
  1236. 'pic' => 'loader.black.gif',
  1237. 'title' => 'Loading...',
  1238. ],
  1239. 'details' => [
  1240. 'pic' => ICON_DETAILS,
  1241. 'title' => 'Details',
  1242. ],
  1243. 'use' => [
  1244. 'pic' => ICON_USE,
  1245. 'title' => 'Use',
  1246. ],
  1247. 'yes' => [
  1248. 'pic' => ICON_YES,
  1249. 'title' => 'Yes',
  1250. ],
  1251. 'no' => [
  1252. 'pic' => ICON_NO,
  1253. 'title' => 'No',
  1254. ],
  1255. // deprecated from here down
  1256. 'close' => [
  1257. 'pic' => ICON_CLOCK,
  1258. 'title' => 'Close',
  1259. ],
  1260. 'reply' => [
  1261. 'pic' => ICON_REPLY,
  1262. 'title' => 'Reply',
  1263. ],
  1264. 'time' => [
  1265. 'pic' => ICON_CLOCK,
  1266. 'title' => 'Time',
  1267. ],
  1268. 'check' => [
  1269. 'pic' => ICON_CHECK,
  1270. 'title' => 'Check',
  1271. ],
  1272. 'role' => [
  1273. 'pic' => ICON_ROLE,
  1274. 'title' => 'Role',
  1275. ],
  1276. 'add' => [
  1277. 'pic' => ICON_ADD,
  1278. 'title' => 'Add',
  1279. ],
  1280. 'remove' => [
  1281. 'pic' => ICON_REMOVE,
  1282. 'title' => 'Remove',
  1283. ],
  1284. 'email' => [
  1285. 'pic' => ICON_EMAIL,
  1286. 'title' => 'Email',
  1287. ],
  1288. 'options' => [
  1289. 'pic' => ICON_SETTINGS,
  1290. 'title' => 'Options',
  1291. ],
  1292. 'lock' => [
  1293. 'pic' => ICON_LOCK,
  1294. 'title' => 'Locked',
  1295. ],
  1296. 'warning' => [
  1297. 'pic' => ICON_WARNING,
  1298. 'title' => 'Warning',
  1299. ],
  1300. 'genderUnknown' => [
  1301. 'pic' => 'gender_icon.gif',
  1302. 'title' => 'genderUnknown',
  1303. ],
  1304. 'genderMale' => [
  1305. 'pic' => 'gender_icon_m.gif',
  1306. 'title' => 'genderMale',
  1307. ],
  1308. 'genderFemale' => [
  1309. 'pic' => 'gender_icon_f.gif',
  1310. 'title' => 'genderFemale',
  1311. ],
  1312. ];
  1313. }
  1314. // Default icons
  1315. if (!defined('ICON_UP')) {
  1316. define('ICON_UP', 'up.gif');
  1317. }
  1318. if (!defined('ICON_DOWN')) {
  1319. define('ICON_DOWN', 'down.gif');
  1320. }
  1321. if (!defined('ICON_EDIT')) {
  1322. define('ICON_EDIT', 'edit.gif');
  1323. }
  1324. if (!defined('ICON_VIEW')) {
  1325. define('ICON_VIEW', 'see.gif');
  1326. }
  1327. if (!defined('ICON_DELETE')) {
  1328. define('ICON_DELETE', 'delete.gif');
  1329. }
  1330. if (!defined('ICON_DETAILS')) {
  1331. define('ICON_DETAILS', 'loupe.gif');
  1332. }
  1333. if (!defined('ICON_OPTIONS')) {
  1334. define('ICON_OPTIONS', 'options.gif');
  1335. }
  1336. if (!defined('ICON_SETTINGS')) {
  1337. define('ICON_SETTINGS', 'options.gif');
  1338. }
  1339. if (!defined('ICON_USE')) {
  1340. define('ICON_USE', 'use.gif');
  1341. }
  1342. if (!defined('ICON_CLOSE')) {
  1343. define('ICON_CLOSE', 'close.gif');
  1344. }
  1345. if (!defined('ICON_REPLY')) {
  1346. define('ICON_REPLY', 'reply.gif');
  1347. }
  1348. if (!defined('ICON_RESET')) {
  1349. define('ICON_RESET', 'reset.gif');
  1350. }
  1351. if (!defined('ICON_HELP')) {
  1352. define('ICON_HELP', 'help.gif');
  1353. }
  1354. if (!defined('ICON_YES')) {
  1355. define('ICON_YES', 'yes.gif');
  1356. }
  1357. if (!defined('ICON_NO')) {
  1358. define('ICON_NO', 'no.gif');
  1359. }
  1360. if (!defined('ICON_CLOCK')) {
  1361. define('ICON_CLOCK', 'clock.gif');
  1362. }
  1363. if (!defined('ICON_CHECK')) {
  1364. define('ICON_CHECK', 'check.gif');
  1365. }
  1366. if (!defined('ICON_ROLE')) {
  1367. define('ICON_ROLE', 'role.gif');
  1368. }
  1369. if (!defined('ICON_ADD')) {
  1370. define('ICON_ADD', 'add.gif');
  1371. }
  1372. if (!defined('ICON_REMOVE')) {
  1373. define('ICON_REMOVE', 'remove.gif');
  1374. }
  1375. if (!defined('ICON_EMAIL')) {
  1376. define('ICON_EMAIL', 'email.gif');
  1377. }
  1378. if (!defined('ICON_LOCK')) {
  1379. define('ICON_LOCK', 'lock.gif');
  1380. }
  1381. if (!defined('ICON_WARNING')) {
  1382. define('ICON_WARNING', 'warning.png');
  1383. }
  1384. if (!defined('ICON_MAP')) {
  1385. define('ICON_MAP', 'map.gif');
  1386. }
  1387. if (!defined('ICON_PREV')) {
  1388. define('ICON_PREV', 'nav_back.png');
  1389. }
  1390. if (!defined('ICON_NEXT')) {
  1391. define('ICON_NEXT', 'nav_forward.png');
  1392. }
  1393. if (!defined('ICON_PREV_DISABLED')) {
  1394. define('ICON_PREV_DISABLED', 'nav_back_grey.png');
  1395. }
  1396. if (!defined('ICON_NEXT_DISABLED')) {
  1397. define('ICON_NEXT_DISABLED', 'nav_forward_grey.png');
  1398. }