| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: date.inc,v 1.29.2.13 2010/09/28 22:43:56 quicksketch Exp $ 3 4 /** 5 * @file 6 * Webform module date component. 7 */ 8 9 /** 10 * Implementation of _webform_defaults_component(). 11 */ 12 function _webform_defaults_date() { 13 return array( 14 'name' => '', 15 'form_key' => NULL, 16 'pid' => 0, 17 'weight' => 0, 18 'value' => '', 19 'mandatory' => 0, 20 'email' => 1, 21 'extra' => array( 22 'timezone' => 'user', 23 'year_start' => '-2', 24 'year_end' => '+2', 25 'year_textfield' => 0, 26 'datepicker' => 1, 27 'title_display' => 0, 28 'description' => '', 29 ), 30 ); 31 } 32 33 /** 34 * Implementation of _webform_theme_component(). 35 */ 36 function _webform_theme_date() { 37 return array( 38 'webform_date' => array( 39 'arguments' => array('element' => NULL), 40 ), 41 'webform_display_date' => array( 42 'arguments' => array('element' => NULL), 43 ), 44 'webform_calendar' => array( 45 'arguments' => array('component' => NULL, 'calendar_classes' => NULL), 46 'template' => 'templates/webform-calendar', 47 ), 48 ); 49 } 50 51 /** 52 * Implementation of _webform_edit_component(). 53 */ 54 function _webform_edit_date($component) { 55 $form = array(); 56 $form['value'] = array( 57 '#type' => 'textfield', 58 '#title' => t('Default value'), 59 '#default_value' => $component['value'], 60 '#description' => t('The default value of the field.') . '<br />' . t('Accepts any date in any <a href="http://www.gnu.org/software/tar/manual/html_chapter/Date-input-formats.html">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'), 61 '#size' => 60, 62 '#maxlength' => 127, 63 '#weight' => 0, 64 ); 65 $form['extra']['timezone'] = array( 66 '#type' => 'radios', 67 '#title' => t('Timezone'), 68 '#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'], 69 '#description' => t('Adjust the default time value according to a specific timezone.'), 70 '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')), 71 '#weight' => 1, 72 '#access' => variable_get('configurable_timezones', 1) && module_exists('date_timezone'), 73 ); 74 75 $form['display']['datepicker'] = array( 76 '#type' => 'checkbox', 77 '#title' => t('Enable popup calendar'), 78 '#default_value' => $component['extra']['datepicker'], 79 '#description' => t('Enable a JavaScript date picker next to the date field.'), 80 '#weight' => 2, 81 '#access' => function_exists('date_popup_load'), 82 '#parents' => array('extra', 'datepicker'), 83 ); 84 85 $form['display']['year_textfield'] = array( 86 '#type' => 'checkbox', 87 '#title' => t('Use a textfield for year'), 88 '#default_value' => $component['extra']['year_textfield'], 89 '#description' => t('If checked, the generated date field will use a textfield for the year. Otherwise it will use a select list.'), 90 '#weight' => 5, 91 '#parents' => array('extra', 'year_textfield'), 92 ); 93 94 $form['validation']['year_start'] = array( 95 '#type' => 'textfield', 96 '#title' => t('Start year'), 97 '#default_value' => $component['extra']['year_start'], 98 '#description' => t('The first year that is allowed to be entered. May be relative (i.e. -2 or +2) or simply the year (i.e. 1950).'), 99 '#size' => 10, 100 '#maxlength' => 4, 101 '#weight' => 3, 102 '#parents' => array('extra', 'year_start'), 103 ); 104 $form['validation']['year_end'] = array( 105 '#type' => 'textfield', 106 '#title' => t('End year'), 107 '#default_value' => $component['extra']['year_end'], 108 '#description' => t('The last year that is allowed to be entered. May be relative (i.e. -2 or +2) or simply the year (i.e. 1950).'), 109 '#size' => 10, 110 '#maxlength' => 4, 111 '#weight' => 4, 112 '#parents' => array('extra', 'year_end'), 113 ); 114 115 return $form; 116 } 117 118 /** 119 * Implementation of _webform_render_component(). 120 */ 121 function _webform_render_date($component, $value = NULL, $filter = TRUE) { 122 $element = array( 123 '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'], 124 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL, 125 '#weight' => $component['weight'], 126 '#type' => 'date', 127 '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'], 128 '#prefix' => '<div class="webform-component webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">', 129 '#suffix' => '</div>', 130 '#required' => $component['mandatory'], 131 '#year_start' => $component['extra']['year_start'], 132 '#year_end' => $component['extra']['year_end'], 133 '#process' => array('webform_expand_date'), 134 '#theme' => 'webform_date', 135 '#pre_render' => array('webform_element_title_display'), 136 '#element_validate' => array('webform_validate_date'), 137 '#webform_component' => $component, 138 ); 139 140 if ($component['extra']['datepicker'] && function_exists('date_popup_load')) { 141 $element['#datepicker'] = TRUE; 142 } 143 144 if (isset($value[0]) && $value[0] !== '') { 145 $value = webform_date_array($value[0], 'date'); 146 $element['#default_value'] = $value; 147 } 148 149 return $element; 150 } 151 152 /** 153 * Form API #process function for Webform date fields. 154 */ 155 function webform_expand_date($element) { 156 $component = $element['#webform_component']; 157 158 // Set defaults according to existing #default_value (set by Form API) 159 if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) { 160 $default_values = array( 161 'month' => $element['#default_value']['month'], 162 'day' => $element['#default_value']['day'], 163 'year' => $element['#default_value']['year'], 164 ); 165 } 166 // Or, if none, use set the defaults of the component. 167 elseif (drupal_strlen($component['value']) > 0) { 168 $timezone = $component['extra']['timezone'] != 'user' ? NULL : 'user'; 169 $default_values = webform_date_array(webform_strtodate('c', $component['value'], $timezone), 'date'); 170 } 171 else { 172 $default_values = array( 173 'day' => NULL, 174 'month' => NULL, 175 'year' => NULL, 176 ); 177 } 178 179 // Let Drupal do it's normal expansion. 180 $element = expand_date($element); 181 182 // Set default values. 183 foreach ($default_values as $type => $value) { 184 switch ($type) { 185 case 'month': 186 $none = t('Month'); 187 break; 188 case 'day': 189 $none = t('Day'); 190 break; 191 case 'year': 192 $none = t('Year'); 193 break; 194 } 195 unset($element[$type]['#value']); 196 $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL; 197 $element[$type]['#options'] = array('' => $none) + $element[$type]['#options']; 198 } 199 200 // Convert relative dates to absolute ones. 201 foreach (array('year_start', 'year_end') as $start_end) { 202 $year = $element['#' . $start_end]; 203 if (strpos($year, '-') === 0 || strpos($year, '+') === 0) { 204 $timezone = $component['extra']['timezone'] != 'user' ? NULL : 'user'; 205 $element['#' . $start_end] = webform_strtodate('Y', $year . ' years', $timezone); 206 } 207 } 208 209 // Tweak the year field. 210 if ($component['extra']['year_textfield']) { 211 $element['year']['#type'] = 'textfield'; 212 $element['year']['#size'] = 5; 213 $element['year']['#maxlength'] = 4; 214 unset($element['year']['#options']); 215 } 216 elseif (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) { 217 $element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($element['#year_start'], $element['#year_end'])); 218 } 219 220 return $element; 221 } 222 223 /** 224 * Theme a webform date element. 225 */ 226 function theme_webform_date($element) { 227 $element['year']['#attributes']['class'] = 'year'; 228 $element['month']['#attributes']['class'] = 'month'; 229 $element['day']['#attributes']['class'] = 'day'; 230 231 // Add error classes to all items within the element. 232 if (form_get_error($element)) { 233 $element['year']['#attributes']['class'] .= ' error'; 234 $element['month']['#attributes']['class'] .= ' error'; 235 $element['day']['#attributes']['class'] .= ' error'; 236 } 237 238 $class = array('webform-container-inline'); 239 240 // Add the JavaScript calendar if available (provided by Date module package). 241 if (!empty($element['#datepicker']) && function_exists('date_popup_load')) { 242 date_popup_load(); 243 244 $class[] = 'webform-datepicker'; 245 $calendar_class = array('webform-calendar'); 246 if ($element['#year_start'] && is_numeric($element['#year_start'])) { 247 $calendar_class[] = 'webform-calendar-start-' . $element['#year_start']; 248 } 249 if ($element['#year_start'] && is_numeric($element['#year_end'])) { 250 $calendar_class[] = 'webform-calendar-end-' . $element['#year_end']; 251 } 252 $calendar_class[] ='webform-calendar-day-' . variable_get('date_first_day', 0); 253 254 $calendar = theme('webform_calendar', $element['#webform_component'], $calendar_class); 255 } 256 257 $output = ''; 258 $output .= '<div class="' . implode(' ', $class) . '">'; 259 foreach (element_children($element) as $key) { 260 $output .= drupal_render($element[$key]); 261 } 262 $output .= isset($calendar) ? $calendar : ''; 263 $output .= '</div>'; 264 return $output; 265 } 266 267 /** 268 * Element validation for Webform date fields. 269 */ 270 function webform_validate_date($element, $form_state) { 271 $component = $element['#webform_component']; 272 $form_key = $component['form_key']; 273 $name = $component['name']; 274 275 // Check if the user filled the required fields. 276 foreach (array('day', 'month', 'year') as $field_type) { 277 if (!is_numeric($element[$field_type]['#value']) && $element['#required']) { 278 form_error($element, t('!name field is required.', array('!name' => $name))); 279 return; 280 } 281 } 282 // Check for a valid date. 283 if ($element['month']['#value'] !== '' || $element['day']['#value'] !== '' || $element['year']['#value'] !== '') { 284 if (!checkdate((int)$element['month']['#value'], (int)$element['day']['#value'], (int)$element['year']['#value'])) { 285 form_error($element, t('Entered !name is not a valid date.', array('!name' => $name))); 286 return; 287 } 288 } 289 // Check the date is between allowed years. 290 if ($element['year']['#value'] !== '' && is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) { 291 // Allow years to be in reverse order. 292 $start = $element['#year_start'] < $element['#year_end'] ? $element['#year_start'] : $element['#year_end']; 293 $end = $element['#year_start'] > $element['#year_end'] ? $element['#year_start'] : $element['#year_end']; 294 if ($element['year']['#value'] < $start || $element['year']['#value'] > $end) { 295 form_error($element['year'], t('The entered date needs to be between the years @start and @end.', array('@start' => $start, '@end' => $end))); 296 return; 297 } 298 } 299 } 300 301 /** 302 * Implementation of _webform_submit_component(). 303 */ 304 function _webform_submit_date($component, $value) { 305 // Convert the date to an ISO 8601 format. 306 return ($value['year'] && $value['month'] && $value['day']) ? webform_date_string($value, 'date') : ''; 307 } 308 309 /** 310 * Implementation of _webform_display_component(). 311 */ 312 function _webform_display_date($component, $value, $format = 'html') { 313 $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'date'); 314 315 return array( 316 '#title' => $component['name'], 317 '#weight' => $component['weight'], 318 '#theme' => 'webform_display_date', 319 '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'), 320 '#post_render' => array('webform_element_wrapper'), 321 '#format' => $format, 322 '#value' => $value, 323 '#webform_component' => $component, 324 ); 325 } 326 327 /** 328 * Format the text output for this component. 329 */ 330 function theme_webform_display_date($element) { 331 $output = ' '; 332 if ($element['#value']['year'] && $element['#value']['month'] && $element['#value']['day']) { 333 $timestamp = webform_strtotime($element['#value']['month'] . '/' . $element['#value']['day'] . '/' . $element['#value']['year']); 334 $format = webform_date_format('medium'); 335 $output = format_date($timestamp, 'custom', $format, 0); 336 } 337 338 return $output; 339 } 340 341 /** 342 * Implementation of _webform_analysis_component(). 343 */ 344 function _webform_analysis_date($component, $sids = array()) { 345 $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array(); 346 $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : ""; 347 $query = 'SELECT no,data ' . 348 ' FROM {webform_submitted_data} ' . 349 ' WHERE nid = %d ' . 350 ' AND cid = %d ' . $sidfilter . 351 ' ORDER BY sid ASC '; 352 353 $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids)); 354 355 $dates = array(); 356 $submissions = 0; 357 while ($row = db_fetch_array($result)) { 358 $submissions++; 359 if ($row['data']) { 360 $dates[] = webform_date_array($row['data']); 361 } 362 } 363 364 // Display stats. 365 $nonblanks = count($dates); 366 $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks)); 367 $rows[1] = array(t('User entered value'), $nonblanks); 368 return $rows; 369 } 370 371 /** 372 * Implementation of _webform_table_component(). 373 */ 374 function _webform_table_date($component, $value) { 375 if ($value[0]) { 376 $timestamp = webform_strtotime($value[0]); 377 $format = webform_date_format('short'); 378 return format_date($timestamp, 'custom', $format, 0); 379 } 380 else { 381 return ''; 382 } 383 } 384 385 /** 386 * Implementation of _webform_csv_headers_component(). 387 */ 388 function _webform_csv_headers_date($component, $export_options) { 389 $header = array(); 390 $header[0] = ''; 391 $header[1] = ''; 392 $header[2] = $component['name']; 393 return $header; 394 } 395 396 /** 397 * Implementation of _webform_csv_data_component(). 398 */ 399 function _webform_csv_data_date($component, $export_options, $value) { 400 if ($value[0]) { 401 $timestamp = webform_strtotime($value[0]); 402 $format = webform_date_format('short'); 403 return format_date($timestamp, 'custom', $format, 0); 404 } 405 else { 406 return ''; 407 } 408 } 409 410 /** 411 * Convert an ISO 8601 date or time into an array. 412 * 413 * This converst full format dates or times. Either a date or time may be 414 * provided, in which case only those portions will be returned. Dashes and 415 * colons must be used, never implied. 416 * 417 * Formats: 418 * Dates: YYYY-MM-DD 419 * Times: HH:MM:SS 420 * Datetimes: YYYY-MM-DDTHH:MM:SS 421 * 422 * @param $string 423 * An ISO 8601 date, time, or datetime. 424 * @param $type 425 * If wanting only specific fields back, specify either "date" or "time". 426 * Leaving empty will return an array with both date and time keys, even if 427 * some are empty. Returns an array with the following keys: 428 * - year 429 * - month 430 * - day 431 * - hour (in 24hr notation) 432 * - minute 433 * - second 434 */ 435 function webform_date_array($string, $type = NULL) { 436 $pattern = '/((\d{4}?)-(\d{2}?)-(\d{2}?))?(T?(\d{2}?):(\d{2}?):(\d{2}?))?/'; 437 $matches = array(); 438 preg_match($pattern, $string, $matches); 439 $matches += array_fill(0, 9, ''); 440 441 $return = array(); 442 443 // Check for a date string. 444 if ($type == 'date' || !isset($type)) { 445 $return['year'] = $matches[2] !== '' ? (int) $matches[2] : ''; 446 $return['month'] = $matches[3] !== '' ? (int) $matches[3] : ''; 447 $return['day'] = $matches[4] !== '' ? (int) $matches[4] : ''; 448 } 449 450 // Check for a time string. 451 if ($type == 'time' || !isset($type)) { 452 $return['hour'] = $matches[6] !== '' ? (int) $matches[6] : ''; 453 $return['minute'] = $matches[7] !== '' ? (int) $matches[7] : ''; 454 $return['second'] = $matches[8] !== '' ? (int) $matches[8] : ''; 455 } 456 457 return $return; 458 } 459 460 /** 461 * Convert an array of a date or time into an ISO 8601 compatible string. 462 * 463 * @param $array 464 * The array to convert to a date or time string. 465 * @param $type 466 * If wanting a specific string format back specify either "date" or "time". 467 * Otherwise a full ISO 8601 date and time string will be returned. 468 */ 469 function webform_date_string($array, $type = NULL) { 470 $string = ''; 471 472 if ($type == 'date' || !isset($type)) { 473 $string .= empty($array['year']) ? '0000' : sprintf('%04d', $array['year']); 474 $string .= '-'; 475 $string .= empty($array['month']) ? '00' : sprintf('%02d', $array['month']); 476 $string .= '-'; 477 $string .= empty($array['day']) ? '00' : sprintf('%02d', $array['day']); 478 } 479 480 if (!isset($type)) { 481 $string .= 'T'; 482 } 483 484 if ($type == 'time' || !isset($type)) { 485 $string .= empty($array['hour']) ? '00' : sprintf('%02d', $array['hour']); 486 $string .= ':'; 487 $string .= empty($array['minute']) ? '00' : sprintf('%02d', $array['minute']); 488 $string .= ':'; 489 $string .= empty($array['second']) ? '00' : sprintf('%02d', $array['second']); 490 } 491 492 return $string; 493 } 494 495 /** 496 * Get a date format according to the site settings. 497 * 498 * @param $size 499 * A choice of 'short', 'medium', or 'long' date formats. 500 */ 501 function webform_date_format($size = 'medium') { 502 // Format date according to site's given format. 503 $format = variable_get('date_format_' . $size, 'D, m/d/Y - H:i'); 504 $time = 'aABgGhHisueIOPTZ'; 505 $day_of_week = 'Dlw'; 506 $special = ',-: '; 507 $date_format = trim($format, $time . $day_of_week . $special); 508 509 // Ensure that a day, month, and year value are present. Use a default 510 // format if all the values are not found. 511 if (!preg_match('/[dj]/', $date_format) || !preg_match('/[FmMn]/', $date_format) || !preg_match('/[oYy]/', $date_format)) { 512 $date_format = 'm/d/Y'; 513 } 514 515 return $date_format; 516 } 517 518 /** 519 * Return a date in the format specied taking into consideration user timezones. 520 */ 521 function webform_strtodate($format, $string, $timezone_name = NULL) { 522 // Adjust the time based on the user or site timezone. 523 // The "timezone_name" variable is provided by DateAPI in Drupal 6. 524 if (variable_get('configurable_timezones', 1) && $timezone_name == 'user') { 525 $timezone_name = isset($GLOBALS['user']->timezone_name) ? $GLOBALS['user']->timezone_name : NULL; 526 } 527 elseif (empty($timezone_name) || $timezone_name == 'user') { 528 $timezone_name = variable_get('date_default_timezone_name', NULL); 529 } 530 531 if (!empty($timezone_name) && class_exists('DateTimeZone')) { 532 $timezone = new DateTimeZone($timezone_name); 533 $datetime = new DateTime($string, $timezone); 534 return $datetime->format($format); 535 } 536 else { 537 return date($format, strtotime($string)); 538 } 539 } 540 541 /** 542 * Get a timestamp in GMT time, ensuring timezone accuracy. 543 */ 544 function webform_strtotime($date) { 545 $current_tz = date_default_timezone_get(); 546 date_default_timezone_set('UTC'); 547 $timestamp = strtotime($date); 548 date_default_timezone_set($current_tz); 549 return $timestamp; 550 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |