FormatHelper.php 47 KB

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