silverstripe-multiform/code/MultiFormObjectDecorator.php

59 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2008-04-18 00:03:51 +02:00
/**
* Decorate {@link DataObject}s which are required to be saved
* to the database directly by a {@link MultiFormStep}.
* Only needed for objects which aren't stored in the session,
* which is the default.
*
* This decorator also augments get() requests to the datalayer
* by automatically filtering out temporary objects.
* You can override this filter by putting the following statement
* in your WHERE clause:
* `<MyDataObjectClass>`.`MultiFormIsTemporary` = 1
*
* @package multiform
*/
class MultiFormObjectDecorator extends DataObjectDecorator {
public function updateDBFields() {
return array(
2008-04-22 13:03:03 +02:00
'db' => array(
2008-04-18 00:03:51 +02:00
'MultiFormIsTemporary' => 'Boolean',
),
2008-04-22 13:03:03 +02:00
'has_one' => array(
2008-04-18 00:03:51 +02:00
'MultiFormSession' => 'MultiFormSession',
)
);
}
public function augmentSQL(SQLQuery &$query) {
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
if(
strpos($query->where[0], ".`ID` = ") === false
&& strpos($query->where[0], ".ID = ") === false
&& strpos($query->where[0], "ID = ") !== 0
&& !$this->wantsTemporary($query)
) {
$query->where[] = "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 0";
2008-04-18 00:03:51 +02:00
}
}
/**
* Determines if the current query is supposed
* to be exempt from the automatic filtering out
* of temporary records.
*
* @param SQLQuery $query
* @return boolean
*/
protected function wantsTemporary($query) {
foreach($query->where as $whereClause) {
if($whereClause == "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 1") return true;
2008-04-18 00:03:51 +02:00
}
return false;
}
}
2008-04-18 00:03:51 +02:00
?>