CountryPrice_SiteTreeExtensions::CanonicalObject()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 3
nop 0
1
<?php
2
3
4
class CountryPrice_SiteTreeExtensions extends SiteTreeExtension
5
{
6
    private static $has_many = array(
7
        'CountryPriceTranslations' => 'CountryPrice_Translation'
8
    );
9
10
    private static $field_labels = array(
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;
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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
    /**
113
     * @var int $countryID
114
     *
115
     * @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...
116
     */
117
    public function getRealEcommerceTranslation($countryID)
118
    {
119
        return CountryPrice_Translation::get()
120
            ->filter(
121
                array(
122
                    "EcommerceCountryID" => $countryID,
123
                    'ParentID' => $this->owner->ID
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
124
                )
125
            )
126
            ->exclude(
127
                array('WithoutTranslation' => 1)
128
            )
129
            ->first();
130
    }
131
132
    /**
133
     * cache for all translations ...
134
     * @var [type]
135
     */
136
    private $_translations_all_cache = [];
137
138
    /**
139
     * @var int $countryID
140
     *
141
     * @return CountryPrice_Translation | null
142
     */
143
    public function getEcommerceTranslation($countryID)
144
    {
145
        if (!isset($this->_translations_all_cache[$countryID])) {
146
            $this->_translations_all_cache[$countryID] = $this->owner
147
                ->CountryPriceTranslations()
148
                ->filter(
149
                    array(
150
                        "EcommerceCountryID" => $countryID,
151
                        'ParentID' => $this->owner->ID,
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
152
                    )
153
                )
154
                ->first();
155
        }
156
        return $this->_translations_all_cache[$countryID];
157
    }
158
159
    /**
160
     * @var int $countryID
161
     *
162
     * @return bool
163
     */
164
    public function thisPageHasTranslation($countryID)
165
    {
166
        return $this->getEcommerceTranslation($countryID) ? true : false;
167
    }
168
}
169