Skip to content

Commit 40b5a58

Browse files
committed
Closes #7: Add support for file uploads. File upload > manual input, so any manual input will be discarded when a file was uploaded.
1 parent 928848e commit 40b5a58

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

assets/components/importx/js/mgr/widget.home.form.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ importX.panel.createImport = function(config) {
8181
,layout: 'fit'
8282
,id: 'importx-panel-import'
8383
,buttonAlign: 'center'
84+
,fileUpload: true
8485
,items: [{
8586
//layout: 'form'
8687
bodyStyle: 'padding: 15px;'
@@ -119,6 +120,12 @@ importX.panel.createImport = function(config) {
119120
,height: 150
120121
,allowBlank: false
121122
,blankText: _('importx.nocsv')
123+
},{
124+
xtype: 'textfield',
125+
fieldLabel: _('importx.csvfile'),
126+
name: 'csv-file',
127+
id: 'csv-file',
128+
inputType: 'file'
122129
},{
123130
html: '<p>'+_('importx.tab.input.sep')+'</p>',
124131
border: false
@@ -165,20 +172,7 @@ importX.panel.createImport = function(config) {
165172
anchor: '100%'
166173
}]
167174
}]
168-
}
169-
/*,{
170-
xtype: 'fileuploadfield',
171-
buttonOnly: true
172-
id: 'form-file',
173-
emptyText: 'Select an image',
174-
fieldLabel: 'Or, choose a file.',
175-
name: 'csvfile',
176-
buttonText: 'Browse',
177-
buttonCfg: {
178-
iconCls: 'upload-icon'
179-
},
180-
buttonOnly: true
181-
}*/]
175+
}]
182176
}]
183177
});
184178
Ext.Ajax.timeout = 0;

core/components/importx/lexicon/en/default.inc.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@
3131
$_lang['importx.importsuccess'] = 'Succesfully imported resources into MODX.';
3232
$_lang['importx.importfailure'] = 'Oops, an error occurred while importing your resources.';
3333
$_lang['importx.tab.input'] = 'CSV Input';
34-
$_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.';
34+
// Modified 2/5/2011
35+
$_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.';
3536
$_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.';
3637
$_lang['importx.csv'] = 'Raw CSV';
38+
// Added 2/5/2011:
39+
$_lang['importx.csvfile'] = 'CSV file upload';
3740
$_lang['importx.separator'] = 'Separator';
3841
$_lang['importx.tab.settings'] = 'Default Settings';
3942
$_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.';
4043
$_lang['importx.err.noparent'] = 'Please choose a Parent to import to. Specify 0 to put new resources in the root of the site.';
4144
$_lang['importx.err.parentnotnumeric'] = 'Parent not numeric.';
4245
$_lang['importx.err.parentlessthanzero'] = 'Parent needs to be a positive integer.';
4346
$_lang['importx.err.nocsv'] = 'Please add your CSV values in order for them to be processed.';
47+
// Added 2/5/2011:
48+
$_lang['importx.err.fileuploadfailed'] = 'Error reading the uploaded file.';
4449
$_lang['importx.err.invalidcsv'] = 'Invalid CSV value posted.';
4550
$_lang['importx.err.notenoughdata'] = 'Not enough data given. Expecting at least one header row, and one data row.';
4651
$_lang['importx.err.elementmismatch'] = 'Element count do not match. Please check for correct syntax on line [[+line]].';
@@ -49,7 +54,10 @@
4954
$_lang['importx.err.intexpected'] = '[[+field]] ([[+int]] is expected to be an integer)';
5055
$_lang['importx.err.tvdoesnotexist'] = '[[+field]] (no TV with an ID of [[+id]])';
5156
$_lang['importx.log.runningpreimport'] = 'Running pre-import tests on submitted data...';
57+
// Added 2/5/2011:
58+
$_lang['importx.log.fileuploadfound'] = 'CSV file upload overriding any manual input. Filename: [[+filename]]';
5259
$_lang['importx.log.preimportclean'] = 'No errors in pre-import found. Preparing import values...';
53-
$_lang['importx.log.importvaluesclean'] = 'No errors found while checking the import values. Running import...';
60+
// Modified 2/5/2011:
61+
$_lang['importx.log.importvaluesclean'] = 'No errors found while checking the import values: [[+count]] items found. Running import...';
5462
$_lang['importx.log.complete'] = 'Importing completed. [[+count]] resources have been imported.';
5563
?>

core/components/importx/processors/startimport.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,25 @@ function logConsole($type,$msg) {
6363
return logConsole('error',$modx->lexicon('importx.err.parentlessthanzero'));
6464
}
6565

66-
$csv = (isset($_POST['csv'])) ? trim($_POST['csv']) : false;
66+
// Handle file uploads
67+
if (!empty($_FILES['csv-file']['name'])) {
68+
logConsole('info',$modx->lexicon('importx.log.fileuploadfound',array('filename' => $_FILES['csv-file']['name'])));
69+
$csv = file_get_contents($_FILES['csv-file']['tmp_name']);
70+
if ($csv === false) { return logConsole('error',$modx->lexicon('importx.err.fileuploadfailed')); }
71+
}
72+
73+
// Only if no file was uploaded check the manual input
74+
if ((!isset($csv) || $csv === false) &&
75+
(isset($_POST['csv']) && !empty($_POST['csv']))) {
76+
$csv = trim($_POST['csv']);
77+
}
78+
79+
// When no CSV detected (file or manual input), throw an error for that.
80+
if (!isset($csv) || ($csv === false) || empty($csv)) {
81+
return logConsole('error',$modx->lexicon('importx.err.nocsv'));
82+
}
83+
84+
// Check a minimum length-ish (debatable - might be a useless check really)
6785
if (strlen($csv) < 10) {
6886
return logConsole('error',$modx->lexicon('importx.err.invalidcsv'),true);
6987
}
@@ -136,7 +154,7 @@ function logConsole($type,$msg) {
136154
if (count($err) > 0) {
137155
return logConsole('error',$modx->lexicon('importx.err.elementmismatch',array('line' => implode(', ',$err))));
138156
}
139-
logConsole('info',$modx->lexicon('importx.log.importvaluesclean'));
157+
logConsole('info',$modx->lexicon('importx.log.importvaluesclean',array('count' => count($lines))));
140158
$resourceCount = 0;
141159
foreach ($lines as $line) {
142160
$response = $modx->runProcessor('resource/create',$line);

test-csv-format.txt renamed to test-csv-format.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pagetitle;alias;isfolder
22
Analysing;analysing;1
33
Communicating;communicating;0
44
Rock solid copy;sepiariverstudios;0
5-
Thank you Working Party!!;working-party-rocks;1
5+
Editing your resources remotely;modxmobile;0

0 commit comments

Comments
 (0)