IMPROVEMENT moved managed models' forms to one panel (ticket #2898)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@65289 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Saophalkun Ponlu 2008-11-05 08:55:07 +00:00
parent 6779388b9e
commit 86017a5ae9
5 changed files with 151 additions and 166 deletions

View File

@ -166,142 +166,12 @@ abstract class ModelAdmin extends LeftAndMain {
} }
/** /**
* Allows to choose which record needs to be created. * Returns managed models' create, search, and import forms
*
* @return Form
*/
protected function ManagedModelsSelect() {
$models = $this->getManagedModels();
$modelMap = array();
foreach($models as $modelName) {
if ($this->hasMethod('alternatePermissionCheck')) {
if ($this->alternatePermissionCheck()) {
$modelMap[$modelName] = singleton($modelName)->singular_name();
}
}
else {
if(singleton($modelName)->canCreate(Member::currentUser())) $modelMap[$modelName] = singleton($modelName)->singular_name();
}
}
$form = new Form(
$this,
"ManagedModelsSelect",
new FieldSet(
new DropdownField('ClassName', _t('ModelAdmin.CLASSTYPE', 'Type', PR_MEDIUM, 'Type of object to add'), $modelMap)
),
new FieldSet(
new FormAction('add', _t('ModelAdmin.CREATE', 'Create'))
)
);
$form->setFormMethod('get');
return $form;
}
/**
* Generate a CSV import form with an option to select
* one of the "importable" models specified through {@link self::$model_importers}.
*
* @return Form
*/
public function ImportForm() {
$models = $this->getManagedModels();
$modelMap = array();
$importers = $this->getModelImporters();
if(!$importers) return false;
foreach($importers as $modelName => $importerClass) {
$modelMap[$modelName] = singleton($modelName)->singular_name();
}
$fields = new FieldSet(
new DropdownField('ClassName', _t('ModelAdmin.CLASSTYPE'), $modelMap),
new FileField('_CsvFile', false)
);
// get HTML specification for each import (column names etc.)
foreach($importers as $modelName => $importerClass) {
$importer = new $importerClass($modelName);
$spec = $importer->getImportSpec();
$specFields = new DataObjectSet();
foreach($spec['fields'] as $name => $desc) {
$specFields->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
}
$specRelations = new DataObjectSet();
foreach($spec['relations'] as $name => $desc) {
$specRelations->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
}
$specHTML = $this->customise(array(
'ModelName' => singleton($modelName)->i18n_singular_name(),
'Fields' => $specFields,
'Relations' => $specRelations,
))->renderWith('ModelAdmin_ImportSpec');
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
}
$actions = new FieldSet(
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
);
$form = new Form(
$this,
"ImportForm",
$fields,
$actions
);
return $form;
}
function add($data, $form, $request) {
$className = $request->requestVar("ClassName");
return $this->$className()->add($request);
}
/**
* Imports the submitted CSV file based on specifications given in
* {@link self::model_importers}.
* Redirects back with a success/failure message.
*
* @todo Figure out ajax submission of files via jQuery.form plugin
*
* @param unknown_type $data
* @param unknown_type $form
* @param unknown_type $request
*/
function import($data, $form, $request) {
$importers = $this->getModelImporters();
$importerClass = $importers[$data['ClassName']];
$loader = new $importerClass($data['ClassName']);
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
$message = '';
if($results->CreatedCount()) $message .= sprintf(
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s records."),
$results->CreatedCount()
);
if($results->UpdatedCount()) $message .= sprintf(
_t('ModelAdmin.UPDATEDRECORDS', "Updated %s records."),
$results->UpdatedCount()
);
if($results->DeletedCount()) $message .= sprintf(
_t('ModelAdmin.DELETEDRECORDS', "Deleted %s records."),
$results->DeletedCount()
);
if(!$results->CreatedCount() && !$results->UpdatedCount()) $message .= _t('ModelAdmin.NOIMPORT', "Nothing to import");
Session::setFormMessage('Form_ImportForm', $message, 'good');
Director::redirect($_SERVER['HTTP_REFERER'] . '#Form_ImportForm_holder');
}
/**
*
* @uses SearchContext * @uses SearchContext
* @uses SearchFilter * @uses SearchFilter
* @return Form * @return DataObjectSet of forms
*/ */
protected function getSearchForms() { protected function getModelForms() {
$modelClasses = $this->getManagedModels(); $modelClasses = $this->getManagedModels();
$forms = new DataObjectSet(); $forms = new DataObjectSet();
@ -309,7 +179,9 @@ abstract class ModelAdmin extends LeftAndMain {
$this->$modelClass()->SearchForm(); $this->$modelClass()->SearchForm();
$forms->push(new ArrayData(array( $forms->push(new ArrayData(array(
'Form' => $this->$modelClass()->SearchForm(), 'SearchForm' => $this->$modelClass()->SearchForm(),
'CreateForm' => $this->$modelClass()->CreateForm(),
'ImportForm' => $this->$modelClass()->ImportForm(),
'Title' => singleton($modelClass)->singular_name(), 'Title' => singleton($modelClass)->singular_name(),
'ClassName' => $modelClass, 'ClassName' => $modelClass,
))); )));
@ -340,7 +212,7 @@ abstract class ModelAdmin extends LeftAndMain {
* *
* @return array * @return array
*/ */
protected function getModelImporters() { function getModelImporters() {
$importers = $this->stat('model_importers'); $importers = $this->stat('model_importers');
// fallback to all defined models if not explicitly defined // fallback to all defined models if not explicitly defined
@ -351,6 +223,7 @@ abstract class ModelAdmin extends LeftAndMain {
return $importers; return $importers;
} }
} }
/** /**
@ -446,6 +319,117 @@ class ModelAdmin_CollectionController extends Controller {
return $form; return $form;
} }
/**
* Create a form that consists of one button
* that directs to a give model's Add form
*/
public function CreateForm() {
$modelName = $this->modelClass;
if ($this->hasMethod('alternatePermissionCheck')) {
if (!$this->alternatePermissionCheck()) return false;
}
else {
if (!singleton($modelName)->canCreate(Member::currentUser())) return false;
}
$buttonLabel = sprintf(_t('ModelAdmin.CREATEBUTTON', "Create a %s", PR_MEDIUM, "Create a new instance from a model class"), singleton($modelName)->i18n_singular_name());
$actions = new FieldSet(
new FormAction('add', $buttonLabel)
);
return new Form($this, "CreateForm", new FieldSet(), $actions);
}
/**
* Generate a CSV import form for a single {@link DataObject} subclass.
*
* @return Form
*/
public function ImportForm() {
$modelName = $this->modelClass;
$importers = $this->parentController->getModelImporters();
if(!$importers) return false;
$fields = new FieldSet(
new HiddenField('ClassName', _t('ModelAdmin.CLASSTYPE'), $modelName),
new FileField('_CsvFile', false)
);
// get HTML specification for each import (column names etc.)
$importerClass = $importers[$modelName];
$importer = new $importerClass($modelName);
$spec = $importer->getImportSpec();
$specFields = new DataObjectSet();
foreach($spec['fields'] as $name => $desc) {
$specFields->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
}
$specRelations = new DataObjectSet();
foreach($spec['relations'] as $name => $desc) {
$specRelations->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
}
$specHTML = $this->customise(array(
'ModelName' => singleton($modelName)->i18n_singular_name(),
'Fields' => $specFields,
'Relations' => $specRelations,
))->renderWith('ModelAdmin_ImportSpec');
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
$actions = new FieldSet(
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
);
$form = new Form(
$this,
"ImportForm",
$fields,
$actions
);
return $form;
}
/**
* Imports the submitted CSV file based on specifications given in
* {@link self::model_importers}.
* Redirects back with a success/failure message.
*
* @todo Figure out ajax submission of files via jQuery.form plugin
*
* @param unknown_type $data
* @param unknown_type $form
* @param unknown_type $request
*/
function import($data, $form, $request) {
$modelName = singleton($data['ClassName'])->i18n_singular_name();
$importers = $this->parentController->getModelImporters();
$importerClass = $importers[$modelName];
$loader = new $importerClass($data['ClassName']);
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
$message = '';
if($results->CreatedCount()) $message .= sprintf(
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s %s."),
$results->CreatedCount()
);
if($results->UpdatedCount()) $message .= sprintf(
_t('ModelAdmin.UPDATEDRECORDS', "Updated %s records."),
$results->UpdatedCount()
);
if($results->DeletedCount()) $message .= sprintf(
_t('ModelAdmin.DELETEDRECORDS', "Deleted %s records."),
$results->DeletedCount()
);
if(!$results->CreatedCount() && !$results->UpdatedCount()) $message .= _t('ModelAdmin.NOIMPORT', "Nothing to import");
Session::setFormMessage('Form_ImportForm', $message, 'good');
Director::redirect($_SERVER['HTTP_REFERER'] . '#Form_ImportForm_holder');
}
/** /**
* Give the flexibilility to show variouse combination of columns in the search result table * Give the flexibilility to show variouse combination of columns in the search result table
*/ */

View File

@ -4,7 +4,8 @@
} }
body.ModelAdmin #left { body.ModelAdmin #left {
width: 260px; width: 270px;
overflow: auto;
} }
body.ModelAdmin #Form_AddForm fieldset { body.ModelAdmin #Form_AddForm fieldset {
@ -100,7 +101,7 @@ body.ModelAdmin #right .tab {
height: 450px; /* @todo add dynamic resizing */ height: 450px; /* @todo add dynamic resizing */
} }
body.ModelAdmin #Form_ImportForm_holder dt { body.ModelAdmin #Form_ImportForm dt {
float: left; float: left;
width: 10em; width: 10em;
margin-right: 2em; margin-right: 2em;
@ -120,3 +121,14 @@ body.ModelAdmin #right .ajaxActions {
right: 30px !important; right: 30px !important;
bottom: 35px !important; bottom: 35px !important;
} }
.tab h3 {
margin-top: 10px;
font-size: 1.2em;
}
form .message {
font-size: 1em;
margin: .3em 0;
padding: 0.3em;
}

