mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API: Move LimitWordCount() to Varchar
This commit is contained in:
parent
65cb182c98
commit
ddb017a6ff
@ -1,22 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* An abstract base class for the string field types (i.e. Varchar and Text)
|
* An abstract base class for the string field types (i.e. Varchar and Text)
|
||||||
|
*
|
||||||
* @package framework
|
* @package framework
|
||||||
* @subpackage model
|
* @subpackage model
|
||||||
* @author Pete Bacon Darwin
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
abstract class StringField extends DBField {
|
abstract class StringField extends DBField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
protected $nullifyEmpty = true;
|
protected $nullifyEmpty = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private static $casting = array(
|
private static $casting = array(
|
||||||
"LimitCharacters" => "Text",
|
"LimitCharacters" => "Text",
|
||||||
|
'LimitWordCount' => 'Text',
|
||||||
|
'LimitWordCountXML' => 'HTMLText',
|
||||||
"LowerCase" => "Text",
|
"LowerCase" => "Text",
|
||||||
"UpperCase" => "Text",
|
"UpperCase" => "Text",
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a string type field with a set of optional parameters
|
* Construct a string type field with a set of optional parameters.
|
||||||
|
*
|
||||||
* @param $name string The name of the field
|
* @param $name string The name of the field
|
||||||
* @param $options array An array of options e.g. array('nullifyEmpty'=>false). See
|
* @param $options array An array of options e.g. array('nullifyEmpty'=>false). See
|
||||||
* {@link StringField::setOptions()} for information on the available options
|
* {@link StringField::setOptions()} for information on the available options
|
||||||
@ -27,6 +36,7 @@ abstract class StringField extends DBField {
|
|||||||
if(is_array($options)){
|
if(is_array($options)){
|
||||||
$this->setOptions($options);
|
$this->setOptions($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct($name);
|
parent::__construct($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,15 +58,20 @@ abstract class StringField extends DBField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether this field stores empty strings rather than converting them to null
|
* Set whether this field stores empty strings rather than converting
|
||||||
|
* them to null.
|
||||||
|
*
|
||||||
* @param $value boolean True if empty strings are to be converted to null
|
* @param $value boolean True if empty strings are to be converted to null
|
||||||
*/
|
*/
|
||||||
public function setNullifyEmpty($value) {
|
public function setNullifyEmpty($value) {
|
||||||
$this->nullifyEmpty = ($value ? true : false);
|
$this->nullifyEmpty = ($value ? true : false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get whether this field stores empty strings rather than converting them to null
|
* Get whether this field stores empty strings rather than converting
|
||||||
* @return bool True if empty strings are to be converted to null
|
* them to null
|
||||||
|
*
|
||||||
|
* @return boolean True if empty strings are to be converted to null
|
||||||
*/
|
*/
|
||||||
public function getNullifyEmpty() {
|
public function getNullifyEmpty() {
|
||||||
return $this->nullifyEmpty;
|
return $this->nullifyEmpty;
|
||||||
@ -93,6 +108,7 @@ abstract class StringField extends DBField {
|
|||||||
*/
|
*/
|
||||||
public function LimitCharacters($limit = 20, $add = '...') {
|
public function LimitCharacters($limit = 20, $add = '...') {
|
||||||
$value = trim($this->value);
|
$value = trim($this->value);
|
||||||
|
|
||||||
if($this->stat('escape_type') == 'xml') {
|
if($this->stat('escape_type') == 'xml') {
|
||||||
$value = strip_tags($value);
|
$value = strip_tags($value);
|
||||||
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
|
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
|
||||||
@ -102,11 +118,55 @@ abstract class StringField extends DBField {
|
|||||||
} else {
|
} else {
|
||||||
$value = (mb_strlen($value) > $limit) ? mb_substr($value, 0, $limit) . $add : $value;
|
$value = (mb_strlen($value) > $limit) ? mb_substr($value, 0, $limit) . $add : $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the current value for this Enum DBField to lowercase.
|
* Limit this field's content by a number of words.
|
||||||
|
*
|
||||||
|
* CAUTION: This is not XML safe. Please use
|
||||||
|
* {@link LimitWordCountXML()} instead.
|
||||||
|
*
|
||||||
|
* @param int $numWords Number of words to limit by.
|
||||||
|
* @param string $add Ellipsis to add to the end of truncated string.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function LimitWordCount($numWords = 26, $add = '...') {
|
||||||
|
$this->value = trim(Convert::xml2raw($this->value));
|
||||||
|
$ret = explode(' ', $this->value, $numWords + 1);
|
||||||
|
|
||||||
|
if(count($ret) <= $numWords - 1) {
|
||||||
|
$ret = $this->value;
|
||||||
|
} else {
|
||||||
|
array_pop($ret);
|
||||||
|
$ret = implode(' ', $ret) . $add;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limit the number of words of the current field's
|
||||||
|
* content. This is XML safe, so characters like &
|
||||||
|
* are converted to &
|
||||||
|
*
|
||||||
|
* @param int $numWords Number of words to limit by.
|
||||||
|
* @param string $add Ellipsis to add to the end of truncated string.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function LimitWordCountXML($numWords = 26, $add = '...') {
|
||||||
|
$ret = $this->LimitWordCount($numWords, $add);
|
||||||
|
|
||||||
|
return Convert::raw2xml($ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the current value for this StringField to lowercase.
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function LowerCase() {
|
public function LowerCase() {
|
||||||
@ -114,11 +174,10 @@ abstract class StringField extends DBField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the current value for this Enum DBField to uppercase.
|
* Converts the current value for this StringField to uppercase.
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function UpperCase() {
|
public function UpperCase() {
|
||||||
return mb_strtoupper($this->value);
|
return mb_strtoupper($this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,41 +38,24 @@ class Text extends StringField {
|
|||||||
* @see DBField::requireField()
|
* @see DBField::requireField()
|
||||||
*/
|
*/
|
||||||
public function requireField() {
|
public function requireField() {
|
||||||
$parts=Array(
|
$parts = array(
|
||||||
'datatype'=>'mediumtext',
|
'datatype' => 'mediumtext',
|
||||||
'character set'=>'utf8',
|
'character set' => 'utf8',
|
||||||
'collate'=>'utf8_general_ci',
|
'collate' => 'utf8_general_ci',
|
||||||
'arrayValue'=>$this->arrayValue
|
'arrayValue' => $this->arrayValue
|
||||||
);
|
);
|
||||||
$values=Array('type'=>'text', 'parts'=>$parts);
|
|
||||||
|
$values= array(
|
||||||
|
'type' => 'text',
|
||||||
|
'parts' => $parts
|
||||||
|
);
|
||||||
|
|
||||||
DB::requireField($this->tableName, $this->name, $values, $this->default);
|
DB::requireField($this->tableName, $this->name, $values, $this->default);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limit this field's content by a number of words.
|
* Return the value of the field stripped of html tags.
|
||||||
* CAUTION: This is not XML safe. Please use
|
|
||||||
* {@link LimitWordCountXML()} instead.
|
|
||||||
*
|
*
|
||||||
* @param int $numWords Number of words to limit by
|
|
||||||
* @param string $add Ellipsis to add to the end of truncated string
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function LimitWordCount($numWords = 26, $add = '...') {
|
|
||||||
$this->value = trim(Convert::xml2raw($this->value));
|
|
||||||
$ret = explode(' ', $this->value, $numWords + 1);
|
|
||||||
|
|
||||||
if(count($ret) <= $numWords - 1) {
|
|
||||||
$ret = $this->value;
|
|
||||||
} else {
|
|
||||||
array_pop($ret);
|
|
||||||
$ret = implode(' ', $ret) . $add;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the value of the field stripped of html tags
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function NoHTML() {
|
public function NoHTML() {
|
||||||
@ -86,27 +69,16 @@ class Text extends StringField {
|
|||||||
public function AbsoluteLinks() {
|
public function AbsoluteLinks() {
|
||||||
return HTTP::absoluteURLs($this->value);
|
return HTTP::absoluteURLs($this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Limit the number of words of the current field's
|
|
||||||
* content. This is XML safe, so characters like &
|
|
||||||
* are converted to &
|
|
||||||
*
|
|
||||||
* @param int $numWords Number of words to limit by
|
|
||||||
* @param string $add Ellipsis to add to the end of truncated string
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function LimitWordCountXML($numWords = 26, $add = '...') {
|
|
||||||
$ret = $this->LimitWordCount($numWords, $add);
|
|
||||||
return Convert::raw2xml($ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limit sentences, can be controlled by passing an integer.
|
* Limit sentences, can be controlled by passing an integer.
|
||||||
|
*
|
||||||
* @param int $sentCount The amount of sentences you want.
|
* @param int $sentCount The amount of sentences you want.
|
||||||
*/
|
*/
|
||||||
public function LimitSentences($sentCount = 2) {
|
public function LimitSentences($sentCount = 2) {
|
||||||
if(!is_numeric($sentCount)) user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
|
if(!is_numeric($sentCount)) {
|
||||||
|
user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
|
||||||
|
}
|
||||||
|
|
||||||
$output = array();
|
$output = array();
|
||||||
$data = trim(Convert::xml2raw($this->value));
|
$data = trim(Convert::xml2raw($this->value));
|
||||||
@ -181,7 +153,7 @@ class Text extends StringField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the same function as the big summary, but doesnt trim new paragraphs off data.
|
* Performs the same function as the big summary, but doesn't trim new paragraphs off data.
|
||||||
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
||||||
*/
|
*/
|
||||||
public function BigSummary($maxWords = 50, $plain = 1) {
|
public function BigSummary($maxWords = 50, $plain = 1) {
|
||||||
@ -348,5 +320,3 @@ class Text extends StringField {
|
|||||||
return new TextField($this->name, $title);
|
return new TextField($this->name, $title);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user