FormatHelper.php 46 KB

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