mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +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 git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@105732 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
df869f9da7
commit
6efa5a8554
@ -94,6 +94,13 @@ abstract class ModelAdmin extends LeftAndMain {
|
|||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
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
|
||||||
@ -310,6 +317,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'
|
||||||
);
|
);
|
||||||
@ -436,6 +445,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.
|
||||||
*
|
*
|
||||||
@ -443,6 +460,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;
|
||||||
|
|
||||||
@ -472,7 +491,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'))
|
||||||
@ -504,24 +523,27 @@ 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');
|
||||||
Director::redirectBack();
|
Director::redirectBack();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import
|
if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import
|
||||||
$loader->deleteExistingRecords = true;
|
$loader->deleteExistingRecords = true;
|
||||||
}
|
}
|
||||||
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
|
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
|
||||||
|
|
||||||
$message = '';
|
$message = '';
|
||||||
if($results->CreatedCount()) $message .= sprintf(
|
if($results->CreatedCount()) $message .= sprintf(
|
||||||
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s records."),
|
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s records."),
|
||||||
|
Loading…
Reference in New Issue
Block a user