2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a short text field.
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2009-10-23 03:29:55 +02:00
|
|
|
class Varchar extends StringField {
|
2008-08-09 04:00:40 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $size;
|
2009-10-23 03:29:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()) {
|
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
|
|
|
}
|
2008-08-09 08:29:50 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see DBField::requireField()
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'type' => 'varchar',
|
|
|
|
'parts' => $parts
|
|
|
|
);
|
|
|
|
|
2008-11-23 02:01:03 +01:00
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 04:00:40 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the first letter of the string followed by a .
|
|
|
|
*/
|
|
|
|
function Initial() {
|
2009-10-23 03:29:55 +02:00
|
|
|
if($this->hasValue()) return $this->value[0] . '.';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2009-10-23 03:29:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the value of the field in rich text format
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function RTF() {
|
|
|
|
return str_replace("\n", '\par ', $this->value);
|
|
|
|
}
|
2009-10-23 03:29:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function LimitCharacters($limit = 20, $add = "...") {
|
|
|
|
$value = trim($this->value);
|
|
|
|
return (strlen($value) > $limit) ? substr($value, 0, $limit) . $add : $value;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
?>
|