PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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