PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/ditto/extenders/summary.extender.inc.php

https://github.com/macarthy/tinymce
PHP | 329 lines | 174 code | 31 blank | 124 comment | 62 complexity | 2a1de9d47da20dfc3137fb425ace954b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Title: Summary
  4. * Purpose:
  5. * Legacy support for the [+summary+] placeholder
  6. */
  7. $placeholders['summary'] = array("introtext,content","determineSummary","@GLOBAL ditto_summary_type");
  8. $placeholders['link'] = array("id","determineLink");
  9. $trunc = isset($trunc) ? $trunc : 1;
  10. /*
  11. Param: trunc
  12. Purpose:
  13. Enable truncation on the summary placeholder
  14. Options:
  15. 0 - off
  16. 1 - on
  17. Default:
  18. 1 - on
  19. */
  20. $splitter = isset($truncAt) ? $truncAt : "<!-- splitter -->";
  21. /*
  22. Param: truncAt
  23. Purpose:
  24. Location to split the content at
  25. Options:
  26. Any unique text or code string that is contained
  27. in the content of each document
  28. Default:
  29. "<!-- splitter -->"
  30. */
  31. $length = isset($truncLen) ? $truncLen : 300;
  32. /*
  33. Param: truncLen
  34. Purpose:
  35. Number of characters to show of the content
  36. Options:
  37. Any number greater than <truncOffset>
  38. Default:
  39. 300
  40. */
  41. $offset = isset($truncOffset) ? $truncOffset : 30;
  42. /*
  43. Param: truncOffset
  44. Purpose:
  45. Number of charactars to "wander" either way of <truncLen>
  46. Options:
  47. Any number greater less than <truncLen>
  48. Default:
  49. 30
  50. */
  51. $text = isset($truncText)? $truncText : "Read more...";
  52. /*
  53. Param: truncText
  54. Purpose:
  55. Text to be displayed in [+link+]
  56. Options:
  57. Any valid text or html
  58. Default:
  59. "Read more..."
  60. */
  61. $trunc_tpl = isset($tplTrunc)? template::fetch($tplTrunc) : false;
  62. /*
  63. Param: tplTrunc
  64. Purpose:
  65. Template to be used for [+link+]
  66. Options:
  67. - Any valid chunk name
  68. - Code via @CODE:
  69. - File via @FILE:
  70. Placeholders:
  71. [+url+] - URL of the document
  72. [+text+] - &truncText
  73. Default:
  74. &truncText
  75. */
  76. $GLOBALS['ditto_summary_link'] = "";
  77. $GLOBALS['ditto_summary_params'] = compact("trunc","splitter","length","offset","text","trunc_tpl");
  78. $GLOBALS['ditto_object'] = $ditto;
  79. // ---------------------------------------------------
  80. // Truncate Functions
  81. // ---------------------------------------------------
  82. if (!function_exists("determineLink")) {
  83. function determineLink($resource) {
  84. global $ditto_object,$ditto_summary_params,$ditto_summary_link;
  85. if ($ditto_summary_link !== false) {
  86. $parameters = array(
  87. "url" => $ditto_summary_link,
  88. "text" => $ditto_summary_params["text"],
  89. );
  90. $tplTrunc = $ditto_summary_params["trunc_tpl"];
  91. if ($tplTrunc !== false) {
  92. $source = $tplTrunc;
  93. } else {
  94. $source = '<a href="[+url+]" title="[+text+]">[+text+]</a>';
  95. }
  96. return $ditto_object->template->replace($parameters,$source);
  97. } else {
  98. return '';
  99. }
  100. }
  101. }
  102. if (!function_exists("determineSummary")) {
  103. function determineSummary($resource) {
  104. global $ditto_summary_params;
  105. $trunc = new truncate();
  106. $p = $ditto_summary_params;
  107. $output = $trunc->execute($resource, $p['trunc'], $p['splitter'], $p['text'], $p['length'], $p['offset'], $p['splitter'], true);
  108. $GLOBALS['ditto_summary_link'] = $trunc->link;
  109. $GLOBALS['ditto_summary_type'] = $trunc->summaryType;
  110. return $output;
  111. }
  112. }
  113. // ---------------------------------------------------
  114. // Truncate Class
  115. // ---------------------------------------------------
  116. if (!class_exists("truncate")) {
  117. class truncate{
  118. var $summaryType,$link;
  119. function html_substr($posttext, $minimum_length = 200, $length_offset = 20, $truncChars=false) {
  120. // $minimum_length:
  121. // The approximate length you want the concatenated text to be
  122. // $length_offset:
  123. // The variation in how long the text can be in this example text
  124. // length will be between 200 and 200-20=180 characters and the
  125. // character where the last tag ends
  126. // Reset tag counter & quote checker
  127. $tag_counter = 0;
  128. $quotes_on = FALSE;
  129. // Check if the text is too long
  130. if (strlen($posttext) > $minimum_length && $truncChars != 1) {
  131. // Reset the tag_counter and pass through (part of) the entire text
  132. $c = 0;
  133. for ($i = 0; $i < strlen($posttext); $i++) {
  134. // Load the current character and the next one
  135. // if the string has not arrived at the last character
  136. $current_char = substr($posttext,$i,1);
  137. if ($i < strlen($posttext) - 1) {
  138. $next_char = substr($posttext,$i + 1,1);
  139. }
  140. else {
  141. $next_char = "";
  142. }
  143. // First check if quotes are on
  144. if (!$quotes_on) {
  145. // Check if it's a tag
  146. // On a "<" add 3 if it's an opening tag (like <a href...)
  147. // or add only 1 if it's an ending tag (like </a>)
  148. if ($current_char == '<') {
  149. if ($next_char == '/') {
  150. $tag_counter += 1;
  151. }
  152. else {
  153. $tag_counter += 3;
  154. }
  155. }
  156. // Slash signifies an ending (like </a> or ... />)
  157. // substract 2
  158. if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
  159. // On a ">" substract 1
  160. if ($current_char == '>') $tag_counter -= 1;
  161. // If quotes are encountered, start ignoring the tags
  162. // (for directory slashes)
  163. if ($current_char == '"') $quotes_on = TRUE;
  164. }
  165. else {
  166. // IF quotes are encountered again, turn it back off
  167. if ($current_char == '"') $quotes_on = FALSE;
  168. }
  169. // Count only the chars outside html tags
  170. if($tag_counter == 2 || $tag_counter == 0){
  171. $c++;
  172. }
  173. // Check if the counter has reached the minimum length yet,
  174. // then wait for the tag_counter to become 0, and chop the string there
  175. if ($c > $minimum_length - $length_offset && $tag_counter == 0) {
  176. $posttext = substr($posttext,0,$i + 1);
  177. return $posttext;
  178. }
  179. }
  180. } return $this->textTrunc($posttext, $minimum_length + $length_offset);
  181. }
  182. function textTrunc($string, $limit, $break=". ") {
  183. // Original PHP code from The Art of Web: www.the-art-of-web.com
  184. // return with no change if string is shorter than $limit
  185. if(strlen($string) <= $limit) return $string;
  186. $string = substr($string, 0, $limit);
  187. if(false !== ($breakpoint = strrpos($string, $break))) {
  188. $string = substr($string, 0, $breakpoint+1);
  189. }
  190. return $string;
  191. }
  192. function closeTags($text) {
  193. global $debug;
  194. $openPattern = "/<([^\/].*?)>/";
  195. $closePattern = "/<\/(.*?)>/";
  196. $endOpenPattern = "/<([^\/].*?)$/";
  197. $endClosePattern = "/<(\/.*?[^>])$/";
  198. $endTags = '';
  199. preg_match_all($openPattern, $text, $openTags);
  200. preg_match_all($closePattern, $text, $closeTags);
  201. if ($debug == 1) {
  202. print_r($openTags);
  203. print_r($closeTags);
  204. }
  205. $c = 0;
  206. $loopCounter = count($closeTags[1]); //used to prevent an infinite loop if the html is malformed
  207. while ($c < count($closeTags[1]) && $loopCounter) {
  208. $i = 0;
  209. while ($i < count($openTags[1])) {
  210. $tag = trim($openTags[1][$i]);
  211. if (strstr($tag, ' ')) {
  212. $tag = substr($tag, 0, strpos($tag, ' '));
  213. }
  214. if ($debug == 1) {
  215. echo $tag . '==' . $closeTags[1][$c] . "\n";
  216. }
  217. if ($tag == $closeTags[1][$c]) {
  218. $openTags[1][$i] = '';
  219. $c++;
  220. break;
  221. }
  222. $i++;
  223. }
  224. $loopCounter--;
  225. }
  226. $results = $openTags[1];
  227. if (is_array($results)) {
  228. $results = array_reverse($results);
  229. foreach ($results as $tag) {
  230. $tag = trim($tag);
  231. if (strstr($tag, ' ')) {
  232. $tag = substr($tag, 0, strpos($tag, ' '));
  233. }
  234. if (!stristr($tag, 'br') && !stristr($tag, 'img') && !empty ($tag)) {
  235. $endTags .= '</' . $tag . '>';
  236. }
  237. }
  238. }
  239. return $text . $endTags;
  240. }
  241. function execute($resource, $trunc, $splitter, $linktext, $truncLen, $truncOffset, $truncsplit, $truncChars) {
  242. $summary = '';
  243. $this->summaryType = "content";
  244. $this->link = false;
  245. $closeTags = true;
  246. // summary is turned off
  247. if ((strstr($resource['content'], $splitter)) && $truncsplit) {
  248. $summary = array ();
  249. // HTMLarea/XINHA encloses it in paragraph's
  250. $summary = explode('<p>' . $splitter . '</p>', $resource['content']);
  251. // For TinyMCE or if it isn't wrapped inside paragraph tags
  252. $summary = explode($splitter, $summary['0']);
  253. $summary = $summary['0'];
  254. $this->link = '[~' . $resource['id'] . '~]';
  255. $this->summaryType = "content";
  256. // fall back to the summary text
  257. } else if (strlen($resource['introtext']) > 0) {
  258. $summary = $resource['introtext'];
  259. $this->link = '[~' . $resource['id'] . '~]';
  260. $this->summaryType = "introtext";
  261. $closeTags = false;
  262. // fall back to the summary text count of characters
  263. } else if (strlen($resource['content']) > $truncLen && $trunc == 1) {
  264. $summary = $this->html_substr($resource['content'], $truncLen, $truncOffset, $truncChars);
  265. $this->link = '[~' . $resource['id'] . '~]';
  266. $this->summaryType = "content";
  267. // and back to where we started if all else fails (short post)
  268. } else {
  269. $summary = $resource['content'];
  270. $this->summaryType = "content";
  271. $this->link = false;
  272. }
  273. // Post-processing to clean up summaries
  274. $summary = ($closeTags === true) ? $this->closeTags($summary) : $summary;
  275. return $summary;
  276. }
  277. }
  278. }
  279. ?>