2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
|
|
|
use NullableField;
|
|
|
|
use TextField;
|
|
|
|
use Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
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
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @see DBHTMLText
|
|
|
|
* @see DBHTMLVarchar
|
|
|
|
* @see DBText
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBVarchar extends DBString {
|
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
|
|
|
*
|
2016-07-01 04:26:18 +02:00
|
|
|
* @param string $name The name of the field
|
|
|
|
* @param int $size The maximum size of the field, in terms of characters
|
|
|
|
* @param array $options Optional parameters, e.g. array("nullifyEmpty"=>false).
|
2012-09-26 23:34:00 +02:00
|
|
|
* See {@link StringField::setOptions()} for information on the available options
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
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() {
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$parts = array(
|
|
|
|
'datatype'=>'varchar',
|
|
|
|
'precision'=>$this->size,
|
2015-09-05 02:50:26 +02:00
|
|
|
'character set'=> $charset,
|
|
|
|
'collate'=> $collation,
|
2009-10-23 03:29:55 +02:00
|
|
|
'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 .
|
2016-06-17 08:49:23 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Initial() {
|
2016-06-30 16:54:44 +02:00
|
|
|
if($this->exists()) {
|
|
|
|
$value = $this->RAW();
|
|
|
|
return $value[0] . '.';
|
|
|
|
}
|
2016-06-17 08:49:23 +02:00
|
|
|
return null;
|
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.
|
2016-06-17 08:49:23 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function URL() {
|
2016-06-30 16:54:44 +02:00
|
|
|
$value = $this->RAW();
|
2016-06-17 08:49:23 +02:00
|
|
|
if(preg_match('#^[a-zA-Z]+://#', $value)) {
|
|
|
|
return $value;
|
|
|
|
} else {
|
|
|
|
return "http://" . $value;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return str_replace("\n", '\par ', $this->RAW());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
public function scaffoldFormField($title = 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
|
|
|
|