diff --git a/core/components/migx/configs/grid/grid.actionbuttons.inc.php b/core/components/migx/configs/grid/grid.actionbuttons.inc.php index 0843b881..84c4d73e 100644 --- a/core/components/migx/configs/grid/grid.actionbuttons.inc.php +++ b/core/components/migx/configs/grid/grid.actionbuttons.inc.php @@ -51,4 +51,9 @@ $gridactionbuttons['uploadfiles']['text'] = "'[[%migx.upload_images]]'"; $gridactionbuttons['uploadfiles']['handler'] = 'this.uploadFiles,this.uploadSuccess,this.loadFromSource'; $gridactionbuttons['uploadfiles']['scope'] = 'this'; -$gridactionbuttons['uploadfiles']['standalone'] = '1'; \ No newline at end of file +$gridactionbuttons['uploadfiles']['standalone'] = '1'; + +$gridactionbuttons['importcsvmigx']['text'] = "'[[%migx.import_csv]]'"; +$gridactionbuttons['importcsvmigx']['handler'] = 'this.selectImportFile,this.importCsvMigx'; +$gridactionbuttons['importcsvmigx']['scope'] = 'this'; +$gridactionbuttons['importcsvmigx']['standalone'] = '1'; \ No newline at end of file diff --git a/core/components/migx/configs/grid/grid.config.inc.php b/core/components/migx/configs/grid/grid.config.inc.php index c0ec67e5..d398c0f8 100644 --- a/core/components/migx/configs/grid/grid.config.inc.php +++ b/core/components/migx/configs/grid/grid.config.inc.php @@ -747,4 +747,88 @@ } "; +$gridfunctions['this.selectImportFile'] = " + selectImportFile: function(btn,e) { + this.browser = MODx.load({ + xtype: 'modx-browser' + ,closeAction: 'close' + ,id: Ext.id() + ,multiple: true + ,source: [[+config.media_source_id]] || MODx.config.default_media_source + ,hideFiles: this.config.hideFiles || false + ,rootVisible: this.config.rootVisible || false + ,allowedFileTypes: this.config.allowedFileTypes || '' + ,wctx: this.config.wctx || 'web' + ,openTo: this.config.openTo || '' + ,rootId: this.config.rootId || '/' + ,hideSourceCombo: this.config.hideSourceCombo || false + ,listeners: { + 'select': {fn: function(data) { + //console.log(this.config); + this.importCsvMigx(data); + //this.fireEvent('select',data); + },scope:this} + } + }); + //} + this.browser.show(btn); + return true; + } +"; + +$gridfunctions['this.importCsvMigx'] = " + importCsvMigx: function(data) { + var recordIndex = 'none'; + var pathname = data.pathname; + if (this.menu.recordIndex == 0){ + recordIndex = 0; + }else{ + recordIndex = this.menu.recordIndex || 'none'; + } + var tv_id = this.config.tv; + MODx.Ajax.request({ + url: this.config.url + ,params: { + action: 'mgr/migxdb/process' + ,processaction: 'importcsvmigx' + ,resource_id: this.config.resource_id + ,co_id: '[[+config.connected_object_id]]' + ,items: Ext.get('tv' + tv_id).dom.value + ,record_index: recordIndex + ,pathname: pathname + } + ,listeners: { + 'success': {fn:function(res){ + if (res.message==''){ + var items = res.object; + var item = null; + Ext.get('tv' + tv_id).dom.value = Ext.util.JSON.encode(items); + this.autoinc = 0; + for(i = 0; i < items.length; i++) { + item = items[i]; + if (item.MIGX_id){ + + if (parseInt(item.MIGX_id) > this.autoinc){ + this.autoinc = item.MIGX_id; + } + }else{ + item.MIGX_id = this.autoinc +1 ; + this.autoinc = item.MIGX_id; + } + items[i] = item; + } + + this.getStore().sortInfo = null; + this.getStore().loadData(items); + var call_collectmigxitems = this.call_collectmigxitems; + this.call_collectmigxitems=true; + this.collectItems(); + this.call_collectmigxitems = call_collectmigxitems; + } + + },scope:this} + } + }); + } +"; diff --git a/core/components/migx/elements/snippets/getimagelist.snippet.php b/core/components/migx/elements/snippets/getimagelist.snippet.php index 675626fe..5d6fa085 100644 --- a/core/components/migx/elements/snippets/getimagelist.snippet.php +++ b/core/components/migx/elements/snippets/getimagelist.snippet.php @@ -1,5 +1,4 @@ setPlaceholder($totalVar, 0); return ''; } @@ -425,7 +425,7 @@ } } -if (empty($o) && !empty($emptyTpl)) { +if (empty($o)) { $template = $migx->getTemplate($emptyTpl); if ($template[$emptyTpl]) { $chunk = $modx->newObject('modChunk'); @@ -440,4 +440,4 @@ return ''; } -return $o; +return $o; \ No newline at end of file diff --git a/core/components/migx/lexicon/de/default.inc.php b/core/components/migx/lexicon/de/default.inc.php index 38a26164..b7dc8dec 100644 --- a/core/components/migx/lexicon/de/default.inc.php +++ b/core/components/migx/lexicon/de/default.inc.php @@ -65,4 +65,5 @@ $_lang['migx.emptythrash_confirm'] = 'Papierkorb wirklich leeren?'; $_lang['migx.emptythrash'] = 'Papierkorb leeren'; $_lang['migx.search'] = 'Suche...'; -$_lang['migx.reset_all'] = 'Alle zurücksetzen'; \ No newline at end of file +$_lang['migx.reset_all'] = 'Alle zurücksetzen'; +$_lang['migx.import_csv'] = 'CSV Importieren'; \ No newline at end of file diff --git a/core/components/migx/lexicon/en/default.inc.php b/core/components/migx/lexicon/en/default.inc.php index a63ef6f2..9753152f 100644 --- a/core/components/migx/lexicon/en/default.inc.php +++ b/core/components/migx/lexicon/en/default.inc.php @@ -65,4 +65,5 @@ $_lang['migx.emptythrash_confirm'] = 'Empty Trash. Are you sure?'; $_lang['migx.emptythrash'] = 'Empty Trash'; $_lang['migx.search'] = 'Search...'; -$_lang['migx.reset_all'] = 'Reset all'; \ No newline at end of file +$_lang['migx.reset_all'] = 'Reset all'; +$_lang['migx.import_csv'] = 'Import CSV'; \ No newline at end of file diff --git a/core/components/migx/processors/mgr/default/importcsvmigx.php b/core/components/migx/processors/mgr/default/importcsvmigx.php new file mode 100644 index 00000000..c239b84b --- /dev/null +++ b/core/components/migx/processors/mgr/default/importcsvmigx.php @@ -0,0 +1,81 @@ +getOption('reference_field', $scriptProperties, 'id'); +$items = $modx->getOption('items', $scriptProperties, ''); +$pathname = $modx->getOption('pathname', $scriptProperties, ''); +$items = !empty($items) ? $this->modx->fromJson($items) : array(); + + +if (!function_exists('parse_csv_file')) { + function parse_csv_file($file, $columnheadings = true, $delimiter = ',', $enclosure = "\"") { + $row = 1; + $rows = array(); + $handle = fopen($file, 'r'); + + while (($data = fgetcsv($handle, 1000, $delimiter, $enclosure)) !== false) { + + if (!($columnheadings == false) && ($row == 1)) { + $fieldnames = $data; + } elseif (!($columnheadings == false)) { + foreach ($data as $key => $value) { + unset($data[$key]); + $data[$fieldnames[$key]] = $value; + } + $rows[] = $data; + } else { + $rows[] = $data; + } + $row++; + } + + fclose($handle); + + $result['fieldnames'] = $fieldnames; + $result['rows'] = $rows; + + + return $result; + } +} + +$result = parse_csv_file($pathname); +//print_r($result); + +$newitems = array(); +$has_referencefield = false; +if (isset($result['rows'])) { + foreach ($result['rows'] as $item) { + if (isset($item[$reference_field])) { + $newitems[$item[$reference_field]] = $item; + $has_referencefield = true; + } else { + $newitems[] = $item; + } + } +} + +$maxID = 0; +$outputitems = array(); +if ($has_referencefield) { + foreach ($items as $item) { + if (isset($item[$reference_field]) && isset($newitems[$item[$reference_field]])) { + if (isset($item['MIGX_id']) && $item['MIGX_id'] > $maxID) { + $maxID = $item['MIGX_id']; + } + $outputitems[] = array_merge($item, $newitems[$item[$reference_field]]); + unset($newitems[$item[$reference_field]]); + } + } +} + +foreach ($newitems as $item) { + $maxID++; + $item['MIGX_id'] = (string )$maxID; + $outputitems[] = $item; +} + + +return $modx->error->success('', $outputitems);