FormatHelper.php 44 KB

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