Adjusted week start for subgroups.
[timetracker.git] / WEB-INF / lib / form / Combobox.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 //  name        CDATA          #IMPLIED  -- field name --
32 //  size        NUMBER         #IMPLIED  -- rows visible --
33 //  multiple    (multiple)     #IMPLIED  -- default is single selection --
34 //  disabled    (disabled)     #IMPLIED  -- unavailable in this context --
35 //  tabindex    NUMBER         #IMPLIED  -- position in tabbing order --
36 //  onfocus     %Script;       #IMPLIED  -- the element got the focus --
37 //  onblur      %Script;       #IMPLIED  -- the element lost the focus --
38 //  onchange    %Script;       #IMPLIED  -- the element value was changed --
39
40 class Combobox extends FormElement {
41     var $mMultiple      = false;
42     var $mOptions       = array();
43     var $mOptionsEmpty  = array();
44     var $mCompareOn = "key"; // or "value"
45     var $mDataDeep = 1;
46     var $mDataKeys = array();
47
48   function __construct($name) {
49     $this->class = 'Combobox';
50     $this->name = $name;
51   }
52
53         function setMultiple($value)    { $this->mMultiple = $value; }
54         function isMultiple() { return $this->mMultiple; }
55         
56         function setData($value)        { $this->mOptions = $value; }
57         function getData() { return $this->mOptions; }
58         
59         function setDataDefault($value) { $this->mOptionsEmpty = $value; }
60         function getDataDefault() { return $this->mOptionsEmpty; }
61         
62         function setDataKeys($keys)     { $this->mDataKeys = $keys; $this->mDataDeep = 2; }
63         function getDataKeys() { return $this->mDataKeys; }
64         
65         
66         function getHtml() {
67
68             if ($this->id=="") $this->id = $this->name;
69             
70                 $html = "\n\t<select";
71                 $html .= " name=\"$this->name\" id=\"$this->id\"";
72                 
73                 if ($this->size!="")
74                   $html .= " size=\"$this->size\"";
75                  
76                 if ($this->mMultiple)
77                   $html .= " multiple";
78
79                 if ($this->on_change!="")
80                    $html .= " onchange=\"$this->on_change\"";
81                    
82                 if ($this->style!="")
83                    $html .= " style=\"$this->style\"";
84                 
85                 if (!$this->isEnabled())
86                    $html .= " disabled";
87                    
88                 $html .= ">\n";   
89                 if (is_array($this->mOptionsEmpty) && (count($this->mOptionsEmpty) > 0))
90                 foreach ($this->mOptionsEmpty as $key=>$value) {
91                         $html .= "<option value=\"".$key."\"";
92                         if (($this->value == $value) && ($this->value != '')) $html .= " selected";
93                         $html .= ">".$value."</option>\n";
94                 }
95                 if (is_array($this->mOptions) && (count($this->mOptions) > 0))
96                 foreach ($this->mOptions as $key=>$value) {
97
98                         if ($this->mDataDeep>1) {
99                                 $key = $value[$this->mDataKeys[0]];
100                                 $value = $value[$this->mDataKeys[1]];
101                         }
102                         $html .= "<option value=\"".$key."\"";
103                         if (($this->value == $key) && ($this->value != '')) $html .= " selected";
104                         $html .= ">".htmlspecialchars($value)."</option>\n";
105                 }
106                 
107                 $html .= "</select>";
108                 
109                 return $html;
110         }
111 }