SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/DocImport/DocImportCSV.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers\DocImport;
  15.  
  16. use Sprout\Helpers\Enc;
  17. use Sprout\Helpers\ImportCSV;
  18.  
  19.  
  20. class DocImportCSV extends DocImport {
  21.  
  22. /**
  23.   * The main load function for a document.
  24.   * Throw an exception on error.
  25.   *
  26.   * @param string $filename The file. The file will exist, but may not be valid
  27.   * @return string|DOMDocument $data Resultant XML data as a string or DOMDocument element
  28.   **/
  29. public function load($filename) {
  30. $csv = new ImportCSV($filename);
  31.  
  32. $out = '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
  33. $out .= '<doc><body>';
  34. $out .= '<table><thead><tr>';
  35.  
  36. $headings = $csv->getHeadings();
  37. foreach ($headings as $hdr) {
  38. $out .= '<th>' . Enc::xml($hdr) . '</th>';
  39. }
  40.  
  41. $out .= '</tr></thead><tbody>';
  42.  
  43. while ($data = $csv->getLine()) {
  44. $out .= '<tr>';
  45. foreach ($data as $col) {
  46. $out .= '<td>' . Enc::xml($col) . '</td>';
  47. }
  48. $out .= '</tr>';
  49. }
  50.  
  51. $out .= '</tbody></table>';
  52. $out .= '</body></doc>';
  53.  
  54. return $out;
  55. }
  56.  
  57. }
  58.