Passed
Push — master ( 4ebc9d...305f3a )
by Nicolaas
02:38
created

thisPageHasTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
4
class CountryPrice_SiteTreeExtensions 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 CanonicalObject()
49
    {
50
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
51
        if($countryObject && $countryObject->Code) {
52
            $object = $this->owner->CountryPriceTranslations()
53
                ->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"')
54
                ->filter(array('EcommerceCountry.Code' => $countryObject->Code))
55
                ->first();
56
            if($object && $object->exists()) {
57
                return $object;
58
            }
59
        }
60
        return false;
61
    }
62
63
    public function loadTranslatedValues($countryID = 0, $variableOrMethod = '')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
64
    {
65
        $translation = null;
66
        if (! $countryID) {
67
            $countryObject = CountryPrice_EcommerceCountry::get_real_country();
68
            if ($countryObject) {
69
                $countryID = $countryObject->ID;
70
            }
71
        }
72
        if ($countryID) {
73
            $key = $this->owner->ID.'_'.$countryID;
74
            if (isset(self::$_translations[$key])) {
75
                $translation = self::$_translations[$key];
76
            } else {
77
                $translation = $this->owner->getRealEcommerceTranslation($countryID);
78
                self::$_translations[$key] = $translation;
79
            }
80
        }
81
        if ($translation) {
82
            $fieldsToReplace = $translation->FieldsToReplace();
83
            foreach ($fieldsToReplace as $replaceFields) {
84
                $pageField = $replaceFields->PageField;
85
                $pageFieldTranslated = $pageField . 'Translated';
86
                $translationField = $replaceFields->TranslationField;
87
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
88
                    if ($translation->hasMethod($translationField)) {
89
                        $this->owner->$pageField = $translation->$translationField();
90
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
91
                    } else {
92
                        $this->owner->$pageField = $translation->$translationField;
93
                        $this->owner->$pageFieldTranslated = $translation->$translationField;
94
                    }
95
                }
96
                if ($variableOrMethod) {
97
                    return $this->owner->$pageField;
98
                }
99
            }
100
        } else {
101
            if ($variableOrMethod) {
102
                if ($translation->hasMethod($variableOrMethod)) {
103
                    return $this->owner->$variableOrMethod();
104
                } else {
105
                    return $this->owner->$variableOrMethod;
106
                }
107
            }
108
        }
109
    }
110
111
    /**
112
     * cache for all translations ...
113
     * @var [type]
114
     */
115
    private $_translations_all_cache = [];
116
117
    /**
118
     * @var int $countryID
119
     *
120
     * @return CountryPrice_Translation | null
0 ignored issues
show
Documentation introduced by
Should the return type not be CountryPrice_Translation|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
     */
122
    public function getRealEcommerceTranslation($countryID)
123
    {
124
        return CountryPrice_Translation::get()
125
            ->filter(
126
                array(
127
                    "EcommerceCountryID" => $countryID,
128
                    'ParentID' => $this->owner->ID
129
                )
130
            )
131
            ->exclude(
132
                array('WithoutTranslation' => 1)
133
            )
134
            ->first();
135
    }
136
137
    /**
138
     * cache for all translations ...
139
     * @var [type]
140
     */
141
    private $_translations_all_cache = [];
142
143
    /**
144
     * @var int $countryID
145
     *
146
     * @return CountryPrice_Translation | null
147
     */
148
    public function getEcommerceTranslation($countryID)
149
    {
150
        if(!isset($this->_translations_all_cache[$countryID])) {
151
            $this->_translations_all_cache[$countryID] = $this->owner
152
                ->CountryPriceTranslations()
153
                ->filter(
154
                    array(
155
                        "EcommerceCountryID" => $countryID,
156
                        'ParentID' => $this->owner->ID,
157
                    )
158
                )
159
                ->first();
160
        }
161
        return $this->_translations_all_cache[$countryID];
162
    }
163
164
    /**
165
     * @var int $countryID
166
     *
167
     * @return bool
168
     */
169
    public function thisPageHasTranslation($countryID)
170
    {
171
        return $this->getEcommerceTranslation($countryID) ? true : false;
172
    }
173
174
175
}
176