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

Language PHP Lines 329
MD5 Hash 2a1de9d47da20dfc3137fb425ace954b Estimated Cost $4,255 (why?)
Repository https://github.com/macarthy/tinymce.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php

/*
 * Title: Summary
 * Purpose:
 *  	Legacy support for the [+summary+] placeholder
*/

$placeholders['summary'] = array("introtext,content","determineSummary","@GLOBAL ditto_summary_type");
$placeholders['link'] = array("id","determineLink");

$trunc = isset($trunc) ? $trunc : 1;
 /*
	Param: trunc

	Purpose:
	Enable truncation on the summary placeholder

	Options:
	0 - off
	1 - on
	
	Default:
	1 - on
*/
$splitter = isset($truncAt) ? $truncAt : "<!-- splitter -->";
 /*
	Param: truncAt

	Purpose:
	Location to split the content at

	Options:
	Any unique text or code string that is contained
	in the content of each document
	
	Default:
	"<!-- splitter -->"
*/
$length = isset($truncLen) ? $truncLen : 300;
 /*
	Param: truncLen

	Purpose:
	Number of characters to show of the content

	Options:
	Any number greater than <truncOffset>

	Default:
	300
*/
$offset = isset($truncOffset) ? $truncOffset : 30;
 /*
	Param: truncOffset

	Purpose:
	Number of charactars to "wander" either way of <truncLen>

	Options:
	Any number greater less than <truncLen>

	Default:
	30
*/
$text = isset($truncText)? $truncText : "Read more...";
 /*
	Param: truncText

	Purpose:
	Text to be displayed in [+link+]

	Options:
	Any valid text or html
	
	Default:
	"Read more..."
*/
$trunc_tpl = isset($tplTrunc)? template::fetch($tplTrunc) : false;
 /*
	Param: tplTrunc

	Purpose:
	Template to be used for [+link+]

	Options:
	- Any valid chunk name
	- Code via @CODE:
	- File via @FILE:
	
	Placeholders:
	[+url+] - URL of the document
	[+text+] - &truncText

	Default:
	&truncText
*/
$GLOBALS['ditto_summary_link'] = "";
$GLOBALS['ditto_summary_params'] = compact("trunc","splitter","length","offset","text","trunc_tpl");
$GLOBALS['ditto_object'] = $ditto;
// ---------------------------------------------------
// Truncate Functions
// ---------------------------------------------------
if (!function_exists("determineLink")) {
	function determineLink($resource) {
		global $ditto_object,$ditto_summary_params,$ditto_summary_link;
		if ($ditto_summary_link !== false) {
			$parameters = array(
			"url" => $ditto_summary_link,
			"text" => $ditto_summary_params["text"],
			);
			$tplTrunc = $ditto_summary_params["trunc_tpl"];
			if ($tplTrunc !== false) {
				$source = $tplTrunc;
			} else {
				$source = '<a href="[+url+]" title="[+text+]">[+text+]</a>';
			}
			return $ditto_object->template->replace($parameters,$source);
		} else {
			return '';
		}
	}
}
if (!function_exists("determineSummary")) {
	function determineSummary($resource) {
		global $ditto_summary_params;
		$trunc = new truncate();
		$p = $ditto_summary_params;
		$output = $trunc->execute($resource, $p['trunc'], $p['splitter'], $p['text'], $p['length'], $p['offset'], $p['splitter'], true);
		$GLOBALS['ditto_summary_link'] = $trunc->link;
		$GLOBALS['ditto_summary_type'] = $trunc->summaryType;
		return $output;
	}
}
// ---------------------------------------------------
// Truncate Class
// ---------------------------------------------------
if (!class_exists("truncate")) {
	class truncate{
		var $summaryType,$link;

		function html_substr($posttext, $minimum_length = 200, $length_offset = 20, $truncChars=false) {

		   // $minimum_length:
		   // The approximate length you want the concatenated text to be


		   // $length_offset:
		   // The variation in how long the text can be in this example text
		   // length will be between 200 and 200-20=180 characters and the
		   // character where the last tag ends

		   // Reset tag counter & quote checker
		   $tag_counter = 0;
		   $quotes_on = FALSE;
		   // Check if the text is too long
		   if (strlen($posttext) > $minimum_length && $truncChars != 1) {

		       // Reset the tag_counter and pass through (part of) the entire text
		       $c = 0;
		       for ($i = 0; $i < strlen($posttext); $i++) {
		           // Load the current character and the next one
		           // if the string has not arrived at the last character
		           $current_char = substr($posttext,$i,1);
		           if ($i < strlen($posttext) - 1) {
		               $next_char = substr($posttext,$i + 1,1);
		           }
		           else {
		               $next_char = "";
		           }
		           // First check if quotes are on
		           if (!$quotes_on) {
		               // Check if it's a tag
		               // On a "<" add 3 if it's an opening tag (like <a href...)
		               // or add only 1 if it's an ending tag (like </a>)
		               if ($current_char == '<') {
		                   if ($next_char == '/') {
		                       $tag_counter += 1;
		                   }
		                   else {
		                       $tag_counter += 3;
		                   }
		               }
		               // Slash signifies an ending (like </a> or ... />)
		               // substract 2
		               if ($current_char == '/' && $tag_counter <> 0) $tag_counter -= 2;
		               // On a ">" substract 1
		               if ($current_char == '>') $tag_counter -= 1;
		               // If quotes are encountered, start ignoring the tags
		               // (for directory slashes)
		               if ($current_char == '"') $quotes_on = TRUE;
		           }
		           else {
		               // IF quotes are encountered again, turn it back off
		               if ($current_char == '"') $quotes_on = FALSE;
		           }

		           // Count only the chars outside html tags
		           if($tag_counter == 2 || $tag_counter == 0){
		               $c++;
		           }

		           // Check if the counter has reached the minimum length yet,
		           // then wait for the tag_counter to become 0, and chop the string there
		           if ($c > $minimum_length - $length_offset && $tag_counter == 0) {
		               $posttext = substr($posttext,0,$i + 1);
		               return $posttext;
		           }
		       }
		   }  return $this->textTrunc($posttext, $minimum_length + $length_offset);
		}

		function textTrunc($string, $limit, $break=". ") {
	  	// Original PHP code from The Art of Web: www.the-art-of-web.com

	    // return with no change if string is shorter than $limit
	    if(strlen($string) <= $limit) return $string;

	    $string = substr($string, 0, $limit);
	    if(false !== ($breakpoint = strrpos($string, $break))) {
	      $string = substr($string, 0, $breakpoint+1);
	    }

	    return $string;
	  }

		function closeTags($text) {
			global $debug;
			$openPattern = "/<([^\/].*?)>/";
			$closePattern = "/<\/(.*?)>/";
			$endOpenPattern = "/<([^\/].*?)$/";
			$endClosePattern = "/<(\/.*?[^>])$/";
			$endTags = '';

			preg_match_all($openPattern, $text, $openTags);
			preg_match_all($closePattern, $text, $closeTags);

			if ($debug == 1) {
				print_r($openTags);
				print_r($closeTags);
			}

			$c = 0;
			$loopCounter = count($closeTags[1]); //used to prevent an infinite loop if the html is malformed
			while ($c < count($closeTags[1]) && $loopCounter) {
				$i = 0;
				while ($i < count($openTags[1])) {
					$tag = trim($openTags[1][$i]);

					if (strstr($tag, ' ')) {
						$tag = substr($tag, 0, strpos($tag, ' '));
					}
					if ($debug == 1) {
						echo $tag . '==' . $closeTags[1][$c] . "\n";
					}
					if ($tag == $closeTags[1][$c]) {
						$openTags[1][$i] = '';
						$c++;
						break;
					}
					$i++;
				}
				$loopCounter--;
			}

			$results = $openTags[1];

			if (is_array($results)) {
				$results = array_reverse($results);

				foreach ($results as $tag) {
					$tag = trim($tag);

					if (strstr($tag, ' ')) {
						$tag = substr($tag, 0, strpos($tag, ' '));
					}
					if (!stristr($tag, 'br') && !stristr($tag, 'img') && !empty ($tag)) {
						$endTags .= '</' . $tag . '>';
					}
				}
			}
			return $text . $endTags;
		}

		function execute($resource, $trunc, $splitter, $linktext, $truncLen, $truncOffset, $truncsplit, $truncChars) {
			$summary = '';
			$this->summaryType = "content";
			$this->link = false;
			$closeTags = true;
			// summary is turned off

			if ((strstr($resource['content'], $splitter)) && $truncsplit) {
				$summary = array ();

				// HTMLarea/XINHA encloses it in paragraph's
				$summary = explode('<p>' . $splitter . '</p>', $resource['content']);

				// For TinyMCE or if it isn't wrapped inside paragraph tags
				$summary = explode($splitter, $summary['0']);

				$summary = $summary['0'];
				$this->link = '[~' . $resource['id'] . '~]';
				$this->summaryType = "content";
		
				// fall back to the summary text
			} else if (strlen($resource['introtext']) > 0) {
					$summary = $resource['introtext'];
					$this->link = '[~' . $resource['id'] . '~]';
					$this->summaryType = "introtext";
					$closeTags = false;
					// fall back to the summary text count of characters
			} else if (strlen($resource['content']) > $truncLen && $trunc == 1) {
					$summary = $this->html_substr($resource['content'], $truncLen, $truncOffset, $truncChars);
					$this->link = '[~' . $resource['id'] . '~]';
					$this->summaryType = "content";
					// and back to where we started if all else fails (short post)
			} else {
				$summary = $resource['content'];
				$this->summaryType = "content";
				$this->link = false;
			}

			// Post-processing to clean up summaries
			$summary = ($closeTags === true) ? $this->closeTags($summary) : $summary;
			return $summary;
		}
	}
}
?>
Back to Top