array('bg' => 'b6e7a7', 'fg' => '000000'), App_Report_Summary_Obj::STATUS_SUBMITTED_LATE => array('bg' => 'e7a7a7', 'fg' => '000000'), App_Report_Summary_Obj::STATUS_PAST_DUE => array('bg' => 'db0000', 'fg' => 'ffffff'), App_Report_Summary_Obj::STATUS_NOT_SUBMITTED => array('bg' => 'ffffff', 'fg' => '000000'), ); protected $_statusTitles = array( App_Report_Summary_Obj::STATUS_SUBMITTED_ON_TIME => 'Submitted on time', App_Report_Summary_Obj::STATUS_SUBMITTED_LATE => 'Submitted late', App_Report_Summary_Obj::STATUS_PAST_DUE => 'Past due. Provide ASAP!', App_Report_Summary_Obj::STATUS_NOT_SUBMITTED => 'Not yet submitted', ); public function __construct(array $options = null) { if ($options) { $this->setOptions($options); } } public function setOptions(array $options) { foreach ($options as $name => $value) { $method = 'set' . ucfirst($name); if (method_exists($this, $method)) { $this->$method($value); } else { throw new Exception('Unknown option "' . $name . '"'); } } return $this; } public function getMode() { return $this->_mode; } public function setMode($mode) { $this->_mode = $mode; return $this; } public function getDataObj() { return $this->_dataObj; } public function setDataObj(App_Report_Summary_Obj $dataObj) { $this->_dataObj = $dataObj; return $this; } public function getSchoolId() { return $this->_schoolId; } public function setSchoolId($schoolId) { $this->_schoolId = $schoolId; return $this; } public function getListOptions() { return $this->_listOptions; } public function setListOptions(array $listOptions) { $this->_listOptions = $listOptions; return $this; } public function getFilename() { return $this->_filename; } public function setFilename($filename) { $this->_filename = $filename; return $this; } public function export() { $schoolId = $this->getSchoolId(); $list = $this->_getExportXlsData(); require_once 'PHPExcel/PHPExcel.php'; $excel = new PHPExcel(); $excel->getProperties() ->setCreator(constant('SITE_NAME')) ->setTitle('Summary Table ' . date('m/d/Y')); $sheet = $excel->setActiveSheetIndex(0); $schoolIds = array_slice(array_keys($list[0]), 3); // col addresses start at 0 // row addresses start as 1 $rowIdx = 1; $colIdx = 0; foreach ($list as $row) { $colIdx = 0; foreach ($row as $info) { $cell = $sheet->getCellByColumnAndRow($colIdx, $rowIdx); $cellStyle = $cell->getStyle(); if (is_array($info)) { $cell->setValueExplicit($info['text']); if (isset($this->_statusColors[$info['status']])) { $colorInfo = $this->_statusColors[$info['status']]; $cellStyle->getFont()->applyFromArray(array('color' => array('rgb' => $colorInfo['fg']))); $cellStyle->getFill()->applyFromArray(array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $colorInfo['bg']), )); } } else { $cell->setValueExplicit($info); } ++$colIdx; } if ($schoolId) { // adding "Report Submitted" column in case we export singe school summary if ($rowIdx === 1) { $value = 'Report Submitted'; } else { $value = empty($info['added']) ? '-' : date('m/d/Y h:i a', strtotime($info['added'])); } $cell = $sheet->getCellByColumnAndRow($colIdx, $rowIdx); $cell->setValueExplicit($value); ++$colIdx; } ++$rowIdx; } $this->_addReportsStatistic($sheet, $rowIdx, $schoolIds); $rowIdx = 1; foreach ($list as $row) { $cell = $sheet->getCellByColumnAndRow($colIdx, $rowIdx); $cell->setValueExplicit(' '); ++$rowIdx; } if ($this->getMode() === self::MODE_FILE) { return $this->_writeFile($excel); } if ($this->getMode() === self::MODE_OUTPUT) { $this->_writeOutput($excel); exit; } return null; } protected function _addReportsStatistic(PHPExcel_Worksheet $sheet, $rowIdx, array $schoolIds) { $statistic = $this->getDataObj()->getStatusStatistics($schoolIds, false); foreach ($this->_statusColors as $status => $colorInfo) { $sheet->mergeCellsByColumnAndRow(0, $rowIdx, 2, $rowIdx); $cell = $sheet->getCellByColumnAndRow(0, $rowIdx); $cell->setValueExplicit($this->_statusTitles[$status]); $cellStyle = $cell->getStyle(); $cellStyle->getFont()->applyFromArray(array('color' => array('rgb' => $colorInfo['fg']))); $cellStyle->getFill()->applyFromArray(array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $colorInfo['bg']), )); $colIdx = 3; foreach ($statistic as $col) { $cell = $sheet->getCellByColumnAndRow($colIdx, $rowIdx); $cell->setValueExplicit($col[$status], PHPExcel_Cell_DataType::TYPE_NUMERIC); $cellStyle = $cell->getStyle(); $cellStyle->getFont()->applyFromArray(array('color' => array('rgb' => $colorInfo['fg']))); $cellStyle->getFill()->applyFromArray(array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $colorInfo['bg']), )); ++$colIdx; } ++$rowIdx; } return $this; } protected function _writeFile(PHPExcel $excel) { $filename = $this->getFilename(); $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save($filename); return $filename; } protected function _writeOutput(PHPExcel $excel) { $filename = 'summary-table ' . date('M d, Y g:i A') . '.xlsx'; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: cache, must-revalidate'); header('Pragma: no-cache'); $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save('php://output'); exit; } protected function _getExportXlsData() { $schoolId = $this->getSchoolId(); $obj = $this->getDataObj(); $schools = $obj->getSchools($schoolId); // [reportId => [schoolId => status, ...], ...] $statuses = $obj->getReportStatuses($schoolId); $dates = ($schoolId) ? $obj->getReportFileDates($schoolId) : null; // [status => title, ...] $statusNames = App_Report_AbstractObj::getStatusPairs(); // [reportId => [schoolId => status, ...], ...] $forcedStatuses = $obj->getForcedStatuses($schoolId); $reports = $this->getDataObj()->getListStatement((array) $this->getListOptions())->fetchAll(); $columns = array( 'dateDue' => 'Date Due', 'section' => 'Section', 'topic' => 'Topic', ); if ($schoolId) { $columns[$schoolId] = 'Status'; } else { foreach ($schools as $school) { $columns[$school['id']] = $school['name']; } } $result = array(); $result[] = $columns; $blankRow = array_fill_keys(array_keys($columns), null); foreach ($reports as $report) { $reportId = $report['id']; $csvRow = $blankRow; $csvRow['dateDue'] = (empty($report['dueDateMonth']) || empty($report['dueDateDay'])) ? '-' : App_Report_AbstractObj::getShortMonthName($report['dueDateMonth']) . ' ' . $report['dueDateDay']; $csvRow['section'] = $report['section']; $csvRow['topic'] = $report['topic']; foreach ($schools as $school) { $id = $school['id']; $cellStatus = isset($statuses[$reportId][$id]) ? $statuses[$reportId][$id] : null; $cellStatusForced = isset($forcedStatuses[$reportId][$id]); $cell = (isset($statusNames[$statuses[$reportId][$id]]) ? $statusNames[$statuses[$reportId][$id]] : 'Unknown'); $csvRow[$id] = array( 'status' => $cellStatus, 'statusForced' => $cellStatusForced, 'text' => $cell, 'added' => ($dates && $dates[$reportId][$id]) ? $dates[$reportId][$id] : null, ); } $result[] = $csvRow; } return $result; } }