Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • AdditionalMenuWidget_Item
  • AdvancedSliderHomepageWidget_Item
  • AssetManagerFolder
  • BannerWidget_Item
  • BaseObjectDecorator
  • BookingOrder
  • BookingPaymentMethod
  • BookingService
  • Boolean
  • ButtonsBlockHomepageWidget_Item
  • CarouselHomepageWidget_Item
  • CatalogRubricsHomepageWidget_CatalogDecorator
  • ClientEmailOrderNotification
  • ClientVKOrderNotification
  • ComponentSet
  • Currency
  • DatabaseAdmin
  • DataObject
  • DataObjectDecorator
  • DataObjectLog
  • DataObjectSet
  • DataObjectSet_Iterator
  • Date
  • DB
  • DBField
  • Decimal
  • DocumentItem
  • DocumentPage_File
  • Double
  • Enum
  • ErrorPageSubsite
  • FileDataObjectTrackingDecorator
  • FileImportDecorator
  • Float
  • ForeignKey
  • Hierarchy
  • HTMLText
  • HTMLVarchar
  • ImportLog_Item
  • Int
  • ManagerEmailOrderNotification
  • Material3D_File
  • MediawebPage_File
  • MediawebPage_Photo
  • MobileContentDecorator
  • Money
  • MultiEnum
  • MySQLDatabase
  • MySQLQuery
  • OrderDataObject
  • OrderHandlersDecorator
  • OrderItemVariationDecorator
  • OrderService
  • OrderServiceOrder
  • OrdersExportDecorator
  • PageIcon
  • PageWidgets
  • Payment
  • PaymentMethodShippingDecorator
  • PaymentOrderExtension
  • Percentage
  • PhotoAlbumItem
  • PhotoAlbumProductLinkDecorator
  • PhotoAlbumWidgetLinkDecorator
  • PhotoGalleryHomepageWidget_Item
  • PrimaryKey
  • Product3DDecorator
  • ProductCatalogCatalogLinkedDecorator
  • RatePeriod
  • RealtyImportLog
  • RealtyImportLog_Item
  • RedirectEntry
  • RoomOrder
  • RoomOrderPerson
  • RoomRate
  • RoomService
  • RoomServiceOrder
  • SberbankPaymentDecorator
  • SeoOpenGraphPageDecorator
  • ServiceOrder
  • ShippingMethodPaymentDecorator
  • ShopCountry
  • SimpleOrderCatalogDecorator
  • SimpleOrderProductDecorator
  • SiteConfigWidgets
  • SiteTreeDecorator
  • SiteTreeImportDecorator
  • SliderHomepageWidget_Item
  • SMSCOrderNotification
  • SMSOrderNotification
  • SortableDataObject
  • SQLMap
  • SQLMap_Iterator
  • SQLQuery
  • SS_Database
  • SS_Datetime
  • SS_Query
  • StringField
  • SubsiteDomain
  • Text
  • TextAnonsWidget_Item
  • Texture3D_File
  • Time
  • Varchar
  • Versioned
  • Versioned_Version
  • VideoCategory
  • VideoEntry
  • VKNotificationQueue
  • WebylonWidget_Item
  • YaMoneyPaymentDecorator
  • Year

