2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Plug-ins for additional functionality in your DataObjects
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* DataObject decorators add extra functionality to your data objects.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-08-16 08:32:49 +02:00
|
|
|
abstract class DataObjectDecorator extends Extension {
|
2007-09-16 02:20:31 +02:00
|
|
|
|
2008-03-10 22:28:35 +01:00
|
|
|
/**
|
|
|
|
* Statics on a {@link DataObject} subclass
|
|
|
|
* which can be decorated onto. This list is
|
|
|
|
* limited for security and performance reasons.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $decoratable_statics = array(
|
|
|
|
'db',
|
|
|
|
'has_one',
|
|
|
|
'indexes',
|
|
|
|
'defaults',
|
|
|
|
'has_many',
|
|
|
|
'many_many',
|
|
|
|
'belongs_many_many',
|
|
|
|
'many_many_extraFields',
|
|
|
|
'searchable_fields',
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Load the extra database fields defined in extraDBFields.
|
2008-03-10 22:28:35 +01:00
|
|
|
*
|
2008-03-11 04:29:49 +01:00
|
|
|
* @todo Rename to "loadExtraStaticFields", as it decorates more than database related fields.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function loadExtraDBFields() {
|
2008-08-12 04:51:33 +02:00
|
|
|
// Don't apply DB fields if the parent object has this extension too
|
|
|
|
if(singleton(get_parent_class($this->owner))->extInstance($this->class)) return;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields = $this->extraDBFields();
|
|
|
|
$className = $this->owner->class;
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($fields) {
|
|
|
|
foreach($fields as $relationType => $fields) {
|
2008-03-10 22:28:35 +01:00
|
|
|
if(in_array($relationType, self::$decoratable_statics)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
eval("$className::\$$relationType = array_merge((array){$className}::\$$relationType, (array)\$fields);");
|
2008-04-26 08:52:36 +02:00
|
|
|
$this->owner->set_stat($relationType, eval("return $className::\$$relationType;"));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-04-26 08:52:36 +02:00
|
|
|
$this->owner->set_uninherited('fieldExists', null);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Edit the given query object to support queries for this extension
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param SQLQuery $query Query to augment.
|
|
|
|
*/
|
2007-08-16 08:32:49 +02:00
|
|
|
function augmentSQL(SQLQuery &$query) {
|
|
|
|
}
|
2007-09-16 02:20:31 +02:00
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Update the database schema as required by this extension.
|
|
|
|
*/
|
2007-08-16 08:32:49 +02:00
|
|
|
function augmentDatabase() {
|
|
|
|
}
|
2008-08-11 04:25:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Augment a write-record request.
|
|
|
|
*
|
|
|
|
* @param SQLQuery $manipulation Query to augment.
|
|
|
|
*/
|
|
|
|
function augmentWrite(&$manipulation) {
|
|
|
|
}
|
2007-09-16 02:20:31 +02:00
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* Define extra database fields
|
|
|
|
*
|
|
|
|
* Return a map where the keys are db, has_one, etc, and the values are
|
|
|
|
* additional fields/relations to be defined.
|
2008-04-09 13:28:32 +02:00
|
|
|
*
|
|
|
|
* Note: please ensure that the static variable that you are overloading is explicitly defined on the class that
|
|
|
|
* you are extending. For example, we have added static $has_one = array() to the Member definition, so that we
|
|
|
|
* can add has_one relationships to Member with decorators.
|
|
|
|
*
|
|
|
|
* If you forget to do this, db/build won't create the new relation. Don't blame us, blame PHP! ;-)
|
2007-09-16 18:55:47 +02:00
|
|
|
*
|
|
|
|
* @return array Returns a map where the keys are db, has_one, etc, and
|
|
|
|
* the values are additional fields/relations to be defined.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function extraDBFields() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:55:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* This function is used to provide modifications to the form in the CMS
|
|
|
|
* by the decorator.
|
|
|
|
* By default, no changes are made - if you want you can overload this
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* @param FieldSet $fields The FieldSet to modify.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function updateCMSFields(FieldSet &$fields) {
|
|
|
|
}
|
2008-08-27 06:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this function is used to provide modifications to the summary fields in CMS
|
|
|
|
* by the decorator
|
|
|
|
* By default, the summaryField() of its owner will merge more fields defined in the decorator's
|
|
|
|
* $extra_fields['summary_fields']
|
|
|
|
*/
|
|
|
|
function updateSummaryFields(&$fields){
|
|
|
|
$extra_fields = $this->extraDBFields();
|
|
|
|
$summary_fields = $extra_fields['summary_fields'];
|
|
|
|
if($summary_fields)$fields = array_merge($fields, $summary_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSummaryFieldsExcludeExtra(&$fields){
|
|
|
|
$extra_fields = $this->extraDBFields();
|
|
|
|
$summary_fields = $extra_fields['summary_fields'];
|
|
|
|
if($summary_fields)$fields = array_merge($fields, $summary_fields);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
?>
|