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
|
|
|
* cache for all translations ... |
113
|
|
|
* @var [type] |
114
|
|
|
*/ |
115
|
|
|
private $_translations_all_cache = []; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var int $countryID |
119
|
|
|
* |
120
|
|
|
* @return CountryPrice_Translation | null |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function getRealEcommerceTranslation($countryID) |
123
|
|
|
{ |
124
|
|
|
return CountryPrice_Translation::get() |
125
|
|
|
->filter( |
126
|
|
|
array( |
127
|
|
|
"EcommerceCountryID" => $countryID, |
128
|
|
|
'ParentID' => $this->owner->ID |
129
|
|
|
) |
130
|
|
|
) |
131
|
|
|
->exclude( |
132
|
|
|
array('WithoutTranslation' => 1) |
133
|
|
|
) |
134
|
|
|
->first(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* cache for all translations ... |
139
|
|
|
* @var [type] |
140
|
|
|
*/ |
141
|
|
|
private $_translations_all_cache = []; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var int $countryID |
145
|
|
|
* |
146
|
|
|
* @return CountryPrice_Translation | null |
147
|
|
|
*/ |
148
|
|
|
public function getEcommerceTranslation($countryID) |
149
|
|
|
{ |
150
|
|
|
if(!isset($this->_translations_all_cache[$countryID])) { |
151
|
|
|
$this->_translations_all_cache[$countryID] = $this->owner |
152
|
|
|
->CountryPriceTranslations() |
153
|
|
|
->filter( |
154
|
|
|
array( |
155
|
|
|
"EcommerceCountryID" => $countryID, |
156
|
|
|
'ParentID' => $this->owner->ID, |
157
|
|
|
) |
158
|
|
|
) |
159
|
|
|
->first(); |
160
|
|
|
} |
161
|
|
|
return $this->_translations_all_cache[$countryID]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @var int $countryID |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function thisPageHasTranslation($countryID) |
170
|
|
|
{ |
171
|
|
|
return $this->getEcommerceTranslation($countryID) ? true : false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.