X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=SimpleExcel.php;h=6c36750ea2e301acfaab413f0d3bc9497c7b89a6;hp=cf9bbb470f800c4882c8f8c766201f7ac4cb1134;hb=586e4eb470252d837ba18b67e4c3c1702131fd1d;hpb=0d788c51041dee4f50de1ce3523208f6ac3fa75a diff --git a/SimpleExcel.php b/SimpleExcel.php index cf9bbb47..6c36750e 100644 --- a/SimpleExcel.php +++ b/SimpleExcel.php @@ -74,8 +74,9 @@ class Pman_Core_SimpleExcel extends Pman var $formats = array(); var $workbook = false; var $worksheet= false; - - function Pman_Core_SimpleExcel($data,$cfg) + var $postRender = array(); + + function __construct($data,$cfg) { // print_r($cfg);exit; require_once 'Spreadsheet/Excel/Writer.php'; @@ -92,11 +93,14 @@ class Pman_Core_SimpleExcel extends Pman $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array(); + $this->formats['_default_date_format_'] = $workbook->addFormat();; + $this->formats['_default_date_format_']->setNumFormat('YYYY-MM-DD'); + foreach($cfg['formats'] as $f=>$fcfg) { $this->formats[$f] = & $workbook->addFormat(); foreach((array)$fcfg as $k=>$v) { - $this->formats[$f]->{'set' . $k}($v); + $this->formats[$f]->{'set' . $k}($v); } } @@ -239,6 +243,7 @@ class Pman_Core_SimpleExcel extends Pman if (empty($data)) { return; } + foreach($cfg['cols'] as $c => $col_cfg) { if (isset($col_cfg['renderer'])) { $hasRender = true; @@ -254,9 +259,10 @@ class Pman_Core_SimpleExcel extends Pman $data->find(); while($data->fetch()) { - $hasRenderRow = $this->addLine($cfg['workbook'], $clo); + $hasRenderRow = $this->addLine($cfg['workbook'], $data); $hasRender = ($hasRender || $hasRenderRow) ? true : false; } + $start_row += $count; } else { foreach($data as $r=>$clo) { @@ -274,8 +280,7 @@ class Pman_Core_SimpleExcel extends Pman continue; } if (!empty($col_cfg['renderer'])) { - - if (is_a($col_cfg['renderer'], 'Closure')) { + if (is_a($col_cfg['renderer'], 'Closure')) { $col_cfg['renderer']($cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl); } else { // not sure if row is correct here...!!!? @@ -288,8 +293,9 @@ class Pman_Core_SimpleExcel extends Pman } } } + $start_row += count($data); + } - $start_row += count($data); if (!empty($cfg['foot'])) { foreach($cfg['foot'] as $row) { @@ -364,6 +370,18 @@ class Pman_Core_SimpleExcel extends Pman } if (isset($col_cfg['renderer'])) { $hasRender = true; + + $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : ''; + if (empty($cl[$col_cfg['dataIndex']])) { + continue; + } + $this->postRender[] = array( + $col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $start_row+$r+1, $c, $cl + ); + + + + continue; } @@ -375,7 +393,20 @@ class Pman_Core_SimpleExcel extends Pman $format = isset($col_cfg['format']) && isset($formats[$col_cfg['format']] ) ? $formats[$col_cfg['format']] : false; // print_R(array($start_row+$r, $c, $v, $format));exit; // handle 0 prefixes.. - if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.') + + if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $v)) { + $dataFormat = 'date'; + $format = empty($format) ? $this->formats['_default_date_format_']: $format; + $ut_to_ed_diff = 86400 * 25569; + $gmt = strtotime('1970-01-01'); + + $v = (strtotime($v) + $ut_to_ed_diff - $gmt) / 86400; + // need to +8hrs to get real time.. + + + } + + if ( (is_numeric($v) && strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.' ) || $dataFormat == 'string' ) { $worksheet->writeString($start_row+$r, $c, $v, $format); @@ -398,6 +429,20 @@ class Pman_Core_SimpleExcel extends Pman function send($fname) { + + if (!empty($this->postRender)) { + foreach($this->postRender as $ar) { + if (is_a($ar[0], 'Closure')) { + $ar[0]($ar[1], $ar[2], $ar[3], $ar[4], $ar[5]); + } else { + // not sure if row is correct here...!!!? + call_user_func($ar[0],$ar[1], $ar[2], $ar[3], $ar[4], $ar[5]); + } + } + + } + + if (!empty($this->workbook)) { $this->workbook->close(); $this->workbook = false;