FIX Santise model classes in ModelAdmin

As ModelAdmin puts classes directly into links, a namespaced class
has its \s turned into /s, breaking the links. This escapes them by
converting each \ to a -, then converting them back when loading.
This commit is contained in:
Simon Welsh 2012-06-29 13:52:33 +12:00
parent 5271504feb
commit 8d670283c9

View File

@ -103,7 +103,7 @@ abstract class ModelAdmin extends LeftAndMain {
$models = $this->getManagedModels(); $models = $this->getManagedModels();
if($this->request->param('ModelClass')) { if($this->request->param('ModelClass')) {
$this->modelClass = $this->request->param('ModelClass'); $this->modelClass = $this->unsanitiseClassName($this->request->param('ModelClass'));
} else { } else {
reset($models); reset($models);
$this->modelClass = key($models); $this->modelClass = key($models);
@ -118,7 +118,7 @@ abstract class ModelAdmin extends LeftAndMain {
} }
public function Link($action = null) { public function Link($action = null) {
if(!$action) $action = $this->modelClass; if(!$action) $action = $this->sanitiseClassName($this->modelClass);
return parent::Link($action); return parent::Link($action);
} }
@ -127,7 +127,7 @@ abstract class ModelAdmin extends LeftAndMain {
$exportButton = new GridFieldExportButton('before'); $exportButton = new GridFieldExportButton('before');
$exportButton->setExportColumns($this->getExportFields()); $exportButton->setExportColumns($this->getExportFields());
$listField = GridField::create( $listField = GridField::create(
$this->modelClass, $this->sanitiseClassName($this->modelClass),
false, false,
$list, $list,
$fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length')) $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
@ -150,7 +150,7 @@ abstract class ModelAdmin extends LeftAndMain {
); );
$form->addExtraClass('cms-edit-form cms-panel-padded center'); $form->addExtraClass('cms-edit-form cms-panel-padded center');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
$form->setFormAction(Controller::join_links($this->Link($this->modelClass), 'EditForm')); $form->setFormAction(Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'EditForm'));
$form->setAttribute('data-pjax-fragment', 'CurrentForm'); $form->setAttribute('data-pjax-fragment', 'CurrentForm');
$this->extend('updateEditForm', $form); $this->extend('updateEditForm', $form);
@ -199,7 +199,7 @@ abstract class ModelAdmin extends LeftAndMain {
new RequiredFields() new RequiredFields()
); );
$form->setFormMethod('get'); $form->setFormMethod('get');
$form->setFormAction($this->Link($this->modelClass)); $form->setFormAction($this->Link($this->sanitiseClassName($this->modelClass)));
$form->addExtraClass('cms-search-form'); $form->addExtraClass('cms-search-form');
$form->disableSecurityToken(); $form->disableSecurityToken();
$form->loadDataFrom($this->request->getVars()); $form->loadDataFrom($this->request->getVars());
@ -234,7 +234,7 @@ abstract class ModelAdmin extends LeftAndMain {
$forms->push(new ArrayData(array ( $forms->push(new ArrayData(array (
'Title' => $options['title'], 'Title' => $options['title'],
'ClassName' => $class, 'ClassName' => $class,
'Link' => $this->Link($class), 'Link' => $this->Link($this->sanitiseClassName($class)),
'LinkOrCurrent' => ($class == $this->modelClass) ? 'current' : 'link' 'LinkOrCurrent' => ($class == $this->modelClass) ? 'current' : 'link'
))); )));
} }
@ -242,6 +242,22 @@ abstract class ModelAdmin extends LeftAndMain {
return $forms; return $forms;
} }
/**
* Sanitise a model class' name for inclusion in a link
* @return string
*/
protected function sanitiseClassName($class) {
return str_replace('\\', '-', $class);
}
/**
* Unsanitise a model class' name from a URL param
* @return string
*/
protected function unsanitiseClassName($class) {
return str_replace('-', '\\', $class);
}
/** /**
* @return array Map of class name to an array of 'title' (see {@link $managed_models}) * @return array Map of class name to an array of 'title' (see {@link $managed_models})
*/ */
@ -350,7 +366,7 @@ abstract class ModelAdmin extends LeftAndMain {
$fields, $fields,
$actions $actions
); );
$form->setFormAction(Controller::join_links($this->Link($this->modelClass), 'ImportForm')); $form->setFormAction(Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'ImportForm'));
$this->extend('updateImportForm', $form); $this->extend('updateImportForm', $form);
@ -419,7 +435,7 @@ abstract class ModelAdmin extends LeftAndMain {
// Show the class name rather than ModelAdmin title as root node // Show the class name rather than ModelAdmin title as root node
$models = $this->getManagedModels(); $models = $this->getManagedModels();
$items[0]->Title = $models[$this->modelClass]['title']; $items[0]->Title = $models[$this->modelClass]['title'];
$items[0]->Link = $this->Link($this->modelClass); $items[0]->Link = $this->Link($this->sanitiseClassName($this->modelClass));
return $items; return $items;
} }