Completed
Push — master ( 898373...fb0bef )
by Nicolaas
04:30
created

AvailableTranslationLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
57
                    ->CountryPriceTranslations()
58
                    ->filter(
59
                        array(
60
                            "EcommerceCountryID" => $countryID,
61
                            'ParentID' => $this->owner->ID,
62
                        )
63
                    )
64
                    ->exclude(
65
                        array('WithoutTranslation' => 1)
66
                    )
67
                    ->first();
68
                self::$_translations[$key] = $translation;
69
            } else {
70
                $translation = self::$_translations[$key];
71
            }
72
        }
73
        if ($translation) {
74
            $fieldsToReplace = $translation->FieldsToReplace();
75
            foreach ($fieldsToReplace as $replaceFields) {
76
                $pageField = $replaceFields->PageField;
77
                $translationField = $replaceFields->TranslationField;
78
                if (! $variableOrMethod || $variableOrMethod === $pageField) {
79
                    if ($translation->hasMethod($translationField)) {
80
                        $pageFieldTranslated = $pageField.'Translated';
81
                        $this->owner->$pageField = $translation->$translationField();
82
                        $this->owner->$pageFieldTranslated = $translation->$translationField();
83
                    } else {
84
                        $this->owner->$pageField = $translation->$translationField;
85
                    }
86
                }
87
                if ($variableOrMethod) {
88
                    return $this->owner->$pageField;
89
                }
90
            }
91
        } else {
92
            if ($variableOrMethod) {
93
                if ($translation->hasMethod($variableOrMethod)) {
94
                    return $this->owner->$variableOrMethod();
95
                } else {
96
                    return $this->owner->$variableOrMethod;
97
                }
98
            }
99
        }
100
    }
101
}
102