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 $value = Convert::xml2raw($this->value);
33 $ret = explode(' ', $value, $numWords + 1);
34
35 if(count($ret) <= $numWords - 1) {
36 $ret = $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 $sentences = explode( '.', $data );
128
129 if( count( $sentences ) )
130 return $sentences[0] . '.';
131 else
132 return $this->Summary(20);
133 }
134
135 136 137
138 function Summary($maxWords = 50) {
139
140
141 $data = Convert::xml2raw($this->value);
142
143 if( !$data )
144 return "";
145
146
147 if( strpos( $data, "\n\n" ) )
148 $data = substr( $data, 0, strpos( $data, "\n\n" ) );
149
150 $sentences = explode( '.', $data );
151
152 $count = count( explode( ' ', $sentences[0] ) );
153
154
155 if( $count > $maxWords ) {
156 return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
157 }
158
159 $result = '';
160 do {
161 $result .= trim(array_shift( $sentences )).'.';
162 if(count($sentences) > 0) {
163 $count += count( explode( ' ', $sentences[0] ) );
164 }
165
166
167 $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
168 (substr_count($result,'<a') != substr_count($result,'</a'));
169
170 } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
171
172 if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
173 $result .= '</a>';
174
175 $result = Convert::raw2xml( $result );
176 return $result;
177 }
178
179 180 181 182
183 function BigSummary($maxWords = 50, $plain = 1) {
184 $result = "";
185
186
187 if($plain) $data = Convert::xml2raw( $this->value, true );
188
189 if( !$data )
190 return "";
191
192 $sentences = explode( '.', $data );
193 $count = count( explode( ' ', $sentences[0] ) );
194
195
196 if( $count > $maxWords ) {
197 return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
198 }
199
200 do {
201 $result .= trim(array_shift($sentences));
202 if($sentences) {
203 $result .= '. ';
204 $count += count(explode(' ', $sentences[0]));
205 }
206
207
208 $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
209 (substr_count($result,'<a') != substr_count($result,'</a'));
210 } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
211
212 if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
213 $result .= '</a>';
214
215 return $result;
216 }
217
218 public function Preformatted(){
219 return nl2br($this->value);
220 }
221
222 223 224
225 function FirstParagraph($plain = 1) {
226
227
228 if($plain && $plain != 'html') {
229 $data = Convert::xml2raw($this->value);
230 if( !$data ) return "";
231
232
233 if( strpos( $data, "\n\n" ) )
234 $data = substr( $data, 0, strpos( $data, "\n\n" ) );
235
236 return $data;
237
238 } else {
239 if(strpos( $this->value, "</p>" ) === false) return $this->value;
240
241 $data = substr( $this->value, 0, strpos( $this->value, "</p>" ) + 4 );
242
243
244 if(strlen($data) < 20 && strpos( $this->value, "</p>", strlen($data) )) $data = substr( $this->value, 0, strpos( $this->value, "</p>", strlen($data) ) + 4 );
245
246 return $data;
247 }
248 }
249
250 251 252 253 254 255 256 257 258 259
260 function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) {
261 if(!$string) $string = $_REQUEST['Search'];
262
263
264 $text = $striphtml ? $this->NoHTML() : $this->value;
265
266
267 $position = (int) stripos($text, $string);
268
269
270 $position = max(0, $position - ($characters / 2));
271
272 if($position > 0) {
273
274 $position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n"));
275 }
276
277 $summary = substr($text, $position, $characters);
278 $stringPieces = explode(' ', $string);
279
280 if($highlight) {
281
282 if($stringPieces) foreach($stringPieces as $stringPiece) {
283 $summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
284 }
285 }
286
287 return trim($summary);
288 }
289
290 291 292 293 294 295
296 function Parse($parser = "TextParser") {
297 if($parser == "TextParser" || is_subclass_of($parser, "TextParser")) {
298 $obj = new $parser($this->value);
299 return $obj->parse();
300 } else {
301
302
303 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);
304 return Convert::raw2xml($this->value);
305 }
306 }
307
308 309 310 311
312 public function scaffoldFormField($title = null, $params = null) {
313 if(!$this->nullifyEmpty) {
314
315 return new NullableField(new TextareaField($this->name, $title));
316 } else {
317
318 return new TextareaField($this->name, $title);
319 }
320 }
321
322 323 324 325
326 public function scaffoldSearchField($title = null, $params = null) {
327 return new TextField($this->name, $title);
328 }
329 }
330
331 ?>
332
[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.
-