2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('form.FormElement');
30 import('form.TableColumn');
32 class Table extends FormElement {
34 var $mColumns = array(); // array of columns in table
35 var $mData = null; // array of rows with data for column cells
36 var $mHeaders = array(); // column headers
37 var $mInteractive = true; // adds a clickable checkbox column to table
38 var $mIAScript = null; // sctipt to execute when a checkbox is clicked
39 var $mKeyField = ''; // identifies a column used as key to access row data
40 var $mColumnFields = array(); // field names (from mData) for data in each column
41 var $mBgColor = '#ffffff';
42 var $mBgColorOver = '#eeeeff';
44 var $mTableOptions = array();
45 var $mRowOptions = array();
46 var $mHeaderOptions = array();
47 var $mProccessed = false;
49 function __construct($name) {
50 $this->class = 'Table';
54 function setKeyField($value) {
55 $this->mKeyField = $value;
58 function setData($data) {
59 if (is_array($data) && isset($data[0]) && is_array($data[0]))
60 $this->mData = &$data;
63 function addColumn($column) {
64 if ($column != null) $column->setTable($this);
65 $this->mColumns[] = &$column;
68 function setInteractive($value) { $this->mInteractive = $value; }
69 function isInteractive() { return $this->mInteractive; }
71 function setIAScript($value) { $this->mIAScript = $value; }
72 function getIAScript() { return $this->mIAScript; }
74 function setWidth($value) { $this->mWidth = $value; }
75 function getWidth() { return $this->mWidth; }
77 function setTableOptions($value) { $this->mTableOptions = $value; }
78 function getTableOptions() { return $this->mTableOptions; }
80 function setRowOptions($value) { $this->mRowOptions = $value; }
81 function getRowOptions() { return $this->mRowOptions; }
83 function setHeaderOptions($value) { $this->mHeaderOptions = $value; }
84 function getHeaderOptions() { return $this->mHeaderOptions; }
86 function getValueAt($rowindex, $colindex) {
87 if (!$this->mProccessed) $this->_process();
88 return @$this->mData[$rowindex][$this->mColumnFields[$colindex]];
91 function getValueAtName($rowindex,$fieldname) {
92 if (!$this->mProccessed) $this->_process();
93 return @$this->mData[$rowindex][$fieldname];
97 $this->mProccessed = true;
99 if ($this->mInteractive) {
100 // Add a column of clickable checkboxes.
101 $column = new TableColumn("","<input type=\"checkbox\" name=\"".$this->name."_all\" onclick=\"setAll(this.checked)\">");
102 import('form.CheckboxCellRenderer');
103 $cb = new CheckboxCellRenderer();
104 if ($this->getIAScript()) $cb->setOnChangeAdd($this->getIAScript()."(this)");
105 $column->setRenderer($cb);
106 $column->setTable($this);
107 array_unshift($this->mColumns, $column);
110 foreach ($this->mColumns as $column) {
111 $this->mColumnFields[] = $column->getField();
112 $this->mHeaders[] = $column->getHeader();
117 if (!$this->mProccessed) $this->_process();
120 if ($this->mInteractive) $html .= $this->_addJavaScript();
123 if (count($this->mTableOptions) > 0) {
124 foreach ($this->mTableOptions as $k=>$v) {
125 $html .= " $k=\"$v\"";
128 $html .= " border=\"1\"";
130 if ($this->mWidth!="") $html .= " width=\"".$this->mWidth."\"";
134 if (($this->mInteractive && (count($this->mHeaders) > 1)) || (!$this->mInteractive && (count($this->mHeaders) > 0))) {
136 if (count($this->mRowOptions) > 0) {
137 foreach ($this->mRowOptions as $k=>$v) {
138 $html .= " $k=\"$v\"";
142 foreach ($this->mHeaders as $header) {
144 if (count($this->mHeaderOptions) > 0) {
145 foreach ($this->mHeaderOptions as $k=>$v) {
146 $html .= " $k=\"$v\"";
149 $html .= ">$header</th>\n";
155 for ($row = 0; $row < count($this->mData); $row++) {
156 $html .= "\n<tr bgcolor=\"".$this->mBgColor."\" onmouseover=\"setRowBackground(this, '".$this->mBgColorOver."')\" onmouseout=\"setRowBackground(this, null)\">\n";
157 for ($col = 0; $col < $this->getColumnCount(); $col++) {
158 if (0 == $col && strtolower(get_class($this->mColumns[$col]->getRenderer())) == 'checkboxcellrenderer') {
159 // Checkbox for the row. Determine if selected.
161 if (is_array($this->value)) {
162 foreach ($this->value as $p) {
163 if ($p == $this->mData[$row][$this->mKeyField]) {
169 // Render control checkbox.
170 $html .= $this->mColumns[$col]->renderCell($this->mData[$row][$this->mKeyField], $row, $col, $selected);
172 // Render regular cell.
173 $html .= $this->mColumns[$col]->renderCell($this->getValueAt($row, $col), $row, $col);
183 function getColumnCount() {
184 return count($this->mColumns);
187 function _addJavaScript() {
188 $html = "<script>\n";
189 // setAll - checks / unchecks all checkboxes in the table.
190 $html .= "function setAll(value) {\n";
191 $html .= "\tfor (var i = 0; i < ".$this->getFormName().".elements.length; i++) {\n";
192 $html .= "\t\tif ((".$this->getFormName().".elements[i].type=='checkbox') && (".$this->getFormName().".elements[i].name=='".$this->name."[]')) {\n";
193 $html .= "\t\t\t".$this->getFormName().".elements[i].checked=value;\n";
194 if ($this->getIAScript()) {
195 $html .= "\t\t\t".$this->getIAScript()."(".$this->getFormName().".elements[i]);\n";
200 $html .= "var rowBgColors;\n";
201 // setRowBackground - sets background for a row.
202 $html .= "function setRowBackground(theRow, thePointerColor) {\n";
203 $html .= "\tif (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {\n";
204 $html .= "\t\treturn false;\n\t}\n\n";
205 $html .= "\tvar row_cells_cnt = theRow.cells.length;\n";
206 $html .= "\tif (thePointerColor != null) {\n";
207 $html .= "\t\trowBgColors = new Array(row_cells_cnt);\n";
208 $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
209 $html .= "\t\t\trowBgColors[c]=theRow.cells[c].bgColor;\n\t\t}\n";
211 $html .= "\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
212 $html .= "\t\ttheRow.cells[c].bgColor = thePointerColor;\n\t}\n\n";
213 $html .= "\tif (thePointerColor == null) {\n";
214 $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
215 $html .= "\t\t\ttheRow.cells[c].bgColor=rowBgColors[c];\n\t\t}\n";
216 $html .= "\t\tdelete rowBgColors;\n";
218 $html .= "\treturn true;\n}\n";
219 $html .= "</script>\n";