~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/util/agExcel2003ExportHelper.class.php

  • Committer: Fabio Albuquerque
  • Date: 2011-08-08 22:10:34 UTC
  • mfrom: (1.26.1 push-trunk)
  • Revision ID: fabiocbalbuquerque@gmail.com-20110808221034-vfr4ggsbxsdllq82
Merges from CUNY SPS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Provides entity phone helper functions and inherits several methods and properties from the
 
5
 * bulk record helper.
 
6
 *
 
7
 * PHP Version 5.3
 
8
 *
 
9
 * LICENSE: This source file is subject to LGPLv2.1 license
 
10
 * that is available through the world-wide-web at the following URI:
 
11
 * http://www.gnu.org/licenses/lgpl-2.1.html
 
12
 *
 
13
 *  Based on the work of Simple excel writer class 
 
14
 * @author Matt Nowack
 
15
 * @license Unlicensed
 
16
 * @link https://gist.github.com/929039
 
17
 * 
 
18
 * @author Clayton Kramer, CUNY SPS
 
19
 *
 
20
 * Copyright of the Sahana Software Foundation, sahanafoundation.org
 
21
 */
 
22
class agExcel2003ExportHelper
 
23
{
 
24
 
 
25
  private $col;
 
26
  private $row;
 
27
  private $data;
 
28
  private $title;
 
29
 
 
30
  /**
 
31
   * Safely encode a string for use as a filename
 
32
   * @param string $title The title to use for the file
 
33
   * @return string The file safe title
 
34
   */
 
35
  static function filename($title)
 
36
  {
 
37
    $result = strtolower(trim($title));
 
38
    $result = str_replace("'", '', $result);
 
39
    $result = preg_replace('#[^a-z0-9_]+#', '-', $result);
 
40
    $result = preg_replace('#\-{2,}#', '-', $result);
 
41
    return preg_replace('#(^\-+|\-+$)#D', '', $result);
 
42
  }
 
43
 
 
44
  /**
 
45
   * Builds a new Excel Spreadsheet object
 
46
   * @return Excel The Spreadsheet
 
47
   */
 
48
  function __construct($title)
 
49
  {
 
50
    $this->title = $title;
 
51
    $this->col = 0;
 
52
    $this->row = 0;
 
53
    $this->data = '';
 
54
    $this->bofMarker();
 
55
  }
 
56
 
 
57
  /**
 
58
   * Transmits the proper headers to cause a download to occur and to identify the file properly
 
59
   * @return nothing
 
60
   */
 
61
  function headers()
 
62
  {
 
63
    header("Content-Type: application/force-download");
 
64
    header("Content-Type: application/octet-stream");
 
65
    header("Content-Type: application/download");
 
66
    header("Content-Disposition: attachment;filename=" . Excel::filename($this->title) . ".xls ");
 
67
    header("Content-Transfer-Encoding: binary ");
 
68
  }
 
69
 
 
70
  /**
 
71
   * Streams the data to the browser
 
72
   * @return nothing
 
73
   */
 
74
  function send()
 
75
  {
 
76
    $this->eofMarker();
 
77
    $this->headers();
 
78
    echo $this->data;
 
79
  }
 
80
 
 
81
  /**
 
82
   * Saves XLS file to a file path location
 
83
   * @param type $file 
 
84
   * @return nothing
 
85
   */
 
86
  function save($file)
 
87
  {
 
88
    $this->eofMarker();
 
89
    $fh = fopen($file, 'w') or die("Error: can't open $file for writing.\n");
 
90
    fwrite($fh, $this->data);
 
91
    fclose($fh);
 
92
  }
 
93
 
 
94
  /**
 
95
   * Writes the Excel Beginning of File marker
 
96
   * @see pack()
 
97
   * @return nothing
 
98
   */
 
99
  private function bofMarker()
 
100
  {
 
101
    $this->data .= pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
 
102
  }
 
103
 
 
104
  /**
 
105
   * Writes the Excel End of File marker
 
106
   * @see pack()
 
107
   * @return nothing
 
108
   */
 
109
  private function eofMarker()
 
110
  {
 
111
    $this->data .= pack("ss", 0x0A, 0x00);
 
112
  }
 
113
 
 
114
  /**
 
115
   * Moves internal cursor left by the amount specified
 
116
   * @param optional integer $amount The amount to move left by, defaults to 1
 
117
   * @return integer The current column after the move
 
118
   */
 
119
  function left($amount = 1)
 
120
  {
 
121
    $this->col -= $amount;
 
122
    if ($this->col < 0) {
 
123
      $this->col = 0;
 
124
    }
 
125
    return $this->col;
 
126
  }
 
127
 
 
128
  /**
 
129
   * Moves internal cursor right by the amount specified
 
130
   * @param optional integer $amount The amount to move right by, defaults to 1
 
131
   * @return integer The current column after the move
 
132
   */
 
133
  function right($amount = 1)
 
134
  {
 
135
    $this->col += $amount;
 
136
    return $this->col;
 
137
  }
 
138
 
 
139
  /**
 
140
   * Moves internal cursor up by amount
 
141
   * @param optional integer $amount The amount to move up by, defaults to 1
 
142
   * @return integer The current row after the move
 
143
   */
 
144
  function up($amount = 1)
 
145
  {
 
146
    $this->row -= $amount;
 
147
    if ($this->row < 0) {
 
148
      $this->row = 0;
 
149
    }
 
150
    return $this->row;
 
151
  }
 
152
 
 
153
  /**
 
154
   * Moves internal cursor down by amount
 
155
   * @param optional integer $amount The amount to move down by, defaults to 1
 
156
   * @return integer The current row after the move
 
157
   */
 
158
  function down($amount = 1)
 
159
  {
 
160
    $this->row += $amount;
 
161
    return $this->row;
 
162
  }
 
163
 
 
164
  /**
 
165
   * Moves internal cursor to the top of the page, row = 0
 
166
   * @return nothing
 
167
   */
 
168
  function top()
 
169
  {
 
170
    $this->row = 0;
 
171
  }
 
172
 
 
173
  /**
 
174
   * Moves internal cursor all the way left, col = 0
 
175
   * @return nothing
 
176
   */
 
177
  function home()
 
178
  {
 
179
    $this->col = 0;
 
180
  }
 
181
 
 
182
  /**
 
183
   * Writes a number to the Excel Spreadsheet
 
184
   * @see pack()
 
185
   * @param integer $value The value to write out
 
186
   * @return nothing
 
187
   */
 
188
  function number($value)
 
189
  {
 
190
    $this->data .= pack("sssss", 0x203, 14, $this->row, $this->col, 0x0);
 
191
    $this->data .= pack("d", $value);
 
192
  }
 
193
 
 
194
  /**
 
195
   * Writes a string (or label) to the Excel Spreadsheet
 
196
   * @see pack()
 
197
   * @param string $value The value to write out
 
198
   * @return nothing
 
199
   */
 
200
  function label($value)
 
201
  {
 
202
    $length = strlen($value);
 
203
    $this->data .= pack("ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length);
 
204
    $this->data .= $value;
 
205
  }
 
206
 
 
207
  /**
 
208
   * Get's the current row number
 
209
   * @return int
 
210
   */
 
211
  function get_row()
 
212
  {
 
213
    return $this->row;
 
214
  }
 
215
 
 
216
}
 
217
 
 
218
?>
 
 
b'\\ No newline at end of file'