FormatHelper.php 44 KB

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