Framework  3.9
data_column.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
39 {
40  var $template;
41  var $sortable;
42  var $title;
43  var $style;
44  var $typeHint;
45  var $onExport;
47  var $exportOnly = false;
48 
62  function DataColumn($title, $template, $sortable = true, $style = null, $typeHint = null, $onExport = null, $sortFormat = null)
63  {
64  $this->template = $template;
65  $this->sortable = $sortable;
66  $this->title = $title;
67  $this->style = $style;
68  $this->typeHint = $typeHint;
69  $this->onExport = $onExport;
70  $this->sortFormat = $sortFormat;
71  }
72 
79  function format($row)
80  {
81  if (is_callable($this->template))
82  {
83  return call_user_func($this->template, $row);
84  }
85  else return $row->format($this->template);
86  }
87 }
88 
93 abstract class FooterColumn
94 {
95  var $colspan;
96  var $style;
97 
98  function FooterColumn($style = "", $colspan = 1)
99  {
100  $this->colspan = $colspan;
101  $this->style = $style;
102  }
103 
108  abstract function format();
109 }
110 
116 {
117  var $text;
118 
125  function FooterTextColumn($text = "", $style = "", $colspan = 1)
126  {
127  $this->text = $text;
128  $this->FooterColumn($style, $colspan);
129  }
130 
135  function format()
136  {
137  return $this->text;
138  }
139 }
140 
146 {
147  var $callback;
148 
155  function FooterValueColumn($callback, $style = "", $colspan = 1)
156  {
157  $this->callback = $callback;
158  $this->FooterColumn($style, $colspan);
159  }
160 
165  function format()
166  {
167  return call_user_func($this->callback);
168  }
169 }
170 
172 {
173  var $total = 0;
174  var $template = "";
175  var $field = "";
176 
184  function FooterTotalColumn($field, $template = "", $style = "", $colspan = 1, $typeHint = Number)
185  {
186  $this->field = $field;
187  $this->typeHint = $typeHint;
188 
189  /*
190  * If the calling function puts a "$" before the
191  * field name, then mark this as type currency and
192  * output the "$" before the total.
193  */
194  if (!is_callable($this->field) && strpos($this->field, "$", 0) !== FALSE)
195  {
196  $this->typeHint = Currency;
197  $this->field = substr($this->field, 1);
198  }
199 
200  /*
201  * In case the field has template formatting instructions,
202  * move those out to the template variable so that
203  * the field var is just the field and can be totalled
204  * correctly.
205  */
206  $matches = array();
207  if (preg_match("/:(.*?)}/", $this->field, $matches))
208  {
209  $this->template = $matches[1];
210  $this->field = str_replace($matches[1], "", $this->field);
211  }
212 
213  $this->FooterColumn($style, $colspan);
214  }
215 
216  function onStartRow($item)
217  {
218  if (is_callable($this->field))
219  {
220  $val = call_user_func($this->field, $item);
221  }
222  else
223  {
224  $val = $item->format($this->field);
225  }
226 
227  // Make the number a raw number value before adding
228  $val = str_replace(",", "", $val);
229 
230  $this->total += $val;
231  }
232 
233  function format()
234  {
235  if(!$this->typeHint || $this->typeHint == Number)
236  {
237  $renderer = new NumberTypeRenderer();
238  }
239  elseif($this->typeHint == Currency)
240  {
241  $renderer = new CurrencyTypeRenderer();
242  $out = "$";
243  }
244 
245  $out .= $renderer->format($this->total, $this->template);
246  return $out;
247  }
248 }
249 ?>
Represents a column in a DataListView output table.
Definition: data_column.inc:39
DataColumn($title, $template, $sortable=true, $style=null, $typeHint=null, $onExport=null, $sortFormat=null)
Creates a new DataColumn.
Definition: data_column.inc:62
$template
The formatting template or formatter function for this data column.
Definition: data_column.inc:40
format($row)
Outputs the text for the column using the given row.
Definition: data_column.inc:79
$sortFormat
Optional explicit format to use as the basis for sorting.
Definition: data_column.inc:46
$sortable
Boolean value indicating whether this column is sortable.
Definition: data_column.inc:41
$style
Any CSS styles to be applied to this column.
Definition: data_column.inc:43
$onExport
Optional user callback function for exporting the column.
Definition: data_column.inc:45
$exportOnly
Boolean value indicating that the column should only be rendered when exporting to Excel.
Definition: data_column.inc:47
$typeHint
Hint to the Excel formatter about the data type to export.
Definition: data_column.inc:44
$title
The title text for this column.
Definition: data_column.inc:42