public function fix_fluent_menu()
    {
        if (!class_exists('Fluent')) {
            return;
        }
        $conf = SiteConfig::current_site_config();
        $localesNames = Fluent::locale_names();
        if ($conf->hasExtension('ActiveLocalesExtension') && $conf->ActiveLocales) {
            $localesNames = $conf->ActiveLocalesNames();
        }
        $locales = json_encode($localesNames);
        $locale = json_encode(Fluent::current_locale());
        // If we have only one locale, set this one as default
        if (count($localesNames) === 1) {
            $locale = json_encode(key($localesNames));
        }
        $param = json_encode(Fluent::config()->query_param);
        $buttonTitle = json_encode(_t('Fluent.ChangeLocale', 'Change Locale'));
        Requirements::block('FluentHeadScript');
        Requirements::insertHeadTags(<<<EOT
<script type="text/javascript">
//<![CDATA[
\tvar fluentLocales = {$locales};
\tvar fluentLocale = {$locale};
\tvar fluentParam = {$param};
\tvar fluentButtonTitle = {$buttonTitle};
//]]>
</script>
EOT
, 'FluentHeadScriptSubsite');
    }
 /**
  * Determines the fields to translate on the given class
  *
  * @return array List of field names and data types
  */
 public static function translated_fields_for($class)
 {
     if (isset(self::$translated_fields_for_cache[$class])) {
         return self::$translated_fields_for_cache[$class];
     }
     return self::$translated_fields_for_cache[$class] = self::without_fluent_fields(function () use($class) {
         $db = DataObject::custom_database_fields($class);
         $filter = Config::inst()->get($class, 'translate', Config::UNINHERITED);
         if ($filter === 'none') {
             return array();
         }
         // Data and field filters
         $fieldsInclude = Fluent::config()->field_include;
         $fieldsExclude = Fluent::config()->field_exclude;
         $dataInclude = Fluent::config()->data_include;
         $dataExclude = Fluent::config()->data_exclude;
         // filter out DB
         if ($db) {
             foreach ($db as $field => $type) {
                 if (!empty($filter)) {
                     // If given an explicit field name filter, then remove non-presented fields
                     if (!in_array($field, $filter)) {
                         unset($db[$field]);
                     }
                 } else {
                     // Without a name filter then check against each filter type
                     if ($fieldsInclude && !Fluent::any_match($field, $fieldsInclude) || $fieldsExclude && Fluent::any_match($field, $fieldsExclude) || $dataInclude && !Fluent::any_match($type, $dataInclude) || $dataExclude && Fluent::any_match($type, $dataExclude)) {
                         unset($db[$field]);
                     }
                 }
             }
         }
         return $db;
     });
 }
 public function LocaleLink($locale)
 {
     // For blank/temp pages such as Security controller fallback to querystring
     if (!$this->owner->exists()) {
         $url = Controller::curr()->getRequest()->getURL();
         return Controller::join_links($url, '?' . Fluent::config()->query_param . '=' . urlencode($locale));
     }
     return parent::LocaleLink($locale);
 }
 public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
 {
     self::$is_at_root = true;
     $this->setDataModel($model);
     $this->pushCurrent();
     $this->init();
     $this->setRequest($request);
     // Check for existing routing parameters, redirecting to another locale automatically if necessary
     $locale = Fluent::get_request_locale();
     if (empty($locale)) {
         // Determine if this user should be redirected
         $locale = $this->getRedirectLocale();
         $this->extend('updateRedirectLocale', $locale);
         // Check if the user should be redirected
         $domainDefault = Fluent::default_locale(true);
         if (Fluent::is_locale($locale) && $locale !== $domainDefault) {
             // Check new traffic with detected locale
             return $this->redirect(Fluent::locale_baseurl($locale));
         }
         // Reset parameters to act in the default locale
         $locale = $domainDefault;
         Fluent::set_persist_locale($locale);
         $params = $request->routeParams();
         $params[Fluent::config()->query_param] = $locale;
         $request->setRouteParams($params);
     }
     if (!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
         $this->response = new SS_HTTPResponse();
         $this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
         return $this->response;
     }
     $localeURL = Fluent::alias($locale);
     $request->setUrl(self::fluent_homepage_link($localeURL));
     $request->match($localeURL . '/$URLSegment//$Action', true);
     $controller = new ModelAsController();
     $result = $controller->handleRequest($request, $model);
     $this->popCurrent();
     return $result;
 }
    public function init()
    {
        $dirName = basename(dirname(dirname(dirname(__FILE__))));
        $locales = json_encode(Fluent::locale_names());
        $locale = json_encode(Fluent::current_locale());
        $param = json_encode(Fluent::config()->query_param);
        $buttonTitle = json_encode(_t('Fluent.ChangeLocale', 'Change Locale'));
        // Force the variables to be written to the head, to ensure these are available for other scripts to pick up.
        Requirements::insertHeadTags(<<<EOT
<script type="text/javascript">
//<![CDATA[
\tvar fluentLocales = {$locales};
\tvar fluentLocale = {$locale};
\tvar fluentParam = {$param};
\tvar fluentButtonTitle = {$buttonTitle};
//]]>
</script>
EOT
, 'FluentHeadScript');
        Requirements::javascript("{$dirName}/javascript/fluent.js");
        Requirements::css("{$dirName}/css/fluent.css");
    }
