2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Single field in the database.
|
|
|
|
* Every field from the database is represented as a sub-class of DBField. In addition to supporting
|
|
|
|
* the creation of the field in the database,
|
2008-08-06 04:43:46 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
abstract class DBField extends ViewableData {
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $value;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $tableName;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $name;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* @var $default mixed Default-value in the database.
|
|
|
|
* Might be overridden on DataObject-level, but still useful for setting defaults on
|
|
|
|
* already existing records after a db-build.
|
|
|
|
*/
|
|
|
|
protected $defaultVal;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function __construct($name) {
|
|
|
|
$this->name = $name;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a DBField object that's not bound to any particular field.
|
|
|
|
* Useful for accessing the classes behaviour for other parts of your code.
|
|
|
|
*/
|
|
|
|
static function create($className, $value, $name = null) {
|
|
|
|
$dbField = Object::create($className, $name);
|
|
|
|
$dbField->setValue($value);
|
|
|
|
return $dbField;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setVal($value) {
|
|
|
|
return $this->setValue($value);
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function setValue($value) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function setTable($tableName) {
|
|
|
|
$this->tableName = $tableName;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function forTemplate() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function HTMLATT() {
|
|
|
|
return Convert::raw2htmlatt($this->value);
|
|
|
|
}
|
2008-04-06 05:52:57 +02:00
|
|
|
function URLATT() {
|
|
|
|
return urlencode($this->value);
|
|
|
|
}
|
2008-04-06 06:02:55 +02:00
|
|
|
function RAWURLATT() {
|
|
|
|
return rawurlencode($this->value);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
function ATT() {
|
|
|
|
return Convert::raw2att($this->value);
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function RAW() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function JS() {
|
|
|
|
return Convert::raw2js($this->value);
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function HTML(){
|
|
|
|
return Convert::raw2xml($this->value);
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function XML(){
|
|
|
|
return Convert::raw2xml($this->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value to be set in the database to blank this field.
|
|
|
|
* Usually it's a choice between null, 0, and ''
|
|
|
|
*/
|
|
|
|
function nullValue() {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves this field to the given data object.
|
|
|
|
*/
|
|
|
|
function saveInto($dataObject) {
|
|
|
|
$fieldName = $this->name;
|
|
|
|
if($fieldName) {
|
|
|
|
$dataObject->$fieldName = $this->value;
|
|
|
|
} else {
|
|
|
|
user_error("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Returns a FormField instance used as a default
|
|
|
|
* for form scaffolding.
|
|
|
|
*
|
|
|
|
* @usedby {@link SearchContext}
|
|
|
|
* @usedby {@link ModelAdmin}
|
|
|
|
* @usedby {@link DataObject::scaffoldFormFields()}
|
|
|
|
*
|
|
|
|
* @param string $title Optional. Localized title of the generated instance
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function scaffoldFormField($title = null) {
|
|
|
|
$field = new TextField($this->name, $title);
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a FormField instance used as a default
|
|
|
|
* for searchform scaffolding.
|
|
|
|
*
|
|
|
|
* @usedby {@link SearchContext}
|
|
|
|
* @usedby {@link ModelAdmin}
|
|
|
|
* @usedby {@link DataObject::scaffoldFormFields()}
|
|
|
|
*
|
|
|
|
* @param string $title Optional. Localized title of the generated instance
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function scaffoldSearchField($title = null) {
|
|
|
|
return $this->scaffoldFormField($title);
|
|
|
|
}
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
|
|
|
* @todo documentation
|
|
|
|
*
|
|
|
|
* @todo figure out how we pass configuration parameters to
|
|
|
|
* search filters
|
|
|
|
*
|
|
|
|
* @return SearchFilter
|
|
|
|
*/
|
|
|
|
public function defaultSearchFilter() {
|
|
|
|
return new ExactMatchSearchFilter();
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add the field to the underlying database.
|
|
|
|
*/
|
|
|
|
abstract function requireField();
|
|
|
|
|
|
|
|
function debug() {
|
|
|
|
return <<<DBG
|
|
|
|
<ul>
|
|
|
|
<li><b>Name:</b>{$this->name}</li>
|
|
|
|
<li><b>Table:</b>{$this->tableName}</li>
|
|
|
|
<li><b>Value:</b>{$this->value}</li>
|
2007-09-14 03:24:30 +02:00
|
|
|
</ul>
|
2007-07-19 12:40:28 +02:00
|
|
|
DBG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|