Completed
Push — master ( fec354...33e41e )
by Nicolaas
02:55
created

CountryPrice_SiteTreeExtions   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 0
loc 137
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 13 1
A AvailableTranslationLinks() 0 5 1
A CanonicalLink() 0 7 1
C loadTranslatedValues() 0 46 13
A getRealEcommerceTranslation() 0 14 1
A getEcommerceTranslation() 0 12 1
1
<?php
2
3
4
class CountryPrice_SiteTreeExtions extends SiteTreeExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many 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...
7
        'CountryPriceTranslations' => 'CountryPrice_Translation'
8
    );
9
10
    private static $field_labels = array(
0 ignored issues
show
Unused Code introduced by
The property $field_labels 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...
11
        'CountryPriceTranslations' => 'Translations'
12
    );
13
14
    /**
15
     * Update Fields
16
     * @return FieldList
17
     */
18
    public function updateCMSFields(FieldList $fields)
19
    {
20
        $fields->addFieldsToTab(
21
            'Root.Translations',
22
            GridField::create(
0 ignored issues
show
Documentation introduced by
\GridField::create('Coun...orOrderItems::create()) is of type object<GridField>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
                'CountryPriceTranslations',
24
                'Translations',
25
                $this->owner->CountryPriceTranslations(),
26
                GridFieldConfigForOrderItems::create()
27
            )
28
        );
29
        return $fields;
30
    }
31
32
    private static $_translations = array();
33
34
    /**
35
     *
36
     * @return DataList
37
     */
38
    public function AvailableTranslationLinks()
39
    {
40
        return $this->owner->CountryPriceTranslations()
41
            ->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"');
42
    }
43
44
    /**
45
     *
46
     * @return CountryPrice_Translation | null
47
     */
48
    public function CanonicalLink()
49
    {
50
        return $this->owner->CountryPriceTranslations()
51
            ->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"')
52
            ->filter(array('EcommerceCountry.IsBackupCountry' => 1))
53
            ->first();
54
    }
55
56
    public function loadTranslatedValues($countryID = 0, $variableOrMethod = '')
57
    {
58
        $translation = null;
59
        if (! $countryID) {
60
            $countryObject = CountryPrice_EcommerceCountry::get_real_country();
61
            if ($countryObject) {
62
                $countryID = $countryObject->ID;
63
            }
64
        }
65
        if ($countryID) {
66
            $key = $this->owner->ID.'_'.$countryID;
67
            if (isset(self::$_translations[$key])) {
68
                $translation = self::$_translations[$key];
69
            } else {
70
                $translation = $this->owner->getRealEcommerceTranslation($countryID);
71
                self::$_translations[$key] = $translation;
72
            }
73
        }
74
        if ($translation) {
75
            $fieldsToReplace = $translation->FieldsToReplace();
76
            foreach ($fieldsToReplace as $replaceFields) {
77
                $pageField = $replaceFields->PageField;
78
                $translationField = $replaceFields->TranslationField;
79
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
80
                    if ($translation->hasMethod($translationField)) {
81
                        $pageFieldTranslated = $pageField.'Translated';
82
                        $this->owner->$pageField = $translation->$translationField();
83
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
84
                    } else {
85
                        $this->owner->$pageField = $translation->$translationField;
86
                    }
87
                }
88
                if ($variableOrMethod) {
89
                    return $this->owner->$pageField;
90
                }
91
            }
92
        } else {
93
            if ($variableOrMethod) {
94
                if ($translation->hasMethod($variableOrMethod)) {
95
                    return $this->owner->$variableOrMethod();
96
                } else {
97
                    return $this->owner->$variableOrMethod;
98
                }
99
            }
100
        }
101
    }
102
103
    /**
104
     * @var int $countryID
105
     *
106
     * @return CountryPrice_Translation | null
107
     */
108
    public function getRealEcommerceTranslation($countryID)
109
    {
110
        return CountryPrice_Translation::get()
111
            ->filter(
112
                array(
113
                    "EcommerceCountryID" => $countryID,
114
                    'ParentID' => $this->owner->ID
115
                )
116
            )
117
            ->exclude(
118
                array('WithoutTranslation' => 1)
119
            )
120
            ->first();
121
    }
122
123
    /**
124
     * @var int $countryID
125
     *
126
     * @return CountryPrice_Translation | null
127
     */
128
    public function getEcommerceTranslation($countryID)
129
    {
130
        return $this->owner
131
            ->CountryPriceTranslations()
132
            ->filter(
133
                array(
134
                    "EcommerceCountryID" => $countryID,
135
                    'ParentID' => $this->owner->ID,
136
                )
137
            )
138
            ->first();
139
    }
140
}
141