Completed
Push — master ( 31b335...9a25ab )
by
unknown
04:15
created

ElementSubsiteExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\ElementalSubsites\Extensions;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use DNADesign\Elemental\Models\ElementalArea;
7
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\Queries\SQLSelect;
10
use SilverStripe\ORM\DataQuery;
11
use SilverStripe\Forms\FieldList;
12
13
/**
14
 * @package elemental
15
 *
16
 * Make elements compatibale with subsites
17
 * Apply this extension to BaseElement
18
 */
19
class ElementSubsiteExtension extends DataExtension
20
{
21
22
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
23
        'Subsite' => Subsite::class
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalSubsites\Extensions\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    );
25
26
    public function updateCMSFields(FieldList $fields) {
27
28
        // add SubsiteID if Subsites is installed andf Elemental has a subsite
29
        if(class_exists('Subsite')) {
30
            $fields->push(new HiddenField('SubsiteID', null, Subsite::currentSubsiteID()));
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalSubsites\Extensions\HiddenField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
        }
32
    }
33
34
    /**
35
     * Update any requests for elements to limit the results to the current site
36
     */
37
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = NULL) {
38
39
        if(!class_exists('Subsite')) {
40
            return;
41
        }
42
43
        if (Subsite::$disable_subsite_filter) {
44
            return;
45
        }
46
47
        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...
48
            return;
49
        }
50
51
        if ($query->filtersOnID()) {
52
            return;
53
        }
54
55
        if (Subsite::$force_subsite) {
56
            $subsiteID = Subsite::$force_subsite;
57
        } else {
58
            $subsiteID = (int)Subsite::currentSubsiteID();
59
        }
60
61
        // The foreach is an ugly way of getting the first key :-)
62
        foreach ($query->getFrom() as $tableName => $info) {
63
            // The tableName should be Element or Element_Live...
64
            if(strpos($tableName, 'Element') !== false) {
65
                $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
66
                break;
67
            }
68
        }
69
    }
70
71
}
72
73
74