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 $mDataKeys      = array();
 
  39     var $lSelNone       = "None";
 
  41   function __construct($name, $value = '') {
 
  42     $this->class = 'CheckboxGroup';
 
  44     $this->value = $value;
 
  47         function setChecked($value)     { $this->mChecked = $value; }
 
  48         function isChecked() { return $this->mChecked; }
 
  50         function setData($value)        { $this->mOptions = $value; }
 
  51         function getData() { return $this->mOptions; }
 
  53         function setDataKeys($keys)     { $this->mDataKeys = $keys; $this->mDataDeep = 2; }
 
  54         function getDataKeys() { return $this->mDataKeys; }
 
  56         function setLayout($value)      { $this->mLayout = $value; }
 
  57         function getLayout() { return $this->mLayout; }
 
  59         function setGroupIn($value)     { $this->mGroupIn = $value; if ($this->mGroupIn<1) $this->mGroupIn = 1;}
 
  60         function getGroupIn() { return $this->mGroupIn; }
 
  62         function localize($i18n) {
 
  63                 $this->lSelAll = $i18n->getKey('label.select_all');
 
  64                 $this->lSelNone = $i18n->getKey('label.select_none');
 
  69             if ($this->id=="") $this->id = $this->name;
 
  71             $renderArray = array();
 
  75             if ($this->mLayout=="H") {
 
  77                 if (is_array($this->mOptions)) {
 
  78                         $renderCols = $this->mGroupIn;
 
  79                         $renderRows = ceil(count($this->mOptions) / $this->mGroupIn);
 
  81                             foreach ($this->mOptions as $optkey=>$optval) {
 
  82                                 if ($this->mDataDeep>1) {
 
  83                                                 $optkey = $optval[$this->mDataKeys[0]];
 
  84                                                 $optval = $optval[$this->mDataKeys[1]];
 
  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\"";
 
  93                                         $html .= " value=\"".htmlspecialchars($optkey)."\"> <label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
 
  94                                         $renderArray[$col][$row] = $html;
 
  97                                         if ($col==$this->mGroupIn) { $col = 0; $row++; }
 
 103             if ($this->mLayout=="V") {
 
 105                 if (is_array($this->mOptions)) {
 
 106                         $renderCols = ceil(count($this->mOptions) / $this->mGroupIn);
 
 107                         $renderRows = $this->mGroupIn;
 
 109                             foreach ($this->mOptions as $optkey=>$optval) {
 
 110                                 if ($this->mDataDeep>1) {
 
 111                                                 $optkey = $optval[$this->mDataKeys[0]];
 
 112                                                 $optval = $optval[$this->mDataKeys[1]];
 
 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\"";
 
 121                                         $html .= " value=\"".htmlspecialchars($optkey)."\"> <label for=\"$this->id"."_".$i."\">".htmlspecialchars($optval)."</label>";
 
 122                                         $renderArray[$col][$row] = $html;
 
 125                                 if ($row==$this->mGroupIn) { $row = 0; $col++; }
 
 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> / <a href="#" onclick="setAll'.$this->name.'(false);return false;">'.$this->lSelNone.'</a>';
 
 134             $html .= "</td></tr>\n";
 
 136             $html .= "\n\t<table width=\"100%\">\n";
 
 137             for ($i = 0; $i < $renderRows; $i++) {
 
 139                 for ($j = 0; $j < $renderCols; $j++) {
 
 140                         $html .= "\t<td width=\"".(floor(100/$renderCols))."%\">".(isset($renderArray[$j][$i])?$renderArray[$j][$i]:" ")."</td>\n";
 
 144             $html .= "</table>\n";
 
 145             $html .= "</td></tr></table>\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";
 
 155                 $str .= "</script>\n";