(merged from branches/roa. use "svn log -c <changeset> -g <module-svn-path>" for detailed commit message)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@60203 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-08-09 02:00:40 +00:00
parent 7e791ea681
commit 05ad3c8e1c

View File

@ -1,13 +1,13 @@
<?php <?php
/** /**
* Generates a three-pane UI for editing one or many model classes, * Generates a three-pane UI for editing model classes,
* with an automatically generated search panel, tabular results * with an automatically generated search panel, tabular results
* and edit forms. * and edit forms.
* Relies on data such as {@link DataObject::$db} and {@DataObject::getCMSFields()} * Relies on data such as {@link DataObject::$db} and {@DataObject::getCMSFields()}
* to scaffold interfaces "out of the box", while at the same time providing * to scaffold interfaces "out of the box", while at the same time providing
* flexibility to customize the default output. * flexibility to customize the default output.
* *
* Add a route: * Add a route (note - this doc is not currently in sync with the code, need to update)
* <code> * <code>
* Director::addRules(50, array('admin/mymodel/$Class/$Action/$ID' => 'MyModelAdmin')); * Director::addRules(50, array('admin/mymodel/$Class/$Action/$ID' => 'MyModelAdmin'));
* </code> * </code>
@ -67,18 +67,15 @@ abstract class ModelAdmin extends LeftAndMain {
/** /**
* Create empty edit form (scaffolded from DataObject->getCMSFields() by default). * Create empty edit form (scaffolded from DataObject->getCMSFields() by default).
* Can be called either through {@link AddForm()} or directly through URL: * Can be called either through {@link AddForm()} or directly through URL:
* "/myadminroute/MyModelClass/add" * "/myadminroute/add/MyModelClass"
*
* @param array $data
* @param Form $form
*/ */
public function add($data, $form) { public function add($data) {
$className = (isset($data['ClassName'])) ? $data['ClassName'] : $this->urlParams['ClassName']; $className = (isset($data['ClassName'])) ? $data['ClassName'] : $this->urlParams['ClassName'];
if(!isset($className) || !in_array($data['ClassName'], $this->getManagedModels())) return false; if(!isset($className) || !in_array($data['ClassName'], $this->getManagedModels())) return false;
return $this->customise(array( $form = $this->getEditForm($data['ClassName']);
'EditForm' => $this->getEditForm($data['ClassName']) return $form->forTemplate();
))->renderWith('LeftAndMain');
} }
/** /**
@ -115,13 +112,9 @@ abstract class ModelAdmin extends LeftAndMain {
$form = new Form( $form = new Form(
$this, $this,
'AddForm', 'AddForm_add',
new FieldSet( new FieldSet(
new DropdownField( new DropdownField('ClassName', 'Type', $modelMap)
'ClassName',
'Type',
$modelMap
)
), ),
new FieldSet( new FieldSet(
new FormAction('add', _t('GenericDataAdmin.CREATE')) new FormAction('add', _t('GenericDataAdmin.CREATE'))
@ -249,27 +242,37 @@ abstract class ModelAdmin extends LeftAndMain {
$form = new Form( $form = new Form(
$this, $this,
"SearchForm_{$modelClass}", "SearchForm_search_{$modelClass}",
$context->getSearchFields(), $context->getSearchFields(),
new FieldSet( new FieldSet(
new FormAction('search', _t('MemberTableField.SEARCH')) new FormAction('search', _t('MemberTableField.SEARCH'))
) )
); );
//can't set generic search form as GET because of weirdness with FormObjectLink
//$form->setFormMethod('get'); //$form->setFormMethod('get');
return $form; return $form;
} }
/** /**
* Another counter intuitive hoop to jump through, as the Form constructor * Overwrite the default form action with the path to the DataObject filter
* is still thoroughly confusing the hell out of me. * (relies on the search form having the specifically named id as 'SearchForm_DataObject')
*
* @todo extract hard-coded prefix and generate from Director settings
*/ */
function FormObjectLink($value) { function FormObjectLink($formName) {
$value = str_replace('SearchForm_', '', $value); $segments = explode('_', $formName);
return "admin/crm/$value/search"; if (count($segments) == 3) {
return Director::absoluteBaseURL()."admin/crm/{$segments[1]}/{$segments[2]}";
} elseif (count($segments) == 2) {
return Director::absoluteBaseURL()."admin/crm/{$segments[1]}";
} else {
return "?executeForm=$formName";
}
} }
/** /**
* Action to execute a search using the model context * Action to render a data object collection, using the model context to provide filters
* and paging.
*/ */
function search() { function search() {
$className = $this->urlParams['ClassName']; $className = $this->urlParams['ClassName'];
@ -283,7 +286,7 @@ abstract class ModelAdmin extends LeftAndMain {
echo "<table>"; echo "<table>";
foreach($results as $row) { foreach($results as $row) {
$uri = Director::absoluteBaseUrl(); $uri = Director::absoluteBaseUrl();
echo "<tr id=\"{$uri}admin/crm/$className/view/$row->ID\">"; echo "<tr id=\"{$uri}admin/crm/view/$className/$row->ID\">";
foreach($model->searchableFields() as $key=>$val) { foreach($model->searchableFields() as $key=>$val) {
echo "<td>"; echo "<td>";
echo $row->getField($key); echo $row->getField($key);
@ -296,8 +299,13 @@ abstract class ModelAdmin extends LeftAndMain {
echo "<p>No results found</p>"; echo "<p>No results found</p>";
} }
} }
die();
} }
/**
* View a generic model using a form object to render a partial HTML
* fragment to be embedded via Ajax calls.
*/
function view() { function view() {
$className = $this->urlParams['ClassName']; $className = $this->urlParams['ClassName'];
$ID = $this->urlParams['ID']; $ID = $this->urlParams['ID'];
@ -308,10 +316,14 @@ abstract class ModelAdmin extends LeftAndMain {
$fields = $model->getCMSFields(); $fields = $model->getCMSFields();
$form = new Form($this, $className, $fields, new FieldSet()); $actions = new FieldSet(
new FormAction('save', 'Save')
);
$form = new Form($this, $className, $fields, $actions);
$form->makeReadonly(); $form->makeReadonly();
$form->loadNonBlankDataFrom($model); $form->loadNonBlankDataFrom($model);
echo $form->forTemplate(); return $form->forTemplate();
} }
} }