2011-03-22 09:50:26 +01:00
|
|
|
<?php
|
2011-03-22 22:15:15 +01:00
|
|
|
/**
|
|
|
|
* @package translatable
|
|
|
|
*/
|
2011-03-22 09:50:26 +01:00
|
|
|
class TranslatableCMSMainExtension extends Extension {
|
|
|
|
|
2013-03-26 01:00:18 +01:00
|
|
|
private static $allowed_actions = array(
|
2011-03-22 22:15:49 +01:00
|
|
|
'createtranslation',
|
|
|
|
);
|
2011-03-22 09:50:26 +01:00
|
|
|
|
|
|
|
function init() {
|
2013-04-26 00:13:46 +02:00
|
|
|
$req = $this->owner->getRequest();
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2012-08-14 11:22:42 +02:00
|
|
|
// Ignore being called on LeftAndMain base class,
|
|
|
|
// which is the case when requests are first routed through AdminRootController
|
|
|
|
// as an intermediary rather than the endpoint controller
|
|
|
|
if(!$this->owner->stat('tree_class')) return;
|
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
// 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.
|
2012-05-15 17:50:04 +02:00
|
|
|
$id = $req->param('ID');
|
2011-03-22 09:50:26 +01:00
|
|
|
if($req->requestVar("Locale")) {
|
|
|
|
$this->owner->Locale = $req->requestVar("Locale");
|
2012-05-15 17:50:04 +02:00
|
|
|
} else if($id && is_numeric($id)) {
|
|
|
|
$record = DataObject::get_by_id($this->owner->stat('tree_class'), $id);
|
|
|
|
if($record && $record->Locale) $this->owner->Locale = $record->Locale;
|
2011-03-22 09:50:26 +01:00
|
|
|
} else {
|
|
|
|
$this->owner->Locale = Translatable::default_locale();
|
2012-09-03 09:26:55 +02:00
|
|
|
if ($this->owner->class == 'CMSPagesController') {
|
2016-03-03 23:24:34 +01:00
|
|
|
// the CMSPagesController always needs to have the locale set,
|
2012-10-02 22:29:56 +02:00
|
|
|
// otherwise page editing will cause an extra
|
2012-09-03 09:26:55 +02:00
|
|
|
// ajax request which looks weird due to multiple "loading"-flashes
|
2013-01-08 19:39:44 +01:00
|
|
|
$getVars = $req->getVars();
|
|
|
|
if(isset($getVars['url'])) unset($getVars['url']);
|
|
|
|
return $this->owner->redirect(Controller::join_links(
|
|
|
|
$this->owner->Link(),
|
|
|
|
$req->param('Action'),
|
|
|
|
$req->param('ID'),
|
|
|
|
$req->param('OtherID'),
|
2013-03-21 04:01:03 +01:00
|
|
|
($query = http_build_query($getVars)) ? "?$query" : null
|
2013-01-08 19:39:44 +01:00
|
|
|
));
|
2012-09-03 09:26:55 +02:00
|
|
|
}
|
2011-03-22 09:50:26 +01:00
|
|
|
}
|
|
|
|
Translatable::set_current_locale($this->owner->Locale);
|
2012-07-16 23:35:28 +02:00
|
|
|
|
2015-06-01 10:53:53 +02:00
|
|
|
// If a locale is set, it needs to match to the current record
|
|
|
|
$requestLocale = $req->requestVar("Locale");
|
2012-07-16 23:35:28 +02:00
|
|
|
$page = $this->owner->currentPage();
|
2012-10-02 22:29:56 +02:00
|
|
|
if(
|
2015-06-01 10:53:53 +02:00
|
|
|
$req->httpMethod() == 'GET' // leave form submissions alone
|
2016-03-03 23:24:34 +01:00
|
|
|
&& $requestLocale
|
|
|
|
&& $page
|
|
|
|
&& $page->hasExtension('Translatable')
|
2012-10-02 22:29:56 +02:00
|
|
|
&& $page->Locale != $requestLocale
|
2012-11-12 14:42:48 +01:00
|
|
|
&& $req->latestParam('Action') != 'EditorToolbar'
|
2012-10-02 22:29:56 +02:00
|
|
|
) {
|
2012-07-16 23:35:28 +02:00
|
|
|
$transPage = $page->getTranslation($requestLocale);
|
|
|
|
if($transPage) {
|
|
|
|
Translatable::set_current_locale($transPage->Locale);
|
|
|
|
return $this->owner->redirect(Controller::join_links(
|
|
|
|
$this->owner->Link('show'),
|
|
|
|
$transPage->ID
|
|
|
|
// ?locale will automatically be added
|
|
|
|
));
|
2012-09-03 13:38:38 +02:00
|
|
|
} else if ($this->owner->class != 'CMSPagesController') {
|
2012-07-16 23:35:28 +02:00
|
|
|
// If the record is not translated, redirect to pages overview
|
|
|
|
return $this->owner->redirect(Controller::join_links(
|
|
|
|
singleton('CMSPagesController')->Link(),
|
2015-06-01 10:52:07 +02:00
|
|
|
'?Locale=' . $requestLocale
|
2012-07-16 23:35:28 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
// collect languages for TinyMCE spellchecker plugin.
|
|
|
|
// see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
|
|
|
|
$langName = i18n::get_locale_name($this->owner->Locale);
|
2012-10-02 22:29:56 +02:00
|
|
|
HtmlEditorConfig::get('cms')->setOption(
|
2016-03-03 23:24:34 +01:00
|
|
|
'spellchecker_languages',
|
2012-10-02 22:29:56 +02:00
|
|
|
"+{$langName}={$this->owner->Locale}"
|
|
|
|
);
|
2012-07-26 15:06:42 +02:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
Requirements::javascript('translatable/javascript/CMSMain.Translatable.js');
|
|
|
|
Requirements::css('translatable/css/CMSMain.Translatable.css');
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
function updateEditForm(&$form) {
|
2013-02-01 18:42:32 +01:00
|
|
|
if($form->getName() == 'RootForm' && SiteConfig::has_extension("Translatable")) {
|
2012-11-13 07:39:36 +01:00
|
|
|
$siteConfig = SiteConfig::current_site_config();
|
2011-03-22 09:50:26 +01:00
|
|
|
$form->Fields()->push(new HiddenField('Locale','', $siteConfig->Locale));
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2012-07-03 01:03:36 +02:00
|
|
|
function updatePageOptions(&$fields) {
|
2011-03-22 09:50:26 +01:00
|
|
|
$fields->push(new HiddenField("Locale", 'Locale', Translatable::get_current_locale()));
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
/**
|
|
|
|
* Create a new translation from an existing item, switch to this language and reload the tree.
|
|
|
|
*/
|
2012-05-15 17:50:04 +02:00
|
|
|
function createtranslation($data, $form) {
|
|
|
|
$request = $this->owner->getRequest();
|
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) return $this->owner->httpError(400);
|
2012-07-26 15:06:42 +02:00
|
|
|
|
2012-05-15 17:50:04 +02:00
|
|
|
$langCode = Convert::raw2sql($request->postVar('NewTransLang'));
|
|
|
|
$record = $this->owner->getRecord($request->postVar('ID'));
|
2012-07-26 15:06:42 +02:00
|
|
|
if(!$record) return $this->owner->httpError(404);
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
$this->owner->Locale = $langCode;
|
|
|
|
Translatable::set_current_locale($langCode);
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
// 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.
|
2016-03-03 23:24:34 +01:00
|
|
|
// @todo Allow in-memory creation of translations that don't
|
2012-10-02 22:29:56 +02:00
|
|
|
// persist in the database before the user requests it
|
2011-03-22 09:50:26 +01:00
|
|
|
$translatedRecord = $record->createTranslation($langCode);
|
|
|
|
|
2012-07-26 15:06:42 +02:00
|
|
|
$url = Controller::join_links(
|
|
|
|
$this->owner->Link('show'),
|
2012-08-07 23:01:39 +02:00
|
|
|
$translatedRecord->ID
|
2011-03-22 09:50:26 +01:00
|
|
|
);
|
2011-03-22 22:15:49 +01:00
|
|
|
|
2012-08-03 11:51:05 +02:00
|
|
|
// set the X-Pjax header to Content, so that the whole admin panel will be refreshed
|
|
|
|
$this->owner->getResponse()->addHeader('X-Pjax', 'Content');
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2012-07-04 18:00:45 +02:00
|
|
|
return $this->owner->redirect($url);
|
2011-03-22 09:50:26 +01:00
|
|
|
}
|
2012-05-15 17:50:04 +02:00
|
|
|
|
|
|
|
function updateLink(&$link) {
|
2013-09-05 16:06:58 +02:00
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
|
2015-06-01 10:52:07 +02:00
|
|
|
if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
|
2012-05-15 17:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLinkWithSearch(&$link) {
|
2013-09-05 16:06:58 +02:00
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
|
2016-03-03 23:24:34 +01:00
|
|
|
if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
|
2012-05-15 17:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateExtraTreeTools(&$html) {
|
2013-09-05 16:06:58 +02:00
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
|
2012-05-15 17:50:04 +02:00
|
|
|
$html = $this->LangForm()->forTemplate() . $html;
|
|
|
|
}
|
2012-08-16 09:30:59 +02:00
|
|
|
|
|
|
|
function updateLinkPageAdd(&$link) {
|
2013-09-05 16:06:58 +02:00
|
|
|
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
|
2015-06-01 10:52:07 +02:00
|
|
|
if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
|
2012-08-16 09:30:59 +02:00
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
/**
|
|
|
|
* Returns a form with all languages with languages already used appearing first.
|
2016-03-03 23:24:34 +01:00
|
|
|
*
|
2011-03-22 09:50:26 +01:00
|
|
|
* @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(
|
2016-03-03 23:24:34 +01:00
|
|
|
'Locale',
|
|
|
|
_t('CMSMain.LANGUAGEDROPDOWNLABEL', 'Language'),
|
|
|
|
array(),
|
|
|
|
'SiteTree',
|
2011-03-22 09:50:26 +01:00
|
|
|
'Locale-English',
|
|
|
|
singleton('SiteTree')
|
|
|
|
);
|
|
|
|
$field->setValue(Translatable::get_current_locale());
|
|
|
|
} else {
|
2016-03-03 23:24:34 +01:00
|
|
|
// user doesn't have permission to switch langs
|
2011-03-22 09:50:26 +01:00
|
|
|
// so just show a string displaying current language
|
|
|
|
$field = new LiteralField(
|
2016-03-03 23:24:34 +01:00
|
|
|
'Locale',
|
2011-03-22 09:50:26 +01:00
|
|
|
i18n::get_locale_name( Translatable::get_current_locale())
|
|
|
|
);
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this->owner,
|
|
|
|
'LangForm',
|
2012-05-15 17:50:04 +02:00
|
|
|
new FieldList(
|
2011-03-22 09:50:26 +01:00
|
|
|
$field
|
|
|
|
),
|
2012-05-15 17:50:04 +02:00
|
|
|
new FieldList(
|
2013-11-14 14:37:24 +01:00
|
|
|
new FormAction('selectlang', _t('CMSMain_left.GO','Go'))
|
2011-03-22 09:50:26 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$form->unsetValidator();
|
2012-05-15 17:50:04 +02:00
|
|
|
$form->addExtraClass('nostyle');
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
return $form;
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
function selectlang($data, $form) {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
/**
|
|
|
|
* Determine if there are more than one languages in our site tree.
|
2016-03-03 23:24:34 +01:00
|
|
|
*
|
2011-03-22 09:50:26 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function MultipleLanguages() {
|
|
|
|
$langs = Translatable::get_existing_content_languages('SiteTree');
|
|
|
|
|
|
|
|
return (count($langs) > 1);
|
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
2011-03-22 09:50:26 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function IsTranslatableEnabled() {
|
2013-02-01 18:42:32 +01:00
|
|
|
return SiteTree::has_extension('Translatable');
|
2011-03-22 09:50:26 +01:00
|
|
|
}
|
2016-03-03 23:24:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects the locale into a new page on creation.
|
|
|
|
*
|
|
|
|
* @param SiteTree $record
|
|
|
|
* @param Form $form
|
|
|
|
*/
|
|
|
|
public function updateDoAdd(SiteTree $record, Form $form) {
|
|
|
|
$data = $form->getData();
|
|
|
|
if(!isset($data['Locale'])) {
|
|
|
|
$data['Locale'] = Translatable::get_current_locale();
|
|
|
|
}
|
|
|
|
$record->Locale = $data['Locale'];
|
|
|
|
}
|
|
|
|
|
2013-10-28 01:26:59 +01:00
|
|
|
}
|