FormatHelper.php 42 KB

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