e5aed81dc67b894bbc77bc1820099579a498c6c3
[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) {
51     $this->class = 'Table';
52     $this->name = $name;
53   }
54   
55   function setKeyField($value) {
56         $this->mKeyField = $value;
57   }
58   
59   function setData($data) {
60         if (is_array($data) && isset($data[0]) && is_array($data[0]))
61       $this->mData = &$data;
62   }
63   
64   function addColumn($column) {
65     if ($column != null) $column->setTable($this);
66     $this->mColumns[] = &$column;
67   }
68   
69   function setInteractive($value) { $this->mInteractive = $value; }
70   function isInteractive() { return $this->mInteractive; }
71   
72   function setIAScript($value) { $this->mIAScript = $value; }
73   function getIAScript() { return $this->mIAScript; }
74                 
75   function setWidth($value) { $this->mWidth = $value; }
76   function getWidth() { return $this->mWidth; }
77
78   function setTableOptions($value) { $this->mTableOptions = $value; }
79   function getTableOptions() { return $this->mTableOptions; }
80   
81   function setRowOptions($value) { $this->mRowOptions = $value; }
82   function getRowOptions() { return $this->mRowOptions; }
83
84   function setHeaderOptions($value) { $this->mHeaderOptions = $value; }
85   function getHeaderOptions() { return $this->mHeaderOptions; }
86   
87   function getValueAt($rowindex, $colindex) {
88     if (!$this->mProccessed) $this->_process();
89     return @$this->mData[$rowindex][$this->mColumnFields[$colindex]];
90   }
91         
92   function getValueAtName($rowindex, $fieldname) {
93     if (!$this->mProccessed) $this->_process();
94     return @$this->mData[$rowindex][$fieldname];
95   }
96
97   function _process() {
98     $this->mProccessed = true;
99         
100     if ($this->mInteractive) {
101       // Add a column of clickable checkboxes.
102       $column = new TableColumn("","<input type=\"checkbox\" name=\"".$this->name."_all\" onclick=\"setAll(this.checked)\">");
103       import('form.CheckboxCellRenderer');
104       $cb = new CheckboxCellRenderer();
105       if ($this->getIAScript()) $cb->setOnChangeAdd($this->getIAScript()."(this)");
106       $column->setRenderer($cb);
107       $column->setTable($this);
108       array_unshift($this->mColumns, $column);
109     }
110     
111     foreach ($this->mColumns as $column) {
112       $this->mColumnFields[] = $column->getField();
113       $this->mHeaders[] = $column->getHeader();
114       $this->mFooters[] = $column->getFooter();
115     }
116   }
117   
118   function getHtml() {
119     if (!$this->mProccessed) $this->_process();
120     
121     $html = "";
122     if ($this->mInteractive) $html .= $this->_addJavaScript();
123
124     $html .= "<table";
125     if (count($this->mTableOptions) > 0) {
126       foreach ($this->mTableOptions as $k=>$v) {
127         $html .= " $k=\"$v\"";
128       }
129     } else {
130       $html .= " border=\"1\"";
131     }
132     if ($this->mWidth!="") $html .= " width=\"".$this->mWidth."\"";
133     $html .= ">\n";
134     
135     // Print headers.
136     if (($this->mInteractive && (count($this->mHeaders) > 1)) || (!$this->mInteractive && (count($this->mHeaders) > 0))) {
137       $html .= "<tr";
138       if (count($this->mRowOptions) > 0) {
139         foreach ($this->mRowOptions as $k=>$v) {
140           $html .= " $k=\"$v\"";
141         }
142       }
143       $html .= ">\n";
144       foreach ($this->mHeaders as $header) {
145         $html .= "<th";
146         if (count($this->mHeaderOptions) > 0) {
147           foreach ($this->mHeaderOptions as $k=>$v) {
148                 $html .= " $k=\"$v\"";
149           }
150         }
151         $html .= ">$header</th>\n";
152       }
153       $html .= "</tr>\n";
154     }
155     
156     // Print rows.
157     for ($row = 0; $row < count($this->mData); $row++) {
158       $html .= "\n<tr bgcolor=\"".$this->mBgColor."\" onmouseover=\"setRowBackground(this, '".$this->mBgColorOver."')\" onmouseout=\"setRowBackground(this, null)\">\n";
159       for ($col = 0; $col < $this->getColumnCount(); $col++) {
160         if (0 == $col && strtolower(get_class($this->mColumns[$col]->getRenderer())) == 'checkboxcellrenderer') {
161           // Checkbox for the row. Determine if selected.
162           $selected = false;
163           if (is_array($this->value)) {
164             foreach ($this->value as $p) {
165               if ($p == $this->mData[$row][$this->mKeyField]) {
166                 $selected = true;
167                 break;
168               }
169             }
170           }
171           // Render control checkbox.
172           $html .= $this->mColumns[$col]->renderCell($this->mData[$row][$this->mKeyField], $row, $col, $selected);
173         } else {
174           // Render regular cell.
175           $html .= $this->mColumns[$col]->renderCell($this->getValueAt($row, $col), $row, $col);
176         }
177       }
178       $html .= "</tr>\n";
179     }
180
181     // Print footers.
182     if (($this->mInteractive && (count($this->mFooters) > 1)) || (!$this->mInteractive && (count($this->mFooters) > 0))) {
183       $html .= "<tr";
184       if (count($this->mRowOptions) > 0) {
185         foreach ($this->mRowOptions as $k=>$v) {
186           $html .= " $k=\"$v\"";
187         }
188       }
189       $html .= ">\n";
190       foreach ($this->mFooters as $footer) {
191         $html .= "<th";
192         if (count($this->mHeaderOptions) > 0) {
193           foreach ($this->mHeaderOptions as $k=>$v) {
194             $html .= " $k=\"$v\"";
195           }
196         }
197         $html .= ">$footer</th>\n";
198       }
199       $html .= "</tr>\n";
200     }
201
202     $html .= "</table>";
203     return $html;
204   }
205   
206   function getColumnCount() {
207     return count($this->mColumns);
208   }
209
210   function _addJavaScript() {
211     $html = "<script>\n";
212     // setAll - checks / unchecks all checkboxes in the table.
213     $html .= "function setAll(value) {\n";
214     $html .= "\tfor (var i = 0; i < ".$this->getFormName().".elements.length; i++) {\n";
215     $html .= "\t\tif ((".$this->getFormName().".elements[i].type=='checkbox') && (".$this->getFormName().".elements[i].name=='".$this->name."[]')) {\n";
216     $html .= "\t\t\t".$this->getFormName().".elements[i].checked=value;\n";
217     if ($this->getIAScript()) {
218       $html .= "\t\t\t".$this->getIAScript()."(".$this->getFormName().".elements[i]);\n";
219     }
220     $html .= "\t}}\n";
221     $html .= "}\n\n";
222     
223     $html .= "var rowBgColors;\n";
224     // setRowBackground - sets background for a row.
225     $html .= "function setRowBackground(theRow, thePointerColor) {\n";
226     $html .= "\tif (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {\n";
227     $html .= "\t\treturn false;\n\t}\n\n";
228     $html .= "\tvar row_cells_cnt = theRow.cells.length;\n";
229     $html .= "\tif (thePointerColor != null) {\n";
230     $html .= "\t\trowBgColors = new Array(row_cells_cnt);\n";
231     $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
232     $html .= "\t\t\trowBgColors[c]=theRow.cells[c].bgColor;\n\t\t}\n";
233     $html .= "\t}\n";
234     $html .= "\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
235     $html .= "\t\ttheRow.cells[c].bgColor = thePointerColor;\n\t}\n\n";
236     $html .= "\tif (thePointerColor == null) {\n";
237     $html .= "\t\tfor (var c = 0; c < row_cells_cnt; c++) {\n";
238     $html .= "\t\t\ttheRow.cells[c].bgColor=rowBgColors[c];\n\t\t}\n";
239     $html .= "\t\tdelete rowBgColors;\n";
240     $html .= "\t}\n";
241     $html .= "\treturn true;\n}\n";
242     $html .= "</script>\n";
243     
244     return $html;
245   }
246 }