Completed
Push — master ( 894f14...355d7f )
by Nicolaas
03:14
created

getCountryPriceTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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->getCountryPriceTranslations(),
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->getCountryPriceTranslations()
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->getCountryPriceTranslations()
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 = '')
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...
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
                $pageFieldTranslated = $pageField . 'Translated';
79
                $translationField = $replaceFields->TranslationField;
80
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
81
                    if ($translation->hasMethod($translationField)) {
82
                        $this->owner->$pageField = $translation->$translationField();
83
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
84
                    } else {
85
                        $this->owner->$pageField = $translation->$translationField;
86
                        $this->owner->$pageFieldTranslated = $translation->$translationField;
87
                    }
88
                }
89
                if ($variableOrMethod) {
90
                    return $this->owner->$pageField;
91
                }
92
            }
93
        } else {
94
            if ($variableOrMethod) {
95
                if ($translation->hasMethod($variableOrMethod)) {
96
                    return $this->owner->$variableOrMethod();
97
                } else {
98
                    return $this->owner->$variableOrMethod;
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * @var int $countryID
106
     *
107
     * @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...
108
     */
109
    public function getRealEcommerceTranslation($countryID)
110
    {
111
        return CountryPrice_Translation::get()
112
            ->filter(
113
                array(
114
                    "EcommerceCountryID" => $countryID,
115
                    'ParentID' => $this->owner->ID
116
                )
117
            )
118
            ->exclude(
119
                array('WithoutTranslation' => 1)
120
            )
121
            ->first();
122
    }
123
124
    /**
125
     * @var int $countryID
126
     *
127
     * @return CountryPrice_Translation | null
128
     */
129
    public function getEcommerceTranslation($countryID)
130
    {
131
        return $this->owner
132
            ->getCountryPriceTranslations()
133
            ->filter(
134
                array(
135
                    "EcommerceCountryID" => $countryID,
136
                    'ParentID' => $this->owner->ID,
137
                )
138
            )
139
            ->first();
140
    }
141
142
    /**
143
     * @var int $countryID
144
     *
145
     * @return bool
146
     */
147
    public function hasCountryLocalInURL($countryID)
148
    {
149
        return $this->getEcommerceTranslation($countryID) ? true : false;
150
    }
151
152
    /**
153
     * we have added this because we got some weird mixups with magical methods
154
     * @return DataList (of CountryPrice_Translation objects)
155
     */
156
    function CountryPriceTranslations()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
157
    {
158
        return $this->getCountryPriceTranslations();
159
    }
160
161
    /**
162
     * we have added this because we got some weird mixups with magical methods
163
     * @return DataList (of CountryPrice_Translation objects)
164
     */
165
    function getCountryPriceTranslations()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
166
    {
167
        return CountryPrice_Translation::get()->filter(
168
            array('CountryPrice_Translation.ParentID' => $this->owner->ID)
169
        );
170
    }
171
172
}
173