mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
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:
parent
71209b7402
commit
bc4c4ff416
@ -95,6 +95,13 @@ abstract class ModelAdmin extends LeftAndMain {
|
|||||||
*/
|
*/
|
||||||
private $currentModel = false;
|
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
|
* List of all {@link DataObject}s which can be imported through
|
||||||
* a subclass of {@link BulkLoader} (mostly CSV data).
|
* a subclass of {@link BulkLoader} (mostly CSV data).
|
||||||
@ -312,6 +319,8 @@ class ModelAdmin_CollectionController extends Controller {
|
|||||||
public $parentController;
|
public $parentController;
|
||||||
protected $modelClass;
|
protected $modelClass;
|
||||||
|
|
||||||
|
public $showImportForm = null;
|
||||||
|
|
||||||
static $url_handlers = array(
|
static $url_handlers = array(
|
||||||
'$Action' => 'handleActionOrID'
|
'$Action' => 'handleActionOrID'
|
||||||
);
|
);
|
||||||
@ -438,6 +447,14 @@ class ModelAdmin_CollectionController extends Controller {
|
|||||||
return $form;
|
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.
|
* Generate a CSV import form for a single {@link DataObject} subclass.
|
||||||
*
|
*
|
||||||
@ -445,6 +462,8 @@ class ModelAdmin_CollectionController extends Controller {
|
|||||||
*/
|
*/
|
||||||
public function ImportForm() {
|
public function ImportForm() {
|
||||||
$modelName = $this->modelClass;
|
$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();
|
$importers = $this->parentController->getModelImporters();
|
||||||
if(!$importers || !isset($importers[$modelName])) return false;
|
if(!$importers || !isset($importers[$modelName])) return false;
|
||||||
|
|
||||||
@ -474,7 +493,7 @@ class ModelAdmin_CollectionController extends Controller {
|
|||||||
))->renderWith('ModelAdmin_ImportSpec');
|
))->renderWith('ModelAdmin_ImportSpec');
|
||||||
|
|
||||||
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
|
$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(
|
$actions = new FieldSet(
|
||||||
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
|
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
|
||||||
@ -506,14 +525,17 @@ class ModelAdmin_CollectionController extends Controller {
|
|||||||
* @param SS_HTTPRequest $request
|
* @param SS_HTTPRequest $request
|
||||||
*/
|
*/
|
||||||
function import($data, $form, $request) {
|
function import($data, $form, $request) {
|
||||||
|
|
||||||
$modelName = $data['ClassName'];
|
$modelName = $data['ClassName'];
|
||||||
|
|
||||||
|
if(!$this->showImportForm() || (is_array($this->showImportForm()) && !in_array($modelName,$this->showImportForm()))) return false;
|
||||||
$importers = $this->parentController->getModelImporters();
|
$importers = $this->parentController->getModelImporters();
|
||||||
$importerClass = $importers[$modelName];
|
$importerClass = $importers[$modelName];
|
||||||
|
|
||||||
$loader = new $importerClass($data['ClassName']);
|
$loader = new $importerClass($data['ClassName']);
|
||||||
|
|
||||||
// File wasn't properly uploaded, show a reminder to the user
|
// 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');
|
$form->sessionMessage(_t('ModelAdmin.NOCSVFILE', 'Please browse for a CSV file to import'), 'good');
|
||||||
$this->redirectBack();
|
$this->redirectBack();
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user