Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SiteConfigSubsites::augmentSQL()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 29
rs 6.7272
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
The private property $has_one is not used, and could 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()));
0 ignored issues
show
Bug introduced by
'SubsiteID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $fields->push(HiddenField::create(/** @scrutinizer ignore-type */ 'SubsiteID', 'SubsiteID', SubsiteState::singleton()->getSubsiteId()));
Loading history...
77
    }
78
}
79