View File

@ -70,11 +70,12 @@ $(document).ready(function() {
/** /**
* Submits a search filter query and attaches event handlers * Submits a search filter query and attaches event handlers
* to the response table * to the response table, excluding the import form because
* file ($_FILES) submission doesn't work using AJAX
* *
* @todo use livequery to manage ResultTable click handlers * @todo use livequery to manage ResultTable click handlers
*/ */
$('#SearchForm_holder .tab form').submit(function () { $('#SearchForm_holder .tab form:not(#Form_ImportForm)').submit(function () {
var $form = $(this); var $form = $(this);
$('#ModelAdminPanel').load($(this).attr('action'), $(this).formToArray(), standardStatusHandler(function(result) { $('#ModelAdminPanel').load($(this).attr('action'), $(this).formToArray(), standardStatusHandler(function(result) {
__lastSearch = $form; __lastSearch = $form;
@ -230,8 +231,8 @@ $(document).ready(function() {
/** /**
* Toggle import specifications * Toggle import specifications
*/ */
$('#Form_ImportForm_holder .spec .details').hide(); $('.importSpec .details').hide();
$('#Form_ImportForm_holder .spec a.detailsLink').click(function() { $('.importSpec a.detailsLink').click(function() {
$('#' + $(this).attr('href').replace(/.*#/,'')).toggle(); $('#' + $(this).attr('href').replace(/.*#/,'')).toggle();
return false; return false;
}); });

View File

@ -1,4 +1,4 @@
<div class="spec" id="SpecFor{$ModelName}"> <div class="importSpec" id="SpecFor{$ModelName}">
<a href="#SpecDetailsFor{$ModelName}" class="detailsLink"><% sprintf(_t('IMPORTSPECLINK', 'Show Specification for %s'),$ModelName) %></a> <a href="#SpecDetailsFor{$ModelName}" class="detailsLink"><% sprintf(_t('IMPORTSPECLINK', 'Show Specification for %s'),$ModelName) %></a>
<div class="details" id="SpecDetailsFor{$ModelName}"> <div class="details" id="SpecDetailsFor{$ModelName}">
<h4><% sprintf(_t('IMPORTSPECTITLE', 'Specification for %s'),$ModelName) %></h4> <h4><% sprintf(_t('IMPORTSPECTITLE', 'Specification for %s'),$ModelName) %></h4>

View File

@ -1,9 +1,9 @@
<div id="LeftPane"> <div id="LeftPane">
<h2><% _t('SEARCHLISTINGS','Search Listings') %></h2> <!-- <h2><% _t('SEARCHLISTINGS','Search Listings') %></h2> -->
<div id="SearchForm_holder" class="leftbottom"> <div id="SearchForm_holder" class="leftbottom">
<% if SearchClassSelector = tabs %> <% if SearchClassSelector = tabs %>
<ul class="tabstrip"> <ul class="tabstrip">
<% control SearchForms %> <% control ModelForms %>
<li class="$FirstLast"><a href="#{$Form.Name}_$ClassName">$Title</a></li> <li class="$FirstLast"><a href="#{$Form.Name}_$ClassName">$Title</a></li>
<% end_control %> <% end_control %>
</ul> </ul>
@ -20,29 +20,17 @@
</p> </p>
<% end_if %> <% end_if %>
<% control SearchForms %> <% control ModelForms %>
<div class="tab" id="{$Form.Name}_$ClassName"> <div class="tab" id="{$Form.Name}_$ClassName">
$Form <h3>Create</h3>
$CreateForm
<h3>Search</h3>
$SearchForm
<h3>Import</h3>
$ImportForm
</div> </div>
<% end_control %> <% end_control %>
</div> </div>
<h2><% _t('ADDLISTING','Add Listing') %></h2>
<div id="AddForm_holder" class="lefttop">
<ul class="tabstrip">
<li class="first"><a href="#Form_ManagedModelsSelect_holder"><% _t('ADD_TAB_HEADER','Add') %></a></li>
<% if ImportForm %><li class="first"><a href="#Form_ImportForm_holder"><% _t('IMPORT_TAB_HEADER','Import') %></a></li><% end_if %>
</ul>
<div class="tab" id="Form_ManagedModelsSelect_holder">
$ManagedModelsSelect
</div>
<% if ImportForm %>
<div class="tab" id="Form_ImportForm_holder">
$ImportForm
</div>
<% end_if %>
</div>
<!--
<div id="ResultTable_holder" class="leftbottom">
</div>
-->
</div> </div>