FormatHelper.php 44 KB

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