|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class CountryPrice_SiteTreeExtions 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
|
|
|
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->getRealEcommerceTranslation($countryID); |
|
57
|
|
|
self::$_translations[$key] = $translation; |
|
58
|
|
|
} else { |
|
59
|
|
|
$translation = self::$_translations[$key]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
if ($translation) { |
|
63
|
|
|
$fieldsToReplace = $translation->FieldsToReplace(); |
|
64
|
|
|
foreach ($fieldsToReplace as $replaceFields) { |
|
65
|
|
|
$pageField = $replaceFields->PageField; |
|
66
|
|
|
$translationField = $replaceFields->TranslationField; |
|
67
|
|
|
if (! $variableOrMethod || $variableOrMethod === $pageField) { |
|
68
|
|
|
if ($translation->hasMethod($translationField)) { |
|
69
|
|
|
$pageFieldTranslated = $pageField.'Translated'; |
|
70
|
|
|
$this->owner->$pageField = $translation->$translationField(); |
|
71
|
|
|
$this->owner->$pageFieldTranslated = $translation->$translationField(); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->owner->$pageField = $translation->$translationField; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
if ($variableOrMethod) { |
|
77
|
|
|
return $this->owner->$pageField; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
if ($variableOrMethod) { |
|
82
|
|
|
if ($translation->hasMethod($variableOrMethod)) { |
|
83
|
|
|
return $this->owner->$variableOrMethod(); |
|
84
|
|
|
} else { |
|
85
|
|
|
return $this->owner->$variableOrMethod; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var int $countryID |
|
93
|
|
|
* |
|
94
|
|
|
* @return CountryPrice_Translation | null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getRealEcommerceTranslation($countryID) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->owner |
|
99
|
|
|
->CountryPriceTranslations() |
|
100
|
|
|
->filter( |
|
101
|
|
|
array( |
|
102
|
|
|
"EcommerceCountryID" => $countryID, |
|
103
|
|
|
'ParentID' => $this->owner->ID, |
|
104
|
|
|
) |
|
105
|
|
|
) |
|
106
|
|
|
->exclude( |
|
107
|
|
|
array('WithoutTranslation' => 1) |
|
108
|
|
|
) |
|
109
|
|
|
->first(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var int $countryID |
|
114
|
|
|
* |
|
115
|
|
|
* @return CountryPrice_Translation | null |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getEcommerceTranslation($countryID) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->owner |
|
120
|
|
|
->CountryPriceTranslations() |
|
121
|
|
|
->filter( |
|
122
|
|
|
array( |
|
123
|
|
|
"EcommerceCountryID" => $countryID, |
|
124
|
|
|
'ParentID' => $this->owner->ID, |
|
125
|
|
|
) |
|
126
|
|
|
) |
|
127
|
|
|
->first(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
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.