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
|
|
|
*/
|
2008-11-02 21:01:49 +01: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.
|
|
|
|
*
|
2009-05-26 02:52:54 +02:00
|
|
|
* Keys are the static names, and the values are whether or not the value is an array that should
|
|
|
|
* be merged.
|
|
|
|
*
|
2008-03-10 22:28:35 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $decoratable_statics = array(
|
2009-05-26 02:52:54 +02:00
|
|
|
'db' => true,
|
|
|
|
'has_one' => true,
|
|
|
|
'indexes' => true,
|
|
|
|
'defaults' => true,
|
|
|
|
'has_many' => true,
|
|
|
|
'many_many' => true,
|
|
|
|
'belongs_many_many' => true,
|
|
|
|
'many_many_extraFields' => true,
|
|
|
|
'searchable_fields' => true,
|
|
|
|
'api_access' => false,
|
2008-03-10 22:28:35 +01:00
|
|
|
);
|
|
|
|
|
2009-05-26 05:46:04 +02:00
|
|
|
private static $extra_statics_loaded = array();
|
|
|
|
|
2009-03-31 21:31:21 +02:00
|
|
|
/**
|
|
|
|
* Set the owner of this decorator.
|
|
|
|
* @param DataObject $owner
|
|
|
|
*/
|
2009-06-04 08:48:44 +02:00
|
|
|
/*
|
|
|
|
function setOwner($owner, $ownerBaseClass = null) {
|
|
|
|
if($owner && !($owner instanceof DataObject)) {
|
2009-03-31 21:31:21 +02:00
|
|
|
user_error(sprintf(
|
|
|
|
"DataObjectDecorator->setOwner(): Trying to decorate an object of class '%s' with '%s',
|
|
|
|
only Dataobject subclasses are supported.",
|
|
|
|
get_class($owner), $this->class),
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-26 05:46:04 +02:00
|
|
|
parent::setOwner($owner, $ownerBaseClass);
|
2009-03-31 21:31:21 +02:00
|
|
|
}
|
2009-06-04 08:48:44 +02:00
|
|
|
*/
|
2009-03-31 21:31:21 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-11-02 01:36:57 +01:00
|
|
|
* Load the extra database fields defined in extraStatics.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-02 01:36:57 +01:00
|
|
|
function loadExtraStatics() {
|
2009-05-26 05:46:04 +02:00
|
|
|
if(!empty(self::$extra_statics_loaded[$this->ownerBaseClass][$this->class])) return;
|
|
|
|
self::$extra_statics_loaded[$this->ownerBaseClass][$this->class] = true;
|
|
|
|
|
|
|
|
// If the extension has been manually applied to a subclass, we should ignore that.
|
|
|
|
if(Object::has_extension(get_parent_class($this->owner), $this->class)) return;
|
2009-03-31 21:31:21 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if($fields = $this->extraStatics()) {
|
2009-05-26 02:52:54 +02:00
|
|
|
foreach($fields as $relation => $newVal) {
|
|
|
|
if(isset(self::$decoratable_statics[$relation])) {
|
2009-05-26 05:46:04 +02:00
|
|
|
$origVal = Object::get_static($this->ownerBaseClass, $relation);
|
2009-05-26 02:52:54 +02:00
|
|
|
|
|
|
|
// Array to be merged
|
|
|
|
if(self::$decoratable_statics[$relation]) {
|
|
|
|
// Can't use add_static_var() here as it would merge the array rather than replacing
|
2009-05-26 05:46:04 +02:00
|
|
|
Object::set_static($this->ownerBaseClass, $relation, array_merge((array)$origVal, $newVal));
|
2009-05-26 02:52:54 +02:00
|
|
|
|
|
|
|
// Value to be overwritten
|
|
|
|
} else {
|
2009-05-26 05:46:04 +02:00
|
|
|
Object::set_static ($this->ownerBaseClass, $relation, $newVal);
|
2009-05-26 02:52:54 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-14 01:16:32 +01:00
|
|
|
|
2009-05-26 05:46:04 +02:00
|
|
|
DataObject::$cache_has_own_table[$this->ownerBaseClass] = null;
|
|
|
|
DataObject::$cache_has_own_table_field[$this->ownerBaseClass] = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-02 01:36:57 +01:00
|
|
|
|
|
|
|
/**
|
2009-05-20 07:48:46 +02:00
|
|
|
* @deprecated 2.4 Use loadExtraStatics()
|
2008-11-02 01:36:57 +01:00
|
|
|
*/
|
|
|
|
function loadExtraDBFields() {
|
2009-05-20 07:48:46 +02:00
|
|
|
user_error('DataObjectDecorator::loadExtraDBFields() is deprecated. Please use loadExtraStatics() instead.', E_USER_NOTICE);
|
2008-11-02 01:36:57 +01:00
|
|
|
return $this->loadExtraStatics();
|
|
|
|
}
|
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-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) {
|
|
|
|
}
|
2008-11-07 13:18:35 +01:00
|
|
|
|
|
|
|
function onBeforeWrite() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAfterWrite() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBeforeDelete() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAfterDelete() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function requireDefaultRecords() {
|
|
|
|
}
|
2007-09-16 02:20:31 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
function populateDefaults() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function can($member) {
|
|
|
|
}
|
|
|
|
|
|
|
|
function canEdit($member) {
|
|
|
|
}
|
|
|
|
|
|
|
|
function canDelete($member) {
|
|
|
|
}
|
|
|
|
|
|
|
|
function canCreate($member) {
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2008-11-02 01:36:57 +01:00
|
|
|
function extraStatics() {
|
|
|
|
return $this->extraDBFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 07:48:46 +02:00
|
|
|
* @deprecated 2.4 Use extraStatics()
|
2008-11-02 01:36:57 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function extraDBFields() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-16 18:55:47 +02:00
|
|
|
* This function is used to provide modifications to the form in the CMS
|
2009-02-02 00:49:53 +01:00
|
|
|
* by the decorator. By default, no changes are made. {@link DataObject->getCMSFields()}.
|
|
|
|
*
|
2008-10-03 18:21:09 +02:00
|
|
|
* Please consider using {@link updateFormFields()} to globally add
|
|
|
|
* formfields to the record. The method {@link updateCMSFields()}
|
|
|
|
* should just be used to add or modify tabs, or fields which
|
|
|
|
* are specific to the CMS-context.
|
|
|
|
*
|
|
|
|
* Caution: Use {@link FieldSet->addFieldToTab()} to add fields.
|
2007-09-16 18:55:47 +02:00
|
|
|
*
|
2008-10-03 18:21:09 +02:00
|
|
|
* @param FieldSet $fields FieldSet with a contained TabSet
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function updateCMSFields(FieldSet &$fields) {
|
|
|
|
}
|
2008-08-27 06:11:58 +02:00
|
|
|
|
2008-10-03 18:21:09 +02:00
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* This function is used to provide modifications to the form used
|
|
|
|
* for front end forms. {@link DataObject->getFrontEndFields()}
|
2008-10-03 18:21:09 +02:00
|
|
|
*
|
|
|
|
* Caution: Use {@link FieldSet->push()} to add fields.
|
|
|
|
*
|
|
|
|
* @param FieldSet $fields FieldSet without TabSet nesting
|
|
|
|
*/
|
2009-02-02 00:49:53 +01:00
|
|
|
function updateFrontEndFields(FieldSet &$fields) {
|
2008-10-03 18:21:09 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* This is used to provide modifications to the form actions
|
|
|
|
* used in the CMS. {@link DataObject->getCMSActions()}.
|
|
|
|
*
|
|
|
|
* @param FieldSet $actions FieldSet
|
|
|
|
*/
|
2008-11-07 13:18:35 +01:00
|
|
|
function updateCMSActions(FieldSet &$actions) {
|
|
|
|
}
|
|
|
|
|
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){
|
2008-11-02 01:36:57 +01:00
|
|
|
$extra_fields = $this->extraStatics();
|
2008-09-05 01:10:20 +02:00
|
|
|
if(isset($extra_fields['summary_fields'])){
|
|
|
|
$summary_fields = $extra_fields['summary_fields'];
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
// if summary_fields were passed in numeric array,
|
|
|
|
// convert to an associative array
|
|
|
|
if($summary_fields && array_key_exists(0, $summary_fields)) {
|
|
|
|
$summary_fields = array_combine(array_values($summary_fields), array_values($summary_fields));
|
|
|
|
}
|
2008-09-05 01:10:20 +02:00
|
|
|
if($summary_fields) $fields = array_merge($fields, $summary_fields);
|
|
|
|
}
|
2008-08-27 06:11:58 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 04:03:16 +01:00
|
|
|
/**
|
|
|
|
* this function is used to provide modifications to the fields labels in CMS
|
|
|
|
* by the decorator
|
|
|
|
* By default, the fieldLabels() of its owner will merge more fields defined in the decorator's
|
|
|
|
* $extra_fields['field_labels']
|
|
|
|
*/
|
|
|
|
function updateFieldLabels(&$lables){
|
2008-11-02 01:36:57 +01:00
|
|
|
$extra_fields = $this->extraStatics();
|
2008-10-28 04:03:16 +01:00
|
|
|
if(isset($extra_fields['field_labels'])){
|
|
|
|
$field_labels = $extra_fields['field_labels'];
|
|
|
|
if($field_labels) $lables = array_merge($lables, $field_labels);
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 23:38:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear any internal caches.
|
|
|
|
*/
|
|
|
|
function flushCache() {
|
|
|
|
}
|
2008-11-02 00:00:50 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-03-14 01:16:32 +01:00
|
|
|
?>
|