FormatHelper.php 44 KB

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