Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thezenmonkey/24e869ca1c5c6d92ebf604dc86d02fd9 to your computer and use it in GitHub Desktop.
Save thezenmonkey/24e869ca1c5c6d92ebf604dc86d02fd9 to your computer and use it in GitHub Desktop.
Controller to Manage Batch Translation
<?php
/**
* Controller to process translate pages batch action.
*
* @package batchactionsplus
*/
class CMSBatchAction_TransalteToController extends LeftAndMain {
private static $url_segment = 'batchtranslateto';
private static $menu_title = 'Translate to';
private static $required_permission_codes = false;
private static $allowed_actions = array (
'TranslatePagesForm',
'index'
);
/**
* Function called by AJAX from LeftAndMain.TranslateTo.js
* and from $this->doTranslatePages().
*
* @param Request $request, String $pageIDs
* @return Form rendered within CMSDialog
*/
public function index($request, $pageIDs = null) {
$form = $this->TranslatePagesForm($pageIDs);
return $this->customise(array(
'Content' => ' ',
'Form' => $form
))->renderWith('CMSDialog');
}
/**
* Presents a form to select a new parent for pages selected with batch actions.
*
* @param string $pageIDs | null
* @return Form $form
*/
public function TranslatePagesForm($pageIDs = null, $locale = null){
//Versioned::reading_stage('Stage'); // Needs this for changes to effect draft tree
$action = FormAction::create('doTranslatePages', 'Create Translation')
->setUseButtonTag('true')
->addExtraClass('ss-ui-button ss-ui-action-constructive batch-form-actions')
->setUseButtonTag(true);
$actions = FieldList::create($action);
$allFields = new CompositeField();
$allFields->addExtraClass('batch-form-body');
if($locale == null){
$locale = $this->getRequest()->getVar('Locale');
} else{
$allFields->push(new LiteralField("ErrorParent",'<p class="message bad">Translateable Error</p>'));
}
if($pageIDs == null){
$pageIDs = $this->getRequest()->getVar('PageIDs');
} else{
$allFields->push(new LiteralField("ErrorParent",'<p class="message bad">Invalid parent selected, please choose another</p>'));
}
$allFields->push(new HiddenField("PageIDs","PageIDs", $pageIDs));
$allFields->push(new LanguageDropdownField("Language", "Choose Translation Language"));
$headings = new CompositeField(
new LiteralField(
'Heading',
sprintf('<h3 class="">%s</h3>', _t('HtmlEditorField.MOVE', 'Create Transaltion...')))
);
$headings->addExtraClass('cms-content-header batch-pages');
$fields = new FieldList(
$headings,
$allFields
);
$form = Form::create(
$this,
'TranslatePagesForm',
$fields,
$actions
);
return $form;
}
/**
* Handles the translation of pages within the sitetree
*
* @param array $data, Form $form
* @return boolean | index function
**/
public function doTranslatePages($data, $form){
$pagesArray = explode(',', $data['PageIDs']);
Translatable::set_current_locale($data['Locale']);
// for each $pageID (needs to be an array)
foreach($pagesArray as $pageID){
$page = SiteTree::get()->byID($pageID);
$page->createTranslation($data['Language']);
}
Translatable::set_current_locale($data['Language']);
return '<input type="hidden" class="close-dialog" />';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment