time.inc 14 KB

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