silverstripe /
silverstripe-subsites
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\Subsites\Extensions; |
||
| 4 | |||
| 5 | use SilverStripe\Forms\FieldList; |
||
| 6 | use SilverStripe\Forms\HiddenField; |
||
| 7 | use SilverStripe\ORM\DataExtension; |
||
| 8 | use SilverStripe\ORM\DataQuery; |
||
| 9 | use SilverStripe\ORM\Queries\SQLSelect; |
||
| 10 | use SilverStripe\SiteConfig\SiteConfig; |
||
| 11 | use SilverStripe\Subsites\Model\Subsite; |
||
| 12 | use SilverStripe\Subsites\State\SubsiteState; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Extension for the SiteConfig object to add subsites support |
||
| 16 | */ |
||
| 17 | class SiteConfigSubsites extends DataExtension |
||
| 18 | { |
||
| 19 | private static $has_one = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 20 | 'Subsite' => Subsite::class, // The subsite that this page belongs to |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Update any requests to limit the results to the current site |
||
| 25 | * @param SQLSelect $query |
||
| 26 | * @param DataQuery|null $dataQuery |
||
| 27 | */ |
||
| 28 | public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) |
||
| 29 | { |
||
| 30 | if (Subsite::$disable_subsite_filter) { |
||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | // If you're querying by ID, ignore the sub-site - this is a bit ugly... |
||
| 35 | if ($query->filtersOnID()) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | $regexp = '/^(.*\.)?("|`)?SubsiteID("|`)?\s?=/'; |
||
| 39 | foreach ($query->getWhereParameterised($parameters) as $predicate) { |
||
| 40 | if (preg_match($regexp, $predicate)) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | $subsiteID = SubsiteState::singleton()->getSubsiteId(); |
||
| 46 | if ($subsiteID === null) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $froms = $query->getFrom(); |
||
| 51 | $froms = array_keys($froms); |
||
| 52 | $tableName = array_shift($froms); |
||
| 53 | if ($tableName !== SiteConfig::getSchema()->tableName(SiteConfig::class)) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function onBeforeWrite() |
||
| 60 | { |
||
| 61 | if ((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) { |
||
|
0 ignored issues
–
show
|
|||
| 62 | $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return a piece of text to keep DataObject cache keys appropriately specific |
||
| 68 | */ |
||
| 69 | public function cacheKeyComponent() |
||
| 70 | { |
||
| 71 | return 'subsite-' . SubsiteState::singleton()->getSubsiteId(); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function updateCMSFields(FieldList $fields) |
||
| 75 | { |
||
| 76 | $fields->push(HiddenField::create('SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId())); |
||
| 77 | } |
||
| 78 | } |
||
| 79 |