Permalink
Browse files

MINOR Initial commit, moved files from 'sapphire' and 'cms' modules

  • Loading branch information...
0 parents commit 2a3cc650dc55cef276e3b1690b9b1dfb4e51a054 @chillu chillu committed Mar 22, 2011
24 LICENSE
@@ -0,0 +1,24 @@
+* Copyright (c) 2007-2011, Silverstripe Ltd.
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+* * Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* * Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in the
+* documentation and/or other materials provided with the distribution.
+* * Neither the name of the <organization> nor the
+* names of its contributors may be used to endorse or promote products
+* derived from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY Silverstripe Ltd. ``AS IS'' AND ANY
+* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+* DISCLAIMED. IN NO EVENT SHALL Silverstripe Ltd. BE LIABLE FOR ANY
+* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,35 @@
+# Translatable module for SilverStripe CMS #
+
+## Introduction ##
+
+Allows translation of DataObject and SiteTree records into multiple languages.
+See `/docs/en/index.md` for details.
+
+## Requirements ##
+
+ * SilverStripe 3.0 (both cms and sapphire modules)
+
+## Maintainers ##
+
+ * Ingo Schommer <ingo at silverstripe dot com>
+
+## TODO ##
+
+This module was originally part of the SilverStripe CMS core codebase.
+While the bulk of the logic has been separated out into this module,
+there are still many places across SilverStripe CMS which this modules relies on:
+
+* CMSBatchActionHandler->handleAction()
+* ContentController->handleRequest()
+* ContentController->ContentLocale()
+* ErrorPage::response_for()
+* LeftAndMain->init()
+* ModelAsController->getNestedController()
+* RootURLController::get_homepage_link()
+* SearchForm
+* SiteConfig
+* SiteTree->RelativeLink()
+* SiteTree->getSiteConfig()
+
+These APIs mostly require either hooks for an Extension subclass,
+or refactoring to allow better customization.
@@ -0,0 +1,4 @@
+<?php
+Object::add_extension('SiteTree', 'Translatable');
+Object::add_extension('SiteConfig', 'Translatable');
+Object::add_extension('CMSMain', 'TranslatableCMSMainExtension');
@@ -0,0 +1,143 @@
+<?php
+class TranslatableCMSMainExtension extends Extension {
+
+ function extraStatics() {
+ return array(
+ 'allowed_actions' => array(
+ 'createtranslation',
+ )
+ );
+ }
+
+ function init() {
+
+ // Locale" attribute is either explicitly added by LeftAndMain Javascript logic,
+ // or implied on a translated record (see {@link Translatable->updateCMSFields()}).
+ // $Lang serves as a "context" which can be inspected by Translatable - hence it
+ // has the same name as the database property on Translatable.
+ $req = $this->owner->getRequest();
+ if($req->requestVar("Locale")) {
+ $this->owner->Locale = $req->requestVar("Locale");
+ } elseif($req->requestVar("locale")) {
+ $this->owner->Locale = $req->requestVar("locale");
+ } else {
+ $this->owner->Locale = Translatable::default_locale();
+ }
+ Translatable::set_current_locale($this->owner->Locale);
+
+ // collect languages for TinyMCE spellchecker plugin.
+ // see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
+ $langName = i18n::get_locale_name($this->owner->Locale);
+ HtmlEditorConfig::get('cms')->setOption('spellchecker_languages', "+{$langName}={$this->owner->Locale}");
+
+ Requirements::javascript('translatable/javascript/CMSMain.Translatable.js');
+ Requirements::css('translatable/css/CMSMain.Translatable.css');
+ }
+
+ function updateEditForm(&$form) {
+ $siteConfig = SiteConfig::current_site_config();
+ if($form->Name() == 'RootForm' && Object::has_extension('SiteConfig',"Translatable")) {
+ $form->Fields()->push(new HiddenField('Locale','', $siteConfig->Locale));
+ }
+ }
+
+ function updatePageOption(&$fields) {
+ $fields->push(new HiddenField("Locale", 'Locale', Translatable::get_current_locale()));
+ }
+
+ /**
+ * Create a new translation from an existing item, switch to this language and reload the tree.
+ */
+ function createtranslation($request) {
+ // Protect against CSRF on destructive action
+ if(!SecurityToken::inst()->checkRequest($request)) return $this->owner->httpError(400);
+
+ $langCode = Convert::raw2sql($request->getVar('newlang'));
+ $originalLangID = (int)$request->getVar('ID');
+
+ $record = $this->owner->getRecord($originalLangID);
+
+ $this->owner->Locale = $langCode;
+ Translatable::set_current_locale($langCode);
+
+ // Create a new record in the database - this is different
+ // to the usual "create page" pattern of storing the record
+ // in-memory until a "save" is performed by the user, mainly
+ // to simplify things a bit.
+ // @todo Allow in-memory creation of translations that don't persist in the database before the user requests it
+ $translatedRecord = $record->createTranslation($langCode);
+
+ $url = sprintf(
+ "%s/%d/?locale=%s",
+ $this->owner->Link('show'),
+ $translatedRecord->ID,
+ $langCode
+ );
+
+ return Director::redirect($url);
+ }
+
+ /**
+ * Returns a form with all languages with languages already used appearing first.
+ *
+ * @return Form
+ */
+ function LangForm() {
+ $member = Member::currentUser(); //check to see if the current user can switch langs or not
+ if(Permission::checkMember($member, 'VIEW_LANGS')) {
+ $field = new LanguageDropdownField(
+ 'Locale',
+ _t('CMSMAIN.LanguageDropdownLabel', 'Language'),
+ array(),
+ 'SiteTree',
+ 'Locale-English',
+ singleton('SiteTree')
+ );
+ $field->setValue(Translatable::get_current_locale());
+ } else {
+ // user doesn't have permission to switch langs
+ // so just show a string displaying current language
+ $field = new LiteralField(
+ 'Locale',
+ i18n::get_locale_name( Translatable::get_current_locale())
+ );
+ }
+
+ $form = new Form(
+ $this->owner,
+ 'LangForm',
+ new FieldSet(
+ $field
+ ),
+ new FieldSet(
+ new FormAction('selectlang', _t('CMSMain_left.ss.GO','Go'))
+ )
+ );
+ $form->unsetValidator();
+
+ return $form;
+ }
+
+ function selectlang($data, $form) {
+ return $this->owner;
+ }
+
+ /**
+ * Determine if there are more than one languages in our site tree.
+ *
+ * @return boolean
+ */
+ function MultipleLanguages() {
+ $langs = Translatable::get_existing_content_languages('SiteTree');
+
+ return (count($langs) > 1);
+ }
+
+ /**
+ * @return boolean
+ */
+ function IsTranslatableEnabled() {
+ return Object::has_extension('SiteTree', 'Translatable');
+ }
+
+}
@@ -0,0 +1,71 @@
+<?php
+/**
+ * An extension to dropdown field, pre-configured to list languages.
+ * The languages already used in the site will be on top.
+ * @package forms
+ * @subpackage fields-relational
+ */
+class LanguageDropdownField extends GroupedDropdownField {
+
+ /**
+ * Create a new LanguageDropdownField
+ * @param string $name
+ * @param string $title
+ * @param array $excludeLocales List of locales that won't be included
+ * @param string $translatingClass Name of the class with translated instances where to look for used languages
+ * @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
+ */
+ function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null) {
+ $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
+ $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
+
+ if('Common-English' == $list) $allLocalesWithTitle = i18n::get_common_languages();
+ else if('Common-Native' == $list) $allLocalesWithTitle = i18n::get_common_languages(true);
+ else if('Locale-English' == $list) $allLocalesWithTitle = i18n::get_common_locales();
+ else if('Locale-Native' == $list) $allLocalesWithTitle = i18n::get_common_locales(true);
+ else $allLocalesWithTitle = i18n::get_locale_list();
+
+ if(isset($allLocales[Translatable::default_locale()])) unset($allLocales[Translatable::default_locale()]);
+
+ // Limit to allowed locales if defined
+ // Check for canTranslate() if an $instance is given
+ $allowedLocales = Translatable::get_allowed_locales();
+ foreach($allLocalesWithTitle as $locale => $localeTitle) {
+ if(
+ ($allowedLocales && !in_array($locale, $allowedLocales))
+ || ($excludeLocales && in_array($locale, $excludeLocales))
+ || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle))
+ ) {
+ unset($allLocalesWithTitle[$locale]);
+ }
+ }
+ // instance specific permissions
+ foreach($allLocalesWithTitle as $locale => $localeTitle) {
+ if($instance && !$instance->canTranslate(null, $locale)) {
+ unset($allLocalesWithTitle[$locale]);
+ }
+ }
+ foreach($usedLocalesWithTitle as $locale => $localeTitle) {
+ if($instance && !$instance->canTranslate(null, $locale)) {
+ unset($usedLocalesWithTitle[$locale]);
+ }
+ }
+
+ // Sort by title (array value)
+ asort($allLocalesWithTitle);
+
+ if(count($usedLocalesWithTitle)) {
+ asort($usedLocalesWithTitle);
+ $source = array(
+ _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle,
+ _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle
+ );
+ } else {
+ $source = $allLocalesWithTitle;
+ }
+
+ parent::__construct($name, $title, $source);
+ }
+}
+
+?>
Oops, something went wrong.

0 comments on commit 2a3cc65

Please sign in to comment.