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