Ejemplo n.º 6
0
 /**
  * Forces regeneration of all locale routes
  */
 public static function regenerate_routes()
 {
     $routes = array();
     // Explicit routes
     foreach (self::locales() as $locale) {
         $url = self::alias($locale);
         $routes[$url . '/$URLSegment!//$Action/$ID/$OtherID'] = array('Controller' => 'ModelAsController', self::config()->query_param => $locale);
         $routes[$url] = array('Controller' => 'FluentRootURLController', self::config()->query_param => $locale);
     }
     // If Google sitemap module is installed then replace default controller with custom controller
     if (class_exists('GoogleSitemapController')) {
         $routes['sitemap.xml'] = 'FluentSitemapController';
     }
     // Merge all other routes (maintain priority)
     foreach (Config::inst()->get('Director', 'rules') as $key => $route) {
         if (!isset($routes[$key])) {
             $routes[$key] = $route;
         }
     }
     // Home page route
     $routes[''] = array('Controller' => 'FluentRootURLController');
     // If we do not wish to detect the locale automatically, fix the home page route
     // to the default locale for this domain.
     if (!Fluent::config()->detect_locale) {
         $routes[''][self::config()->query_param] = static::default_locale(true);
     }
     // If default locale doesn't have prefix, replace default route with
     // the default locale for this domain
     if (static::disable_default_prefix()) {
         $routes['$URLSegment//$Action/$ID/$OtherID'] = array('Controller' => 'ModelAsController', self::config()->query_param => static::default_locale(true));
     }
     $singleton = singleton(__CLASS__);
     $singleton->extend('updateRegenerateRoutes', $routes);
     // Load into core routes
     Config::inst()->remove('Director', 'rules');
     Config::inst()->update('Director', 'rules', $routes);
     $singleton->extend('onAfterRegenerateRoutes');
 }
 /**
  * Amend freshly created DataQuery objects with the "should filter admin?" option, current locale and frontend status
  *
  * @param SQLQuery
  * @param DataQuery
  */
 public function augmentDataQueryCreation(SQLQuery $query, DataQuery $dataQuery)
 {
     $dataQuery->setQueryParam('Fluent.FilterAdmin', Fluent::config()->filter_admin);
     $dataQuery->setQueryParam('Fluent.Locale', Fluent::current_locale());
     $dataQuery->setQueryParam('Fluent.IsFrontend', Fluent::is_frontend());
 }
 /**
  * Adds a UI message to indicate whether you're editing in the default locale or not
  *
  * @param  FieldList $fields
  * @return $this
  */
 protected function addLocaleIndicatorMessage(FieldList $fields)
 {
     if (Fluent::config()->disable_current_locale_message) {
         return $this;
     }
     // If the field is already present, don't add it a second time
     if ($fields->fieldByName('CurrentLocaleMessage')) {
         return $this;
     }
     $localeNames = Fluent::locale_names();
     $isDefaultLocale = Fluent::default_locale() === Fluent::current_locale();
     $messageClass = $isDefaultLocale ? 'good' : 'notice';
     $message = $isDefaultLocale ? _t('Fluent.DefaultLocale', 'This is the default locale') : _t('Fluent.DefaultLocaleIs', 'The default locale is') . ' ' . $localeNames[Fluent::default_locale()];
     $fields->unshift(LiteralField::create('CurrentLocaleMessage', sprintf('<p class="message %s">' . _t('Fluent.EditingIn', 'Please note: You are editing in') . ' %s. %s.</p>', $messageClass, $localeNames[Fluent::current_locale()], $message)));
     return $this;
 }