/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
require($EXCEL_WRITER_PATH . "Spreadsheet/Excel/Writer.php");

class EXCELHandler extends DATAHandler {
 var $xls;
 var $xls_time_format;
 var $xls_time_subsec_format;
 var $xls_data_format;
 var $sheet;

 var $row;

 var $time_format;
 var $time_subsec_format;
 var $data_format;
 var $time_width;
 var $data_width;
 
 var $string_format;
 var $tmpfile = false;
 
 var $collisions = array();
  
 const MAX_TITLE_CHARS = 31;
 
 function __construct(&$opts = NULL, STREAMHandler $h  = NULL) {
    global $CSV_DATE_FORMAT;
    global $EXCEL_DATE_FORMAT;
    global $EXCEL_SUBSEC_FORMAT;
    
    $this->content_type = "application/vnd.ms-excel";
    $this->extension = "xls"; 

    parent::__construct($opts, $h);
    
    $this->multigroup = true;
    $this->filewriter = true;

    if ($opts) {
	$this->time_format = $opts['date_format'];
	$this->time_width = $opts['date_width'];
	$this->time_subsec_format = $opts['subsec_format'];
	$this->time_subsec_width = $opts['subsec_width'];
	$this->data_format = $opts['value_format'];
	$this->data_width = $opts['value_width'];
    }

    if (!$this->time_format) $this->time_format = $EXCEL_DATE_FORMAT;
    if (!$this->time_width) $this->time_width = 20;
    if (!$this->time_subsec_format) $this->time_subsec_format = $EXCEL_SUBSEC_FORMAT;
    if (!$this->time_subsec_width) $this->time_width = 26;
    if (!$this->data_format) $this->data_format = "0.0000E+##";
    if (!$this->data_width) $this->data_width = 12;
    
    if (preg_match('/^\s*text\s*(\((.*)\))?\s*$/i', $this->time_subsec_format, $m)) {
	$this->time_subsec_format = false;
	if ($m[2]) $this->string_format = $m[2];
	else $this->string_format = $CSV_DATE_FORMAT;
    }
 }

 function SequenceStart($flags = 0) {
    parent::SequenceStart($flags);
    
    if ((!$this->h)||($this->h->stdout)) {
	$this->tmpfile = false;
	$this->xls = new Spreadsheet_Excel_Writer();
    } else {
	$this->tmpfile = GetTmpFile("adei_stream_excel_", "xls");
	$this->xls = new Spreadsheet_Excel_Writer($this->tmpfile);
    }

    $this->xls_time_format = $this->xls->addFormat();
    $this->xls_time_format->setNumFormat($this->time_format);
    if ($this->time_subsec_format) {
	$this->xls_time_subsec_format = $this->xls->addFormat();
	$this->xls_time_subsec_format->setNumFormat($this->time_subsec_format);
    }
    $this->xls_data_format = $this->xls->addFormat();
    $this->xls_data_format->setNumFormat($this->data_format);
 }

 function GroupStart($title, $subseconds = false, $flags = 0) {
    $fixed_title = preg_replace('/[^\w\d_]+/', '_', $title);
    $val = substr($fixed_title, 0, EXCELHandler::MAX_TITLE_CHARS);

    $this->sheet = $this->xls->addWorksheet($val);
    if ($this->sheet instanceof PEAR_Error) {
	    // Handling name collisions
	if (strlen($fixed_title) >  EXCELHandler::MAX_TITLE_CHARS) {
	    if (isset($this->collisions[$val])) $this->collisions[$val]++;
	    else $this->collisions[$val] = 1;
	    $val = substr($val, 0, EXCELHandler::MAX_TITLE_CHARS - strlen($this->collisions[$val]) - 1) . "@" . $this->collisions[$val];
	    $this->sheet = $this->xls->addWorksheet($val);
	    if ($this->sheet instanceof PEAR_Error) {
		throw new ADEIException(translate("Error encountered while creating Excel sheet (%s). Info: %s", $title, $this->sheet->toString()));
	    }
	}
    }

    $this->row = 0;
    
    parent::GroupStart($title, $subseconds, $flags);
 }

 function SequenceEnd($flags = 0) {
    $this->xls->close();
    
    if ($this->tmpfile) {
	if ($this->filemode)
	    $this->h->WriteData($this->tmpfile);
	else
	    $this->h->WriteFile($this->tmpfile);
	unlink($this->tmpfile);
    }
    
    parent::SequenceEnd($flags);
    
 }

 function DataHeaders(&$names, $flags = 0) {
    $this->sheet->write(0,0,"Date/Time");
    $this->sheet->setColumn(0,0,$this->time_width);
    
    foreach (array_keys($names) as $i) {
	$this->sheet->write(0, $i + 1, $names[$i]['name']);
        $this->sheet->setColumn(0, $i + 1, $this->data_width);
    }
 }

 function DataVector(&$time, &$values, $flags = 0) {
    if ($this->subseconds) {
	if ($this->time_subsec_format) {	
	    $val = dsMathPreciseAdd($time, 2209161600) / 86400; /* 86400 * 25569 */
    	    $this->sheet->write(++$this->row, 0, $val, $this->xls_time_subsec_format);
	} else {
	    $subsec = strchr(sprintf("%.6F", $time), '.');
	    $val = date($this->string_format, $time) . $subsec;
	    $this->sheet->write(++$this->row, 0, $val);
	}
    } else {
	$val = ($time + 2209161600) / 86400; /* 86400 * 25569 */
    	$this->sheet->write(++$this->row, 0, $val, $this->xls_time_format);
    }
    
    foreach (array_keys($values) as $i) {
	$this->sheet->write($this->row, $i + 1, $values[$i], $this->xls_data_format);
    }
 }
 
}

?>