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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('form.FormElement');
31 class CheckboxGroup extends FormElement {
32 var $mChecked = false;
33 var $mOptions = array();
36 var $cClassName = "CheckboxGroup";
37 var $mDataKeys = array();
40 var $lSelNone = "None";
42 function CheckboxGroup($name,$value="")
45 $this->mValue = $value;
48 function setChecked($value) { $this->mChecked = $value; }
49 function isChecked() { return $this->mChecked; }
51 function setData($value) { $this->mOptions = $value; }
52 function getData() { return $this->mOptions; }
54 function setDataKeys($keys) { $this->mDataKeys = $keys; $this->mDataDeep = 2; }
55 function getDataKeys() { return $this->mDataKeys; }
57 function setLayout($value) { $this->mLayout = $value; }
58 function getLayout() { return $this->mLayout; }
60 function setGroupIn($value) { $this->mGroupIn = $value; if ($this->mGroupIn<1) $this->mGroupIn = 1;}
61 function getGroupIn() { return $this->mGroupIn; }
63 function setLocalization($i18n) {
64 FormElement::setLocalization($i18n);
65 $this->lSelAll = $i18n->getKey('label.select_all');
66 $this->lSelNone = $i18n->getKey('label.select_none');
69 function toStringControl() {
70 if (!$this->isRenderable()) return "";
72 if ($this->mId=="") $this->mId = $this->mName;
74 $renderArray = array();
78 if ($this->mLayout=="H") {
80 if (is_array($this->mOptions)) {
81 $renderCols = $this->mGroupIn;
82 $renderRows = ceil(count($this->mOptions) / $this->mGroupIn);
84 foreach ($this->mOptions as $optkey=>$optval) {
85 if ($this->mDataDeep>1) {
86 $optkey = $optval[$this->mDataKeys[0]];
87 $optval = $optval[$this->mDataKeys[1]];
89 $html = "<input type=\"checkbox\" name=\"$this->mName[]\" id=\"$this->mId"."_".$i."\"";
90 if (is_array($this->mValue)) {
91 foreach ($this->mValue as $value) {
92 if (($value == $optkey) && ($value != null))
93 $html .= " checked=\"true\"";
96 $html .= " value=\"".htmlspecialchars($optkey)."\"> <label for=\"$this->mId"."_".$i."\">".htmlspecialchars($optval)."</label>";
97 $renderArray[$col][$row] = $html;
100 if ($col==$this->mGroupIn) { $col = 0; $row++; }
106 if ($this->mLayout=="V") {
108 if (is_array($this->mOptions)) {
109 $renderCols = ceil(count($this->mOptions) / $this->mGroupIn);
110 $renderRows = $this->mGroupIn;
112 foreach ($this->mOptions as $optkey=>$optval) {
113 if ($this->mDataDeep>1) {
114 $optkey = $optval[$this->mDataKeys[0]];
115 $optval = $optval[$this->mDataKeys[1]];
117 $html = "<input type=\"checkbox\" name=\"$this->mName[]\" id=\"$this->mId"."_".$i."\"";
118 if (is_array($this->mValue)) {
119 foreach ($this->mValue as $value) {
120 if (($value == $optkey) && ($value != null))
121 $html .= " checked=\"true\"";
124 $html .= " value=\"".htmlspecialchars($optkey)."\"> <label for=\"$this->mId"."_".$i."\">".htmlspecialchars($optval)."</label>";
125 $renderArray[$col][$row] = $html;
128 if ($row==$this->mGroupIn) { $row = 0; $col++; }
135 $html = "\n\t<table style=\"".$this->mStyle."\"><tr><td align=\"center\" bgcolor=\"eeeeee\">\n";
136 $html .= '<a href="#" onclick="setAll'.$this->getName().'(true);return false;">'.$this->lSelAll.'</a> / <a href="#" onclick="setAll'.$this->getName().'(false);return false;">'.$this->lSelNone.'</a>';
137 $html .= "</td></tr>\n";
139 $html .= "\n\t<table width=\"100%\">\n";
140 for ($i = 0; $i < $renderRows; $i++) {
142 for ($j = 0; $j < $renderCols; $j++) {
143 $html .= "\t<td width=\"".(floor(100/$renderCols))."%\">".(isset($renderArray[$j][$i])?$renderArray[$j][$i]:" ")."</td>\n";
147 $html .= "</table>\n";
148 $html .= "</td></tr></table>\n";
151 $str .= "function setAll".$this->getName()."(value) {\n";
152 $str .= "\tvar formInputs = document.getElementsByTagName(\"input\");\n";
153 $str .= "\tfor (var i = 0; i < formInputs.length; i++) {\n";
154 $str .= "\t\tif ((formInputs.item(i).type=='checkbox') && (formInputs.item(i).name=='".$this->getName()."[]')) {\n";
155 $str .= "\t\tformInputs.item(i).checked=value;\n";
158 $str .= "</script>\n";