| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * Webform module time component. 6 */ 7 8 // Time depends on functions provided by date. 9 webform_component_include('date'); 10 11 /** 12 * Implements _webform_defaults_component(). 13 */ 14 function _webform_defaults_time() { 15 return array( 16 'name' => '', 17 'form_key' => NULL, 18 'pid' => 0, 19 'weight' => 0, 20 'value' => '', 21 'mandatory' => 0, 22 'extra' => array( 23 'timezone' => 'user', 24 'hourformat' => '12-hour', 25 'minuteincrements' => 1, 26 'title_display' => 0, 27 'description' => '', 28 'private' => FALSE, 29 ), 30 ); 31 } 32 33 /** 34 * Implements _webform_theme_component(). 35 */ 36 function _webform_theme_time() { 37 return array( 38 'webform_time' => array( 39 'arguments' => array('element' => NULL), 40 'file' => 'components/time.inc', 41 ), 42 'webform_display_time' => array( 43 'arguments' => array('element' => NULL), 44 'file' => 'components/time.inc', 45 ), 46 ); 47 } 48 49 /** 50 * Implements _webform_edit_component(). 51 */ 52 function _webform_edit_time($component) { 53 $form = array(); 54 $form['value'] = array( 55 '#type' => 'textfield', 56 '#title' => t('Default value'), 57 '#default_value' => $component['value'], 58 '#description' => t('The default value of the field.') . '<br />' . t('Accepts a time 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 now, +2 hours, and 10:30pm are all valid.'), 59 '#size' => 60, 60 '#maxlength' => 127, 61 '#weight' => 0, 62 ); 63 $form['extra']['timezone'] = array( 64 '#type' => 'radios', 65 '#title' => t('Default value timezone'), 66 '#default_value' => $component['extra']['timezone'], 67 '#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'), 68 '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')), 69 '#weight' => 2, 70 '#access' => variable_get('configurable_timezones', 1) && module_exists('date_timezone'), 71 ); 72 $form['display']['hourformat'] = array( 73 '#type' => 'radios', 74 '#title' => t('Time format'), 75 '#default_value' => $component['extra']['hourformat'], 76 '#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')), 77 '#weight' => 2, 78 '#parents' => array('extra', 'hourformat'), 79 ); 80 $form['display']['minuteincrements'] = array( 81 '#type' => 'select', 82 '#title' => t('Minute increments'), 83 '#default_value' => $component['extra']['minuteincrements'], 84 '#options' => array( 85 1 => t('1 minute'), 86 5 => t('5 minute'), 87 10 => t('10 minute'), 88 15 => t('15 minute'), 89 30 => t('30 minute'), 90 ), 91 '#weight' => 3, 92 '#parents' => array('extra', 'minuteincrements'), 93 ); 94 return $form; 95 } 96 97 /** 98 * Implements _webform_render_component(). 99 */ 100 function _webform_render_time($component, $value = NULL, $filter = TRUE) { 101 $node = isset($component['nid']) ? node_load($component['nid']) : NULL; 102 103 $element = array( 104 '#type' => 'webform_time', 105 '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'], 106 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before', 107 '#required' => $component['mandatory'], 108 '#weight' => $component['weight'], 109 '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'], 110 '#element_validate' => array('webform_validate_time'), 111 '#hourformat' => $component['extra']['hourformat'], 112 '#minuteincrements' => $component['extra']['minuteincrements'], 113 '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'], 114 '#timezone' => $component['extra']['timezone'], 115 '#process' => array('webform_expand_time'), 116 '#theme' => 'webform_time', 117 '#theme_wrappers' => array('webform_element_wrapper'), 118 '#pre_render' => array('webform_element_title_display'), 119 '#post_render' => array('webform_element_wrapper'), 120 '#translatable' => array('title', 'description'), 121 ); 122 123 // Set the value from Webform if available. 124 if (!empty($value[0])) { 125 $element['#default_value'] = $value[0]; 126 } 127 128 return $element; 129 } 130 131 /** 132 * Form API #process function for Webform time fields. 133 */ 134 function webform_expand_time($element) { 135 // Expand the default value from a string into an array. 136 if (!empty($element['#default_value'])) { 137 // Adjust the time based on the user or site timezone. 138 // The "timezone_name" variable is provided by DateAPI in Drupal 6. 139 if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') { 140 $timezone_name = isset($GLOBALS['user']->timezone_name) ? $GLOBALS['user']->timezone_name : NULL; 141 } 142 else { 143 $timezone_name = variable_get('date_default_timezone_name', NULL); 144 } 145 146 if (!empty($timezone_name) && class_exists('DateTimeZone')) { 147 $default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time'); 148 } 149 else { 150 $default_values = webform_date_array(date('c', strtotime($element['#default_value'])), 'time'); 151 } 152 } 153 else { 154 $default_values = array( 155 'hour' => '', 156 'minute' => '', 157 'second' => '', 158 ); 159 } 160 161 $first_hour = 0; 162 $last_hour = 23; 163 if ($element['#hourformat'] == '12-hour') { 164 $first_hour = 1; 165 $last_hour = 12; 166 $default_values = webform_time_convert($default_values, '12-hour'); 167 $default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am'; 168 } 169 170 // Generate the choices for drop-down selects. 171 $hours[''] = t('hour'); 172 $minutes[''] = t('minute'); 173 for ($i = $first_hour; $i <= $last_hour; $i++) { 174 $hours[$i] = $i; 175 } 176 for ($i = 0; $i <= 59; $i += $element['#minuteincrements']) { 177 $minutes[$i] = $i < 10 ? "0$i" : $i; 178 } 179 $ampms = array('am' => t('am'), 'pm' => t('pm')); 180 181 // Adjust the default for minutes if needed, rounding up to the closest value. 182 if (!isset($minutes[$default_values['minute']])) { 183 foreach ($minutes as $minute => $padded_minute) { 184 if ($minute > $default_values['minute']) { 185 $default_values['minute'] = $minute; 186 break; 187 } 188 } 189 } 190 191 // If the above loop didn't set a value, it's because rounding up would go to 192 // the next hour. This gets quite a bit more complicated, since we need to 193 // deal with looping around on hours, as well as flipping am/pm. 194 if (!isset($minutes[$default_values['minute']])) { 195 $default_values['minute'] = 0; 196 $default_values['hour']++; 197 // If the hour rolls over also, set hour to the first hour in the list. 198 if (!isset($hours[$default_values['hour']])) { 199 $default_values['hour'] = $element['#hourformat'] == '12-hour' ? 1 : 0; 200 } 201 // If the hour has been incremented to 12:00 in 12-hour format, flip am/pm. 202 // Note that technically midnight and noon are neither am or pm, but common 203 // convention (and US standard) is to represent 12:00am as midnight. 204 // See http://en.wikipedia.org/wiki/Midnight#Start_and_end_of_day. 205 if ($element['#hourformat'] == '12-hour' && $default_values['hour'] == 12) { 206 $default_values['ampm'] = $default_values['ampm'] == 'am' ? 'pm' : 'am'; 207 } 208 } 209 210 $element['hour'] = array( 211 '#prefix' => '', 212 '#type' => 'select', 213 '#default_value' => $default_values['hour'], 214 '#options' => $hours, 215 ); 216 $element['minute'] = array( 217 '#prefix' => ':', 218 '#type' => 'select', 219 '#default_value' => $default_values['minute'], 220 '#options' => $minutes, 221 ); 222 if (strcmp($element['#hourformat'], '12-hour') == 0) { 223 $element['ampm'] = array( 224 '#type' => 'radios', 225 '#default_value' => $default_values['ampm'], 226 '#options' => $ampms, 227 ); 228 } 229 230 // Set the overall default value. 231 if ($default_values['hour'] !== '') { 232 $element['#default_value'] = webform_date_string($default_values); 233 } 234 235 return $element; 236 } 237 238 /** 239 * Theme a webform time element. 240 */ 241 function theme_webform_time($element) { 242 // Add error classes to all items within the element. 243 if (form_get_error($element)) { 244 $element['hour']['#attributes']['class'] = 'error'; 245 $element['minute']['#attributes']['class'] = 'error'; 246 } 247 248 $output = '<div class="webform-container-inline">' . drupal_render($element['hour']) . drupal_render($element['minute']) . drupal_render($element['ampm']) . '</div>'; 249 250 $element['#type'] = 'element'; 251 return theme('form_element', $element, $output); 252 } 253 254 function webform_validate_time($element, $form_state) { 255 $form_key = $element['#webform_component']['form_key']; 256 $name = $element['#webform_component']['name']; 257 258 // Check if the user filled the required fields. 259 foreach ($element['#hourformat'] == '12-hour' ? array('hour', 'minute', 'ampm') : array('hour', 'minute') as $field_type) { 260 if ($element[$field_type]['#value'] === '' && $element['#required']) { 261 form_error($element, t('%field field is required.', array('%field' => $name))); 262 return; 263 } 264 } 265 266 // Check for a valid time. 267 if ($element['hour']['#value'] !== '' || $element['minute']['#value'] !== '') { 268 if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || (isset($element['ampm']) && $element['ampm']['#value'] === '')) { 269 form_error($element, t('Entered %name is not a valid time.', array('%name' => $name))); 270 return; 271 } 272 } 273 } 274 275 /** 276 * Implements _webform_submit_component(). 277 */ 278 function _webform_submit_time($component, $value) { 279 // Convert to 24-hour time before string conversion. 280 if ($component['extra']['hourformat'] == '12-hour') { 281 $value = webform_time_convert($value, '24-hour'); 282 } 283 284 // Convert the value into a ISO 8601 string. 285 return $value['hour'] !== '' ? webform_date_string($value, 'time') : ''; 286 } 287 288 /** 289 * Implements _webform_display_component(). 290 */ 291 function _webform_display_time($component, $value, $format = 'html') { 292 $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'time'); 293 if ($component['extra']['hourformat'] == '12-hour') { 294 $value = webform_time_convert($value, '12-hour'); 295 } 296 297 return array( 298 '#title' => $component['name'], 299 '#weight' => $component['weight'], 300 '#theme' => 'webform_display_time', 301 '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'), 302 '#post_render' => array('webform_element_wrapper'), 303 '#format' => $format, 304 '#hourformat' => $component['extra']['hourformat'], 305 '#value' => $value, 306 '#pre_render' => array('webform_element_title_display'), 307 '#translatable' => array('title'), 308 ); 309 } 310 311 /** 312 * Format the output of data for this component. 313 */ 314 function theme_webform_display_time($element) { 315 $output = ' '; 316 if (isset($element['#value']['hour']) && $element['#value']['hour'] !== '' && isset($element['#value']['minute']) && $element['#value']['minute'] !== '') { 317 if ($element['#hourformat'] == '24-hour') { 318 $output = sprintf('%02d', $element['#value']['hour']) . ':' . sprintf('%02d', $element['#value']['minute']); 319 } 320 else { 321 $output = $element['#value']['hour'] . ':' . sprintf('%02d', $element['#value']['minute']) . ' ' . $element['#value']['ampm']; 322 } 323 } 324 return $output; 325 } 326 327 /** 328 * Implements _webform_analysis_component(). 329 */ 330 function _webform_analysis_time($component, $sids = array()) { 331 $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array(); 332 $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : ""; 333 $query = 'SELECT no,data ' . 334 ' FROM {webform_submitted_data} ' . 335 ' WHERE nid = %d ' . 336 ' AND cid = %d ' . $sidfilter . 337 ' ORDER BY sid ASC '; 338 339 $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids)); 340 341 $times = array(); 342 $submissions = 0; 343 while ($row = db_fetch_array($result)) { 344 $submissions++; 345 if ($row['data']) { 346 $times[] = webform_date_array($row['data']); 347 } 348 } 349 350 // Display stats. 351 $nonblanks = count($times); 352 $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks)); 353 $rows[1] = array(t('User entered value'), $nonblanks); 354 return $rows; 355 } 356 357 /** 358 * Implements _webform_table_component(). 359 */ 360 function _webform_table_time($component, $value) { 361 if ($value[0]) { 362 $time = webform_date_array($value[0], 'time'); 363 if ($component['extra']['hourformat'] == '24-hour') { 364 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); 365 } 366 else { 367 $time = webform_time_convert($time, '12-hour'); 368 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; 369 } 370 } 371 else { 372 return ''; 373 } 374 } 375 376 /** 377 * Implements _webform_csv_headers_component(). 378 */ 379 function _webform_csv_headers_time($component, $export_options) { 380 $header = array(); 381 $header[0] = ''; 382 $header[1] = ''; 383 $header[2] = $component['name']; 384 return $header; 385 } 386 387 /** 388 * Implements _webform_csv_data_component(). 389 */ 390 function _webform_csv_data_time($component, $export_options, $value) { 391 if ($value[0]) { 392 $time = webform_date_array($value[0], 'time'); 393 if ($component['extra']['hourformat'] == '24-hour') { 394 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); 395 } 396 else { 397 $time = webform_time_convert($time, '12-hour'); 398 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; 399 } 400 } 401 else { 402 return ''; 403 } 404 } 405 406 /** 407 * Convert a time between a 24-hour and a 12-hour value. 408 * 409 * @param $array 410 * An array of hour, minute, second, and optionally ampm. 411 * @param $format 412 * Either 12-hour or 24-hour. 413 * @return 414 * An array with hour, minute, second, and ampm (if using "12-hour"). 415 */ 416 function webform_time_convert($array, $format) { 417 if ($array['hour'] !== '') { 418 if ($format == '12-hour') { 419 $array['ampm'] = ($array['hour'] >= 12 && $array['hour'] < 24) ? 'pm' : 'am'; 420 $array['hour'] = ($array['hour'] > 12 || $array['hour'] == 0) ? abs($array['hour'] - 12) : (int) $array['hour']; 421 } 422 elseif ($format == '24-hour' && isset($array['ampm'])) { 423 $array['hour'] = ($array['hour'] < 12 && $array['ampm'] == 'pm') ? $array['hour'] + 12 : (int) $array['hour']; 424 $array['hour'] = ($array['hour'] == 12 && $array['ampm'] == 'am') ? 0 : $array['hour']; 425 } 426 } 427 428 if ($format == '12-hour' && !isset($array['ampm'])) { 429 $array['ampm'] = ''; 430 } 431 elseif ($format == '24-hour' && isset($array['ampm'])) { 432 unset($array['ampm']); 433 } 434 435 return $array; 436 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |