(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@60265 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-08-10 23:29:30 +00:00
parent 7ec72c8254
commit 66b916abc5
5 changed files with 124 additions and 33 deletions

View File

@ -190,18 +190,48 @@ abstract class ModelAdmin extends LeftAndMain {
public function ImportForm() {
$models = $this->getManagedModels();
$modelMap = array();
foreach($this->getModelImporters() as $modelName => $spec) $modelMap[$modelName] = singleton($modelName)->singular_name();
$importers = $this->getModelImporters();
if(!$importers) return false;
foreach($importers as $modelName => $importerClass) {
$modelMap[$modelName] = singleton($modelName)->singular_name();
}
$fields = new FieldSet(
new DropdownField('ClassName', 'Type', $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' => $modelName,
'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",
new FieldSet(
new DropdownField('ClassName', 'Type', $modelMap),
new FileField('_CsvFile', false)
),
new FieldSet(
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
)
$fields,
$actions
);
return $form;
}
@ -223,7 +253,10 @@ abstract class ModelAdmin extends LeftAndMain {
* @param unknown_type $request
*/
function import($data, $form, $request) {
$loader = new DemandPointBulkLoader($data['ClassName']);
$importers = $this->getModelImporters();
$importerClass = $importers[$data['ClassName']];
$loader = new $importerClass($data['ClassName']);
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
$resultsCount = ($results) ? $results->Count() : 0;
@ -275,9 +308,9 @@ abstract class ModelAdmin extends LeftAndMain {
*/
protected function getModelImporters() {
$importers = $this->stat('model_importers');
// fallback to all defined models if not explicitly defined
if(!$importers) {
if(is_null($importers)) {
$models = $this->getManagedModels();
foreach($models as $modelName) $importers[$modelName] = 'CsvBulkLoader';
}
@ -374,23 +407,42 @@ class ModelAdmin_CollectionController extends Controller {
* Action to render a data object collection, using the model context to provide filters
* and paging.
*
* @todo push this HTML structure out into separate template
* @return string
*/
function search($request) {
$filteredParams = $request->getVars();
unset($filteredParams['ctf']);
$form = $this->ResultsForm();
return $form->forTemplate();
}
/**
* Gets the search query generated on the SearchContext from
* {@link DataObject::getDefaultSearchContext()},
* and the current GET parameters on the request.
*
* @return SQLQuery
*/
function getSearchQuery() {
$context = singleton($this->modelClass)->getDefaultSearchContext();
return $context->getQuery($this->request->getVars());
}
/**
* Shows results from the "search" action in a TableListField.
*
* @return Form
*/
function ResultsForm() {
$model = singleton($this->modelClass);
$context = $model->getDefaultSearchContext();
$length = $this->parentController->stat('page_length');
$results = $context->getResults($request->getVars());
$summaryFields = $model->summaryFields();
$tf = new TableListField(
$this->modelClass,
$this->modelClass,
$summaryFields
);
$tf->setCustomSourceItems($results);
$tf->setPageSize($length);
$tf->setCustomQuery($this->getSearchQuery());
$tf->setPageSize($this->parentController->stat('page_length'));
$tf->setShowPagination(true);
$tf->setPermissions(array('view'));
$url = '<a href=\"' . $this->Link() . '/$ID/edit\">$value</a>';
@ -407,17 +459,18 @@ class ModelAdmin_CollectionController extends Controller {
),
new FieldSet()
);
// HACK to preserve search parameters on TableField
// ajax actions like pagination
$filteredParams = $this->request->getVars();
unset($filteredParams['ctf']);
unset($filteredParams['url']);
unset($filteredParams['action_search']);
$tf->setExtraLinkParams($filteredParams);
if(isset($_POST['paginate']) && $_POST['paginate']==1){
$response = $tf->renderWith("TableListField");
FormResponse::update_dom_id($tf->id(), $response);
FormResponse::set_non_ajax_content($response);
return FormResponse::respond();
}else{
return $form->forTemplate();
}
return $form;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -28,10 +28,10 @@ body.ModelAdmin #SearchForm_holder {
body.ModelAdmin #ResultTable_holder {
overflow:auto;
}
body.ModelAdmin #right{
overflow:auto;
}
body.ModelAdmin #right{
overflow:auto;
}
body.ModelAdmin #right table.results {
@ -59,4 +59,10 @@ body.ModelAdmin #right table.results td.active {
body.ModelAdmin #right .tab {
height: 450px; /* @todo add dynamic resizing */
}
body.ModelAdmin #Form_ImportForm_holder dt {
float: left;
width: 10em;
margin-right: 2em;
}

View File

@ -119,6 +119,15 @@ jQuery(document).ready(function() {
return false;
});
/**
* Toggle import specifications
*/
jQuery('#Form_ImportForm_holder .spec .details').hide();
jQuery('#Form_ImportForm_holder .spec a.detailsLink').click(function() {
jQuery('#' + jQuery(this).attr('href').replace(/.*#/,'')).toggle();
return false;
});
});
/**

View File

@ -0,0 +1,21 @@
<div class="spec" id="SpecFor{$ModelName}">
<a href="#SpecDetailsFor{$ModelName}" class="detailsLink"><% sprintf(_t('IMPORTSPECLINK', 'Show Specification for %s'),$ModelName) %></a>
<div class="details" id="SpecDetailsFor{$ModelName}">
<h4><% sprintf(_t('IMPORTSPECTITLE', 'Specification for %s'),$ModelName) %></h4>
<h5><% _t('IMPORTSPECFIELDS', 'Database columns') %></h5>
<% control Fields %>
<dl>
<dt><em>$Name</em></dt>
<dd>$Description</dd>
</dl>
<% end_control %>
<h5><% _t('IMPORTSPECRELATIONS', 'Relations') %></h5>
<% control Relations %>
<dl>
<dt><em>$Name</em></dt>
<dd>$Description</dd>
</dl>
<% end_control %>
</div>
</div>

View File

@ -3,14 +3,16 @@
<div id="AddForm_holder" class="lefttop">
<ul class="tabstrip">
<li class="first"><a href="#Form_ManagedModelsSelect_holder"><% _t('ADD_TAB_HEADER','Add') %></a></li>
<li class="first"><a href="#Form_ImportForm_holder"><% _t('IMPORT_TAB_HEADER','Import') %></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>
<h2><% _t('SEARCHLISTINGS','Search Listings') %></h2>
<div id="SearchForm_holder" class="leftbottom">