1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class CountryPrice_Translation extends DataObject |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private static $automatically_create_dummy_translations_for_products_and_productgroups = true; |
|
|
|
|
9
|
|
|
|
10
|
|
|
private static $dependencies = array( |
|
|
|
|
11
|
|
|
'CountryURLProvider' => '%$CountryURLProvider', |
12
|
|
|
); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* automatically populated by the dependency manager. |
16
|
|
|
* |
17
|
|
|
* @var CountryURLProvider |
18
|
|
|
*/ |
19
|
|
|
public $CountryURLProvider = null; |
20
|
|
|
|
21
|
|
|
private static $db = array( |
|
|
|
|
22
|
|
|
'Title' => 'Varchar(200)', |
23
|
|
|
'UseOriginalTitle' => 'Boolean', |
24
|
|
|
'Content' => 'HTMLText', |
25
|
|
|
'UseOriginalContent' => 'Boolean', |
26
|
|
|
'WithoutTranslation' => 'Boolean' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $field_labels = array( |
|
|
|
|
30
|
|
|
'Title' => 'Page Title', |
31
|
|
|
'Content' => 'Page Content', |
32
|
|
|
'EcommerceCountryID' => 'Country', |
33
|
|
|
'WithoutTranslation' => 'Price Difference Only' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
private static $has_one = array( |
|
|
|
|
37
|
|
|
'EcommerceCountry' => 'EcommerceCountry', |
38
|
|
|
'Parent' => 'SiteTree' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
private static $summary_fields = array( |
|
|
|
|
42
|
|
|
'EcommerceCountry.Name' => 'Country', |
43
|
|
|
'Title' => 'Title', |
44
|
|
|
'WithoutTranslation.Nice' => 'Price Difference Only' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
private static $casting = array( |
|
|
|
|
48
|
|
|
'Link' => 'Varchar' |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $locale_get_parameter = 'ecomlocale'; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Standard SS variable. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private static $singular_name = 'Translation'; |
|
|
|
|
62
|
|
|
public function i18n_singular_name() |
63
|
|
|
{ |
64
|
|
|
return self::$singular_name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Standard SS variable. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private static $plural_name = 'Translations'; |
|
|
|
|
73
|
|
|
public function i18n_plural_name() |
74
|
|
|
{ |
75
|
|
|
return self::$plural_name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function get_country_url_provider() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$obj = Injector::inst()->get('CountryPrice_Translation'); |
81
|
|
|
return $obj->CountryURLProvider; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* CMS Fields |
86
|
|
|
* @return FieldList |
87
|
|
|
*/ |
88
|
|
|
public function getCMSFields() |
89
|
|
|
{ |
90
|
|
|
$fields = parent::getCMSFields(); |
91
|
|
|
$withoutTranslationField = $fields->dataFieldByName('WithoutTranslation'); |
92
|
|
|
$withoutTranslationField = CheckboxField::create( |
93
|
|
|
'WithoutTranslation', |
94
|
|
|
$withoutTranslationField->Title() |
95
|
|
|
) |
96
|
|
|
->setDescription('The page is <em>translated</em> for search engines because it has prices for this country. '); |
97
|
|
|
|
98
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray(); |
99
|
|
|
$countryDropdownField = DropdownField::create( |
100
|
|
|
'EcommerceCountryID', |
101
|
|
|
$fields->dataFieldByName('EcommerceCountryID')->Title(), |
102
|
|
|
array('' => '-- make sure to select a country --')+$countries |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$fields->removeFieldFromTab("Root.Main", 'ParentID'); |
106
|
|
|
if($this->WithoutTranslation) { |
|
|
|
|
107
|
|
|
return FieldList::create( |
108
|
|
|
array( |
109
|
|
|
$countryDropdownField, |
110
|
|
|
$withoutTranslationField |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} else { |
114
|
|
|
$fields->addFieldToTab( |
115
|
|
|
'Root.Main', |
116
|
|
|
$countryDropdownField, |
117
|
|
|
'Title' |
118
|
|
|
); |
119
|
|
|
$fields->addFieldToTab( |
120
|
|
|
'Root.Main', |
121
|
|
|
$withoutTranslationField, |
122
|
|
|
'Title' |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
$dbFields = $this->inheritedDatabaseFields(); |
126
|
|
|
foreach($dbFields as $dbField => $fieldType) { |
127
|
|
|
$useField = 'UseOriginal'.$dbField; |
128
|
|
|
if(!empty($this->$useField)) { |
129
|
|
|
$fields->replaceField( |
130
|
|
|
$dbField, |
131
|
|
|
$fields->dataFieldByName($dbField)->performReadonlyTransformation() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
if($fields->dataFieldByName($useField)){ |
135
|
|
|
$fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use original value for ') . $dbField); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return $fields; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function canCreate($member = null) |
142
|
|
|
{ |
143
|
|
|
if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) { |
144
|
|
|
return parent::canCreate($member); |
145
|
|
|
} |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
protected function validate() |
153
|
|
|
{ |
154
|
|
|
$validation = parent::validate(); |
155
|
|
|
if ($validation->valid()) { |
156
|
|
|
if ($this->exists()) { |
157
|
|
|
$existing = CountryPrice_Translation::get() |
158
|
|
|
->exclude(array("ID" => $this->ID)) |
159
|
|
|
->filter( |
160
|
|
|
array( |
161
|
|
|
"EcommerceCountryID" => $this->EcommerceCountryID, |
|
|
|
|
162
|
|
|
"ParentID" => $this->ParentID |
|
|
|
|
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
if ($existing->count() > 0) { |
166
|
|
|
$validation->error( |
167
|
|
|
'There is already an entry for this country and page' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
return $validation; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* 'PageField' => 'Title' |
177
|
|
|
* 'TranslationField' => 'Title' |
178
|
|
|
* |
179
|
|
|
* @return ArrayList |
180
|
|
|
*/ |
181
|
|
|
public function FieldsToReplace() |
182
|
|
|
{ |
183
|
|
|
$al = ArrayList::create(); |
184
|
|
|
$al->push( |
185
|
|
|
ArrayData::create( |
186
|
|
|
array( |
187
|
|
|
'PageField' => 'Title', |
188
|
|
|
'TranslationField' => 'Title' |
189
|
|
|
) |
190
|
|
|
) |
191
|
|
|
); |
192
|
|
|
$al->push( |
193
|
|
|
ArrayData::create( |
194
|
|
|
array( |
195
|
|
|
'PageField' => 'Content', |
196
|
|
|
'TranslationField' => 'Content' |
197
|
|
|
) |
198
|
|
|
) |
199
|
|
|
); |
200
|
|
|
$this->extend('updateFieldsToReplace', $al); |
201
|
|
|
foreach($al as $fieldToReplace) { |
202
|
|
|
$ignoreField = 'UseOriginal' . $fieldToReplace->PageField; |
203
|
|
|
if(!empty($this->owner->$ignoreField)) { |
|
|
|
|
204
|
|
|
$al->remove($fieldToReplace); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
return $al; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return string |
|
|
|
|
212
|
|
|
*/ |
213
|
|
|
public function Link() |
214
|
|
|
{ |
215
|
|
|
return $this->getLink(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
|
|
|
|
220
|
|
|
*/ |
221
|
|
|
public function getLink() |
222
|
|
|
{ |
223
|
|
|
$link = $this->Parent()->Link(); |
|
|
|
|
224
|
|
|
if ($this->EcommerceCountryID) { |
|
|
|
|
225
|
|
|
$hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($link); |
226
|
|
|
if($hasCountrySegment){ |
227
|
|
|
$link = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($this->EcommerceCountry()->Code, $link); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
else { |
230
|
|
|
$link = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($this->EcommerceCountry()->Code, $link); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
return Director::absoluteURL($link); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function requireDefaultRecords() |
237
|
|
|
{ |
238
|
|
|
parent::requireDefaultRecords(); |
239
|
|
|
if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) { |
240
|
|
|
$prices = CountryPrice::get(); |
241
|
|
|
$ecommerceCountries = array(); |
242
|
|
|
foreach($prices as $price) { |
243
|
|
|
if($countryObject = $price->CountryObject()) { |
244
|
|
|
if($buyable = $price->Buyable()) { |
245
|
|
|
if($buyable instanceof Product) { |
246
|
|
|
if($buyable->ID && $countryObject->ID) { |
247
|
|
|
$filter = array( |
248
|
|
|
'EcommerceCountryID' => $countryObject->ID, |
249
|
|
|
'ParentID' => $buyable->ID |
250
|
|
|
); |
251
|
|
|
$ecommerceCountries[$countryObject->ID] = $countryObject; |
252
|
|
View Code Duplication |
if(! CountryPrice_Translation::get()->filter($filter)->first()) { |
|
|
|
|
253
|
|
|
DB::alteration_message( |
254
|
|
|
'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code, |
255
|
|
|
'created' |
256
|
|
|
); |
257
|
|
|
$obj = CountryPrice_Translation::create($filter); |
258
|
|
|
$obj->WithoutTranslation = true; |
|
|
|
|
259
|
|
|
$obj->write(); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
if(count($ecommerceCountries)) { |
267
|
|
|
foreach(ProductGroup::get() as $productGroup) { |
268
|
|
|
foreach($ecommerceCountries as $countryID => $countryObject) { |
269
|
|
|
$filter = array( |
270
|
|
|
'EcommerceCountryID' => $countryObject->ID, |
271
|
|
|
'ParentID' => $productGroup->ID |
272
|
|
|
); |
273
|
|
View Code Duplication |
if(! CountryPrice_Translation::get()->filter($filter)->first()) { |
|
|
|
|
274
|
|
|
DB::alteration_message( |
275
|
|
|
'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code, |
276
|
|
|
'created' |
277
|
|
|
); |
278
|
|
|
$obj = CountryPrice_Translation::create($filter); |
279
|
|
|
$obj->WithoutTranslation = true; |
|
|
|
|
280
|
|
|
$obj->write(); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
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.