<?
/**
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Excel
 * @copyright  Copyright (c) 2009 Goweb (http://www.goweb.pt)
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 */

/** Goweb_Excel_Maker */
Goweb::loadClass('Goweb_Excel_Maker');

/**
 * Goweb_Excel_Select_Excel
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Excel
 * @copyright  Copyright (c) 2009 Goweb (http://www.goweb.pt)
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 */
class Goweb_Excel_Maker_Xls {
    public function sqlToExcel($result, $fileName = 'Goweb', $labels = array()) {
        
        require_once LIBRARY_PATH. '/External/PHPExcel.php';
        require_once LIBRARY_PATH. '/External/PHPExcel/IOFactory.php';
        $workbook = new PHPExcel();
        $workbook->getProperties()->setCreator("Goweb SiteManager 5")
        ->setLastModifiedBy("Goweb SiteManager 5");

        $format_top_center_bold = array(
                    'alignment' => array('vertical' => PHPExcel_Style_Alignment::VERTICAL_TOP,
            							 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER),
                'font' => array( 'bold' => true));
        $linReg  = count($result);
        $linha = 1;
        $worksheet = $workbook->getActiveSheet();

        foreach ($result as $row) {
            $coluna = 0;
            if ($linha == 1){
                //$worksheet->setColumn(0,0,count($row));
                //$worksheet->setColumnAutoWidth(0, count($row));
                foreach ($row as $field => $value) {
                    
                    $worksheet->setCellValueByColumnAndRow($coluna, $linha, html_entity_decode((isset($labels[$field]) ? $labels[$field] : $field), ENT_COMPAT,CHARSET));
                    $worksheet->getStyleByColumnAndRow($coluna, $linha)->applyFromArray($format_top_center_bold);
                    //$worksheet->getStyleByColumnAndRow($coluna, $linha)->getFont()->setBold(true);
                    //$worksheet->writeString($linha, $coluna, (isset($labels[$field]) ? $labels[$field] : $field), $format_top_center_bold);
                    $coluna++;
                }
                $coluna = 0;
                $linha++;
            }
            foreach ($row as $field => $value) {
                $worksheet->setCellValueByColumnAndRow($coluna, $linha, html_entity_decode($value, ENT_COMPAT,CHARSET));
                //$worksheet->writeString($linha, $coluna, $value);
                $coluna++;
            }
            $linha++;
        }
        for ($i=0; $i<$coluna;$i++) $worksheet->getColumnDimensionByColumn($i)->setAutoSize(true);
        $request = Goweb::registry('request');
        $export = $request->export;
        switch ($export) {
        	case 'xls':
        	    $format = 'Excel5';
        	    $extension = 'xls';
        	break;
        	
        	case 'ods':
        	    throw new Goweb_Exception('Not Implemented');
        	    $format = 'OOCalc';
        	    $extension = 'ods';
        	break;
        	
        	case 'xlsx':
        	    $format = 'Excel2007';
        	    $extension = 'xlsx';
        	break;
        	
        	default:
        	    $format = 'CSV';
        	    $extension = 'csv';
        	break;
        }
        
        $objWriter = PHPExcel_IOFactory::createWriter($workbook, $format);
        $filename = trim($fileName) . '_' . date("His").'.'.$extension;
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        header("Content-Transfer-Encoding: binary");
        if (!get_cfg_var('safe_mode')) set_time_limit(0);             
        $objWriter->save('php://output');  
        exit();
    }
}
