X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=ExcelToJson.php;h=e981d32901cb7910e25123b2dcd4a4f95dc6b694;hp=9c5675e3eee7dc64198765a2dff1785d7660a48e;hb=refs%2Fheads%2Fwip_alan_T5884_add_photo_to_report;hpb=4ce282bac28f4994abebcec4d6456b3c0978f430 diff --git a/ExcelToJson.php b/ExcelToJson.php index 9c5675e3..e981d329 100644 --- a/ExcelToJson.php +++ b/ExcelToJson.php @@ -12,7 +12,7 @@ class Pman_Core_ExcelToJson extends Pman_Roo return parent::getAuth(); } - function post() + function post($v) { $this->transObj = DB_DataObject::Factory('invhist_transfer'); @@ -29,8 +29,13 @@ class Pman_Core_ExcelToJson extends Pman_Roo require_once 'File/Convert.php'; $fc = new File_Convert($img->getStoreName(), $img->mimetype ); - $csv = $fc->convert('text/csv'); - $this->importCsv($csv); + $csv = $fc->convert('text/csv'); + $ret = $this->importCsv($csv); + + + + $this->jdata($ret['data'], false, isset($ret['extra']) ? $ret['extra'] : array() ); + } function importCsv($csv) @@ -42,19 +47,52 @@ class Pman_Core_ExcelToJson extends Pman_Roo $this->jerr("invalid file"); } + + $cols = false; + $header = false; $rows = array(); + $extra = array(); while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) { - print_r(array_filter($n)); - } - exit; - if (empty($cols)) { - $this->jerr("could not find a row with " . implode(' / ', $req)); + + + if(!strlen(trim(implode('', $n)))){ // blank line; + if ($header) { + continue; + } + $header = true; + continue; + } + + if(!$header){ + $extra[preg_replace(array('/\s/', '/\:/'), '', $n[0])] = $n[1]; + continue; + } + + if(!$cols){ + $cols = array(); + foreach($n as $k) { + $cols[] = strtoupper(trim($k)); + } + + if (empty($cols)) { + continue; + } + + + continue; + } + + foreach($cols as $i=>$k) { + $row[$k] = $n[$i]; + } + $rows[] = $row; + } - fclose($fh); - exit; + + + return array('extra' => $extra, 'data' => $rows);; } - -} +} \ No newline at end of file