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->getCountryPriceTranslations(), |
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->getCountryPriceTranslations() |
41
|
|
|
->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @return CountryPrice_Translation | null |
47
|
|
|
*/ |
48
|
|
|
public function CanonicalLink() |
49
|
|
|
{ |
50
|
|
|
return $this->owner->getCountryPriceTranslations() |
51
|
|
|
->innerJoin('EcommerceCountry', '"EcommerceCountry"."ID" = "CountryPrice_Translation"."EcommerceCountryID"') |
52
|
|
|
->filter(array('EcommerceCountry.IsBackupCountry' => 1)) |
53
|
|
|
->first(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function loadTranslatedValues($countryID = 0, $variableOrMethod = '') |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$translation = null; |
59
|
|
|
if (! $countryID) { |
60
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
61
|
|
|
if ($countryObject) { |
62
|
|
|
$countryID = $countryObject->ID; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if ($countryID) { |
66
|
|
|
$key = $this->owner->ID.'_'.$countryID; |
67
|
|
|
if (isset(self::$_translations[$key])) { |
68
|
|
|
$translation = self::$_translations[$key]; |
69
|
|
|
} else { |
70
|
|
|
$translation = $this->owner->getRealEcommerceTranslation($countryID); |
71
|
|
|
self::$_translations[$key] = $translation; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
if ($translation) { |
75
|
|
|
$fieldsToReplace = $translation->FieldsToReplace(); |
76
|
|
|
foreach ($fieldsToReplace as $replaceFields) { |
77
|
|
|
$pageField = $replaceFields->PageField; |
78
|
|
|
$pageFieldTranslated = $pageField . 'Translated'; |
79
|
|
|
$translationField = $replaceFields->TranslationField; |
80
|
|
|
if (! $variableOrMethod || $variableOrMethod === $pageField) { |
81
|
|
|
if ($translation->hasMethod($translationField)) { |
82
|
|
|
$this->owner->$pageField = $translation->$translationField(); |
83
|
|
|
$this->owner->$pageFieldTranslated = $translation->$translationField(); |
84
|
|
|
} else { |
85
|
|
|
$this->owner->$pageField = $translation->$translationField; |
86
|
|
|
$this->owner->$pageFieldTranslated = $translation->$translationField; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if ($variableOrMethod) { |
90
|
|
|
return $this->owner->$pageField; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
if ($variableOrMethod) { |
95
|
|
|
if ($translation->hasMethod($variableOrMethod)) { |
96
|
|
|
return $this->owner->$variableOrMethod(); |
97
|
|
|
} else { |
98
|
|
|
return $this->owner->$variableOrMethod; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var int $countryID |
106
|
|
|
* |
107
|
|
|
* @return CountryPrice_Translation | null |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
public function getRealEcommerceTranslation($countryID) |
110
|
|
|
{ |
111
|
|
|
return CountryPrice_Translation::get() |
112
|
|
|
->filter( |
113
|
|
|
array( |
114
|
|
|
"EcommerceCountryID" => $countryID, |
115
|
|
|
'ParentID' => $this->owner->ID |
116
|
|
|
) |
117
|
|
|
) |
118
|
|
|
->exclude( |
119
|
|
|
array('WithoutTranslation' => 1) |
120
|
|
|
) |
121
|
|
|
->first(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var int $countryID |
126
|
|
|
* |
127
|
|
|
* @return CountryPrice_Translation | null |
128
|
|
|
*/ |
129
|
|
|
public function getEcommerceTranslation($countryID) |
130
|
|
|
{ |
131
|
|
|
return $this->owner |
132
|
|
|
->getCountryPriceTranslations() |
133
|
|
|
->filter( |
134
|
|
|
array( |
135
|
|
|
"EcommerceCountryID" => $countryID, |
136
|
|
|
'ParentID' => $this->owner->ID, |
137
|
|
|
) |
138
|
|
|
) |
139
|
|
|
->first(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @var int $countryID |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function hasCountryLocalInURL($countryID) |
148
|
|
|
{ |
149
|
|
|
return $this->getEcommerceTranslation($countryID) ? true : false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* we have added this because we got some weird mixups with magical methods |
154
|
|
|
* @return DataList (of CountryPrice_Translation objects) |
155
|
|
|
*/ |
156
|
|
|
function CountryPriceTranslations() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
return $this->getCountryPriceTranslations(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* we have added this because we got some weird mixups with magical methods |
163
|
|
|
* @return DataList (of CountryPrice_Translation objects) |
164
|
|
|
*/ |
165
|
|
|
function getCountryPriceTranslations() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
return CountryPrice_Translation::get()->filter( |
168
|
|
|
array('CountryPrice_Translation.ParentID' => $this->owner->ID) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
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.