mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-30 14:36:06 +02:00
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90747 467b73ca-7a2a-4603-9d3b-597d59a354a9
94 lines
2.3 KiB
PHP
94 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Represents a short text field.
|
|
* @package sapphire
|
|
* @subpackage model
|
|
*/
|
|
class Varchar extends StringField {
|
|
|
|
protected $size;
|
|
|
|
/**
|
|
* Construct a new short text field
|
|
* @param $name string The name of the field
|
|
* @param $size int The maximum size of the field, in terms of characters
|
|
* @param $options array Optional parameters, e.g. array("nullifyEmpty"=>false). See {@link StringField::setOptions()} for information on the available options
|
|
* @return unknown_type
|
|
*/
|
|
function __construct($name, $size = 50, $options = array()) {
|
|
$this->size = $size ? $size : 50;
|
|
parent::__construct($name, $options);
|
|
}
|
|
|
|
/**
|
|
* (non-PHPdoc)
|
|
* @see DBField::requireField()
|
|
*/
|
|
function requireField() {
|
|
$parts = array(
|
|
'datatype'=>'varchar',
|
|
'precision'=>$this->size,
|
|
'character set'=>'utf8',
|
|
'collate'=>'utf8_general_ci',
|
|
'arrayValue'=>$this->arrayValue
|
|
);
|
|
|
|
$values = array(
|
|
'type' => 'varchar',
|
|
'parts' => $parts
|
|
);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
|
}
|
|
|
|
/**
|
|
* Return the first letter of the string followed by a .
|
|
*/
|
|
function Initial() {
|
|
if($this->hasValue()) return $this->value[0] . '.';
|
|
}
|
|
|
|
/**
|
|
* Ensure that the given value is an absolute URL.
|
|
*/
|
|
function URL() {
|
|
if(ereg('^[a-zA-Z]+://', $this->value)) return $this->value;
|
|
else return "http://" . $this->value;
|
|
}
|
|
|
|
/**
|
|
* Return the value of the field in rich text format
|
|
* @return string
|
|
*/
|
|
function RTF() {
|
|
return str_replace("\n", '\par ', $this->value);
|
|
}
|
|
|
|
/**
|
|
* Returns the value of the string, limited to the specified number of characters
|
|
* @param $limit int Character limit
|
|
* @param $add string Extra string to add to the end of the limited string
|
|
* @return string
|
|
*/
|
|
function LimitCharacters($limit = 20, $add = "...") {
|
|
$value = trim($this->value);
|
|
return (strlen($value) > $limit) ? substr($value, 0, $limit) . $add : $value;
|
|
}
|
|
|
|
/**
|
|
* (non-PHPdoc)
|
|
* @see DBField::scaffoldFormField()
|
|
*/
|
|
public function scaffoldFormField($title = null, $params = null) {
|
|
if(!$this->nullifyEmpty) {
|
|
// Allow the user to select if it's null instead of automatically assuming empty string is
|
|
return new NullableField(new TextField($this->name, $title));
|
|
} else {
|
|
// Automatically determine null (empty string)
|
|
return parent::scaffoldFormField($title);
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|