Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it works with Translatable module #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions code/ModelAdmin/SEOEditorAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function init()
parent::init();
Requirements::css('silverstripe-seo-editor/css/seo-editor.css');
Requirements::javascript('silverstripe-seo-editor/javascript/seo-editor.js');

if(class_exists('Translatable')) Requirements::javascript('silverstripe-seo-editor/javascript/SEOEditor.Translatable.js');
}

/**
Expand All @@ -62,6 +64,25 @@ public function getSearchContext()
CheckboxField::create('RemoveEmptyMetaTitles', 'Remove Empty MetaTitles'),
CheckboxField::create('RemoveEmptyMetaDescriptions', 'Remove Empty MetaDescriptions')
);

if(class_exists('Translatable')) {
$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')
);
$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())
);
}
$fields->insertBefore($field, 'Title');
}

$context->setFields($fields);
$filters = array(
Expand All @@ -73,7 +94,8 @@ public function getSearchContext()
$context->setFilters($filters);

// Namespace fields, for easier detection if a search is present
foreach ($context->getFields() as $field) $field->setName(sprintf('q[%s]', $field->getName()));
foreach ($context->getFields() as $field)
if($field->getName() != 'locale') $field->setName(sprintf('q[%s]', $field->getName()));
foreach ($context->getFilters() as $filter) $filter->setFullName(sprintf('q[%s]', $filter->getFullName()));

return $context;
Expand Down Expand Up @@ -256,4 +278,4 @@ private function removeEmptyAttributes(SS_List $list, $type)
}


}
}
30 changes: 30 additions & 0 deletions javascript/SEOEditor.Translatable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* File: SEOEditor.Translatable.js
*/
(function($) {
$.entwine('ss', function($){
/**
* whenever a new value is selected, reload the whole CMS in the new locale
*/
$('.SEOEditorAdmin #Form_SearchForm_locale').entwine({
onchange: function(e) {
// Get new locale code
locale = {locale: $(e.target).val()};

// Check existing url
search = /locale=[^&]*/;
url = document.location.href;
if(url.match(search)) {
// Replace locale code
url = url.replace(search, $.param(locale));
} else {
// Add locale code
url = $.path.addSearchParams(url, locale);
}
$('.cms-container').loadPanel(url);
return false;
}
});

});
}(jQuery));
11 changes: 9 additions & 2 deletions javascript/seo-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

var $this = $(this);
var id = $this.closest('tr').attr('data-id');
var url = $this.closest('.ss-gridfield').attr('data-url') + "/update/" + id;
var preURL = $this.closest('.ss-gridfield').attr('data-url');
var url;

if(preURL.indexOf('?') != -1)
url = preURL.substr(0, preURL.indexOf('?')) + "/update/" + id + preURL.substr(preURL.indexOf('?'));
else
url = preURL + "/update/" + id;

var data = $this.attr('name') + '=' + $(this).val();

statusMessage('Saving changes', 'good');
Expand Down Expand Up @@ -40,4 +47,4 @@

});

}(jQuery));
}(jQuery));