Completed
Push — master ( 5cf2d8...46bcff )
by Damian
13s
created

SiteConfigSubsites::augmentSQL()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 9
nop 2
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
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

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 View Code Duplication
        if ((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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