Version: current

     1 <?php
     2 /**
     3  * Represents a short text field.
     4  * @package sapphire
     5  * @subpackage model
     6  */
     7 class Varchar extends StringField {
     8
     9     protected $size;
    10
    11     /**
    12      * Construct a new short text field
    13      * @param $name string The name of the field
    14      * @param $size int The maximum size of the field, in terms of characters
    15      * @param $options array Optional parameters, e.g. array("nullifyEmpty"=>false). See {@link StringField::setOptions()} for information on the available options
    16      * @return unknown_type
    17      */
    18     function __construct($name, $size = 50, $options = array()) {
    19         $this->size = $size ? $size : 50;
    20         parent::__construct($name, $options);
    21     }
    22
    23     /**
    24      * (non-PHPdoc)
    25      * @see DBField::requireField()
    26      */
    27     function requireField() {
    28         $parts = array(
    29             'datatype'=>'varchar',
    30             'precision'=>$this->size,
    31             'character set'=>'utf8',
    32             'collate'=>'utf8_unicode_ci',
    33             'arrayValue'=>$this->arrayValue
    34         );
    35
    36         $values = array(
    37             'type' => 'varchar',
    38             'parts' => $parts
    39         );
    40
    41         DB::requireField($this->tableName, $this->name, $values);
    42     }
    43
    44     /**
    45      * Return the first letter of the string followed by a .
    46      */
    47     function Initial() {
    48         if($this->hasValue()) return $this->value[0] . '.';
    49     }
    50
    51     /**
    52      * Ensure that the given value is an absolute URL.
    53      */
    54     function URL() {
    55         if(ereg('^[a-zA-Z]+://', $this->value)) return $this->value;
    56         else return "http://" . $this->value;
    57     }
    58
    59     /**
    60      * Return the value of the field in rich text format
    61      * @return string
    62      */
    63     function RTF() {
    64         return str_replace("\n", '\par ', $this->value);
    65     }
    66
    67     /**
    68      * Returns the value of the string, limited to the specified number of characters
    69      * @param $limit int Character limit
    70      * @param $add string Extra string to add to the end of the limited string
    71      * @return string
    72      */
    73     function LimitCharacters($limit = 20, $add = "...") {
    74         $value = trim($this->value);
    75         return (mb_strlen($value) > $limit) ? mb_substr($value, 0, $limit) . $add : $value;
    76     }
    77
    78     /**
    79      * (non-PHPdoc)
    80      * @see DBField::scaffoldFormField()
    81      */
    82     public function scaffoldFormField($title = null, $params = null) {
    83         if(!$this->nullifyEmpty) {
    84             // Allow the user to select if it's null instead of automatically assuming empty string is
    85             return new NullableField(new TextField($this->name, $title));
    86         } else {
    87             // Automatically determine null (empty string)
    88             return parent::scaffoldFormField($title);
    89         }
    90     }
    91 }
    92
    93 ?>
    94

[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. -