Removed obsolete code.
[timetracker.git] / WEB-INF / lib / form / CheckboxGroup.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
31 class CheckboxGroup extends FormElement {
32     var $mChecked       = false;
33     var $mOptions       = array();
34     var $mLayout        = "V";
35     var $mGroupIn       = 1;
36     var $mDataKeys      = array();
37     var $mDataDeep      = 1;
38     var $lSelAll        = "All";
39     var $lSelNone       = "None";
40
41   function __construct($name) {
42     $this->class = 'CheckboxGroup';
43     $this->name = $name;
44   }
45
46         function setChecked($value)     { $this->mChecked = $value; }
47         function isChecked() { return $this->mChecked; }
48         
49         function setData($value)        { $this->mOptions = $value; }
50         function getData() { return $this->mOptions; }
51         
52         function setDataKeys($keys)     { $this->mDataKeys = $keys; $this->mDataDeep = 2; }
53         function getDataKeys() { return $this->mDataKeys; }
54         
55         function setLayout($value)      { $this->mLayout = $value; }
56         function getLayout() { return $this->mLayout; }
57         
58         function setGroupIn($value)     { $this->mGroupIn = $value; if ($this->mGroupIn<1) $this->mGroupIn = 1;}
59         function getGroupIn() { return $this->mGroupIn; }
60         
61         function localize($i18n) {
62                 $this->lSelAll = $i18n->getKey('label.select_all');
63                 $this->lSelNone = $i18n->getKey('label.select_none');
64         }
65                 
66         function getHtml() {
67
68             if ($this->id=="") $this->id = $this->name;
69             
70             $renderArray = array();
71             $renderCols = 0;
72             $renderRows = 0;
73             
74             if ($this->mLayout=="H") {
75                 $i = 0;
76                 if (is_array($this->mOptions)) {
77                         $renderCols = $this->mGroupIn;
78                         $renderRows = ceil(count($this->mOptions) / $this->mGroupIn);
79                         $col = $row = 0;
80                             foreach ($this->mOptions as $optkey=>$optval) {
81                                 if ($this->mDataDeep>1) {
82                                                 $optkey = $optval[$this->mDataKeys[0]];
83                                                 $optval = $optval[$this->mDataKeys[1]];
84                                         }
85                                 $html = "<input type=\"checkbox\" name=\"$this->name[]\" id=\"$this->id"."_".$i."\"";
86                                 if (is_array($this->value)) {
87                                         foreach ($this->value as $element) {
88                                                 if (($element == $optkey) && ($element != null))
89                                                         $html .= " checked=\"true\"";
90                                         }
91                                 }
92                                         $html .= " value=\"".htmlspecialchars($optkey)."\">&nbsp;<label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
93                                         $renderArray[$col][$row] = $html;
94                                         
95                                 $col++;                         
96                                         if ($col==$this->mGroupIn) { $col = 0; $row++; }
97                                 $i++;
98                             }
99                 }
100             }
101             
102             if ($this->mLayout=="V") {
103                         $i = 0;
104                 if (is_array($this->mOptions)) {
105                         $renderCols = ceil(count($this->mOptions) / $this->mGroupIn);
106                         $renderRows = $this->mGroupIn;
107                         $col = $row = 0;
108                             foreach ($this->mOptions as $optkey=>$optval) {
109                                 if ($this->mDataDeep>1) {
110                                                 $optkey = $optval[$this->mDataKeys[0]];
111                                                 $optval = $optval[$this->mDataKeys[1]];
112                                         }
113                                 $html = "<input type=\"checkbox\" name=\"$this->name[]\" id=\"$this->id"."_".$i."\"";
114                                 if (is_array($this->value)) {
115                                         foreach ($this->value as $element) {
116                                                 if (($element == $optkey) && ($element != null))
117                                                         $html .= " checked=\"true\"";
118                                         }
119                                 }
120                                         $html .= " value=\"".htmlspecialchars($optkey)."\">&nbsp;<label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
121                                         $renderArray[$col][$row] = $html;
122
123                                         $row++;
124                                 if ($row==$this->mGroupIn) { $row = 0; $col++; }
125                                 $i++;
126                             }
127                 }
128             }
129             
130             
131             $html = "\n\t<table style=\"".$this->style."\"><tr><td align=\"center\" bgcolor=\"eeeeee\">\n";
132             $html .= '<a href="#" onclick="setAll'.$this->name.'(true);return false;">'.$this->lSelAll.'</a>&nbsp;/&nbsp;<a href="#" onclick="setAll'.$this->name.'(false);return false;">'.$this->lSelNone.'</a>';
133             $html .= "</td></tr>\n";
134             $html .= "<tr><td>";
135             $html .= "\n\t<table width=\"100%\">\n";
136             for ($i = 0; $i < $renderRows; $i++) {
137                 $html .= "<tr>";
138                 for ($j = 0; $j < $renderCols; $j++) {
139                         $html .= "\t<td width=\"".(floor(100/$renderCols))."%\">".(isset($renderArray[$j][$i])?$renderArray[$j][$i]:"&nbsp;")."</td>\n";
140                 }
141                 $html .= "</tr>\n";
142             }
143             $html .= "</table>\n";
144             $html .= "</td></tr></table>\n";
145             
146             $str = "<script>\n";
147                 $str .= "function setAll".$this->name."(value) {\n";
148                 $str .= "\tvar formInputs = document.getElementsByTagName(\"input\");\n";
149                 $str .= "\tfor (var i = 0; i < formInputs.length; i++) {\n";
150         $str .= "\t\tif ((formInputs.item(i).type=='checkbox') && (formInputs.item(i).name=='".$this->name."[]')) {\n";
151         $str .= "\t\tformInputs.item(i).checked=value;\n";
152         $str .= "\t}\n}\n";
153                 $str .= "}\n";
154                 $str .= "</script>\n";
155             
156                 return $html.$str;
157         }
158 }