* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_Renderer'); /** * Datasource Renderer Spreadsheet * * @version 0.2.0 * @package WB * @subpackage db */ class WBDatasource_Renderer_Spreadsheet extends WBDatasource_Renderer { /** * Temporary File * @var WBFile_Spreadsheet */ protected $file; /** * Current Sheet Position * @var int */ private $sheetPos = -1; /** * Current Row in Data Sheet * * @var int */ private $rowPos = 0; /** * Set Additional Variables * * @param array */ public function setVars($vars = array()) { parent::setVars($vars); $this->prepareSpreadSheet(); } private function prepareSpreadSheet() { if (-1 < $this->sheetPos) { return; } // start with -1 $this->sheetPos = -1; $this->rowPos = 0; $this->file = WBClass::create('WBFile_Spreadsheet'); $this->file->setSpreadsheetType('XLS'); $this->file->mkdir('var/tmp'); $this->file->tempnam('dsrSpreadsheet'); $this->file->open('w'); $prop = $this->file->getProperties(); $prop->setCreator('Wombat'); if (!empty($this->vars['title'])) { $prop->setTitle($this->vars['title']); } if (!empty($this->vars['subject'])) { $prop->setSubject($this->vars['title']); } if (!empty($this->vars['description'])) { $prop->setDescription($this->vars['description']); } if (!empty($this->vars['keyword'])) { $prop->setKeywords($this->vars['keyword']); } if (!empty($this->vars['category'])) { $prop->setCategory($this->vars['category']); } } /** * cleanup * * remove temporary file */ public function __destruct() { if ($this->file) { $this->file->close(); $this->file->unlink(); } } /** * Start Rendering * * Open stream to render view * @param string $datasource name separated with slashes */ public function start($datasource) { $this->rowPos = 0; $title = $datasource; $dsInfo = $this->getDatasourceInfo(); if (!empty($dsInfo['name'])) { $title = $dsInfo['name']; } if (-1 == $this->sheetPos) { ++$this->sheetPos; $this->file->setActiveSheetIndex($this->sheetPos); $this->file->getActiveSheet() ->setTitle(basename($title)); } else { ++$this->sheetPos; $this->file->addSheet(basename($title)); $this->file->setActiveSheetIndex($this->sheetPos); } /* // this blows file size $this->file->getActiveSheet() ->getStyle('A1:AZ1000') ->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP); */ } /** * render single item * * * @param array $item */ public function renderItem($item) { if (0 == $this->rowPos) { $this->configureMap($item); } $this->file->write($item); ++$this->rowPos; } /** * Configure Map and head * * Use datasource map and info or associative keys of (first) item * * @param array */ private function configureMap($item) { // fallback: map from item $itemMap = $this->getItemMap(); if (empty($itemMap)) { $map = array_flip(array_keys($item)); $this->file->setMap($map); $this->addDatasourceInfo($map); foreach ($map as $k => &$v) { $v = $k; } $this->file->write($map); return; } $map = array(); $head = array(); foreach ($itemMap as $i => $m) { if (!is_numeric($i) || !is_array($m)) { continue; } $head[$m['name']] = $m['title']; if (!isset($m['visible'])) { $m['visible'] = 1; } if (1 > $m['visible']) { continue; } $map[] = $m['name']; $head[$m['name']] = $m['title']; } $map = array_flip($map); $this->file->setMap($map); $this->addDatasourceInfo($map); $this->file->write($head); } /** * Add Datasource Info to Spreadsheet * * @param array column map */ private function addDatasourceInfo($map) { $dsInfo = $this->getDatasourceInfo(); if (empty($dsInfo['title'])) { return; } $map = array_flip($map); $tmp = array( $map[0] => $dsInfo['title'] ); $this->file->write($tmp); if (!empty($dsInfo['brief'])) { $tmp = array( $map[0] => $dsInfo['brief'] ); $this->file->write($tmp); } $this->file->write(array()); } /** * finish rendering * * Things to do after iterating through datasource */ public function end() { $this->file->setActiveSheetIndex(0); } /** * Get Actual File * * @return WBFile_Spreadsheet */ public function getFile() { $this->file->close(); return $this->file; } }