<?php
/**
 * PHPExcel Facade class.
 * 
 * If you've tried to program using the PHPExcel package, you might find it's API rather
 * cumbersome to use just to create a simple Excel file.
 * 
 * This Facade class tries to harness the basic functionality of the package, and simplifies
 * the API to make it really easy and quick.
 * 
 * This currently outputs the file to the browser in Excel 5 format.
 * 
 * It can easily be changed to output in Excel 2007 format, but since 2007 can read 5 and the eariler versions
 * of excel can read 5 but not Excel 2007 format using Excel 5 format seems like a no-brainer.
 * 
 * @author Kevin Korb - http://www.kevinkorb.com
 *
 */
class PHPExcelFacade {
    
    private 
$name_of_file;
    private 
$writable_dir;
    private 
$objPHPExcel;
    
    public function 
__construct($name_of_file$writable_dir_ending_in_slash) {
        
$this->name_of_file $name_of_file;
        
$this->writable_dir $writable_dir_ending_in_slash;
        
        
$this->objPHPExcel = new PHPExcel();
        
$this->setMetaData();
        
$this->setActiveSheet(0);
    }
    
    public function 
outputToBrowser() {
        require_once(
"PHPExcel/Writer/Excel5.php");
        
$objWriter = new PHPExcel_Writer_Excel5($this->objPHPExcel);
        
$filename $this->writable_dir.rand(1,99999999).'.xls';
        if(!
is_writable($this->writable_dir)) {
            throw new 
Exception("Directory: $this->writable_dir is not writable.");
        }
        
$objWriter->save($filename);
        
$contents file_get_contents($filename);
        
unlink($filename);
        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: private");
        
header("Content-type: application/octet-stream");
        
header("Content-Disposition: attachment; filename=$this->name_of_file");
        
header("Accept-Ranges: bytes");
        echo 
$contents;
        exit;
    }
    
    public function 
setActiveSheet($int) {
        
$this->objPHPExcel->setActiveSheetIndex($int);
    }
    
    public function 
setMetaData($creator ''$last_modified_by ''$title ''$subject ''$description '') {
        
$this->objPHPExcel->getProperties()->setCreator($creator);
        
$this->objPHPExcel->getProperties()->setLastModifiedBy($last_modified_by);
        
$this->objPHPExcel->getProperties()->setTitle($title);
        
$this->objPHPExcel->getProperties()->setSubject($subject);
        
$this->objPHPExcel->getProperties()->setDescription($description);
    }
    
    public function 
setText($cell$text$bold false$underline false$size false$font false) {
        
$this->objPHPExcel->getActiveSheet()->SetCellValue($cell$text);
        if(
$bold !== false) {
            
$this->objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->setBold(true);
        }
        if(
$underline !== false) {
            
$this->objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
        }
        if(
$size !== false) {
            
$this->objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->setSize($size);
        }
        if(
$font !== false) {
            throw new 
Exception("FONTS HAVE NOT BEEN IMPLEMENTED IN THE FACADE CLASS YET");
        }
    }
    
    public function 
setCellForegroundColor($cell$colorRGB) {
        
$this->objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->getColor()->setRGB($colorRGB);
    }
    
    public function 
mergeCells($top_left$bottom_right) {
        
$this->objPHPExcel->getActiveSheet()->mergeCells("$top_left:$bottom_right");
    }
    
    public function 
setColumnWidth($column 'A'$width_inches 12) {
        
$this->objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth($width_inches);
    }
    
    public function 
setCellHorizontalAllignment($cell$alignment '(center|left|right)') {
        switch(
strtolower($alignment)) {
            case 
"center":
                
$align PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
                break;
            case 
"left":
                
$align PHPExcel_Style_Alignment::HORIZONTAL_LEFT;
                break;
            case 
"right":
                
$align PHPExcel_Style_Alignment::HORIZONTAL_RIGHT;
                break;
            default:
                
$align PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
                break;
        }
        
$this->objPHPExcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal($align);
    }
    
}