<?php

if ($_POST['xls_submit']){

    set_time_limit(10);
    require_once "class.writeexcel_workbook.inc.php";
    require_once "class.writeexcel_worksheet.inc.php";

    $site_id = $_POST['site_id'];
    $site_name = $_POST['site_name'];
    $title = array($_POST['title'], '');

    $fname = tempnam("../tmp", "demo.xls");
    $workbook =& new writeexcel_workbook($fname);
    
    $counter = 0;
    foreach ($_SESSION['XLS_ARRAY']['data'] as $index => $key){
        $name = "worksheet" . $counter++;
        $$name =& $workbook->addworksheet($index);
    }
    
    #######################################################################
    #
    # Define Global Values and Styles
    #

    $title_style  =& $workbook->addformat(array(
        bold    => 1,
        color   => 'black',
        size    => 9));

    $subtitle_style  =& $workbook->addformat(array(
        bold    => 1,
        color   => "gray",
        size    => 14,
        merge   => 1));

    $text_style  =& $workbook->addformat(array(
        color   => "black",
        size    => 8));

    $num1_format =& $workbook->addformat(array(num_format => '#,##0'));

    //$title = array("License Data for site: $site_name", '');

    #######################################################################
    #
    # Loop through session data and build info in tabs
    #

/*
    $tab_counter = 0;
    foreach ($_SESSION["XLS_ARRAY"]['data'] as $index => $key){
        $name = "worksheet" . $tab_counter++;
        
        $subtitle = array("- ".$index." -", "");
        $$name->write_row('A1', $title, $title_style);
		$$name->set_column("A:D", 18);
		$$name->write_row('A2', array('Date', NULL), $num1_format);

		if (is_array($_SESSION["XLS_ARRAY"]['columns'][$index])){

			$letters = array(1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E');
			$column = 2;

			foreach ($_SESSION["XLS_ARRAY"]['columns'][$index] as $header_index => $header_value){
				$$name->write_row($letters[$column] . '2', array($header_value, NULL), $num1_format);
				$column++;
			}
		}
		else
		{
			$$name->write_row('B2', array($index, NULL),  $num1_format);
		}

        $row_counter = 3;
        if (is_array($key)){
            foreach ($key as $key_index => $value_array){

                if (ereg("Page Turn", $index))
                    $date = date("m/y", strtotime($value_array['actual_date']));
                else
                    $date = date("m/d/y", strtotime($value_array['actual_date']));

                $$name->write_row("A".$row_counter, array($date, ""), $text_style);
                $$name->write_row("B".$row_counter, array($value_array['value'], ""), $num1_format);
				$$name->write_row("C".$row_counter, array($value_array['value2'], ""), $num1_format);
				$$name->write_row("D".$row_counter, array($value_array['value3'], ""), $num1_format);
                $$name->write_row("E".$row_counter, array($value_array['name'], ""), $text_style);
                $row_counter++;
            }
        }
	}
*/

    $tab_counter = 0;
    foreach ($_SESSION["XLS_ARRAY"]['data'] as $index => $key){
        $name = "worksheet" . $tab_counter++;

        $subtitle = array("- ".$index." -", "");
        $$name->write_row('A1', $title, $title_style);
        $$name->set_column("A:D", 18);
        $$name->write_row('A2', array('Date', NULL), $num1_format);

        if (is_array($_SESSION["XLS_ARRAY"]['columns'][$index])){

            $letters = array(1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E');
            $column = 2;

            foreach ($_SESSION["XLS_ARRAY"]['columns'][$index] as $header_index => $header_value){
                $$name->write_row($letters[$column] . '2', array($header_value, NULL), $num1_format);
                $column++;
            }
        }
        else
        {
            $$name->write_row('B2', array($index, NULL),  $num1_format);
        }

        //$$name->set_column("A:B", 16);

        $row_counter = 3;
        if (is_array($key)){
            foreach ($key as $key_index => $value_array){

                if (ereg("Page Turn", $index))
                    $date = date("m/y", strtotime($value_array['actual_date']));
                else
                    $date = date("m/d/y", strtotime($value_array['actual_date']));

                $$name->write_row("A".$row_counter, array($date, ""), $text_style);
                $$name->write_row("B".$row_counter, array($value_array['value'], ""), $num1_format);
                $$name->write_row("C".$row_counter, array($value_array['value2'], ""), $num1_format);
                $$name->write_row("D".$row_counter, array($value_array['value3'], ""), $num1_format);
                $$name->write_row("E".$row_counter, array($value_array['value4'], ""), $num1_format);
                $$name->write_row("F".$row_counter, array($value_array['name'], ""), $text_style);
                $row_counter++;
            }
        }
	}

    #######################################################################
    #
    # Some text examples
    #

    /*
    $text_format =& $workbook->addformat(array(
        bold    => 1,
        italic  => 1,
        color   => "red",
        size    => 18,
        font    => "Comic Sans MS"));
    */

    /*$worksheet->write('A2', "Text");
    $worksheet->write('B2', "Hello Excel");
    $worksheet->write('A3', "Formatted text");
    $worksheet->write('B3', "Hello Excel", $text_format);*/

    #######################################################################
    #
    # Some numeric examples
    #

    /*
    $num1_format =& $workbook->addformat(array(num_format => '$#,##0.00'));
    $num2_format =& $workbook->addformat(array(num_format => ' d mmmm yyy'));

    $worksheet->write('A4', "Numbers");
    $worksheet->write('B4', 1234.56);
    $worksheet->write('A5', "Formatted numbers");
    $worksheet->write('B5', 1234.56, $num1_format);
    $worksheet->write('A6', "Formatted numbers");
    $worksheet->write('B6', 37257, $num2_format);
    */

    #######################################################################
    #
    # Formulae
    #
    /*
    $worksheet->set_selection('B7');
    $worksheet->write('A7', 'Formulas and functions, "=SIN(PI()/4)"');
    $worksheet->write('B7', '=SIN(PI()/4)');
    */

    #######################################################################
    #
    # Hyperlinks
    #

    //$worksheet->write('A8', "Hyperlinks");
    //$worksheet->write('B8',  'http://www.php.net/');

    #######################################################################
    #
    # Images
    #

    //$worksheet1->write("A9", "Images");
    //$worksheet1->insert_bitmap("B9", "tmp/popchart_img/Page_Turns_(Hits)_10515.bmp", 16, 8);

    #######################################################################
    #
    # Misc
    #

    //$worksheet->write('A17', "Page/printer setup");
    //$worksheet->write('A18', "Multiple worksheets");

    #######################################################################
    #
    # Close file and send headers to browser
    #


    $workbook->close();

    header ("Expires: Mon, 1 Apr 1974 05:00:00 GMT");
    header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
    header ("Pragma: private");
    header ("Cache-control: private, must-revalidate");
    header ("Content-Type: application/x-msexcel");
    header ("Content-Disposition: attachment; filename=csi.xls");
    header ("Content-Description: PHP Generated XLS Data");

    $fh = fopen($fname, "rb");
    fpassthru($fh);
    unlink($fname);
}

?>
