Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Translatable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Translatable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 154 | class Translatable extends DataExtension implements TestOnly { |
||
| 155 | |||
| 156 | const QUERY_LOCALE_FILTER_ENABLED = 'Translatable.LocaleFilterEnabled'; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The 'default' language. |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | protected static $default_locale = 'en_US'; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The language in which we are reading dataobjects. |
||
| 166 | * |
||
| 167 | * @var string |
||
| 168 | */ |
||
| 169 | protected static $current_locale = null; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * A cached list of existing tables |
||
| 173 | * |
||
| 174 | * @var mixed |
||
| 175 | */ |
||
| 176 | protected static $tableList = null; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * An array of fields that can be translated. |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | protected $translatableFields = null; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * A map of the field values of the original (untranslated) DataObject record |
||
| 186 | * @var array |
||
| 187 | */ |
||
| 188 | protected $original_values = null; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * If this is set to TRUE then {@link augmentSQL()} will automatically add a filter |
||
| 192 | * clause to limit queries to the current {@link get_current_locale()}. This camn be |
||
| 193 | * disabled using {@link disable_locale_filter()} |
||
| 194 | * |
||
| 195 | * @var bool |
||
| 196 | */ |
||
| 197 | protected static $locale_filter_enabled = true; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var array All locales in which a translation can be created. |
||
| 201 | * This limits the choice in the CMS language dropdown in the |
||
| 202 | * "Translation" tab, as well as the language dropdown above |
||
| 203 | * the CMS tree. If not set, it will default to showing all |
||
| 204 | * common locales. |
||
| 205 | */ |
||
| 206 | protected static $allowed_locales = null; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var boolean Check other languages for URLSegment values (only applies to {@link SiteTree}). |
||
| 210 | * Turn this off to handle language setting yourself, e.g. through language-specific subdomains |
||
| 211 | * or URL path prefixes like "/en/mypage". |
||
| 212 | */ |
||
| 213 | private static $enforce_global_unique_urls = true; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Exclude these fields from translation |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | * @config |
||
| 220 | */ |
||
| 221 | private static $translate_excluded_fields = array( |
||
| 222 | 'ViewerGroups', |
||
| 223 | 'EditorGroups', |
||
| 224 | 'CanViewType', |
||
| 225 | 'CanEditType', |
||
| 226 | 'NewTransLang', |
||
| 227 | 'createtranslation' |
||
| 228 | ); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Reset static configuration variables to their default values |
||
| 232 | */ |
||
| 233 | static function reset() { |
||
| 234 | self::enable_locale_filter(); |
||
| 235 | self::$default_locale = 'en_US'; |
||
| 236 | self::$current_locale = null; |
||
| 237 | self::$allowed_locales = null; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Choose the language the site is currently on. |
||
| 242 | * |
||
| 243 | * If $_GET['locale'] is currently set, then that locale will be used. |
||
| 244 | * Otherwise the member preference (if logged |
||
| 245 | * in) or default locale will be used. |
||
| 246 | * |
||
| 247 | * @todo Re-implement cookie and member option |
||
| 248 | * |
||
| 249 | * @param $langsAvailable array A numerical array of languages which are valid choices (optional) |
||
| 250 | * @return string Selected language (also saved in $current_locale). |
||
| 251 | */ |
||
| 252 | static function choose_site_locale($langsAvailable = array()) { |
||
| 253 | self::set_current_locale(self::default_locale()); |
||
| 254 | return self::$current_locale; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the current reading language. |
||
| 259 | * This value has to be set before the schema is built with translatable enabled, |
||
| 260 | * any changes after this can cause unintended side-effects. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | static function default_locale() { |
||
| 265 | return self::$default_locale; |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the current reading language. |
||
| 271 | * If its not chosen, call {@link choose_site_locale()}. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | static function get_current_locale() { |
||
| 276 | return (self::$current_locale) ? self::$current_locale : self::choose_site_locale(); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set the reading language, either namespaced to 'site' (website content) |
||
| 281 | * or 'cms' (management backend). This value is used in {@link augmentSQL()} |
||
| 282 | * to "auto-filter" all SELECT queries by this language. |
||
| 283 | * See {@link disable_locale_filter()} on how to override this behaviour temporarily. |
||
| 284 | * |
||
| 285 | * @param string $lang New reading language. |
||
| 286 | */ |
||
| 287 | static function set_current_locale($locale) { |
||
| 288 | self::$current_locale = $locale; |
||
| 289 | } |
||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public static function locale_filter_enabled() { |
||
| 297 | return self::$locale_filter_enabled; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Enables automatic filtering by locale. This is normally called after is has been |
||
| 302 | * disabled using {@link disable_locale_filter()}. |
||
| 303 | * |
||
| 304 | * @param $enabled (default true), if false this call is a no-op - see {@link disable_locale_filter()} |
||
| 305 | */ |
||
| 306 | public static function enable_locale_filter($enabled = true) { |
||
| 307 | if ($enabled) { |
||
| 308 | self::$locale_filter_enabled = true; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Disables automatic locale filtering in {@link augmentSQL()}. This can be re-enabled |
||
| 314 | * using {@link enable_locale_filter()}. |
||
| 315 | * |
||
| 316 | * Note that all places that disable the locale filter should generally re-enable it |
||
| 317 | * before returning from that block of code (function, etc). This is made easier by |
||
| 318 | * using the following pattern: |
||
| 319 | * |
||
| 320 | * <code> |
||
| 321 | * $enabled = Translatable::disable_locale_filter(); |
||
| 322 | * // do some work here |
||
| 323 | * Translatable::enable_locale_filter($enabled); |
||
| 324 | * return $whateverYouNeedTO; |
||
| 325 | * </code> |
||
| 326 | * |
||
| 327 | * By using this pattern, the call to enable the filter will not re-enable it if it |
||
| 328 | * was not enabled initially. That will keep code that called your function from |
||
| 329 | * breaking if it had already disabled the locale filter since it will not expect |
||
| 330 | * calling your function to change the global state by re-enabling the filter. |
||
| 331 | * |
||
| 332 | * @return boolean true if the locale filter was enabled, false if it was not |
||
| 333 | */ |
||
| 334 | public static function disable_locale_filter() { |
||
| 335 | $enabled = self::$locale_filter_enabled; |
||
| 336 | self::$locale_filter_enabled = false; |
||
| 337 | return $enabled; |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | function setOwner($owner, $ownerBaseClass = null) { |
||
| 344 | parent::setOwner($owner, $ownerBaseClass); |
||
| 345 | |||
| 346 | // setting translatable fields by inspecting owner - this should really be done in the constructor |
||
| 347 | if($this->owner && $this->translatableFields === null) { |
||
| 348 | $this->translatableFields = array_merge( |
||
| 349 | array_keys($this->owner->db()), |
||
| 350 | array_keys($this->owner->has_many()), |
||
| 351 | array_keys($this->owner->many_many()) |
||
| 352 | ); |
||
| 353 | foreach (array_keys($this->owner->has_one()) as $fieldname) { |
||
| 354 | $this->translatableFields[] = $fieldname.'ID'; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | // FIXME - REMOVING THIS BREAKS TEST, BUT ZERO TEST COVERAGE... |
||
| 360 | static function get_extra_config($class, $extensionClass, $args = null) { |
||
| 361 | $config = array(); |
||
| 362 | $config['defaults'] = array( |
||
| 363 | "Locale" => Translatable::default_locale() // as an overloaded getter as well: getLang() |
||
| 364 | ); |
||
| 365 | $config['db'] = array( |
||
| 366 | "Locale" => "DBLocale", |
||
| 367 | //"TranslationMasterID" => "Int" // optional relation to a "translation master" |
||
| 368 | ); |
||
| 369 | return $config; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Check if a given SQLQuery filters on the Locale field |
||
| 374 | * |
||
| 375 | * @param SQLQuery $query |
||
| 376 | * @return boolean |
||
| 377 | */ |
||
| 378 | protected function filtersOnLocale($query) { |
||
| 379 | foreach($query->getWhere() as $condition) { |
||
| 380 | if(preg_match('/("|\'|`)Locale("|\'|`)/', $condition)) return true; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Changes any SELECT query thats not filtering on an ID |
||
| 386 | * to limit by the current language defined in {@link get_current_locale()}. |
||
| 387 | * It falls back to "Locale='' OR Lang IS NULL" and assumes that |
||
| 388 | * this implies querying for the default language. |
||
| 389 | * |
||
| 390 | * Use {@link disable_locale_filter()} to temporarily disable this "auto-filtering". |
||
| 391 | */ |
||
| 392 | public function augmentSQL(SQLQuery &$query, DataQuery $dataQuery = null) { |
||
| 393 | // If the record is saved (and not a singleton), and has a locale, |
||
| 394 | // limit the current call to its locale. This fixes a lot of problems |
||
| 395 | // with other extensions like Versioned |
||
| 396 | if($this->owner->ID && !empty($this->owner->Locale)) { |
||
| 397 | $locale = $this->owner->Locale; |
||
| 398 | } else { |
||
| 399 | $locale = Translatable::get_current_locale(); |
||
| 400 | } |
||
| 401 | |||
| 402 | $baseTable = ClassInfo::baseDataClass($this->owner->class); |
||
| 403 | if( |
||
| 404 | $locale |
||
| 405 | // unless the filter has been temporarily disabled |
||
| 406 | && self::locale_filter_enabled() |
||
| 407 | // or it was disabled when the DataQuery was created |
||
| 408 | && $dataQuery->getQueryParam(self::QUERY_LOCALE_FILTER_ENABLED) |
||
| 409 | // DataObject::get_by_id() should work independently of language |
||
| 410 | && !$query->filtersOnID() |
||
| 411 | // the query contains this table |
||
| 412 | // @todo Isn't this always the case?! |
||
| 413 | && array_search($baseTable, array_keys($query->getFrom())) !== false |
||
| 414 | // or we're already filtering by Lang (either from an earlier augmentSQL() |
||
| 415 | // call or through custom SQL filters) |
||
| 416 | && !$this->filtersOnLocale($query) |
||
| 417 | //&& !$query->filtersOnFK() |
||
| 418 | ) { |
||
| 419 | $qry = sprintf('"%s"."Locale" = \'%s\'', $baseTable, Convert::raw2sql($locale)); |
||
| 420 | $query->addWhere($qry); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | function augmentDataQueryCreation(SQLQuery &$sqlQuery, DataQuery &$dataQuery) { |
||
| 425 | $enabled = self::locale_filter_enabled(); |
||
| 426 | $dataQuery->setQueryParam(self::QUERY_LOCALE_FILTER_ENABLED, $enabled); |
||
| 427 | } |
||
| 428 | |||
| 429 | // FIXME - NO TEST COVERAGE BUT REQUIRED |
||
| 430 | function augmentDatabase() { |
||
| 431 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 432 | if($this->owner->class != $baseDataClass) return; |
||
| 433 | |||
| 434 | $fields = array( |
||
| 435 | 'OriginalID' => 'Int', |
||
| 436 | 'TranslationGroupID' => 'Int', |
||
| 437 | ); |
||
| 438 | $indexes = array( |
||
| 439 | 'OriginalID' => true, |
||
| 440 | 'TranslationGroupID' => true |
||
| 441 | ); |
||
| 442 | |||
| 443 | // Add new tables if required |
||
| 444 | DB::requireTable("{$baseDataClass}_translationgroups", $fields, $indexes); |
||
| 445 | |||
| 446 | // Remove 2.2 style tables |
||
| 447 | DB::dontRequireTable("{$baseDataClass}_lang"); |
||
| 448 | if($this->owner->hasExtension('Versioned')) { |
||
| 449 | DB::dontRequireTable("{$baseDataClass}_lang_Live"); |
||
| 450 | DB::dontRequireTable("{$baseDataClass}_lang_versions"); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @todo Find more appropriate place to hook into database building |
||
| 456 | */ |
||
| 457 | public function requireDefaultRecords() { |
||
| 458 | // @todo This relies on the Locale attribute being on the base data class, and not any subclasses |
||
| 459 | if($this->owner->class != ClassInfo::baseDataClass($this->owner->class)) return false; |
||
| 460 | |||
| 461 | // Permissions: If a group doesn't have any specific TRANSLATE_<locale> edit rights, |
||
| 462 | // but has CMS_ACCESS_CMSMain (general CMS access), then assign TRANSLATE_ALL permissions as a default. |
||
| 463 | // Auto-setting permissions based on these intransparent criteria is a bit hacky, |
||
| 464 | // but unavoidable until we can determine when a certain permission code was made available first |
||
| 465 | // (see http://open.silverstripe.org/ticket/4940) |
||
| 466 | $groups = Permission::get_groups_by_permission(array( |
||
| 467 | 'CMS_ACCESS_CMSMain', |
||
| 468 | 'CMS_ACCESS_LeftAndMain', |
||
| 469 | 'ADMIN' |
||
| 470 | )); |
||
| 471 | if($groups) foreach($groups as $group) { |
||
| 472 | $codes = $group->Permissions()->column('Code'); |
||
| 473 | $hasTranslationCode = false; |
||
| 474 | foreach($codes as $code) { |
||
| 475 | if(preg_match('/^TRANSLATE_/', $code)) $hasTranslationCode = true; |
||
| 476 | } |
||
| 477 | // Only add the code if no more restrictive code exists |
||
| 478 | if(!$hasTranslationCode) Permission::grant($group->ID, 'TRANSLATE_ALL'); |
||
| 479 | } |
||
| 480 | |||
| 481 | // If the Translatable extension was added after the first records were already |
||
| 482 | // created in the database, make sure to update the Locale property if |
||
| 483 | // if wasn't set before |
||
| 484 | $idsWithoutLocale = DB::query(sprintf( |
||
| 485 | 'SELECT "ID" FROM "%s" WHERE "Locale" IS NULL OR "Locale" = \'\'', |
||
| 486 | ClassInfo::baseDataClass($this->owner->class) |
||
| 487 | ))->column(); |
||
| 488 | if(!$idsWithoutLocale) return; |
||
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Add a record to a "translation group", |
||
| 496 | * so its relationship to other translations |
||
| 497 | * based off the same object can be determined later on. |
||
| 498 | * See class header for further comments. |
||
| 499 | * |
||
| 500 | * @param int $originalID Either the primary key of the record this new translation is based on, |
||
| 501 | * or the primary key of this record, to create a new translation group |
||
| 502 | * @param boolean $overwrite |
||
| 503 | */ |
||
| 504 | public function addTranslationGroup($originalID, $overwrite = false) { |
||
| 505 | if(!$this->owner->exists()) return false; |
||
| 506 | |||
| 507 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 508 | $existingGroupID = $this->getTranslationGroup($originalID); |
||
| 509 | |||
| 510 | // Remove any existing groups if overwrite flag is set |
||
| 511 | if($existingGroupID && $overwrite) { |
||
| 512 | $sql = sprintf( |
||
| 513 | 'DELETE FROM "%s_translationgroups" WHERE "TranslationGroupID" = %d AND "OriginalID" = %d', |
||
| 514 | $baseDataClass, |
||
| 515 | $existingGroupID, |
||
| 516 | $this->owner->ID |
||
| 517 | ); |
||
| 518 | DB::query($sql); |
||
| 519 | $existingGroupID = null; |
||
| 520 | } |
||
| 521 | |||
| 522 | // Add to group (only if not in existing group or $overwrite flag is set) |
||
| 523 | if(!$existingGroupID) { |
||
| 524 | $sql = sprintf( |
||
| 525 | 'INSERT INTO "%s_translationgroups" ("TranslationGroupID","OriginalID") VALUES (%d,%d)', |
||
| 526 | $baseDataClass, |
||
| 527 | $originalID, |
||
| 528 | $this->owner->ID |
||
| 529 | ); |
||
| 530 | DB::query($sql); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Gets the translation group for the current record. |
||
| 536 | * This ID might equal the record ID, but doesn't have to - |
||
| 537 | * it just points to one "original" record in the list. |
||
| 538 | * |
||
| 539 | * @return int Numeric ID of the translationgroup in the <classname>_translationgroup table |
||
| 540 | */ |
||
| 541 | public function getTranslationGroup() { |
||
| 542 | if(!$this->owner->exists()) return false; |
||
| 543 | |||
| 544 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 545 | return DB::query( |
||
| 546 | sprintf( |
||
| 547 | 'SELECT "TranslationGroupID" FROM "%s_translationgroups" WHERE "OriginalID" = %d', |
||
| 548 | $baseDataClass, |
||
| 549 | $this->owner->ID |
||
| 550 | ) |
||
| 551 | )->value(); |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Removes a record from the translation group lookup table. |
||
| 556 | * Makes no assumptions on other records in the group - meaning |
||
| 557 | * if this happens to be the last record assigned to the group, |
||
| 558 | * this group ceases to exist. |
||
| 559 | */ |
||
| 560 | public function removeTranslationGroup() { |
||
| 561 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 562 | DB::query( |
||
| 563 | sprintf('DELETE FROM "%s_translationgroups" WHERE "OriginalID" = %d', $baseDataClass, $this->owner->ID) |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | |||
| 567 | // FIXME - no coverage but is called |
||
| 568 | function isVersionedTable($table) { |
||
| 569 | return false; |
||
| 570 | } |
||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Recursively creates translations for parent pages in this language |
||
| 575 | * if they aren't existing already. This is a necessity to make |
||
| 576 | * nested pages accessible in a translated CMS page tree. |
||
| 577 | * It would be more userfriendly to grey out untranslated pages, |
||
| 578 | * but this involves complicated special cases in AllChildrenIncludingDeleted(). |
||
| 579 | * |
||
| 580 | * {@link SiteTree->onBeforeWrite()} will ensure that each translation will get |
||
| 581 | * a unique URL across languages, by means of {@link SiteTree::get_by_link()} |
||
| 582 | * and {@link Translatable->alternateGetByURL()}. |
||
| 583 | */ |
||
| 584 | function onBeforeWrite() { |
||
| 585 | // If language is not set explicitly, set it to current_locale. |
||
| 586 | // This might be a bit overzealous in assuming the language |
||
| 587 | // of the content, as a "single language" website might be expanded |
||
| 588 | // later on. See {@link requireDefaultRecords()} for batch setting |
||
| 589 | // of empty Locale columns on each dev/build call. |
||
| 590 | if(!$this->owner->Locale) { |
||
| 591 | $this->owner->Locale = Translatable::get_current_locale(); |
||
| 592 | } |
||
| 593 | |||
| 594 | // Specific logic for SiteTree subclasses. |
||
| 595 | // If page has untranslated parents, create (unpublished) translations |
||
| 596 | // of those as well to avoid having inaccessible children in the sitetree. |
||
| 597 | // Caution: This logic is very sensitve to infinite loops when translation status isn't determined properly |
||
| 598 | // If a parent for the newly written translation was existing before this |
||
| 599 | // onBeforeWrite() call, it will already have been linked correctly through createTranslation() |
||
| 600 | if( |
||
| 601 | class_exists('SiteTree') |
||
| 602 | && $this->owner->hasField('ParentID') |
||
| 603 | && $this->owner instanceof SiteTree |
||
| 604 | ) { |
||
| 605 | if( |
||
| 606 | !$this->owner->ID |
||
| 607 | && $this->owner->ParentID |
||
| 608 | && !$this->owner->Parent()->hasTranslation($this->owner->Locale) |
||
| 609 | ) { |
||
| 610 | $parentTranslation = $this->owner->Parent()->createTranslation($this->owner->Locale); |
||
| 611 | $this->owner->ParentID = $parentTranslation->ID; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | // Has to be limited to the default locale, the assumption is that the "page type" |
||
| 616 | // dropdown is readonly on all translations. |
||
| 617 | if($this->owner->ID && $this->owner->Locale == Translatable::default_locale()) { |
||
| 618 | $changedFields = $this->owner->getChangedFields(); |
||
| 619 | $changed = isset($changedFields['ClassName']); |
||
| 620 | |||
| 621 | if ($changed && $this->owner->hasExtension('Versioned')) { |
||
| 622 | // this is required because when publishing a node the before/after |
||
| 623 | // values of $changedFields['ClassName'] will be the same because |
||
| 624 | // the record was already written to the stage/draft table and thus |
||
| 625 | // the record was updated, and then publish('Stage', 'Live') is |
||
| 626 | // called, which uses forceChange, which will make all the fields |
||
| 627 | // act as though they are changed, although the before/after values |
||
| 628 | // will be the same |
||
| 629 | // So, we load one from the current stage and test against it |
||
| 630 | // This is to prevent the overhead of writing all translations when |
||
| 631 | // the class didn't actually change. |
||
| 632 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 633 | $currentStage = Versioned::current_stage(); |
||
| 634 | $fresh = Versioned::get_one_by_stage( |
||
| 635 | $baseDataClass, |
||
| 636 | Versioned::current_stage(), |
||
| 637 | '"ID" = ' . $this->owner->ID, |
||
| 638 | null |
||
| 639 | ); |
||
| 640 | if ($fresh) { |
||
| 641 | $changed = $changedFields['ClassName']['after'] != $fresh->ClassName; |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | if($changed) { |
||
| 646 | $this->owner->ClassName = $changedFields['ClassName']['before']; |
||
| 647 | $translations = $this->owner->getTranslations(); |
||
| 648 | $this->owner->ClassName = $changedFields['ClassName']['after']; |
||
| 649 | if($translations) foreach($translations as $translation) { |
||
| 650 | $translation->setClassName($this->owner->ClassName); |
||
| 651 | $translation = $translation->newClassInstance($translation->ClassName); |
||
| 652 | $translation->populateDefaults(); |
||
| 653 | $translation->forceChange(); |
||
| 654 | $translation->write(); |
||
| 655 | } |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | // see onAfterWrite() |
||
| 660 | if(!$this->owner->ID) { |
||
| 661 | $this->owner->_TranslatableIsNewRecord = true; |
||
| 662 | } |
||
| 663 | } |
||
| 664 | |||
| 665 | function onAfterWrite() { |
||
| 666 | // hacky way to determine if the record was created in the database, |
||
| 667 | // or just updated |
||
| 668 | if($this->owner->_TranslatableIsNewRecord) { |
||
| 669 | // this would kick in for all new records which are NOT |
||
| 670 | // created through createTranslation(), meaning they don't |
||
| 671 | // have the translation group automatically set. |
||
| 672 | $translationGroupID = $this->getTranslationGroup(); |
||
| 673 | if(!$translationGroupID) { |
||
| 674 | $this->addTranslationGroup( |
||
| 675 | $this->owner->_TranslationGroupID ? $this->owner->_TranslationGroupID : $this->owner->ID |
||
| 676 | ); |
||
| 677 | } |
||
| 678 | unset($this->owner->_TranslatableIsNewRecord); |
||
| 679 | unset($this->owner->_TranslationGroupID); |
||
| 680 | } |
||
| 681 | |||
| 682 | } |
||
| 683 | |||
| 684 | |||
| 685 | |||
| 686 | /** |
||
| 687 | * Attempt to get the page for a link in the default language that has been translated. |
||
| 688 | * |
||
| 689 | * @param string $URLSegment |
||
| 690 | * @param int|null $parentID |
||
| 691 | * @return SiteTree |
||
| 692 | */ |
||
| 693 | public function alternateGetByLink($URLSegment, $parentID) { |
||
| 694 | // If the parentID value has come from a translated page, |
||
| 695 | // then we need to find the corresponding parentID value |
||
| 696 | // in the default Locale. |
||
| 697 | if ( |
||
| 698 | is_int($parentID) |
||
| 699 | && $parentID > 0 |
||
| 700 | && ($parent = DataObject::get_by_id('SiteTree', $parentID)) |
||
| 701 | && ($parent->isTranslation()) |
||
| 702 | ) { |
||
| 703 | $parentID = $parent->getTranslationGroup(); |
||
| 704 | } |
||
| 705 | |||
| 706 | // Find the locale language-independent of the page |
||
| 707 | self::disable_locale_filter(); |
||
| 708 | $default = SiteTree::get()->where(sprintf ( |
||
| 709 | '"URLSegment" = \'%s\'%s', |
||
| 710 | Convert::raw2sql($URLSegment), |
||
| 711 | (is_int($parentID) ? " AND \"ParentID\" = $parentID" : null) |
||
| 712 | ))->First(); |
||
| 713 | self::enable_locale_filter(); |
||
| 714 | |||
| 715 | return $default; |
||
| 716 | } |
||
| 717 | |||
| 718 | //-----------------------------------------------------------------------------------------------// |
||
| 719 | |||
| 720 | |||
| 721 | public function updateRelativeLink(&$base, &$action) { |
||
| 722 | // Prevent home pages for non-default locales having their urlsegments |
||
| 723 | // reduced to the site root. |
||
| 724 | if($base === null && $this->owner->Locale != self::default_locale()){ |
||
| 725 | $base = $this->owner->URLSegment; |
||
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | |||
| 730 | |||
| 731 | function extendWithSuffix($table) { |
||
| 732 | return $table; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Gets all related translations for the current object, |
||
| 737 | * excluding itself. See {@link getTranslation()} to retrieve |
||
| 738 | * a single translated object. |
||
| 739 | * |
||
| 740 | * Getter with $stage parameter is specific to {@link Versioned} extension, |
||
| 741 | * mostly used for {@link SiteTree} subclasses. |
||
| 742 | * |
||
| 743 | * @param string $locale |
||
| 744 | * @param string $stage |
||
| 745 | * @return DataObjectSet |
||
| 746 | */ |
||
| 747 | function getTranslations($locale = null, $stage = null) { |
||
| 748 | if(!$this->owner->exists()) return new ArrayList(); |
||
| 749 | |||
| 750 | // HACK need to disable language filtering in augmentSQL(), |
||
| 751 | // as we purposely want to get different language |
||
| 752 | // also save state of locale-filter, revert to this state at the |
||
| 753 | // end of this method |
||
| 754 | $localeFilterEnabled = false; |
||
| 755 | if(self::locale_filter_enabled()) { |
||
| 756 | self::disable_locale_filter(); |
||
| 757 | $localeFilterEnabled = true; |
||
| 758 | } |
||
| 759 | |||
| 760 | $translationGroupID = $this->getTranslationGroup(); |
||
| 761 | |||
| 762 | $baseDataClass = ClassInfo::baseDataClass($this->owner->class); |
||
| 763 | $filter = sprintf('"%s_translationgroups"."TranslationGroupID" = %d', $baseDataClass, $translationGroupID); |
||
| 764 | if($locale) { |
||
| 765 | $filter .= sprintf(' AND "%s"."Locale" = \'%s\'', $baseDataClass, Convert::raw2sql($locale)); |
||
| 766 | } else { |
||
| 767 | // exclude the language of the current owner |
||
| 768 | $filter .= sprintf(' AND "%s"."Locale" != \'%s\'', $baseDataClass, $this->owner->Locale); |
||
| 769 | } |
||
| 770 | $currentStage = Versioned::current_stage(); |
||
| 771 | $joinOnClause = sprintf('"%s_translationgroups"."OriginalID" = "%s"."ID"', $baseDataClass, $baseDataClass); |
||
| 772 | if($this->owner->hasExtension("Versioned")) { |
||
| 773 | if($stage) Versioned::reading_stage($stage); |
||
| 774 | $translations = Versioned::get_by_stage( |
||
| 775 | $baseDataClass, |
||
| 776 | Versioned::current_stage(), |
||
| 777 | $filter, |
||
| 778 | null |
||
| 779 | )->leftJoin("{$baseDataClass}_translationgroups", $joinOnClause); |
||
| 780 | if($stage) Versioned::reading_stage($currentStage); |
||
| 781 | } else { |
||
| 782 | $class = $this->owner->class; |
||
| 783 | $translations = $baseDataClass::get() |
||
| 784 | ->where($filter) |
||
| 785 | ->leftJoin("{$baseDataClass}_translationgroups", $joinOnClause); |
||
| 786 | } |
||
| 787 | |||
| 788 | // only re-enable locale-filter if it was enabled at the beginning of this method |
||
| 789 | if($localeFilterEnabled) { |
||
| 790 | self::enable_locale_filter(); |
||
| 791 | } |
||
| 792 | |||
| 793 | return $translations; |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Gets an existing translation based on the language code. |
||
| 798 | * Use {@link hasTranslation()} as a quicker alternative to check |
||
| 799 | * for an existing translation without getting the actual object. |
||
| 800 | * |
||
| 801 | * @param String $locale |
||
| 802 | * @return DataObject Translated object |
||
| 803 | */ |
||
| 804 | function getTranslation($locale, $stage = null) { |
||
| 805 | $translations = $this->getTranslations($locale, $stage); |
||
| 806 | return ($translations) ? $translations->First() : null; |
||
| 807 | } |
||
| 808 | |||
| 809 | |||
| 810 | |||
| 811 | /** |
||
| 812 | * Enables automatic population of SiteConfig fields using createTranslation if |
||
| 813 | * created outside of the Translatable module |
||
| 814 | * @var boolean |
||
| 815 | */ |
||
| 816 | public static $enable_siteconfig_generation = true; |
||
| 817 | |||
| 818 | |||
| 819 | |||
| 820 | |||
| 821 | /** |
||
| 822 | * Get a list of languages with at least one element translated in (including the default language) |
||
| 823 | * |
||
| 824 | * @param string $className Look for languages in elements of this class |
||
| 825 | * @param string $where Optional SQL WHERE statement |
||
| 826 | * @return array Map of languages in the form locale => langName |
||
| 827 | */ |
||
| 828 | static function get_existing_content_languages($className = 'SiteTree', $where = '') { |
||
| 829 | $baseTable = ClassInfo::baseDataClass($className); |
||
| 830 | $query = new SQLQuery("Distinct \"Locale\"","\"$baseTable\"",$where, '', "\"Locale\""); |
||
| 831 | $dbLangs = $query->execute()->column(); |
||
| 832 | $langlist = array_merge((array)Translatable::default_locale(), (array)$dbLangs); |
||
| 833 | $returnMap = array(); |
||
| 834 | $allCodes = array_merge( |
||
| 835 | Config::inst()->get('i18n', 'all_locales'), |
||
| 836 | Config::inst()->get('i18n', 'common_locales') |
||
| 837 | ); |
||
| 838 | foreach ($langlist as $langCode) { |
||
| 839 | if($langCode && isset($allCodes[$langCode])) { |
||
| 840 | if(is_array($allCodes[$langCode])) { |
||
| 841 | $returnMap[$langCode] = $allCodes[$langCode]['name']; |
||
| 842 | } else { |
||
| 843 | $returnMap[$langCode] = $allCodes[$langCode]; |
||
| 844 | } |
||
| 845 | } |
||
| 846 | } |
||
| 847 | return $returnMap; |
||
| 848 | } |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Get the RelativeLink value for a home page in another locale. This is found by searching for the default home |
||
| 852 | * page in the default language, then returning the link to the translated version (if one exists). |
||
| 853 | * |
||
| 854 | * @return string |
||
| 855 | */ |
||
| 856 | public static function get_homepage_link_by_locale($locale) { |
||
| 857 | $originalLocale = self::get_current_locale(); |
||
| 858 | |||
| 859 | self::set_current_locale(self::default_locale()); |
||
| 860 | $original = SiteTree::get_by_link(RootURLController::config()->default_homepage_link); |
||
| 861 | self::set_current_locale($originalLocale); |
||
| 862 | |||
| 863 | if($original) { |
||
| 864 | if($translation = $original->getTranslation($locale)) return trim($translation->RelativeLink(true), '/'); |
||
| 865 | } |
||
| 866 | } |
||
| 867 | |||
| 868 | |||
| 869 | /** |
||
| 870 | * @deprecated 2.4 Use {@link Translatable::get_homepage_link_by_locale()} |
||
| 871 | */ |
||
| 872 | static function get_homepage_urlsegment_by_locale($locale) { |
||
| 873 | user_error ( |
||
| 874 | 'Translatable::get_homepage_urlsegment_by_locale() is deprecated, please use get_homepage_link_by_locale()', |
||
| 875 | E_USER_NOTICE |
||
| 876 | ); |
||
| 877 | |||
| 878 | return self::get_homepage_link_by_locale($locale); |
||
| 879 | } |
||
| 880 | |||
| 881 | |||
| 882 | |||
| 883 | /** |
||
| 884 | * Return a piece of text to keep DataObject cache keys appropriately specific |
||
| 885 | */ |
||
| 886 | function cacheKeyComponent() { |
||
| 887 | return 'locale-'.self::get_current_locale(); |
||
| 888 | } |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Extends the SiteTree::validURLSegment() method, to do checks appropriate |
||
| 892 | * to Translatable |
||
| 893 | * |
||
| 894 | * @return bool |
||
| 895 | */ |
||
| 896 | public function augmentValidURLSegment() { |
||
| 897 | $reEnableFilter = false; |
||
| 898 | if(!Config::inst()->get('Translatable', 'enforce_global_unique_urls')) { |
||
| 899 | self::enable_locale_filter(); |
||
| 900 | } elseif(self::locale_filter_enabled()) { |
||
| 901 | self::disable_locale_filter(); |
||
| 902 | $reEnableFilter = true; |
||
| 903 | } |
||
| 904 | |||
| 905 | $IDFilter = ($this->owner->ID) ? "AND \"SiteTree\".\"ID\" <> {$this->owner->ID}" : null; |
||
| 906 | $parentFilter = null; |
||
| 907 | |||
| 908 | if (Config::inst()->get('SiteTree', 'nested_urls')) { |
||
| 909 | if($this->owner->ParentID) { |
||
| 910 | $parentFilter = " AND \"SiteTree\".\"ParentID\" = {$this->owner->ParentID}"; |
||
| 911 | } else { |
||
| 912 | $parentFilter = ' AND "SiteTree"."ParentID" = 0'; |
||
| 913 | } |
||
| 914 | } |
||
| 915 | |||
| 916 | $existingPage = SiteTree::get() |
||
| 917 | // disable get_one cache, as this otherwise may pick up results from when locale_filter was on |
||
| 918 | ->where("\"URLSegment\" = '{$this->owner->URLSegment}' $IDFilter $parentFilter")->First(); |
||
| 919 | if($reEnableFilter) self::enable_locale_filter(); |
||
| 920 | |||
| 921 | // By returning TRUE or FALSE, we overrule the base SiteTree->validateURLSegment() logic |
||
| 922 | return !$existingPage; |
||
| 923 | } |
||
| 924 | |||
| 925 | } |
||
| 926 |