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