PageRenderTime 26ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

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