More refactoring.
[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() {
62           global $i18n;
63           $this->lSelAll = $i18n->getKey('label.select_all');
64           $this->lSelNone = $i18n->getKey('label.select_none');
65         }
66                 
67         function getHtml() {
68
69             if ($this->id=="") $this->id = $this->name;
70             
71             $renderArray = array();
72             $renderCols = 0;
73             $renderRows = 0;
74             
75             if ($this->mLayout=="H") {
76                 $i = 0;
77                 if (is_array($this->mOptions)) {
78                         $renderCols = $this->mGroupIn;
79                         $renderRows = ceil(count($this->mOptions) / $this->mGroupIn);
80                         $col = $row = 0;
81                             foreach ($this->mOptions as $optkey=>$optval) {
82                                 if ($this->mDataDeep>1) {
83                                                 $optkey = $optval[$this->mDataKeys[0]];
84                                                 $optval = $optval[$this->mDataKeys[1]];
85                                         }
86                                 $html = "<input type=\"checkbox\" name=\"$this->name[]\" id=\"$this->id"."_".$i."\"";
87                                 if (is_array($this->value)) {
88                                         foreach ($this->value as $element) {
89                                                 if (($element == $optkey) && ($element != null))
90                                                         $html .= " checked=\"true\"";
91                                         }
92                                 }
93                                         $html .= " value=\"".htmlspecialchars($optkey)."\">&nbsp;<label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
94                                         $renderArray[$col][$row] = $html;
95                                         
96                                 $col++;                         
97                                         if ($col==$this->mGroupIn) { $col = 0; $row++; }
98                                 $i++;
99                             }
100                 }
101             }
102             
103             if ($this->mLayout=="V") {
104                         $i = 0;
105                 if (is_array($this->mOptions)) {
106                         $renderCols = ceil(count($this->mOptions) / $this->mGroupIn);
107                         $renderRows = $this->mGroupIn;
108                         $col = $row = 0;
109                             foreach ($this->mOptions as $optkey=>$optval) {
110                                 if ($this->mDataDeep>1) {
111                                                 $optkey = $optval[$this->mDataKeys[0]];
112                                                 $optval = $optval[$this->mDataKeys[1]];
113                                         }
114                                 $html = "<input type=\"checkbox\" name=\"$this->name[]\" id=\"$this->id"."_".$i."\"";
115                                 if (is_array($this->value)) {
116                                         foreach ($this->value as $element) {
117                                                 if (($element == $optkey) && ($element != null))
118                                                         $html .= " checked=\"true\"";
119                                         }
120                                 }
121                                         $html .= " value=\"".htmlspecialchars($optkey)."\">&nbsp;<label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
122                                         $renderArray[$col][$row] = $html;
123
124                                         $row++;
125                                 if ($row==$this->mGroupIn) { $row = 0; $col++; }
126                                 $i++;
127                             }
128                 }
129             }
130             
131             
132             $html = "\n\t<table style=\"".$this->style."\"><tr><td align=\"center\" bgcolor=\"eeeeee\">\n";
133             $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>';
134             $html .= "</td></tr>\n";
135             $html .= "<tr><td>";
136             $html .= "\n\t<table width=\"100%\">\n";
137             for ($i = 0; $i < $renderRows; $i++) {
138                 $html .= "<tr>";
139                 for ($j = 0; $j < $renderCols; $j++) {
140                         $html .= "\t<td width=\"".(floor(100/$renderCols))."%\">".(isset($renderArray[$j][$i])?$renderArray[$j][$i]:"&nbsp;")."</td>\n";
141                 }
142                 $html .= "</tr>\n";
143             }
144             $html .= "</table>\n";
145             $html .= "</td></tr></table>\n";
146             
147             $str = "<script>\n";
148                 $str .= "function setAll".$this->name."(value) {\n";
149                 $str .= "\tvar formInputs = document.getElementsByTagName(\"input\");\n";
150                 $str .= "\tfor (var i = 0; i < formInputs.length; i++) {\n";
151         $str .= "\t\tif ((formInputs.item(i).type=='checkbox') && (formInputs.item(i).name=='".$this->name."[]')) {\n";
152         $str .= "\t\tformInputs.item(i).checked=value;\n";
153         $str .= "\t}\n}\n";
154                 $str .= "}\n";
155                 $str .= "</script>\n";
156             
157                 return $html.$str;
158         }
159 }