Completed
Push — master ( 80c72a...a98ebf )
by
unknown
8s
created

ElementalSubsiteExtension::augmentSQL()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 10
nop 2
dl 0
loc 33
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\ElementalSubsites\Extensions;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DataQuery;
11
use SilverStripe\ORM\Queries\SQLSelect;
12
use SilverStripe\Subsites\Model\Subsite;
13
use SilverStripe\Subsites\State\SubsiteState;
14
15
/**
16
 * Make elements compatible with subsites
17
 * Apply this extension to BaseElement
18
 */
19
class ElementalSubsiteExtension extends DataExtension
20
{
21
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
22
        'Subsite' => Subsite::class,
23
    ];
24
25
    public function updateCMSFields(FieldList $fields)
26
    {
27
        // add SubsiteID if Subsites is installed and Elemental has a subsite
28
        if (class_exists(Subsite::class)) {
29
            $fields->push(
30
                HiddenField::create('SubsiteID', null, 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

30
                HiddenField::create(/** @scrutinizer ignore-type */ 'SubsiteID', null, SubsiteState::singleton()->getSubsiteId())
Loading history...
31
            );
32
        }
33
    }
34
35
    /**
36
     * Update any requests for elements to limit the results to the current site
37
     */
38
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
39
    {
40
        if (!class_exists(Subsite::class)) {
41
            return;
42
        }
43
44
        if (Subsite::$disable_subsite_filter) {
45
            return;
46
        }
47
48
        if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) {
0 ignored issues
show
introduced by
The condition $dataQuery && $dataQuery...site.filter') === false can never be true.
Loading history...
49
            return;
50
        }
51
52
        if ($query->filtersOnID()) {
53
            return;
54
        }
55
56
        if (Subsite::$force_subsite) {
57
            $subsiteID = Subsite::$force_subsite;
58
        } else {
59
            $subsiteID = (int) SubsiteState::singleton()->getSubsiteId();
60
        }
61
62
        // Get the base table name
63
        $elementTableName = DataObject::getSchema()->baseDataTable(BaseElement::class);
64
65
        // The foreach is an ugly way of getting the first key :-)
66
        foreach ($query->getFrom() as $tableName => $info) {
67
            // The tableName should be Element or Element_Live...
68
            if (substr($tableName, 0, strlen($elementTableName)) === $elementTableName) {
69
                $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
70
                break;
71
            }
72
        }
73
    }
74
}
75