2008-08-09 07:22:38 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Apply this interface to any {@link DBField} that doesn't have a 1-1 mapping with a database field.
|
|
|
|
* This includes multi-value fields and transformed fields
|
2008-08-11 06:13:06 +02:00
|
|
|
*
|
|
|
|
* @todo Unittests for loading and saving composite values (see GIS module for existing similiar unittests)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-27 02:05:10 +02:00
|
|
|
* Example with a combined street name and number:
|
|
|
|
* <code>
|
2015-09-04 05:49:22 +02:00
|
|
|
* class Street extends CompositeDBField {
|
|
|
|
* private static $composite_db = return array(
|
2009-05-27 02:05:10 +02:00
|
|
|
* "Number" => "Int",
|
|
|
|
* "Name" => "Text"
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* </code>
|
2008-08-11 06:13:06 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-11 06:13:06 +02:00
|
|
|
* @subpackage model
|
2008-08-09 07:22:38 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
abstract class CompositeDBField extends DBField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
|
|
|
* Similiar to {@link DataObject::$db},
|
|
|
|
* holds an array of composite field names.
|
|
|
|
* Don't include the fields "main name",
|
|
|
|
* it will be prefixed in {@link requireField()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* @config
|
|
|
|
* @var array
|
2009-05-27 02:05:10 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
private static $composite_db = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
2015-09-04 05:49:22 +02:00
|
|
|
* Either the parent dataobject link, or a record of saved values for each field
|
|
|
|
*
|
|
|
|
* @var array|DataObject
|
2009-05-27 02:05:10 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
protected $record = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
2015-09-04 05:49:22 +02:00
|
|
|
* Write all nested fields into a manipulation
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-27 02:05:10 +02:00
|
|
|
* @param array $manipulation
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function writeToManipulation(&$manipulation) {
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
// Write sub-manipulation
|
|
|
|
$fieldObject = $this->dbObject($field);
|
|
|
|
$fieldObject->writeToManipulation($manipulation);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
|
|
|
* Add all columns which are defined through {@link requireField()}
|
|
|
|
* and {@link $composite_db}, or any additional SQL that is required
|
2013-06-21 00:32:08 +02:00
|
|
|
* to get to these columns. Will mostly just write to the {@link SQLSelect->select}
|
2009-05-27 02:05:10 +02:00
|
|
|
* array.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param SQLSelect $query
|
2009-05-27 02:05:10 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function addToQuery(&$query) {
|
|
|
|
parent::addToQuery($query);
|
|
|
|
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
$table = $this->getTable();
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
if($table) {
|
|
|
|
$query->selectField("\"{$table}\".\"{$key}\"");
|
|
|
|
} else {
|
|
|
|
$query->selectField("\"{$key}\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
|
|
|
* Return array in the format of {@link $composite_db}.
|
|
|
|
* Used by {@link DataObject->hasOwnDatabaseField()}.
|
2015-09-04 05:49:22 +02:00
|
|
|
*
|
2009-05-27 02:05:10 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function compositeDatabaseFields() {
|
|
|
|
return $this->config()->composite_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function isChanged() {
|
|
|
|
// When unbound, use the local changed flag
|
|
|
|
if(! ($this->record instanceof DataObject) ) {
|
|
|
|
return $this->isChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer to parent record
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
if($this->record->isChanged($key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
2015-09-04 05:49:22 +02:00
|
|
|
* Composite field defaults to exists only if all fields have values
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-27 02:05:10 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function exists() {
|
|
|
|
// By default all fields
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
$fieldObject = $this->dbObject($field);
|
|
|
|
if(!$fieldObject->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireField() {
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec){
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
DB::requireField($this->tableName, $key, $spec);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:05:10 +02:00
|
|
|
/**
|
2015-09-04 05:49:22 +02:00
|
|
|
* Assign the given value.
|
|
|
|
* If $record is assigned to a dataobject, this field becomes a loose wrapper over
|
|
|
|
* the records on that object instead.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* @param type $value
|
|
|
|
* @param DataObject $record
|
|
|
|
* @param type $markChanged
|
|
|
|
* @return type
|
2009-05-27 02:05:10 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
|
|
|
$this->isChanged = $markChanged;
|
|
|
|
|
|
|
|
// When given a dataobject, bind this field to that
|
|
|
|
if($record instanceof DataObject) {
|
|
|
|
$this->bindTo($record);
|
|
|
|
$record = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
// Check value
|
|
|
|
if($value instanceof CompositeDBField) {
|
|
|
|
// Check if saving from another composite field
|
|
|
|
$this->setField($field, $value->getField($field));
|
|
|
|
|
|
|
|
} elseif(isset($value[$field])) {
|
|
|
|
// Check if saving from an array
|
|
|
|
$this->setField($field, $value[$field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load from $record
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
if(isset($record[$key])) {
|
|
|
|
$this->setField($field, $record[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind this field to the dataobject, and set the underlying table to that of the owner
|
|
|
|
*
|
|
|
|
* @param DataObject $dataObject
|
|
|
|
*/
|
|
|
|
public function bindTo($dataObject) {
|
|
|
|
$this->record = $dataObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveInto($dataObject) {
|
|
|
|
foreach($this->compositeDatabaseFields() as $field => $spec) {
|
|
|
|
// Save into record
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
$dataObject->setField($key, $this->getField($field));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get value of a single composite field
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getField($field) {
|
|
|
|
// Skip invalid fields
|
|
|
|
$fields = $this->compositeDatabaseFields();
|
|
|
|
if(!isset($fields[$field])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check bound object
|
|
|
|
if($this->record instanceof DataObject) {
|
|
|
|
$key = $this->getName().$field;
|
|
|
|
return $this->record->getField($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check local record
|
|
|
|
if(isset($this->record[$field])) {
|
|
|
|
return $this->record[$field];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasField($field) {
|
|
|
|
$fields = $this->compositeDatabaseFields();
|
|
|
|
return isset($fields[$field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set value of a single composite field
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param mixed $value
|
|
|
|
* @param bool $markChanged
|
|
|
|
*/
|
|
|
|
public function setField($field, $value, $markChanged = true) {
|
|
|
|
// Skip non-db fields
|
|
|
|
if(!$this->hasField($field)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set changed
|
|
|
|
if($markChanged) {
|
|
|
|
$this->isChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set bound object
|
|
|
|
if($this->record instanceof DataObject) {
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
return $this->record->setField($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set local record
|
|
|
|
$this->record[$field] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a db object for the named field
|
|
|
|
*
|
|
|
|
* @param string $field Field name
|
|
|
|
* @return DBField|null
|
|
|
|
*/
|
|
|
|
public function dbObject($field) {
|
|
|
|
$fields = $this->compositeDatabaseFields();
|
|
|
|
if(!isset($fields[$field])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build nested field
|
|
|
|
$key = $this->getName() . $field;
|
|
|
|
$spec = $fields[$field];
|
|
|
|
$fieldObject = Object::create_from_string($spec, $key);
|
|
|
|
$fieldObject->setValue($this->getField($field), null, false);
|
|
|
|
return $fieldObject;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-12 02:04:58 +02:00
|
|
|
}
|