BUGFIX: validate file in import from CSV form

MINOR: Clear DB checkbox unchecked by default
ENHANCEMENT: Ability to hide form by className or for the whole ModelAdmin (from r105732)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@112481 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-15 02:33:30 +00:00
parent 71209b7402
commit bc4c4ff416

View File

@ -95,6 +95,13 @@ abstract class ModelAdmin extends LeftAndMain {
*/
private $currentModel = false;
/**
* Change this variable if you don't want the Import from CSV form to appear.
* This variable can be a boolean or an array.
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClasstwo')
*/
public $showImportForm = true;
/**
* List of all {@link DataObject}s which can be imported through
* a subclass of {@link BulkLoader} (mostly CSV data).
@ -312,6 +319,8 @@ class ModelAdmin_CollectionController extends Controller {
public $parentController;
protected $modelClass;
public $showImportForm = null;
static $url_handlers = array(
'$Action' => 'handleActionOrID'
);
@ -438,6 +447,14 @@ class ModelAdmin_CollectionController extends Controller {
return $form;
}
/**
* Checks if a CSV import form should be generated by a className criteria or in general for ModelAdmin.
*/
function showImportForm() {
if($this->showImportForm === null) return $this->parentController->showImportForm;
else return $this->showImportForm;
}
/**
* Generate a CSV import form for a single {@link DataObject} subclass.
*
@ -445,6 +462,8 @@ class ModelAdmin_CollectionController extends Controller {
*/
public function ImportForm() {
$modelName = $this->modelClass;
// check if a import form should be generated
if(!$this->showImportForm() || (is_array($this->showImportForm()) && !in_array($modelName,$this->showImportForm()))) return false;
$importers = $this->parentController->getModelImporters();
if(!$importers || !isset($importers[$modelName])) return false;
@ -474,7 +493,7 @@ class ModelAdmin_CollectionController extends Controller {
))->renderWith('ModelAdmin_ImportSpec');
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
$fields->push(new CheckboxField('EmptyBeforeImport', 'Clear Database before import', true));
$fields->push(new CheckboxField('EmptyBeforeImport', 'Clear Database before import', false));
$actions = new FieldSet(
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
@ -506,14 +525,17 @@ class ModelAdmin_CollectionController extends Controller {
* @param SS_HTTPRequest $request
*/
function import($data, $form, $request) {
$modelName = $data['ClassName'];
if(!$this->showImportForm() || (is_array($this->showImportForm()) && !in_array($modelName,$this->showImportForm()))) return false;
$importers = $this->parentController->getModelImporters();
$importerClass = $importers[$modelName];
$loader = new $importerClass($data['ClassName']);
// File wasn't properly uploaded, show a reminder to the user
if(empty($_FILES['_CsvFile']['tmp_name'])) {
if(empty($_FILES['_CsvFile']['tmp_name']) || $_FILES['_CsvFile']['type'] != 'text/csv' || file_get_contents($_FILES['_CsvFile']['tmp_name']) == '') {
$form->sessionMessage(_t('ModelAdmin.NOCSVFILE', 'Please browse for a CSV file to import'), 'good');
$this->redirectBack();
return false;