Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / form / dateselector.php
blob3687e612400549b8324832e7b87f3a44d5e838e4
1 <?php
2 global $CFG;
3 require_once "$CFG->libdir/form/group.php";
4 require_once "$CFG->libdir/formslib.php";
6 /**
7 * Class for a group of elements used to input a date.
9 * Emulates moodle print_date_selector function
11 * @author Jamie Pratt <me@jamiep.org>
12 * @access public
14 class MoodleQuickForm_date_selector extends MoodleQuickForm_group
16 /**
17 * Control the fieldnames for form elements
19 * startyear => integer start of range of years that can be selected
20 * stopyear => integer last year that can be selected
21 * timezone => float/string timezone
22 * applydst => apply users daylight savings adjustment?
23 * optional => if true, show a checkbox beside the date to turn it on (or off)
25 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
26 'timezone'=>99, 'applydst'=>true, 'optional'=>false);
28 /**
29 * These complement separators, they are appended to the resultant HTML
30 * @access private
31 * @var array
33 var $_wrap = array('', '');
35 /**
36 * Class constructor
38 * @access public
39 * @param string Element's name
40 * @param mixed Label(s) for an element
41 * @param array Options to control the element's display
42 * @param mixed Either a typical HTML attribute string or an associative array
44 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
46 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
47 $this->_persistantFreeze = true;
48 $this->_appendName = true;
49 $this->_type = 'date_selector';
50 // set the options, do not bother setting bogus ones
51 if (is_array($options)) {
52 foreach ($options as $name => $value) {
53 if (isset($this->_options[$name])) {
54 if (is_array($value) && is_array($this->_options[$name])) {
55 $this->_options[$name] = @array_merge($this->_options[$name], $value);
56 } else {
57 $this->_options[$name] = $value;
64 // }}}
65 // {{{ _createElements()
67 function _createElements()
69 $this->_elements = array();
70 for ($i=1; $i<=31; $i++) {
71 $days[$i] = $i;
73 for ($i=1; $i<=12; $i++) {
74 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
76 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
77 $years[$i] = $i;
79 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
80 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
81 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
82 // If optional we add a checkbox which the user can use to turn if on
83 if($this->_options['optional']) {
84 $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
86 foreach ($this->_elements as $element){
87 if (method_exists($element, 'setHiddenLabel')){
88 $element->setHiddenLabel(true);
94 // }}}
95 // {{{ onQuickFormEvent()
97 /**
98 * Called by HTML_QuickForm whenever form event is made on this element
100 * @param string $event Name of event
101 * @param mixed $arg event arguments
102 * @param object $caller calling object
103 * @since 1.0
104 * @access public
105 * @return void
107 function onQuickFormEvent($event, $arg, &$caller)
109 switch ($event) {
110 case 'updateValue':
111 // constant values override both default and submitted ones
112 // default values are overriden by submitted
113 $value = $this->_findValue($caller->_constantValues);
114 if (null === $value) {
115 // if no boxes were checked, then there is no value in the array
116 // yet we don't want to display default value in this case
117 if ($caller->isSubmitted()) {
118 $value = $this->_findValue($caller->_submitValues);
119 } else {
120 $value = $this->_findValue($caller->_defaultValues);
123 $requestvalue=$value;
124 if ($value == 0) {
125 $value = time();
127 if (!is_array($value)) {
128 $currentdate = usergetdate($value);
129 $value = array(
130 'day' => $currentdate['mday'],
131 'month' => $currentdate['mon'],
132 'year' => $currentdate['year']);
133 // If optional, default to off, unless a date was provided
134 if($this->_options['optional']) {
135 $value['off'] = ($requestvalue == 0) ? true : false;
137 } else {
138 $value['off'] = (isset($value['off'])) ? true : false;
140 if (null !== $value){
141 $this->setValue($value);
143 break;
144 case 'createElement':
145 if($arg[2]['optional']) {
146 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
148 return parent::onQuickFormEvent($event, $arg, $caller);
149 break;
150 default:
151 return parent::onQuickFormEvent($event, $arg, $caller);
153 } // end func onQuickFormEvent
155 // {{{ toHtml()
157 function toHtml()
159 include_once('HTML/QuickForm/Renderer/Default.php');
160 $renderer =& new HTML_QuickForm_Renderer_Default();
161 $renderer->setElementTemplate('{element}');
162 parent::accept($renderer);
163 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
166 // }}}
167 // {{{ accept()
169 function accept(&$renderer, $required = false, $error = null)
171 $renderer->renderElement($this, $required, $error);
174 // }}}
177 * Output a timestamp. Give it the name of the group.
179 * @param array $submitValues
180 * @param bool $assoc
181 * @return array
183 function exportValue(&$submitValues, $assoc = false)
185 $value = null;
186 $valuearray = array();
187 foreach ($this->_elements as $element){
188 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
189 if ($thisexport!=null){
190 $valuearray += $thisexport;
193 if (count($valuearray)){
194 if($this->_options['optional']) {
195 // If checkbox is on, the value is zero, so go no further
196 if(!empty($valuearray['off'])) {
197 $value[$this->getName()]=0;
198 return $value;
202 $value[$this->getName()]=make_timestamp($valuearray['year'],
203 $valuearray['month'],
204 $valuearray['day'],
205 0,0,0,
206 $this->_options['timezone'],
207 $this->_options['applydst']);
209 return $value;
210 } else {
211 return null;
215 // }}}