2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2010-10-15 01:58:09 +02:00
|
|
|
* Class Varchar represents a variable-length string of up to 255 characters, designed to store raw text
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* @see HTMLText
|
|
|
|
* @see HTMLVarchar
|
|
|
|
* @see Text
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2009-10-23 03:29:55 +02:00
|
|
|
class Varchar extends StringField {
|
2012-01-31 01:07:24 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2012-01-31 01:07:24 +01:00
|
|
|
"Initial" => "Text",
|
|
|
|
"URL" => "Text",
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $size;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2009-10-23 03:29:55 +02:00
|
|
|
* Construct a new short text field
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @param $name string The name of the field
|
|
|
|
* @param $size int The maximum size of the field, in terms of characters
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param $options array Optional parameters, e.g. array("nullifyEmpty"=>false).
|
|
|
|
* See {@link StringField::setOptions()} for information on the available options
|
2009-10-23 03:29:55 +02:00
|
|
|
* @return unknown_type
|
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public function __construct($name = null, $size = 50, $options = array()) {
|
2007-09-09 06:02:54 +02:00
|
|
|
$this->size = $size ? $size : 50;
|
2009-10-23 03:29:55 +02:00
|
|
|
parent::__construct($name, $options);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-05 19:41:11 +01:00
|
|
|
/**
|
|
|
|
* Allow the ability to access the size of the field programatically. This
|
|
|
|
* can be useful if you want to have text fields with a length limit that
|
|
|
|
* is dictated by the DB field.
|
|
|
|
*
|
|
|
|
* TextField::create('Title')->setMaxLength(singleton('SiteTree')->dbObject('Title')->getSize())
|
|
|
|
*
|
|
|
|
* @return int The size of the field
|
|
|
|
*/
|
|
|
|
public function getSize() {
|
|
|
|
return $this->size;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2009-10-23 03:29:55 +02:00
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see DBField::requireField()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$parts = array(
|
|
|
|
'datatype'=>'varchar',
|
|
|
|
'precision'=>$this->size,
|
|
|
|
'character set'=>'utf8',
|
|
|
|
'collate'=>'utf8_general_ci',
|
|
|
|
'arrayValue'=>$this->arrayValue
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$values = array(
|
|
|
|
'type' => 'varchar',
|
|
|
|
'parts' => $parts
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the first letter of the string followed by a .
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Initial() {
|
2012-04-11 04:48:06 +02:00
|
|
|
if($this->exists()) return $this->value[0] . '.';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Ensure that the given value is an absolute URL.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function URL() {
|
2012-02-27 22:14:02 +01:00
|
|
|
if(preg_match('#^[a-zA-Z]+://#', $this->value)) return $this->value;
|
2007-07-19 12:40:28 +02:00
|
|
|
else return "http://" . $this->value;
|
|
|
|
}
|
2009-10-23 03:29:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the value of the field in rich text format
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RTF() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return str_replace("\n", '\par ', $this->value);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see DBField::scaffoldFormField()
|
|
|
|
*/
|
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2009-11-03 23:27:56 +01:00
|
|
|
if(!$this->nullifyEmpty) {
|
|
|
|
// Allow the user to select if it's null instead of automatically assuming empty string is
|
2009-10-23 03:29:55 +02:00
|
|
|
return new NullableField(new TextField($this->name, $title));
|
|
|
|
} else {
|
2009-11-03 23:27:56 +01:00
|
|
|
// Automatically determine null (empty string)
|
2009-10-23 03:29:55 +02:00
|
|
|
return parent::scaffoldFormField($title);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|