Source of file MultiFormObjectDecorator.php
Size: 2,575 Bytes - Last Modified: 2021-12-23T10:33:19+00:00
/var/www/docs.ssmods.com/process/src/src/extensions/MultiFormObjectDecorator.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | <?php namespace SilverStripe\MultiForm\Extensions; use SilverStripe\MultiForm\Models\MultiFormSession; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataQuery; use SilverStripe\ORM\Queries\SQLSelect; /** * 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 * */ class MultiFormObjectDecorator extends DataExtension { private static $db = [ 'MultiFormIsTemporary' => 'Boolean', ]; private static $has_one = [ 'MultiFormSession' => MultiFormSession::class, ]; /** * Augment any queries to MultiFormObjectDecorator and only * return anything that isn't considered temporary. * @param SQLSelect $query * @param DataQuery|null $dataQuery */ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) { $where = $query->getWhere(); if (!$where && !$this->wantsTemporary($query)) { $from = array_values($query->getFrom()); $query->addWhere("{$from[0]}.\"MultiFormIsTemporary\" = '0'"); return; } $filterKey = key($where[0]); if (strpos($filterKey, ".`ID` = ") === false && strpos($filterKey, ".ID = ") === false && strpos($filterKey, "ID = ") !== 0 && !$this->wantsTemporary($query) ) { $from = array_values($query->getFrom()); $query->addWhere("{$from[0]}.\"MultiFormIsTemporary\" = '0'"); } } /** * Determines if the current query is supposed * to be exempt from the automatic filtering out * of temporary records. * * @param SQLSelect $query * @return boolean */ protected function wantsTemporary($query) { foreach ($query->getWhere() as $whereClause) { $from = array_values($query->getFrom()); // SQLQuery will automatically add double quotes and single quotes to values, so check against that. $key = key($whereClause); if ($key == "{$from[0]}.\"MultiFormIsTemporary\" = ?" && current($whereClause[$key]) == 1) { return true; } } return false; } } |