Completed
Push — master ( 1ce86f...d3a9a3 )
by Nicolaas
02:34
created

getRealEcommerceTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 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
    public function loadTranslatedValues($countryID = 0, $variableOrMethod = '')
45
    {
46
        $translation = null;
47
        if (! $countryID) {
48
            $countryObject = CountryPrice_EcommerceCountry::get_real_country();
49
            if ($countryObject) {
50
                $countryID = $countryObject->ID;
51
            }
52
        }
53
        if ($countryID) {
54
            $key = $this->owner->ID.'_'.$countryID;
55
            if (! isset(self::$_translations[$key])) {
56
                $translation = $this->owner->getRealEcommerceTranslation($countryID);
57
                self::$_translations[$key] = $translation;
58
            } else {
59
                $translation = self::$_translations[$key];
60
            }
61
        }
62
        if ($translation) {
63
            $fieldsToReplace = $translation->FieldsToReplace();
64
            foreach ($fieldsToReplace as $replaceFields) {
65
                $pageField = $replaceFields->PageField;
66
                $translationField = $replaceFields->TranslationField;
67
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
68
                    if ($translation->hasMethod($translationField)) {
69
                        $pageFieldTranslated = $pageField.'Translated';
70
                        $this->owner->$pageField = $translation->$translationField();
71
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
72
                    } else {
73
                        $this->owner->$pageField = $translation->$translationField;
74
                    }
75
                }
76
                if ($variableOrMethod) {
77
                    return $this->owner->$pageField;
78
                }
79
            }
80
        } else {
81
            if ($variableOrMethod) {
82
                if ($translation->hasMethod($variableOrMethod)) {
83
                    return $this->owner->$variableOrMethod();
84
                } else {
85
                    return $this->owner->$variableOrMethod;
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * @var int $countryID
93
     *
94
     * @return CountryPrice_Translation | null
95
     */
96
    public function getRealEcommerceTranslation($countryID)
97
    {
98
        return $this->owner
99
            ->CountryPriceTranslations()
100
            ->filter(
101
                array(
102
                    "EcommerceCountryID" => $countryID,
103
                    'ParentID' => $this->owner->ID,
104
                )
105
            )
106
            ->exclude(
107
                array('WithoutTranslation' => 1)
108
            )
109
            ->first();
110
    }
111
112
    /**
113
     * @var int $countryID
114
     *
115
     * @return CountryPrice_Translation | null
116
     */
117
    public function getEcommerceTranslation($countryID)
118
    {
119
        return $this->owner
120
            ->CountryPriceTranslations()
121
            ->filter(
122
                array(
123
                    "EcommerceCountryID" => $countryID,
124
                    'ParentID' => $this->owner->ID,
125
                )
126
            )
127
            ->first();
128
    }
129
}
130