| [ Index ] |
PHP Cross Reference of Web Application Component Toolkit |
[Summary view] [Print] [Text view]
1 <?php 2 //-------------------------------------------------------------------------------- 3 // Copyright 2004 Procata, Inc. 4 // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html) 5 //-------------------------------------------------------------------------------- 6 /** 7 * @package WACT_COMPONENT 8 * @version $Id: select_date.inc.php,v 1.3 2004/11/24 23:12:02 quipo Exp $ 9 */ 10 //-------------------------------------------------------------------------------- 11 /** 12 * SelectSingleComponent definition 13 */ 14 require_once WACT_ROOT . 'template/components/form/form.inc.php'; 15 require_once 'select.inc.php'; 16 17 //-------------------------------------------------------------------------------- 18 /** 19 * extends SelectSingleComponent with new methods for month handling 20 */ 21 class SelectMonth extends SelectSingleComponent 22 { 23 /** 24 * @var string strftime() parameter for option tag contents 25 */ 26 var $format = '&B'; // == long 27 28 /** 29 * @var string strftime() parameter for option tag values 30 */ 31 var $valueFormat = '%m'; // == numeric 32 33 /** 34 * translate human-readable string in strftime() parameter 35 */ 36 function getFormat($format) { 37 switch (strtolower($format)) { 38 case 'numeric': 39 return '%m'; 40 case 'short': 41 return '%b'; 42 case 'long': 43 default: 44 return '%B'; 45 } 46 } 47 48 /** 49 * Allowed options are 'numeric', 'long', 'short'. 50 * @param string 51 */ 52 function setValueFormat($format='numeric') { 53 $this->valueFormat = $this->getFormat($format); 54 } 55 56 /** 57 * Allowed options are 'numeric', 'long', 'short'. 58 * @param string 59 */ 60 function setFormat($format='long') { 61 $this->format = $this->getFormat($format); 62 } 63 64 /** 65 * set option tag choices 66 */ 67 function fillChoices() { 68 $months = array(); 69 for ($i=1; $i<=12; $i++) { 70 $months[strftime($this->valueFormat, mktime(0, 0, 0, $i, 1, 2000))] 71 = strftime($this->format, mktime(0, 0, 0, $i, 1, 2000)); 72 } 73 $this->setChoices($months); 74 } 75 76 /** 77 * set selection 78 */ 79 function setSelectedMonth($m) { 80 $this->setSelection(strftime($this->valueFormat, mktime(0, 0, 0, $m, 1, 2000))); 81 } 82 } 83 //-------------------------------------------------------------------------------- 84 85 /** 86 * Runtime form:selectdate API 87 * @todo EXPERIMENTAL 88 * @see http://wact.sourceforge.net/index.php/FormSelectDateComponent 89 * @access public 90 * @package WACT_COMPONENT 91 */ 92 class FormSelectDateComponent extends FormComponent 93 { 94 var $selectYear = null; 95 var $selectMonth = null; 96 var $selectDay = null; 97 var $selectedTime = array(); 98 99 var $setDefaultSelection = false; 100 var $asArray = false; 101 var $groupName; 102 103 /** 104 * the compiler complains if not defined... 105 */ 106 function isVisible() { 107 return true; 108 } 109 110 /** 111 * @param string 'name' attribute of the form:selectdate tag 112 */ 113 function setGroupName($name) { 114 $this->groupName = $name; 115 } 116 117 function setAsArray() { 118 $this->asArray = $this->getAttribute('asArray'); 119 } 120 121 /** 122 * @param mixed int|string unix timestamp or ISO-8601 timestamp 123 * @param CodeWriter 124 * @return array 125 * @access private 126 */ 127 function parseTime($time=null) 128 { 129 if (is_integer($time)) { 130 //$time = unix timestamp 131 return array( 132 'year' => date('Y', $time), 133 'month' => date('m', $time), 134 'day' => date('d', $time) 135 ); 136 } 137 $len = (is_string($time)) ? strlen($time) : 0; 138 if ($len == 14) { 139 //$time = mysql timestamp YYYYMMDDHHMMSS 140 return array( 141 'year' => (int)substr($time, 0, 4), 142 'month' => (int)substr($time, 4, 2), 143 'day' => (int)substr($time, 6, 2), 144 ); 145 } 146 147 if ($len == 10 || $len == 19) { 148 //$time = ISO-8601 timestamp YYYY-MM-DD or YYYY-MM-DD HH:MM:SS 149 return array( 150 'year' => (int)substr($time, 0, 4), 151 'month' => (int)substr($time, 5, 2), 152 'day' => (int)substr($time, 8, 2), 153 ); 154 } 155 //if everything failed, try with strtotime 156 if (empty($time)) { 157 $time = 'now'; 158 } 159 $time = strtotime($time); 160 if (!is_numeric($time) || $time == -1) { 161 $time = strtotime('now'); 162 } 163 return array( 164 'year' => date('Y', $time), 165 'month' => date('m', $time), 166 'day' => date('d', $time), 167 ); 168 } 169 170 /** 171 * build SelectSimpleComponent object and set options for years 172 */ 173 function prepareYear() 174 { 175 $this->selectYear = new SelectSingleComponent(); //SelectYear 176 $this->addChild($this->selectYear); 177 178 $start = ($this->hasAttribute('startYear') ? $this->getAttribute('startYear') : date('Y')); 179 $end = ($this->hasAttribute('endYear') ? $this->getAttribute('endYear') : date('Y')); 180 if ((strpos($start.'', '+') !== false) || (strpos($start.'', '-') !== false)) { 181 $start += date('Y'); 182 } 183 if ((strpos($end.'', '+') !== false) || (strpos($end.'', '-') !== false)) { 184 $end += date('Y'); 185 } 186 187 $years = array(); 188 for ($i=$start; $i<=$end; $i++) { 189 $years[$i] = $i; 190 } 191 $this->selectYear->setChoices($years); 192 if ($this->setDefaultSelection) { 193 $this->selectYear->setSelection($this->selectedTime['year']); 194 } 195 196 //maintain selection through pages 197 $FormComponent = &$this->findParentByClass('FormComponent'); 198 if ($y = $FormComponent->_getValue($this->groupName.'_Year')) { 199 $this->selectYear->setSelection($y); 200 } 201 if ($date = $FormComponent->_getValue($this->groupName)) { 202 if (is_array($date) && array_key_exists('Year', $date)) { 203 $this->selectYear->setSelection($date['Year']); 204 } else { 205 $this->selectedTime = $this->parseTime($date); 206 $this->selectYear->setSelection($this->selectedTime['year']); 207 } 208 } 209 } 210 211 /** 212 * build SelectSimpleComponent object and set options for months 213 */ 214 function prepareMonth() 215 { 216 $this->selectMonth = new SelectMonth(); 217 $this->addChild($this->selectMonth); 218 219 if ($this->hasAttribute('monthValueFormat')) { 220 $this->selectMonth->setValueFormat($this->getAttribute('monthValueFormat')); 221 } 222 if ($this->setDefaultSelection) { 223 $this->selectMonth->setSelectedMonth($this->selectedTime['month']); 224 } 225 226 //maintain selection through pages 227 $FormComponent = &$this->findParentByClass('FormComponent'); 228 if ($m = $FormComponent->_getValue($this->groupName.'_Month')) { 229 $this->selectMonth->setSelection($m); 230 } 231 if ($date = $FormComponent->_getValue($this->groupName)) { 232 if (is_array($date) && array_key_exists('Month', $date)) { 233 $this->selectMonth->setSelection($date['Month']); 234 } else { 235 $this->selectedTime = $this->parseTime($date); 236 $this->selectMonth->setSelectedMonth($this->selectedTime['month']); 237 } 238 } 239 } 240 241 /** 242 * build SelectSimpleComponent object and set options for days 243 */ 244 function prepareDay() 245 { 246 247 $this->selectDay = new SelectSingleComponent(); // new SelectDay 248 $this->addChild($this->selectDay); 249 250 $days = array(); 251 for ($i=1; $i<=31; $i++) { 252 $days[$i] = $i; 253 } 254 $this->selectDay->setChoices($days); 255 if ($this->setDefaultSelection) { 256 $this->selectDay->setSelection($this->selectedTime['day']); 257 } 258 259 //maintain selection through pages 260 $FormComponent = &$this->findParentByClass('FormComponent'); 261 if ($d = $FormComponent->_getValue($this->groupName.'_Day')) { 262 $this->selectDay->setSelection($d); 263 } 264 if ($date = $FormComponent->_getValue($this->groupName)) { 265 if (is_array($date) && array_key_exists('Day', $date)) { 266 $this->selectDay->setSelection($date['Day']); 267 } else { 268 $this->selectedTime = $this->parseTime($date); 269 $this->selectDay->setSelection($this->selectedTime['day']); 270 } 271 } 272 } 273 274 /** 275 * override default behaviour when onInitial() is called 276 */ 277 function setSelection($time=null) { 278 if (is_null($time)) { 279 $time = time(); 280 } 281 $this->selectedTime = $this->parseTime($time); 282 $this->setDefaultSelection = true; 283 } 284 285 /** 286 * @return SelectSingleComponent object 287 * @access protected 288 */ 289 function & getYear() { 290 return $this->selectYear; 291 } 292 293 /** 294 * @return SelectSingleComponent object 295 * @access protected 296 */ 297 function & getMonth() { 298 return $this->selectMonth; 299 } 300 301 /** 302 * @return SelectSingleComponent object 303 * @access protected 304 */ 305 function & getDay() { 306 return $this->selectDay; 307 } 308 } 309 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Nov 28 19:36:09 2004 | Cross-referenced by PHPXref 0.5 |