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( |
|
|
|
|
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 = '') |
|
|
|
|
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
|
|
|
/** |
113
|
|
|
* @var int $countryID |
114
|
|
|
* |
115
|
|
|
* @return CountryPrice_Translation | null |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
public function getRealEcommerceTranslation($countryID) |
118
|
|
|
{ |
119
|
|
|
return CountryPrice_Translation::get() |
120
|
|
|
->filter( |
121
|
|
|
array( |
122
|
|
|
"EcommerceCountryID" => $countryID, |
123
|
|
|
'ParentID' => $this->owner->ID |
|
|
|
|
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, |
|
|
|
|
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
|
|
|
|
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: