[[!getCsvFileItems? &file=`assets/files/csvtest.csv` &columnheadings=`1` ]]
<?php /* [[!getCsvFileItems? &file=`assets/files/csvtest.csv` &columnheadings=`1` ]] */ $file = $modx->getOption('file', $scriptProperties, ''); $delimiter = $modx->getOption('delimiter', $scriptProperties, ','); $columnheadings = $modx->getOption('columnheadings', $scriptProperties, false); $outerTpl = $modx->getOption('outerTpl', $scriptProperties, '@CODE:<table>[[+heading]]<tbody>[[+rows]]</tbody></table>'); $headingTpl = $modx->getOption('headingTpl', $scriptProperties, '@CODE:<thead><tr>[[+_fields]]</tr></thead>'); $headingFieldTpl = $modx->getOption('headingFieldTpl', $scriptProperties, '@CODE:<th>[[+_field]]</th>'); $rowTpl = $modx->getOption('rowTpl', $scriptProperties, '@CODE:<tr>[[+_fields]]</tr>'); $rowFieldTpl = $modx->getOption('rowFieldTpl', $scriptProperties, '@CODE:<td>[[+_field]]</td>'); if (!function_exists('parse_csv_file')) { function parse_csv_file($file, $columnheadings = false, $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; } } if (!function_exists('parseCsvGetChunk')) { function parseCsvGetChunk($tpl, $properties) { global $modx; if (substr($tpl, 0, 6) == "@CODE:") { $template = substr($tpl, 6); $chunk = $modx->newObject('modChunk'); $chunk->setCacheable(false); $chunk->setContent($template); $output = $chunk->process($properties); } else { $output = $modx->getChunk($tpl, $properties); } return $output; } } $file = $modx->getOption('base_path') . $file; $result = parse_csv_file($file, $columnheadings, $delimiter); $output = array(); $output['heading'] = ''; if ($columnheadings) { $heading['_fields'] = ''; foreach ($result['fieldnames'] as $fieldname) { $properties = array(); $properties['_field'] = $fieldname; $heading['_fields'] .= parseCsvGetChunk($headingFieldTpl, $properties); } $output['heading'] = parseCsvGetChunk($headingTpl, $heading); } $rows = array(); foreach ($result['rows'] as $row) { $row['_fields'] = ''; foreach ($row as $field) { $properties = array(); $properties['_field'] = $field; $row['_fields'] .= parseCsvGetChunk($rowFieldTpl, $properties); } $rows[] = parseCsvGetChunk($rowTpl, $row); } $output['rows'] = implode('', $rows); return parseCsvGetChunk($outerTpl, $output); ?>
with some little tricks it should be possible to have a import csv - button above the MIGX-grid, which could open a modal-window with a textarea, where you can paste the csv-code and import it as json for MIGX or have an upload-button for uploading a csv-file and importing it at the same time into the MIGX-grid.
[[!getCsvFileItems? &file=`assets/files/csvtest.csv` &columnheadings=`1` ]]
something like that snippet should do it:
[[!getCsvFileItems? &file=`assets/files/csvtest.csv` &columnheadings=`1` ]]
<!--?php /* [[!getCsvFileItems? &file=`assets/files/csvtest.csv` &columnheadings=`1` ]] */ $file = $modx--->getOption('file', $scriptProperties, ''); $delimiter = $modx->getOption('delimiter', $scriptProperties, ','); $columnheadings = $modx->getOption('columnheadings', $scriptProperties, false); $outerTpl = $modx->getOption('outerTpl', $scriptProperties, '@CODE:[[+heading]][[+rows]]<table><tbody></tbody></table>'); $headingTpl = $modx->getOption('headingTpl', $scriptProperties, '@CODE:[[+_fields]]'); $headingFieldTpl = $modx->getOption('headingFieldTpl', $scriptProperties, '@CODE:[[+_field]]'); $rowTpl = $modx->getOption('rowTpl', $scriptProperties, '@CODE:[[+_fields]]'); $rowFieldTpl = $modx->getOption('rowFieldTpl', $scriptProperties, '@CODE:[[+_field]]'); if (!function_exists('parse_csv_file')) { function parse_csv_file($file, $columnheadings = false, $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; } } if (!function_exists('parseCsvGetChunk')) { function parseCsvGetChunk($tpl, $properties) { global $modx; if (substr($tpl, 0, 6) == "@CODE:") { $template = substr($tpl, 6); $chunk = $modx->newObject('modChunk'); $chunk->setCacheable(false); $chunk->setContent($template); $output = $chunk->process($properties); } else { $output = $modx->getChunk($tpl, $properties); } return $output; } } $file = $modx->getOption('base_path') . $file; $result = parse_csv_file($file, $columnheadings, $delimiter); $output = array(); $output['heading'] = ''; if ($columnheadings) { $heading['_fields'] = ''; foreach ($result['fieldnames'] as $fieldname) { $properties = array(); $properties['_field'] = $fieldname; $heading['_fields'] .= parseCsvGetChunk($headingFieldTpl, $properties); } $output['heading'] = parseCsvGetChunk($headingTpl, $heading); } $rows = array(); foreach ($result['rows'] as $row) { $row['_fields'] = ''; foreach ($row as $field) { $properties = array(); $properties['_field'] = $field; $row['_fields'] .= parseCsvGetChunk($rowFieldTpl, $properties); } $rows[] = parseCsvGetChunk($rowTpl, $row); } $output['rows'] = implode('', $rows); return parseCsvGetChunk($outerTpl, $output); ?>
&tpl=`yourChunk`