function testLocaleFilteringEnabledAndDisabled()
 {
     $this->assertTrue(Translatable::locale_filter_enabled());
     // get our base page to use for testing
     $origPage = $this->objFromFixture('Page', 'testpage_en');
     $origPage->MenuTitle = 'unique-key-used-in-my-query';
     $origPage->write();
     $origPage->publish('Stage', 'Live');
     // create a translation of it so that we can see if translations are filtered
     $translatedPage = $origPage->createTranslation('de_DE');
     $translatedPage->MenuTitle = $origPage->MenuTitle;
     $translatedPage->write();
     $translatedPage->publish('Stage', 'Live');
     $where = sprintf("\"MenuTitle\" = '%s'", Convert::raw2sql($origPage->MenuTitle));
     // make sure that our query was filtered
     $this->assertEquals(1, Page::get()->where($where)->count());
     // test no filtering with disabled locale filter
     Translatable::disable_locale_filter();
     $this->assertEquals(2, Page::get()->where($where)->count());
     Translatable::enable_locale_filter();
     // make sure that our query was filtered after re-enabling the filter
     $this->assertEquals(1, Page::get()->where($where)->count());
     // test effectiveness of disabling locale filter with 3.x delayed querying
     // see https://github.com/silverstripe/silverstripe-translatable/issues/113
     Translatable::disable_locale_filter();
     // create the DataList while the locale filter is disabled
     $dataList = Page::get()->where($where);
     Translatable::enable_locale_filter();
     // but don't use it until later - after the filter is re-enabled
     $this->assertEquals(2, $dataList->count());
 }
 public function augmentSQL(SQLQuery &$query, DataQuery $dataQuery = null)
 {
     if ($this->owner->ID && !empty($this->owner->Locale)) {
         $locale = $this->owner->Locale;
     } else {
         $locale = Translatable::get_current_locale();
     }
     if ($locale && Translatable::locale_filter_enabled()) {
         $qry = sprintf('"ContentModule"."Locale" = \'%s\'', Convert::raw2sql($locale));
         $query->addWhere($qry);
     }
 }
 /**
  * Returns the state of other modules before compatibility mode is started.
  *
  * @return array
  */
 public static function start()
 {
     $compatibility = array(self::SUBSITES => null, self::TRANSLATABLE => null);
     if (ClassInfo::exists("Subsite")) {
         $compatibility[self::SUBSITES] = Subsite::$disable_subsite_filter;
         Subsite::disable_subsite_filter(true);
     }
     if (ClassInfo::exists("Translatable")) {
         $compatibility[self::TRANSLATABLE] = Translatable::locale_filter_enabled();
         Translatable::disable_locale_filter();
     }
     return $compatibility;
 }
Example #4
0
 /**
  * Disables automatic locale filtering in {@link augmentSQL()}. This can be re-enabled
  * using {@link enable_locale_filter()}.
  */
 public static function disable_locale_filter()
 {
     self::$locale_filter_enabled = false;
 }
 /**
  * When the SiteConfig object is automatically instantiated, we should ensure that
  * 1. All SiteConfig objects belong to the same group
  * 2. Defaults are correctly initiated from the base object
  * 3. The creation mechanism uses the createTranslation function in order to be consistent
  * This function ensures that any already created "vanilla" SiteConfig object is populated 
  * correctly with translated values.
  * This function DOES populate the ID field with the newly created object ID
  * @see SiteConfig
  */
 protected function populateSiteConfigDefaults()
 {
     // Work-around for population of defaults during database initialisation.
     // When the database is being setup singleton('SiteConfig') is called.
     if (!DB::getConn()->hasTable($this->owner->class)) {
         return;
     }
     if (!DB::getConn()->hasField($this->owner->class, 'Locale')) {
         return;
     }
     if (DB::getConn()->isSchemaUpdating()) {
         return;
     }
     // Find the best base translation for SiteConfig
     $enabled = Translatable::locale_filter_enabled();
     Translatable::disable_locale_filter();
     $existingConfig = SiteConfig::get()->filter(array('Locale' => Translatable::default_locale()))->first();
     if (!$existingConfig) {
         $existingConfig = SiteConfig::get()->first();
     }
     if ($enabled) {
         Translatable::enable_locale_filter();
     }
     // Stage this SiteConfig and copy into the current object
     if ($existingConfig && !$existingConfig->getTranslation(Translatable::get_current_locale()) && $existingConfig->canTranslate(null, Translatable::get_current_locale())) {
         // Create an unsaved "staging" translated object using the correct createTranslation mechanism
         $stagingConfig = $existingConfig->createTranslation(Translatable::get_current_locale(), false);
         $this->owner->update($stagingConfig->toMap());
     }
     // Maintain single translation group for SiteConfig
     if ($existingConfig) {
         $this->owner->_TranslationGroupID = $existingConfig->getTranslationGroup();
     }
     $this->owner->Locale = Translatable::get_current_locale();
 }