Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 6efa5a8554fb166c5be6e2b126aebdd138268ef5 1 parent df869f9
@cbarberis cbarberis authored sminnee committed
Showing with 26 additions and 4 deletions.
  1. +26 −4 code/ModelAdmin.php
View
30 code/ModelAdmin.php
@@ -94,6 +94,13 @@
* @var string
*/
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
@@ -310,6 +317,8 @@ class ModelAdmin_CollectionController extends Controller {
public $parentController;
protected $modelClass;
+ public $showImportForm = null;
+
static $url_handlers = array(
'$Action' => 'handleActionOrID'
);
@@ -437,12 +446,22 @@ public function CreateForm() {
}
/**
+ * 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.
*
* @return Form
*/
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;
@@ -472,7 +491,7 @@ public function ImportForm() {
))->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'))
@@ -504,24 +523,27 @@ public function ImportForm() {
* @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');
Director::redirectBack();
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;
}
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
-
+
$message = '';
if($results->CreatedCount()) $message .= sprintf(
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s records."),
Please sign in to comment.
Something went wrong with that request. Please try again.