1 <?php
2 3 4 5 6
7 class Text extends StringField {
8 static $casting = array(
9 "AbsoluteLinks" => "HTMLText",
10 );
11
12 13 14 15
16 function requireField() {
17 $parts=Array('datatype'=>'mediumtext', 'character set'=>'utf8', 'collate'=>'utf8_unicode_ci', 'arrayValue'=>$this->arrayValue);
18 $values=Array('type'=>'text', 'parts'=>$parts);
19 DB::requireField($this->tableName, $this->name, $values, $this->default);
20 }
21
22 23 24 25 26 27 28 29 30
31 function LimitWordCount($numWords = 26, $add = '...') {
32 $this->value = trim(Convert::xml2raw($this->value));
33 $ret = explode(' ', $this->value, $numWords + 1);
34
35 if(count($ret) <= $numWords - 1) {
36 $ret = $this->value;
37 } else {
38 array_pop($ret);
39 $ret = implode(' ', $ret) . $add;
40 }
41
42 return $ret;
43 }
44
45 46 47 48
49 function NoHTML() {
50 return strip_tags($this->value);
51 }
52 53 54 55
56 function EscapeXML() {
57 return str_replace(array('&','<','>','"'), array('&','<','>','"'), $this->value);
58 }
59
60 61 62 63
64 function AbsoluteLinks() {
65 return HTTP::absoluteURLs($this->value);
66 }
67
68 69 70 71 72 73 74 75 76
77 function LimitCharacters($limit = 20, $add = "...") {
78 $value = trim($this->value);
79 return (mb_strlen($value) > $limit) ? mb_substr($value, 0, $limit) . $add : $value;
80 }
81
82 83 84 85 86 87 88 89 90
91 function LimitWordCountXML($numWords = 26, $add = '...') {
92 $ret = $this->LimitWordCount($numWords, $add);
93 return Convert::raw2xml($ret);
94 }
95
96 97 98 99
100 function LimitSentences($sentCount = 2) {
101 if(!is_numeric($sentCount)) user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
102
103 $output = array();
104 $data = trim(Convert::xml2raw($this->value));
105 $sentences = explode('.', $data);
106
107 if ($sentCount == 0) return '';
108
109 for($i = 0; $i < $sentCount; $i++) {
110 if(isset($sentences[$i])) {
111 $sentence = trim($sentences[$i]);
112 if(!empty($sentence)) $output[] .= $sentence;
113 }
114 }
115
116 return count($output)==0 ? '' : implode($output, '. ') . '.';
117 }
118
119
120 121 122
123 function FirstSentence() {
124 $data = Convert::xml2raw( $this->value );
125 if( !$data ) return "";
126
127
128 $sentences = explode( '.', $data );
129
130 if( count( $sentences ) )
131 return $sentences[0] . '.';
132 else
133 return $this->Summary(20);
134 }
135
136 137 138
139 function Summary($maxWords = 50) {
140
141
142
143 $data = Convert::xml2raw( $this->value );
144
145
146 if( !$data )
147 return "";
148
149
150 if( strpos( $data, "\n\n" ) )
151 $data = substr( $data, 0, strpos( $data, "\n\n" ) );
152
153 $sentences = explode( '.', $data );
154
155 $count = count( explode( ' ', $sentences[0] ) );
156
157
158 if( $count > $maxWords ) {
159 return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
160 }
161
162 $result = '';
163 do {
164 $result .= trim(array_shift( $sentences )).'.';
165 if(count($sentences) > 0) {
166 $count += count( explode( ' ', $sentences[0] ) );
167 }
168
169
170 $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
171 (substr_count($result,'<a') != substr_count($result,'</a'));
172
173 } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
174
175 if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
176 $result .= '</a>';
177
178 $result = Convert::raw2xml( $result );
179 return $result;
180 }
181
182 183 184 185
186 function BigSummary($maxWords = 50, $plain = 1) {
187 $result = "";
188
189
190 if($plain) $data = Convert::xml2raw( $this->value, true );
191
192 if( !$data )
193 return "";
194
195 $sentences = explode( '.', $data );
196 $count = count( explode( ' ', $sentences[0] ) );
197
198
199 if( $count > $maxWords ) {
200 return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
201 }
202
203 do {
204 $result .= trim(array_shift($sentences));
205 if($sentences) {
206 $result .= '. ';
207 $count += count(explode(' ', $sentences[0]));
208 }
209
210
211 $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
212 (substr_count($result,'<a') != substr_count($result,'</a'));
213 } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
214
215 if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
216 $result .= '</a>';
217
218 return $result;
219 }
220
221 222 223
224 function FirstParagraph($plain = 1) {
225
226
227 if($plain && $plain != 'html') {
228 $data = Convert::xml2raw( $this->value, true );
229 if( !$data ) return "";
230
231
232 if( strpos( $data, "\n\n" ) )
233 $data = substr( $data, 0, strpos( $data, "\n\n" ) );
234
235 return $data;
236
237 } else {
238 if(strpos( $this->value, "</p>" ) === false) return $this->value;
239
240 $data = substr( $this->value, 0, strpos( $this->value, "</p>" ) + 4 );
241
242
243 if(strlen($data) < 20 && strpos( $this->value, "</p>", strlen($data) )) $data = substr( $this->value, 0, strpos( $this->value, "</p>", strlen($data) ) + 4 );
244
245 return $data;
246 }
247 }
248
249 250 251 252 253 254 255 256 257 258
259 function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) {
260 if(!$string) $string = $_REQUEST['Search'];
261
262
263 $text = $striphtml ? $this->NoHTML() : $this->value;
264
265
266 $position = (int) stripos($text, $string);
267
268
269 $position = max(0, $position - ($characters / 2));
270
271 if($position > 0) {
272
273 $position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n"));
274 }
275
276 $summary = substr($text, $position, $characters);
277 $stringPieces = explode(' ', $string);
278
279 if($highlight) {
280
281 if($stringPieces) foreach($stringPieces as $stringPiece) {
282 $summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
283 }
284 }
285
286 return trim($summary);
287 }
288
289 290 291 292 293 294
295 function Parse($parser = "TextParser") {
296 if($parser == "TextParser" || is_subclass_of($parser, "TextParser")) {
297 $obj = new $parser($this->value);
298 return $obj->parse();
299 } else {
300
301
302 user_error("Couldn't find an appropriate TextParser sub-class to create (Looked for '$parser'). Make sure it sub-classes TextParser and that you've done ?flush=1.", E_USER_WARNING);
303 return Convert::raw2xml($this->value);
304 }
305 }
306
307 308 309 310
311 public function scaffoldFormField($title = null, $params = null) {
312 if(!$this->nullifyEmpty) {
313
314 return new NullableField(new TextareaField($this->name, $title));
315 } else {
316
317 return new TextareaField($this->name, $title);
318 }
319 }
320
321 322 323 324
325 public function scaffoldSearchField($title = null, $params = null) {
326 return new TextField($this->name, $title);
327 }
328 }
329
330 ?>
331
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-