Work in progress enhancing week view.
[timetracker.git] / WEB-INF / lib / form / Table.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('form.FormElement');
30 import('form.TableColumn');
31
32 class Table extends FormElement {
33
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 $mFooters       = array(); // column footers
38   var $mInteractive   = true;    // adds a clickable checkbox column to table
39   var $mIAScript      = null;    // sctipt to execute when a checkbox is clicked
40   var $mKeyField      = '';      // identifies a column used as key to access row data
41   var $mColumnFields  = array(); // field names (from mData) for data in each column
42   var $mBgColor       = '#ffffff';
43   var $mBgColorOver   = '#eeeeff';
44   var $mWidth         = '';
45   var $mTableOptions  = array();
46   var $mRowOptions    = array();
47   var $mHeaderOptions = array();
48   var $mProccessed    = false;
49         
50   function __construct($name, $cssClass = null) {
51     $this->class = 'Table';
52     $this->name = $name;
53     $this->cssClass = $cssClass;
54   }
55   
56   function setKeyField($value) {
57     $this->mKeyField = $value;
58   }
59   
60   function setData($data) {
61     if (is_array($data) && isset($data[0]) && is_array($data[0]))
62       $this->mData = &$data;
63   }
64   
65   function addColumn($column) {
66     if ($column != null) $column->setTable($this);
67     $this->mColumns[] = &$column;
68   }
69   
70   function setInteractive($value) { $this->mInteractive = $value; }
71   function isInteractive() { return $this->mInteractive; }
72   
73   function setIAScript($value) { $this->mIAScript = $value; }
74   function getIAScript() { return $this->mIAScript; }
75                 
76   function setWidth($value) { $this->mWidth = $value; }
77   function getWidth() { return $this->mWidth; }
78
79   function setTableOptions($value) { $this->mTableOptions = $value; }
80   function getTableOptions() { return $this->mTableOptions; }
81   
82   function setRowOptions($value) { $this->mRowOptions = $value; }
83   function getRowOptions() { return $this->mRowOptions; }
84
85   function setHeaderOptions($value) { $this->mHeaderOptions = $value; }
86   function getHeaderOptions() { return $this->mHeaderOptions; }
87   
88   function getValueAt($rowindex, $colindex) {
89     if (!$this->mProccessed) $this->_process();
90     return @$this->mData[$rowindex][$this->mColumnFields[$colindex]];
91   }
92         
93   function getValueAtName($rowindex, $fieldname) {
94     if (!$this->mProccessed) $this->_process();
95     return @$this->mData[$rowindex][$fieldname];
96   }
97
98   function _process() {
99     $this->mProccessed = true;
100         
101     if ($this->mInteractive) {
102       // Add a column of clickable checkboxes.
103       $column = new TableColumn("","<input type=\"checkbox\" name=\"".$this->name."_all\" onclick=\"setAll(this.checked)\">");
104       import('form.CheckboxCellRenderer');
105       $cb = new CheckboxCellRenderer();
106       if ($this->getIAScript()) $cb->setOnChangeAdd($this->getIAScript()."(this)");
107       $column->setRenderer($cb);
108       $column->setTable($this);
109       array_unshift($this->mColumns, $column);
110     }
111     
112     foreach ($this->mColumns as $column) {
113       $this->mColumnFields[] = $column->getField();
114       $this->mHeaders[] = $column->getHeader();
115       $this->mFooters[] = $column->getFooter();
116     }
117   }
118   
119   function getHtml() {
120     if (!$this->mProccessed) $this->_process();
121     
122     $html = "";
123     if ($this->mInteractive) $html .= $this->_addJavaScript();
124
125     $html .= "<table";
126     if ($this->cssClass) {
127       $html .= " class=\"".$this->cssClass."\"";
128     }
129     if (count($this->mTableOptions) > 0) {
130       foreach ($this->mTableOptions as $k=>$v) {
131         $html .= " $k=\"$v\"";
132       }
133     } else {
134       $html .= " border=\"1\"";
135     }
136     if ($this->mWidth!="") $html .= " width=\"".$this->mWidth."\"";
137     $html .= ">\n";
138     
139     // Print headers.
140     if (($this->mInteractive && (count($this->mHeaders) > 1)) || (!$this->mInteractive && (count($this->mHeaders) > 0))) {
141       $html .= "<tr";
142       if (count($this->mRowOptions) > 0) {
143         foreach ($this->mRowOptions as $k=>$v) {
144           $html .= " $k=\"$v\"";
145         }
146       }
147       $html .= ">\n";
148       foreach ($this->mHeaders as $header) {
149         $html .= "<th";
150         if (count($this->mHeaderOptions) > 0) {
151           foreach ($this->mHeaderOptions as $k=>$v) {
152                 $html .= " $k=\"$v\"";
153           }
154         }
155         $html .= ">$header</th>\n";
156       }
157       $html .= "</tr>\n";
158     }
159     
160     // Print rows.
161     for ($row = 0; $row < count($this->mData); $row++) {
162       $html .= "\n<tr bgcolor=\"".$this->mBgColor."\" onmouseover=\"setRowBackground(this, '".$this->mBgColorOver."')\" onmouseout=\"setRowBackground(this, null)\">\n";
163       for ($col = 0; $col < $this->getColumnCount(); $col++) {
164         if (0 == $col && strtolower(get_class($this->mColumns[$col]->getRenderer())) == 'checkboxcellrenderer') {
165           // Checkbox for the row. Determine if selected.
166           $selected = false;
167           if (is_array($this->value)) {
168             foreach ($this->value as $p) {
169               if ($p == $this->mData[$row][$this->mKeyField]) {
170                 $selected = true;
171                 break;
172               }
173             }
174           }
175           // Render control checkbox.
176           $html .= $this->mColumns[$col]->renderCell($this->mData[$row][$this->mKeyField], $row, $col, $selected);
177         } else {
178           // Render regular cell.
179           $html .= $this->mColumns[$col]->renderCell($this->getValueAt($row, $col), $row, $col);
180         }
181       }
182       $html .= "</tr>\n";
183     }
184
185     // Print footers.
186     if (($this->mInteractive && (count($this->mFooters) > 1)) || (!$this->mInteractive && (count($this->mFooters) > 0))) {
187       $html .= "<tr";
188       if (count($this->mRowOptions) > 0) {
189         foreach ($this->mRowOptions as $k=>$v) {
190           $html .= " $k=\"$v\"";
191         }
192       }
193       $html .= ">\n";
194       foreach ($this->mFooters as $footer) {
195         $html .= "<th";
196         if (count($this->mHeaderOptions) > 0) {
197           foreach ($this->mHeaderOptions as $k=>$v) {
198             $html .= " $k=\"$v\"";
199           }
200         }
201         $html .= ">$footer</th>\n";
202       }
203       $html .= "</tr>\n";
204     }
205
206     $html .= "</table>";
207     return $html;
208   }
209   
210   function getColumnCount() {
211     return count($this->mColumns);
212   }
213
214   function _addJavaScript() {
215     $html = "<script>\n";
216     // setAll - checks / unchecks all checkboxes in the table.
217     $html .= "function setAll(value) {\n";
218     $html .= "\tfor (var i = 0; i < ".$this->getFormName().".elements.length; i++) {\n";
219     $html .= "\t\tif ((".$this->getFormName().".elements[i].type=='checkbox') && (".$this->getFormName().".elements[i].name=='".$this->name."[]')) {\n";
220     $html .= "\t\t\t".$this->getFormName().".elements[i].checked=value;\n";
221     if ($this->getIAScript()) {
222       $html .= "\t\t\t".$this->getIAScript()."(".$this->getFormName().".elements[i]);\n";
223     }
224     $html .= "\t}}\n";
225     $html .= "}\n\n";
226     
227     $html .= "var rowBgColors;\n";
228     // setRowBackground - sets background for a row.
229     $html .= "function setRowBackground(theRow, thePointerColor) {\n";
230     $html .= "\tif (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {\n";
231     $html .= "\t\treturn false;\n\t}\n\n";
232     $html .= "\tvar row_cells_cnt = theRow.cells.length;\n";
233     $html .= "\tif (thePointerColor != null) {\n";
234     $html .= "\t\trowBgColors = new Array(row_cells_cnt);\n";
235     $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
236     $html .= "\t\t\trowBgColors[c]=theRow.cells[c].bgColor;\n\t\t}\n";
237     $html .= "\t}\n";
238     $html .= "\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
239     $html .= "\t\ttheRow.cells[c].bgColor = thePointerColor;\n\t}\n\n";
240     $html .= "\tif (thePointerColor == null) {\n";
241     $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
242     $html .= "\t\t\ttheRow.cells[c].bgColor=rowBgColors[c];\n\t\t}\n";
243     $html .= "\t\tdelete rowBgColors;\n";
244     $html .= "\t}\n";
245     $html .= "\treturn true;\n}\n";
246     $html .= "</script>\n";
247     
248     return $html;
249   }
250 }