Interfaces

  • CompositeDBField
  • CurrentPageIdentifier
  • DataObjectInterface
  1 <?php
  2 /**
  3  * Represents a long text field.
  4  * @package sapphire
  5  * @subpackage model
  6  */
  7 class Text extends StringField {
  8     static $casting = array(
  9         "AbsoluteLinks" => "HTMLText",
 10     );
 11     
 12     /**
 13      * (non-PHPdoc)
 14      * @see DBField::requireField()
 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      * Limit this field's content by a number of words.
 24      * CAUTION: This is not XML safe. Please use
 25      * {@link LimitWordCountXML()} instead.
 26      *
 27      * @param int $numWords Number of words to limit by
 28      * @param string $add Ellipsis to add to the end of truncated string
 29      * @return string
 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      * Return the value of the field stripped of html tags
 47      * @return string
 48      */
 49     function NoHTML() {
 50         return strip_tags($this->value);
 51     }
 52     /**
 53      * Return the value of the field with XML tags escaped.
 54      * @return string
 55      */
 56     function EscapeXML() {
 57         return str_replace(array('&','<','>','"'), array('&amp;','&lt;','&gt;','&quot;'), $this->value);
 58     }
 59     
 60     /**
 61      * Return the value of the field with relative links converted to absolute urls.
 62      * @return string
 63      */
 64     function AbsoluteLinks() {
 65         return HTTP::absoluteURLs($this->value);
 66     }
 67     
 68     /**
 69      * Limit this field's content by a number of characters.
 70      * CAUTION: Does not take into account HTML tags, so it
 71      * has the potential to return malformed HTML.
 72      *
 73      * @param int $limit Number of characters to limit by
 74      * @param string $add Ellipsis to add to the end of truncated string
 75      * @return string
 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      * Limit the number of words of the current field's
 84      * content. This is XML safe, so characters like &
 85      * are converted to &amp;
 86      *
 87      * @param int $numWords Number of words to limit by
 88      * @param string $add Ellipsis to add to the end of truncated string
 89      * @return string
 90      */
 91     function LimitWordCountXML($numWords = 26, $add = '...') {
 92         $ret = $this->LimitWordCount($numWords, $add);
 93         return Convert::raw2xml($ret);
 94     }
 95 
 96     /**
 97      * Limit sentences, can be controlled by passing an integer.
 98      * @param int $sentCount The amount of sentences you want.
 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      * Caution: Not XML/HTML-safe - does not respect closing tags.
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      * Caution: Not XML/HTML-safe - does not respect closing tags.
137      */
138     function Summary($maxWords = 50) {
139         // get first sentence?
140         // this needs to be more robust
141         $data = Convert::xml2raw($this->value);
142     
143         if( !$data )
144             return "";
145         
146         // grab the first paragraph, or, failing that, the whole content
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         // if the first sentence is too long, show only the first $maxWords words
155         if( $count > $maxWords ) {
156             return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
157         }
158         // add each sentence while there are enough words to do so
159         $result = '';
160         do {
161             $result .= trim(array_shift( $sentences )).'.';
162             if(count($sentences) > 0) {
163                 $count += count( explode( ' ', $sentences[0] ) );
164             }
165             
166             // Ensure that we don't trim half way through a tag or a link
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     * Performs the same function as the big summary, but doesnt trim new paragraphs off data.
181     * Caution: Not XML/HTML-safe - does not respect closing tags.
182     */
183     function BigSummary($maxWords = 50, $plain = 1) {
184         $result = "";
185         // get first sentence?
186         // this needs to be more robust
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         // if the first sentence is too long, show only the first $maxWords words
196         if( $count > $maxWords ) {
197             return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
198         }
199         // add each sentence while there are enough words to do so
200         do {
201             $result .= trim(array_shift($sentences));
202             if($sentences) {
203                 $result .= '. ';
204                 $count += count(explode(' ', $sentences[0]));
205             }
206             
207             // Ensure that we don't trim half way through a tag or a link
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      * Caution: Not XML/HTML-safe - does not respect closing tags.
224      */
225     function FirstParagraph($plain = 1) {
226         // get first sentence?
227         // this needs to be more robust
228         if($plain && $plain != 'html') {
229             $data = Convert::xml2raw($this->value);
230             if( !$data ) return "";
231 
232             // grab the first paragraph, or, failing that, the whole content
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      * Perform context searching to give some context to searches, optionally
252      * highlighting the search term.
253      * 
254      * @param int $characters Number of characters in the summary
255      * @param boolean $string Supplied string ("keywords")
256      * @param boolean $striphtml Strip HTML?
257      * @param boolean $highlight Add a highlight <span> element around search query?
258      * @return string
259      */
260     function ContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) {
261         if(!$string) $string = $_REQUEST['Search']; // Use the default "Search" request variable (from SearchForm)
262 
263         // Remove HTML tags so we don't have to deal with matching tags
264         $text = $striphtml ? $this->NoHTML() : $this->value;
265         
266         // Find the search string
267         $position = (int) stripos($text, $string);
268         
269         // We want to search string to be in the middle of our block to give it some context
270         $position = max(0, $position - ($characters / 2));
271         
272         if($position > 0) {
273             // We don't want to start mid-word
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             // Add a span around all key words from the search term as well
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      * Allows a sub-class of TextParser to be rendered.
292      * 
293      * @see TextParser for implementation details.
294      * @return string
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             // Fallback to using raw2xml and show a warning
302             // TODO Don't kill script execution, we can continue without losing complete control of the app
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      * (non-PHPdoc)
310      * @see DBField::scaffoldFormField()
311      */
312     public function scaffoldFormField($title = null, $params = null) {
313         if(!$this->nullifyEmpty) {
314             // Allow the user to select if it's null instead of automatically assuming empty string is
315             return new NullableField(new TextareaField($this->name, $title));
316         } else {
317             // Automatically determine null (empty string)
318             return new TextareaField($this->name, $title);
319         }
320     }
321     
322     /**
323      * (non-PHPdoc)
324      * @see DBField::scaffoldSearchField()
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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0