* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBITS' , 'WBITS_TicketList_Exporter' ); /** * Wombat Incident Ticket System Ticket List Exporter: Spreadsheet * * @version 1.0.0 * @package WB * @subpackage content */ class WBITS_TicketList_Exporter_Spreadsheet extends WBITS_TicketList_Exporter { /** * @var WBFile */ private $tmpFile; /** * @var WBFile_Spreadsheet */ private $sheet; /** * @var WBDictionary_URL */ private $dictURL; /** * @var WBDatasource_Table */ private $table; /** * 2nd Constructor */ protected function init() { $this->tmpFile = WBClass::create('WBFile'); //$this->tmpFile->tempdir('tic-'); $this->tmpFile->mkdir('var/tmp/tic'); $this->dictURL = WBClass::create('WBDictionary_URL'); $this->table = WBClass::create('WBDatasource_Table'); } public function realpath() { return $this->tmpFile->realpath() . '/tickets.xls'; } /** * Event Handler to Start Export * */ protected function onExportStart() { $cols = $this->config['column']; if (empty($this->config['namespaces'][$this->config['namespace']]['column'])) { $this->config['namespaces'][$this->config['namespace']]['column'] = array(); } foreach ($this->config['namespaces'][$this->config['namespace']]['column'] as $k => $v) { if (empty($v['export'])) { if (isset($cols[$k])) { unset($cols[$k]); } continue; } if (!isset($cols[$k])) { $cols[$k] = $v; } else { $cols[$k] = array_merge($cols[$k], $v); } } $this->sheet = WBClass::create('WBFile_Spreadsheet'); $this->sheet->setSpreadsheetType('xls'); $this->sheet->touch($this->tmpFile->dirname() . '/tickets.xls'); $this->sheet->open('w'); $this->sheet->getProperties() ->setCreator('Wombat') ->setTitle('Ticketliste'); //->setSubject($data['title']) //->setDescription('List of active newsletter subscribers'); //->setKeywords('office PhpSpreadsheet php') //->setCategory('Test result file'); $this->sheet->setActiveSheetIndex(0) ->setTitle('Tickets'); $this->sheet->getActiveSheet()->getStyle('A1:AZ1000') ->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP); $map = array_flip(array_keys($cols)); $this->sheet->setMap($map); $head = array(); foreach ($cols as $k => $v) { $head[$k] = $v['name']; } $this->sheet->write($head); $colNames = $this->sheet->getColumnNames(); // timestamp columns foreach (array('created', 'changed', 'statuschanged', 'deadline') as $dc) { if (!isset($map[$dc])) { continue; } $c = $colNames[$map[$dc]]; $this->sheet->getActiveSheet() ->getStyle($c . '2:' . $c . '1000') ->getNumberFormat() ->setFormatCode( \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME ); } } /** * Event Handler for Each List Item * * @param array list item data */ protected function exportListItem($item) { // well known columns $item['obscureid'] = $item['obscure'] . $item['id']; foreach ($item as $k => &$v) { if (0 < $v && preg_match('/urlid$/', $k)) { $this->dictURL->load($v); $v = $this->dictURL->getWord(); continue; } $v = trim($v); switch ($k) { case 'assigneduid': case 'owneruid': case 'customeruid': $v = $this->uid2Name($v); break; case 'created': case 'changed': case 'statuschanged': case 'deadline': $ts = strtotime($v); $v = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($ts); break; } } $this->sheet->write($item); } public function uid2Name($uid) { $data = $this->table->get('user', $uid); if (empty($data)) { return ''; } $data = $data[0]; return sprintf('%s, %s (%d)', $data['surname'], $data['forename'], $data['id']); } /** * Event Handler at End of Export * */ protected function onExportEnd() { $this->sheet->setActiveSheetIndex(0); $this->sheet->close(); } }