Skip to content

Commit

Permalink
Closes #7: Add support for file uploads. File upload > manual input, …
Browse files Browse the repository at this point in the history
…so any manual input will be discarded when a file was uploaded.
  • Loading branch information
Mark-H committed May 2, 2011
1 parent 928848e commit 40b5a58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
22 changes: 8 additions & 14 deletions assets/components/importx/js/mgr/widget.home.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ importX.panel.createImport = function(config) {
,layout: 'fit'
,id: 'importx-panel-import'
,buttonAlign: 'center'
,fileUpload: true
,items: [{
//layout: 'form'
bodyStyle: 'padding: 15px;'
Expand Down Expand Up @@ -119,6 +120,12 @@ importX.panel.createImport = function(config) {
,height: 150
,allowBlank: false
,blankText: _('importx.nocsv')
},{
xtype: 'textfield',
fieldLabel: _('importx.csvfile'),
name: 'csv-file',
id: 'csv-file',
inputType: 'file'
},{
html: '<p>'+_('importx.tab.input.sep')+'</p>',
border: false
Expand Down Expand Up @@ -165,20 +172,7 @@ importX.panel.createImport = function(config) {
anchor: '100%'
}]
}]
}
/*,{
xtype: 'fileuploadfield',
buttonOnly: true
id: 'form-file',
emptyText: 'Select an image',
fieldLabel: 'Or, choose a file.',
name: 'csvfile',
buttonText: 'Browse',
buttonCfg: {
iconCls: 'upload-icon'
},
buttonOnly: true
}*/]
}]
}]
});
Ext.Ajax.timeout = 0;
Expand Down
12 changes: 10 additions & 2 deletions core/components/importx/lexicon/en/default.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@
$_lang['importx.importsuccess'] = 'Succesfully imported resources into MODX.';
$_lang['importx.importfailure'] = 'Oops, an error occurred while importing your resources.';
$_lang['importx.tab.input'] = 'CSV Input';
$_lang['importx.tab.input.desc'] = 'Paste your raw text, separating records with a newline and fields with a semi-colon (;) or the separator of your choice, in the field below.';
// Modified 2/5/2011
$_lang['importx.tab.input.desc'] = 'Paste or upload your CSV input, separating records with a newline and fields with a semi-colon (;) or the separator of your choice specified in the field below.';
$_lang['importx.tab.input.sep'] = 'When your CSV formatted entry uses a different separator, you can declare it here. Leave empty to use a semi-colon.';
$_lang['importx.csv'] = 'Raw CSV';
// Added 2/5/2011:
$_lang['importx.csvfile'] = 'CSV file upload';
$_lang['importx.separator'] = 'Separator';
$_lang['importx.tab.settings'] = 'Default Settings';
$_lang['importx.tab.settings.desc'] = 'Specify the default settings to be used. You may override these per record by referencing the fieldname in your CSV formatted values.';
$_lang['importx.err.noparent'] = 'Please choose a Parent to import to. Specify 0 to put new resources in the root of the site.';
$_lang['importx.err.parentnotnumeric'] = 'Parent not numeric.';
$_lang['importx.err.parentlessthanzero'] = 'Parent needs to be a positive integer.';
$_lang['importx.err.nocsv'] = 'Please add your CSV values in order for them to be processed.';
// Added 2/5/2011:
$_lang['importx.err.fileuploadfailed'] = 'Error reading the uploaded file.';
$_lang['importx.err.invalidcsv'] = 'Invalid CSV value posted.';
$_lang['importx.err.notenoughdata'] = 'Not enough data given. Expecting at least one header row, and one data row.';
$_lang['importx.err.elementmismatch'] = 'Element count do not match. Please check for correct syntax on line [[+line]].';
Expand All @@ -49,7 +54,10 @@
$_lang['importx.err.intexpected'] = '[[+field]] ([[+int]] is expected to be an integer)';
$_lang['importx.err.tvdoesnotexist'] = '[[+field]] (no TV with an ID of [[+id]])';
$_lang['importx.log.runningpreimport'] = 'Running pre-import tests on submitted data...';
// Added 2/5/2011:
$_lang['importx.log.fileuploadfound'] = 'CSV file upload overriding any manual input. Filename: [[+filename]]';
$_lang['importx.log.preimportclean'] = 'No errors in pre-import found. Preparing import values...';
$_lang['importx.log.importvaluesclean'] = 'No errors found while checking the import values. Running import...';
// Modified 2/5/2011:
$_lang['importx.log.importvaluesclean'] = 'No errors found while checking the import values: [[+count]] items found. Running import...';
$_lang['importx.log.complete'] = 'Importing completed. [[+count]] resources have been imported.';
?>
22 changes: 20 additions & 2 deletions core/components/importx/processors/startimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,25 @@ function logConsole($type,$msg) {
return logConsole('error',$modx->lexicon('importx.err.parentlessthanzero'));
}

$csv = (isset($_POST['csv'])) ? trim($_POST['csv']) : false;
// Handle file uploads
if (!empty($_FILES['csv-file']['name'])) {
logConsole('info',$modx->lexicon('importx.log.fileuploadfound',array('filename' => $_FILES['csv-file']['name'])));
$csv = file_get_contents($_FILES['csv-file']['tmp_name']);
if ($csv === false) { return logConsole('error',$modx->lexicon('importx.err.fileuploadfailed')); }
}

// Only if no file was uploaded check the manual input
if ((!isset($csv) || $csv === false) &&
(isset($_POST['csv']) && !empty($_POST['csv']))) {
$csv = trim($_POST['csv']);
}

// When no CSV detected (file or manual input), throw an error for that.
if (!isset($csv) || ($csv === false) || empty($csv)) {
return logConsole('error',$modx->lexicon('importx.err.nocsv'));
}

// Check a minimum length-ish (debatable - might be a useless check really)
if (strlen($csv) < 10) {
return logConsole('error',$modx->lexicon('importx.err.invalidcsv'),true);
}
Expand Down Expand Up @@ -136,7 +154,7 @@ function logConsole($type,$msg) {
if (count($err) > 0) {
return logConsole('error',$modx->lexicon('importx.err.elementmismatch',array('line' => implode(', ',$err))));
}
logConsole('info',$modx->lexicon('importx.log.importvaluesclean'));
logConsole('info',$modx->lexicon('importx.log.importvaluesclean',array('count' => count($lines))));
$resourceCount = 0;
foreach ($lines as $line) {
$response = $modx->runProcessor('resource/create',$line);
Expand Down
2 changes: 1 addition & 1 deletion test-csv-format.txt → test-csv-format.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pagetitle;alias;isfolder
Analysing;analysing;1
Communicating;communicating;0
Rock solid copy;sepiariverstudios;0
Thank you Working Party!!;working-party-rocks;1
Editing your resources remotely;modxmobile;0

0 comments on commit 40b5a58

Please sign in to comment.