FormatHelper.php 46 KB

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