Skip to content

Commit

Permalink
import csv to MIGX
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno17 committed Dec 10, 2015
1 parent 13eb297 commit 0c882a5
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 6 deletions.
7 changes: 6 additions & 1 deletion core/components/migx/configs/grid/grid.actionbuttons.inc.php
Expand Up @@ -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';
$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';
84 changes: 84 additions & 0 deletions core/components/migx/configs/grid/grid.config.inc.php
Expand Up @@ -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}
}
});
}
";

@@ -1,5 +1,4 @@
<?php

/**
* getImageList
*
Expand Down Expand Up @@ -158,6 +157,7 @@
}

if (empty($outputvalue)) {
$modx->setPlaceholder($totalVar, 0);
return '';
}

Expand Down Expand Up @@ -425,7 +425,7 @@
}
}

if (empty($o) && !empty($emptyTpl)) {
if (empty($o)) {
$template = $migx->getTemplate($emptyTpl);
if ($template[$emptyTpl]) {
$chunk = $modx->newObject('modChunk');
Expand All @@ -440,4 +440,4 @@
return '';
}

return $o;
return $o;
3 changes: 2 additions & 1 deletion core/components/migx/lexicon/de/default.inc.php
Expand Up @@ -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';
$_lang['migx.reset_all'] = 'Alle zurücksetzen';
$_lang['migx.import_csv'] = 'CSV Importieren';
3 changes: 2 additions & 1 deletion core/components/migx/lexicon/en/default.inc.php
Expand Up @@ -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';
$_lang['migx.reset_all'] = 'Reset all';
$_lang['migx.import_csv'] = 'Import CSV';
81 changes: 81 additions & 0 deletions core/components/migx/processors/mgr/default/importcsvmigx.php
@@ -0,0 +1,81 @@
<?php

//print_r($scriptProperties);
//todo: modify scriptProperties by hook-snippet

$reference_field = $modx->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);

0 comments on commit 0c882a5

Please sign in to comment.