silverstripe-multiform/code/extensions/MultiFormObjectDecorator.php

57 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.
2015-11-02 01:38:50 +01:00
*
2008-04-18 00:03:51 +02:00
* 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 DataExtension {
2015-11-02 01:38:50 +01:00
private static $db = array(
'MultiFormIsTemporary' => 'Boolean',
);
2015-11-02 01:38:50 +01:00
private static $has_one = array(
'MultiFormSession' => 'MultiFormSession',
);
2015-11-02 01:38:50 +01:00
public function augmentSQL(SQLSelect $query) {
2008-04-18 00:03:51 +02:00
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
if(
2015-11-02 01:38:50 +01:00
strpos($query->where[0], ".`ID` = ") === false
&& strpos($query->where[0], ".ID = ") === false
2008-04-18 00:03:51 +02:00
&& strpos($query->where[0], "ID = ") !== 0
&& !$this->wantsTemporary($query)
) {
2015-11-02 01:38:50 +01:00
$query->where[] = "\"{$query->from[0]}\".\"MultiFormIsTemporary\" = 0";
2008-04-18 00:03:51 +02:00
}
}
2015-11-02 01:38:50 +01:00
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
}
2008-04-18 00:03:51 +02:00
return false;
}
}