PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/newssystem/class.FormatLongString.inc.php

http://flaimo-php.googlecode.com/
PHP | 403 lines | 121 code | 48 blank | 234 comment | 7 complexity | 9333cd9204858a5158f1066bdad111f5 MD5 | raw file
  1. <?php
  2. /**
  3. * Base class
  4. */
  5. include_once('class.FormatString.inc.php');
  6. /**
  7. * Some batch functions for long strings based on the FormatString class
  8. *
  9. * @access public
  10. * @author Michael Wimmer <flaimo@gmx.net>
  11. * @copyright Michael Wimmer
  12. * @link http://www.flaimo.com/ flaimo.com
  13. * @package FLP
  14. * @see FormatString
  15. * @version 1.000
  16. */
  17. class FormatLongString extends FormatString
  18. {
  19. /*-------------------*/
  20. /* V A R I A B L E S */
  21. /*-------------------*/
  22. /**
  23. * Startingpoint where the displayed string should start. You can leave this at 0 if you want the text to start from the beginning
  24. *
  25. * @var int
  26. * @access private
  27. * @see AbstractText()
  28. */
  29. var $slashdot_start;
  30. /**
  31. * The minimum of characters a text must have before the AbstractText function starts working
  32. *
  33. * @var int
  34. * @access private
  35. * @see AbstractText()
  36. */
  37. var $max_length_start;
  38. /**
  39. * The maximum of characters that should be displayed then
  40. *
  41. * @var int
  42. * @access private
  43. * @see AbstractText()
  44. */
  45. var $counter_start;
  46. /**
  47. * Minimum length the shortend text must have
  48. *
  49. * @var int
  50. * @access private
  51. * @see AbstractText()
  52. */
  53. var $min_length_start;
  54. /**
  55. * +/- how many characters (starting from the first searchterm appearance) should be shown in the search result text
  56. *
  57. * @var int
  58. * @access private
  59. * @see SearchResult()
  60. */
  61. var $walk_start;
  62. /**
  63. * Character(s) to be displayed between the shortend text and the "more"
  64. *
  65. * @var string
  66. * @access private
  67. * @see SearchResult(), AbstractText()
  68. */
  69. var $seperator;
  70. /**
  71. * Name for the variable to be attached to the "more" link
  72. *
  73. * @var mixed
  74. * @access private
  75. * @see SearchResult(), AbstractText()
  76. */
  77. var $id;
  78. /**
  79. * Name of the page where the full text is displayed
  80. *
  81. * @var string
  82. * @access private
  83. * @see SearchResult(), AbstractText()
  84. */
  85. var $showpage;
  86. /*-----------------------*/
  87. /* C O N S T R U C T O R */
  88. /*-----------------------*/
  89. /**
  90. * Constructor
  91. *
  92. * Calls the constructor of the base class + sets some basic values
  93. *
  94. * @param (string) $input_highlight String that should be highlighted with html tags
  95. * @return (void)
  96. * @access private
  97. * @see FormatString::FormatString()
  98. * @since 1.000 - 2002/10/10
  99. */
  100. function FormatLongString($input_highlight = '')
  101. {
  102. parent::FormatString($input_highlight); // passes the optional searchterm to the parent class.
  103. $this->max_length_start = (int) 800;
  104. $this->counter_start = (int) 500;
  105. $this->min_length_start = (int) 50;
  106. $this->walk_start = (int) 150;
  107. $this->slashdot_start = 0;
  108. $this->seperator = (string) '&#8230;';
  109. $this->id = (string) 'id';
  110. $this->showpage = (string) $_SERVER['PHP_SELF'];
  111. include('ads.inc.php');
  112. } // end function
  113. /*-------------------*/
  114. /* F U N C T I O N S */
  115. /*-------------------*/
  116. /**
  117. * +/- how many characters (starting from the first searchterm appearance) should be shown in the search result text
  118. *
  119. * @return (int) $walk_start
  120. * @access public
  121. * @see $walk_start
  122. * @since 1.000 - 2002/10/10
  123. */
  124. function getWalkStart()
  125. {
  126. return (int) $this->walk_start;
  127. } // end function
  128. /**
  129. * The minimum of characters a text must have before the AbstractText function starts working
  130. *
  131. * @return (int) $max_length_start
  132. * @access public
  133. * @see $max_length_start
  134. * @since 1.000 - 2002/10/10
  135. */
  136. function getMaxLengthStart()
  137. {
  138. return (int) $this->max_length_start;
  139. } // end function
  140. /**
  141. * The maximum of characters that should be displayed then
  142. *
  143. * @return (int) $counter_start
  144. * @access public
  145. * @see $counter_start
  146. * @since 1.000 - 2002/10/10
  147. */
  148. function getCounterStart()
  149. {
  150. return (int) $this->counter_start;
  151. } // end function
  152. /**
  153. * Minimum length the shortend text must have
  154. *
  155. * @return (int) $min_length_start
  156. * @access public
  157. * @see $min_length_start
  158. * @since 1.000 - 2002/10/10
  159. */
  160. function getMinLengthStart()
  161. {
  162. return (int) $this->min_length_start;
  163. } // end function
  164. /**
  165. * Startingpoint where the displayed string should start
  166. *
  167. * @return (int) $slashdot_start
  168. * @access public
  169. * @see $slashdot_start
  170. * @since 1.000 - 2002/10/10
  171. */
  172. function getSlashDotStart()
  173. {
  174. return (int) $this->slashdot_start;
  175. } // end function
  176. /**
  177. * Returns the "more" in the langage set
  178. *
  179. * @return (string) $more
  180. * @access public
  181. * @see setMore(); $more
  182. * @since 1.000 - 2002/10/10
  183. */
  184. function getMore()
  185. {
  186. return (string) $this->more;
  187. } // end function
  188. /**
  189. * Name of the page where the full text is displayed
  190. *
  191. * @return (string) $showpage
  192. * @access public
  193. * @see $showpage
  194. * @since 1.000 - 2002/10/10
  195. */
  196. function getShowpage()
  197. {
  198. return (string) $this->showpage;
  199. } // end function
  200. /**
  201. * Name for the variable to be attached to the "more" link
  202. *
  203. * @return (mixed) $id
  204. * @access public
  205. * @see $id
  206. * @since 1.000 - 2002/10/10
  207. */
  208. function getID()
  209. {
  210. return (string) $this->id;
  211. } // end function
  212. /**
  213. * Character(s) to be displayed between the shortend text and the "more"
  214. *
  215. * @return (string) $seperator
  216. * @access public
  217. * @see $seperator
  218. * @since 1.000 - 2002/10/10
  219. */
  220. function getSeperator()
  221. {
  222. return (string) $this->seperator;
  223. } // end function
  224. /**
  225. * Sets the "more" in the langage set
  226. *
  227. * @param (string) $string
  228. * @return (void)
  229. * @access public
  230. * @see getMore(), $more
  231. * @since 1.000 - 2002/10/10
  232. */
  233. function setMore($string)
  234. {
  235. $this->more = (string) $string;
  236. } // end function
  237. //------------------------------------------------
  238. /**
  239. * Combines a buch of functions from the parent class to format long strings
  240. *
  241. * @param (string) $text
  242. * @return (string) Transformed input-string
  243. * @access public
  244. * @since 1.000 - 2002/10/10
  245. */
  246. function FormatLongText($text) // PHP5: public
  247. {
  248. return (string) parent::SpecialChar(wordwrap(parent::SpecialWords(parent::Highlight(nl2br(htmlentities(parent::WordFilter(strip_tags($text)))))),75));
  249. } // end function
  250. /**
  251. * Combines a buch of functions from the parent class to format one-liners
  252. *
  253. * @param (string) $string
  254. * @return (string) Transformed input-string
  255. * @access public
  256. * @since 1.000 - 2002/10/10
  257. */
  258. function FormatShortText($string)
  259. {
  260. return (string) trim(parent::SpecialChar(nl2br(parent::Highlight(htmlentities(strip_tags($string))))));
  261. } // end function
  262. //------------------------------------------------
  263. /**
  264. * Displays only a small part of a longer string
  265. *
  266. * then searches for the last dot in this string. There the text
  267. * is cut again and and a "... [more] is attached with a link to the
  268. * full version of the text.
  269. * To change the "more" to your language, please take a look at my
  270. * SelectLanguage Class that is required in the FormatString class
  271. * anyway.
  272. *
  273. * @param (string) $text
  274. * @param (int) $id ID number of the article
  275. * @return (string) $text Transformed input-string (shortend text ... [more])
  276. * @access public
  277. * @since 1.000 - 2002/10/10
  278. */
  279. function AbstractText($text,$id = 1) // PHP5: public
  280. {
  281. $lastdot = (int) $this->getSlashDotStart();
  282. $counter = (int) $this->getCounterStart();
  283. $text_raw = $text = (string) parent::WordFilter($text); // First use "badwords" filter
  284. if (strlen($text) > $this->getMaxLengthStart()) // Checks if text is long enough fur cutting
  285. {
  286. while ($lastdot < $this->getMinLengthStart())
  287. {
  288. $shorttext_raw = (string) substr($text, 0, $counter);
  289. $lastdot = (int) strrpos($shorttext_raw,'.'); // Searches for the last do in the string
  290. $counter += (int) 50; // Stringsize is encreased by 50 characters if no dot is found or the shortend text is below 50 characters
  291. } // end while
  292. $text = (string) substr($text, 0, $lastdot); // Final cutting of the original text
  293. } // end if
  294. // Bunch of Stringfilters.... strlen(trim($id))
  295. $text = (string) parent::SpecialChar(wordwrap(parent::SpecialWords(parent::Highlight(nl2br(htmlentities(strip_tags($text))))),75));
  296. if (strlen($text_raw) > $this->getMaxLengthStart() and strlen(trim($id)) > 0) { $text .= (string) ' ' . $this->getSeperator() . ' <a href="' . $this->getShowpage() . '?' . $this->getID() . '=' . $id . '">' . $this->lg->__('more_news') . '</a>'; } // Adds a link for the full text and the "...[more]"
  297. return (string) $text;
  298. } // end function
  299. //------------------------------------------------
  300. /**
  301. * Displays only a small part of the string where a serachterm was found
  302. *
  303. * Takes a string, looks for the first appearance of the search term
  304. * and shortens the text +/- a certain amount of characters from that
  305. * point. Also adds a "more" like in the AbstractText function
  306. *
  307. * @param (string) $text
  308. * @param (int) $id ID number of the article
  309. * @return (string) Transformed input-string (shortend text ... [more])
  310. * @access public
  311. * @since 1.000 - 2002/10/10
  312. */
  313. function SearchResult($text, $id = 1)
  314. {
  315. $text_raw = (string) $text;
  316. $temp = stristr($text, parent::getHighlightWord());
  317. $searchfound = (int) strlen($text) - strlen($temp);
  318. $startcut = (int) ((($searchfound - $this->getWalkStart()) <= 0) ? 0 : ($searchfound - $this->getWalkStart())); // +/- characters from the searchterm
  319. $endcut = (int) ((($searchfound + $this->getWalkStart()) >= (strlen($text) - strlen(parent::getHighlightWord()))) ? strlen($text) : ($searchfound+$this->getWalkStart()));
  320. $text_raw = (string) substr($text, $startcut, ($endcut-$startcut));
  321. $firstend = (int) (($startcut == 0) ? 0 : strpos($text_raw,' ')); // Looks for space character
  322. $lastend = (int) ((($searchfound + $this->getWalkStart()) >= (strlen($text) - strlen(parent::getHighlightWord()))) ? $endcut : strrpos($text_raw,' '));
  323. $text_raw = (string) substr($text_raw, $firstend, ($lastend-$firstend));
  324. $firstdots = (string) (($startcut != 0) ? $this->getSeperator() . ' ' : ''); // Adds dots in front of the shortend string
  325. $lastdots = (string) (($lastend != $endcut) ? ' ' . $this->getSeperator() : ''); // Adds dots at the end of the shortend string
  326. $text_raw = (string) parent::SpecialChar(wordwrap(parent::SpecialWords(parent::Highlight(nl2br(htmlentities(strip_tags($text_raw))))),75));
  327. return (string) $firstdots . $text_raw . $lastdots . ' <a href="' . $this->getShowpage() . '?' . $this->getID() . '=' . $id . parent::SetURLencoder(parent::getCSSHighlight(),addslashes(urlencode(parent::getHighlightWord())),'&') . '">' . $this->lg->__('more_news') . '</a>';
  328. } // end function
  329. //------------------------------------------------
  330. /**
  331. * Adds text-ads to strings
  332. *
  333. * This function should check a string for certain words, make a
  334. * link out of them and mark them as an ad. Somehow crashes the php
  335. * engine sometimes. Must be used AFTER htmlentities/nl2br/wordwrap
  336. *
  337. * @param (string) $text
  338. * @return (string) $string Transformed input-string
  339. * @access public
  340. * @deprec 1.000 - 2002/10/10
  341. * @since 1.000 - 2002/10/09
  342. */
  343. function HighlightAds($string) // must be used AFTER htmlentities/nl2br/wordwrap // Bringt Apache zum abstürzten // PHP5: public
  344. {
  345. if (isset($this->adlist[$this->lg->getLangContent()]))
  346. {
  347. foreach ($this->adlist[$this->lg->getLangContent()] as $ad => $link)
  348. {
  349. $string = (string) str_replace(htmlentities($ad), '<a href="' . $link . '" class="ad" title="Paid Advertisement Link" target="_blank">' . htmlentities($ad) . '<span class="ad">°</span></a>', $string , 1);
  350. } // end foreach
  351. } // end if
  352. return (string) $string;
  353. } // end function
  354. } // end class FormatLongString
  355. ?>