| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: time.inc,v 1.24.2.11 2010/09/28 22:43:55 quicksketch Exp $ 3 4 /** 5 * @file 6 * Webform module time component. 7 */ 8 9 // Time depends on functions provided by date. 10 webform_component_include('date'); 11 12 /** 13 * Implementation of _webform_defaults_component(). 14 */ 15 function _webform_defaults_time() { 16 return array( 17 'name' => '', 18 'form_key' => NULL, 19 'pid' => 0, 20 'weight' => 0, 21 'value' => '', 22 'mandatory' => 0, 23 'email' => 1, 24 'extra' => array( 25 'timezone' => 'user', 26 'hourformat' => '12-hour', 27 'title_display' => 0, 28 'description' => '', 29 ), 30 ); 31 } 32 33 /** 34 * Implementation of _webform_theme_component(). 35 */ 36 function _webform_theme_time() { 37 return array( 38 'webform_time' => array( 39 'arguments' => array('element' => NULL), 40 ), 41 'webform_display_time' => array( 42 'arguments' => array('element' => NULL), 43 ), 44 ); 45 } 46 47 /** 48 * Implementation of _webform_edit_component(). 49 */ 50 function _webform_edit_time($component) { 51 $form = array(); 52 $form['value'] = array( 53 '#type' => 'textfield', 54 '#title' => t('Default value'), 55 '#default_value' => $component['value'], 56 '#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.'), 57 '#size' => 60, 58 '#maxlength' => 127, 59 '#weight' => 0, 60 ); 61 $form['extra']['timezone'] = array( 62 '#type' => 'radios', 63 '#title' => t('Timezone'), 64 '#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'], 65 '#description' => t('Adjust the default time value according to a specific timezone.'), 66 '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')), 67 '#weight' => 0, 68 '#access' => variable_get('configurable_timezones', 1) && module_exists('date_timezone'), 69 ); 70 $form['display']['hourformat'] = array( 71 '#type' => 'radios', 72 '#title' => t('Time Format'), 73 '#default_value' => isset($component['extra']['hourformat']) ? $component['extra']['hourformat'] : '12-hour', 74 '#description' => t('Format the display of the time in 12 or 24 hours.'), 75 '#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')), 76 '#weight' => 2, 77 '#parents' => array('extra', 'hourformat'), 78 ); 79 return $form; 80 } 81 82 /** 83 * Implementation of _webform_render_component(). 84 */ 85 function _webform_render_time($component, $value = NULL, $filter = TRUE) { 86 if (drupal_strlen($component['value']) > 0) { 87 // Adjust the time based on the user or site timezone. 88 // The "timezone_name" variable is provided by DateAPI in Drupal 6. 89 if (variable_get('configurable_timezones', 1) && $component['extra']['timezone'] == 'user') { 90 $timezone_name = isset($GLOBALS['user']->timezone_name) ? $GLOBALS['user']->timezone_name : NULL; 91 } 92 else { 93 $timezone_name = variable_get('date_default_timezone_name', NULL); 94 } 95 96 if (isset($timezone_name) && class_exists('DateTimeZone')) { 97 $timezone = new DateTimeZone($timezone_name); 98 $datetime = new DateTime($component['value'], $timezone); 99 $default_values = webform_date_array($datetime->format('c'), 'time'); 100 } 101 else { 102 $default_values = webform_date_array(date('c', strtotime($component['value'])), 'time'); 103 } 104 } 105 else { 106 $default_values = array( 107 'hour' => '', 108 'minute' => '', 109 'second' => '', 110 ); 111 } 112 113 $first_hour = 0; 114 $last_hour = 23; 115 if ($component['extra']['hourformat'] == '12-hour') { 116 $first_hour = 1; 117 $last_hour = 12; 118 $default_values = webform_time_convert($default_values, '12-hour'); 119 $default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am'; 120 } 121 122 // Generate the choices for drop-down selects. 123 $hours[''] = t('hour'); 124 $minutes[''] = t('minute'); 125 for ($i = $first_hour; $i <= $last_hour; $i++) $hours[$i] = $i; 126 for ($i = 0; $i <= 59; $i++) $minutes[$i] = $i < 10 ? "0$i" : $i; 127 $ampms = array('am' => t('am'), 'pm' => t('pm')); 128 129 $element = array( 130 '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'], 131 '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL, 132 '#required' => $component['mandatory'], 133 '#weight' => $component['weight'], 134 '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'], 135 '#prefix' => '<div class="webform-component webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">', 136 '#suffix' => '</div>', 137 '#theme' => 'webform_time', 138 '#element_validate' => array('webform_validate_time'), 139 '#hourformat' => $component['extra']['hourformat'], 140 '#pre_render' => array('webform_element_title_display'), 141 '#webform_component' => $component, 142 ); 143 144 $element['hour'] = array( 145 '#prefix' => '', 146 '#type' => 'select', 147 '#default_value' => $default_values['hour'], 148 '#options' => $hours, 149 ); 150 $element['minute'] = array( 151 '#prefix' => ':', 152 '#type' => 'select', 153 '#default_value' => $default_values['minute'], 154 '#options' => $minutes, 155 ); 156 if ($component['extra']['hourformat'] == '12-hour') { 157 $element['ampm'] = array( 158 '#type' => 'radios', 159 '#default_value' => $default_values['ampm'], 160 '#options' => $ampms, 161 ); 162 } 163 164 if (isset($value[0]) && $value[0] !== '') { 165 $value = webform_date_array($value[0], 'time'); 166 if ($component['extra']['hourformat'] == '12-hour') { 167 $value = webform_time_convert($value, '12-hour'); 168 } 169 170 $element['hour']['#default_value'] = $value['hour']; 171 $element['minute']['#default_value'] = $value['minute']; 172 if (isset($value['ampm'])) { 173 $element['ampm']['#default_value'] = $value['ampm']; 174 } 175 } 176 177 return $element; 178 } 179 180 /** 181 * Theme a webform time element. 182 */ 183 function theme_webform_time($element) { 184 // Add error classes to all items within the element. 185 if (form_get_error($element)) { 186 $element['hour']['#attributes']['class'] = 'error'; 187 $element['minute']['#attributes']['class'] = 'error'; 188 } 189 190 $output = '<div class="webform-container-inline">' . drupal_render($element['hour']) . drupal_render($element['minute']) . drupal_render($element['ampm']) . '</div>'; 191 192 $element['#type'] = 'element'; 193 return theme('form_element', $element, $output); 194 } 195 196 function webform_validate_time($element, $form_state) { 197 $form_key = $element['#webform_component']['form_key']; 198 $name = $element['#webform_component']['name']; 199 200 // Check if the user filled the required fields. 201 foreach ($element['#hourformat'] == '12-hour' ? array('hour', 'minute', 'ampm') : array('hour', 'minute') as $field_type) { 202 if ($element[$field_type]['#value'] == '' && $element['#required']) { 203 form_error($element, t('%field field is required.', array('%field' => $name))); 204 return; 205 } 206 } 207 208 // Check for a valid time. 209 if ($element['hour']['#value'] !== '' || $element['minute']['#value'] !== '') { 210 if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || (isset($element['ampm']) && $element['ampm']['#value'] === '')) { 211 form_error($element, t('Entered %name is not a valid time.', array('%name' => $name))); 212 return; 213 } 214 } 215 } 216 217 /** 218 * Implementation of _webform_submit_component(). 219 */ 220 function _webform_submit_time($component, $value) { 221 // Convert to 24-hour time before string conversion. 222 if ($component['extra']['hourformat'] == '12-hour') { 223 $value = webform_time_convert($value, '24-hour'); 224 } 225 226 // Convert the value into a ISO 8601 string. 227 return $value['hour'] !== '' ? webform_date_string($value, 'time') : ''; 228 } 229 230 /** 231 * Implementation of _webform_display_component(). 232 */ 233 function _webform_display_time($component, $value, $format = 'html') { 234 $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'time'); 235 if ($component['extra']['hourformat'] == '12-hour') { 236 $value = webform_time_convert($value, '12-hour'); 237 } 238 239 return array( 240 '#title' => $component['name'], 241 '#weight' => $component['weight'], 242 '#theme' => 'webform_display_time', 243 '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'), 244 '#post_render' => array('webform_element_wrapper'), 245 '#format' => $format, 246 '#hourformat' => $component['extra']['hourformat'], 247 '#value' => $value, 248 '#pre_render' => array('webform_element_title_display'), 249 '#webform_component' => $component, 250 ); 251 } 252 253 /** 254 * Format the output of data for this component. 255 */ 256 function theme_webform_display_time($element) { 257 $output = ' '; 258 if (isset($element['#value']['hour']) && isset($element['#value']['minute'])) { 259 if ($element['#hourformat'] == '24-hour') { 260 $output = sprintf('%02d', $element['#value']['hour']) . ':' . sprintf('%02d', $element['#value']['minute']); 261 } 262 else { 263 $output = $element['#value']['hour'] . ':' . sprintf('%02d', $element['#value']['minute']) . ' ' . $element['#value']['ampm']; 264 } 265 } 266 return $output; 267 } 268 269 /** 270 * Implementation of _webform_analysis_component(). 271 */ 272 function _webform_analysis_time($component, $sids = array()) { 273 $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array(); 274 $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : ""; 275 $query = 'SELECT no,data ' . 276 ' FROM {webform_submitted_data} ' . 277 ' WHERE nid = %d ' . 278 ' AND cid = %d ' . $sidfilter . 279 ' ORDER BY sid ASC '; 280 281 $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids)); 282 283 $times = array(); 284 $submissions = 0; 285 while ($row = db_fetch_array($result)) { 286 $submissions++; 287 if ($row['data']) { 288 $times[] = webform_date_array($row['data']); 289 } 290 } 291 292 // Display stats. 293 $nonblanks = count($times); 294 $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks)); 295 $rows[1] = array(t('User entered value'), $nonblanks); 296 return $rows; 297 } 298 299 /** 300 * Implementation of _webform_table_component(). 301 */ 302 function _webform_table_time($component, $value) { 303 if ($value[0]) { 304 $time = webform_date_array($value[0], 'time'); 305 if ($component['extra']['hourformat'] == '24-hour') { 306 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); 307 } 308 else { 309 $time = webform_time_convert($time, '12-hour'); 310 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; 311 } 312 } 313 else { 314 return ''; 315 } 316 } 317 318 /** 319 * Implementation of _webform_csv_headers_component(). 320 */ 321 function _webform_csv_headers_time($component, $export_options) { 322 $header = array(); 323 $header[0] = ''; 324 $header[1] = ''; 325 $header[2] = $component['name']; 326 return $header; 327 } 328 329 /** 330 * Implementation of _webform_csv_data_component(). 331 */ 332 function _webform_csv_data_time($component, $export_options, $value) { 333 if ($value[0]) { 334 $time = webform_date_array($value[0], 'time'); 335 if ($component['extra']['hourformat'] == '24-hour') { 336 return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']); 337 } 338 else { 339 $time = webform_time_convert($time, '12-hour'); 340 return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm']; 341 } 342 } 343 else { 344 return ''; 345 } 346 } 347 348 /** 349 * Convert a time between a 24-hour and a 12-hour value. 350 * 351 * @param $array 352 * An array of hour, minute, second, and optionally ampm. 353 * @param $format 354 * Either 12-hour or 24-hour. 355 * @return 356 * An array with hour, minute, second, and ampm (if using "12-hour"). 357 */ 358 function webform_time_convert($array, $format) { 359 if ($array['hour'] !== '') { 360 if ($format == '12-hour') { 361 $array['ampm'] = ($array['hour'] >= 12 && $array['hour'] < 24) ? 'pm' : 'am'; 362 $array['hour'] = ($array['hour'] > 12 || $array['hour'] == 0) ? abs($array['hour'] - 12) : (int) $array['hour']; 363 } 364 elseif ($format == '24-hour' && isset($array['ampm'])) { 365 $array['hour'] = ($array['hour'] < 12 && $array['ampm'] == 'pm') ? $array['hour'] + 12 : (int) $array['hour']; 366 } 367 } 368 369 if ($format == '12-hour' && !isset($array['ampm'])) { 370 $array['ampm'] = ''; 371 } 372 elseif ($format == '24-hour' && isset($array['ampm'])) { 373 unset($array['ampm']); 374 } 375 376 return $array; 377 }
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 |