diff --git a/assets/components/importx/js/mgr/widget.home.form.js b/assets/components/importx/js/mgr/widget.home.form.js index 4c70d67..faf14ff 100644 --- a/assets/components/importx/js/mgr/widget.home.form.js +++ b/assets/components/importx/js/mgr/widget.home.form.js @@ -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;' @@ -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: '

'+_('importx.tab.input.sep')+'

', border: false @@ -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; diff --git a/core/components/importx/lexicon/en/default.inc.php b/core/components/importx/lexicon/en/default.inc.php index 776fecb..3d9c989 100644 --- a/core/components/importx/lexicon/en/default.inc.php +++ b/core/components/importx/lexicon/en/default.inc.php @@ -31,9 +31,12 @@ $_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.'; @@ -41,6 +44,8 @@ $_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]].'; @@ -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.'; ?> \ No newline at end of file diff --git a/core/components/importx/processors/startimport.php b/core/components/importx/processors/startimport.php index 00d217a..5ab53df 100644 --- a/core/components/importx/processors/startimport.php +++ b/core/components/importx/processors/startimport.php @@ -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); } @@ -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); diff --git a/test-csv-format.txt b/test-csv-format.csv similarity index 70% rename from test-csv-format.txt rename to test-csv-format.csv index b6a2b19..b2272c8 100644 --- a/test-csv-format.txt +++ b/test-csv-format.csv @@ -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 \ No newline at end of file +Editing your resources remotely;modxmobile;0 \ No newline at end of file