time.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. 'title_display' => 0,
  23. 'description' => '',
  24. 'private' => FALSE,
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements _webform_theme_component().
  30. */
  31. function _webform_theme_time() {
  32. return array(
  33. 'webform_time' => array(
  34. 'render element' => 'element',
  35. 'file' => 'components/time.inc',
  36. ),
  37. 'webform_display_time' => array(
  38. 'render element' => 'element',
  39. 'file' => 'components/time.inc',
  40. ),
  41. );
  42. }
  43. /**
  44. * Implements _webform_edit_component().
  45. */
  46. function _webform_edit_time($component) {
  47. $form = array();
  48. $form['value'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Default value'),
  51. '#default_value' => $component['value'],
  52. '#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.'),
  53. '#size' => 60,
  54. '#maxlength' => 127,
  55. '#weight' => 0,
  56. );
  57. $form['extra']['timezone'] = array(
  58. '#type' => 'radios',
  59. '#title' => t('Default value timezone'),
  60. '#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'],
  61. '#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'),
  62. '#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
  63. '#weight' => 2,
  64. '#access' => variable_get('configurable_timezones', 1),
  65. );
  66. $form['display']['hourformat'] = array(
  67. '#type' => 'radios',
  68. '#title' => t('Time Format'),
  69. '#default_value' => isset($component['extra']['hourformat']) ? $component['extra']['hourformat'] : '12-hour',
  70. '#description' => t('Format the display of the time in 12 or 24 hours.'),
  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. return $form;
  76. }
  77. /**
  78. * Implements _webform_render_component().
  79. */
  80. function _webform_render_time($component, $value = NULL, $filter = TRUE) {
  81. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  82. $element = array(
  83. '#type' => 'webform_time',
  84. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  85. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  86. '#required' => $component['mandatory'],
  87. '#weight' => $component['weight'],
  88. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  89. '#element_validate' => array('webform_validate_time'),
  90. '#hourformat' => $component['extra']['hourformat'],
  91. '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
  92. '#timezone' => $component['extra']['timezone'],
  93. '#process' => array('webform_expand_time'),
  94. '#theme' => 'webform_time',
  95. '#theme_wrappers' => array('webform_element'),
  96. '#translatable' => array('title', 'description'),
  97. );
  98. // Set the value from Webform if available.
  99. if (!empty($value[0])) {
  100. $element['#default_value'] = $value[0];
  101. }
  102. return $element;
  103. }
  104. /**
  105. * Form API #process function for Webform time fields.
  106. */
  107. function webform_expand_time($element) {
  108. // Expand the default value from a string into an array.
  109. if (!empty($element['#default_value'])) {
  110. // Adjust the time based on the user or site timezone.
  111. if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') {
  112. $timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
  113. }
  114. else {
  115. $timezone_name = variable_get('date_default_timezone', 'UTC');
  116. }
  117. $default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time');
  118. }
  119. else {
  120. $default_values = array(
  121. 'hour' => '',
  122. 'minute' => '',
  123. 'second' => '',
  124. );
  125. }
  126. $first_hour = 0;
  127. $last_hour = 23;
  128. if ($element['#hourformat'] == '12-hour') {
  129. $first_hour = 1;
  130. $last_hour = 12;
  131. $default_values = webform_time_convert($default_values, '12-hour');
  132. $default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am';
  133. }
  134. // Generate the choices for drop-down selects.
  135. $hours[''] = t('hour');
  136. $minutes[''] = t('minute');
  137. for ($i = $first_hour; $i <= $last_hour; $i++) $hours[$i] = $i;
  138. for ($i = 0; $i <= 59; $i++) $minutes[$i] = $i < 10 ? "0$i" : $i;
  139. $ampms = array('am' => t('am'), 'pm' => t('pm'));
  140. $element['hour'] = array(
  141. '#prefix' => '',
  142. '#type' => 'select',
  143. '#default_value' => $default_values['hour'],
  144. '#options' => $hours,
  145. );
  146. $element['minute'] = array(
  147. '#prefix' => ':',
  148. '#type' => 'select',
  149. '#default_value' => $default_values['minute'],
  150. '#options' => $minutes,
  151. );
  152. if (strcmp($element['#hourformat'], '12-hour') == 0) {
  153. $element['ampm'] = array(
  154. '#type' => 'radios',
  155. '#default_value' => $default_values['ampm'],
  156. '#options' => $ampms,
  157. );
  158. }
  159. // Set the overall default value.
  160. if ($default_values['hour'] !== '') {
  161. $element['#default_value'] = webform_date_string($default_values);
  162. }
  163. return $element;
  164. }
  165. /**
  166. * Theme a webform time element.
  167. */
  168. function theme_webform_time($variables) {
  169. $element = $variables['element'];
  170. // Add error classes to all items within the element.
  171. if (form_get_error($element)) {
  172. $element['hour']['#attributes']['class'] = array('error');
  173. $element['minute']['#attributes']['class'] = array('error');
  174. }
  175. $output = '<div class="webform-container-inline">' . drupal_render($element['hour']) . drupal_render($element['minute']) . drupal_render($element['ampm']) . '</div>';
  176. return $output;
  177. }
  178. function webform_validate_time($element, $form_state) {
  179. $form_key = $element['#webform_component']['form_key'];
  180. $name = $element['#webform_component']['name'];
  181. // Check if the user filled the required fields.
  182. foreach ($element['#hourformat'] == '12-hour' ? array('hour', 'minute', 'ampm') : array('hour', 'minute') as $field_type) {
  183. if ($element[$field_type]['#value'] === '' && $element['#required']) {
  184. form_error($element, t('%field field is required.', array('%field' => $name)));
  185. return;
  186. }
  187. }
  188. // Check for a valid time.
  189. if ($element['hour']['#value'] !== '' || $element['minute']['#value'] !== '') {
  190. if (!is_numeric($element['hour']['#value']) || !is_numeric($element['minute']['#value']) || (isset($element['ampm']) && $element['ampm']['#value'] === '')) {
  191. form_error($element, t('Entered %name is not a valid time.', array('%name' => $name)));
  192. return;
  193. }
  194. }
  195. }
  196. /**
  197. * Implements _webform_submit_component().
  198. */
  199. function _webform_submit_time($component, $value) {
  200. // Convert to 24-hour time before string conversion.
  201. if ($component['extra']['hourformat'] == '12-hour') {
  202. $value = webform_time_convert($value, '24-hour');
  203. }
  204. // Convert the value into a ISO 8601 string.
  205. return $value['hour'] !== '' ? webform_date_string($value, 'time') : '';
  206. }
  207. /**
  208. * Implements _webform_display_component().
  209. */
  210. function _webform_display_time($component, $value, $format = 'html') {
  211. $value = webform_date_array(isset($value[0]) ? $value[0] : '', 'time');
  212. if ($component['extra']['hourformat'] == '12-hour') {
  213. $value = webform_time_convert($value, '12-hour');
  214. }
  215. return array(
  216. '#title' => $component['name'],
  217. '#weight' => $component['weight'],
  218. '#theme' => 'webform_display_time',
  219. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  220. '#format' => $format,
  221. '#hourformat' => $component['extra']['hourformat'],
  222. '#value' => $value,
  223. '#translatable' => array('title'),
  224. );
  225. }
  226. /**
  227. * Format the output of data for this component.
  228. */
  229. function theme_webform_display_time($variables) {
  230. $element = $variables['element'];
  231. $output = ' ';
  232. if (isset($element['#value']['hour']) && $element['#value']['hour'] !== '' && isset($element['#value']['minute']) && $element['#value']['minute'] !== '') {
  233. if ($element['#hourformat'] == '24-hour') {
  234. $output = sprintf('%02d', $element['#value']['hour']) . ':' . sprintf('%02d', $element['#value']['minute']);
  235. }
  236. else {
  237. $output = $element['#value']['hour'] . ':' . sprintf('%02d', $element['#value']['minute']) . ' ' . $element['#value']['ampm'];
  238. }
  239. }
  240. return $output;
  241. }
  242. /**
  243. * Implements _webform_analysis_component().
  244. */
  245. function _webform_analysis_time($component, $sids = array()) {
  246. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  247. ->fields('wsd', array('no', 'data'))
  248. ->condition('nid', $component['nid'])
  249. ->condition('cid', $component['cid'])
  250. ->orderBy('sid');
  251. if (count($sids)) {
  252. $query->condition('sid', $sids, 'IN');
  253. }
  254. $result = $query->execute();
  255. $times = array();
  256. $submissions = 0;
  257. foreach ($result as $row) {
  258. $submissions++;
  259. if ($row['data']) {
  260. $times[] = webform_date_array($row['data']);
  261. }
  262. }
  263. // Display stats.
  264. $nonblanks = count($times);
  265. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  266. $rows[1] = array(t('User entered value'), $nonblanks);
  267. return $rows;
  268. }
  269. /**
  270. * Implements _webform_table_component().
  271. */
  272. function _webform_table_time($component, $value) {
  273. if ($value[0]) {
  274. $time = webform_date_array($value[0], 'time');
  275. if ($component['extra']['hourformat'] == '24-hour') {
  276. return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']);
  277. }
  278. else {
  279. $time = webform_time_convert($time, '12-hour');
  280. return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm'];
  281. }
  282. }
  283. else {
  284. return '';
  285. }
  286. }
  287. /**
  288. * Implements _webform_csv_headers_component().
  289. */
  290. function _webform_csv_headers_time($component, $export_options) {
  291. $header = array();
  292. $header[0] = '';
  293. $header[1] = '';
  294. $header[2] = $component['name'];
  295. return $header;
  296. }
  297. /**
  298. * Implements _webform_csv_data_component().
  299. */
  300. function _webform_csv_data_time($component, $export_options, $value) {
  301. if ($value[0]) {
  302. $time = webform_date_array($value[0], 'time');
  303. if ($component['extra']['hourformat'] == '24-hour') {
  304. return sprintf('%02d', $time['hour']) . ':' . sprintf('%02d', $time['minute']);
  305. }
  306. else {
  307. $time = webform_time_convert($time, '12-hour');
  308. return $time['hour'] . ':' . sprintf('%02d', $time['minute']) . ' ' . $time['ampm'];
  309. }
  310. }
  311. else {
  312. return '';
  313. }
  314. }
  315. /**
  316. * Convert a time between a 24-hour and a 12-hour value.
  317. *
  318. * @param $array
  319. * An array of hour, minute, second, and optionally ampm.
  320. * @param $format
  321. * Either 12-hour or 24-hour.
  322. * @return
  323. * An array with hour, minute, second, and ampm (if using "12-hour").
  324. */
  325. function webform_time_convert($array, $format) {
  326. if ($array['hour'] !== '') {
  327. if ($format == '12-hour') {
  328. $array['ampm'] = ($array['hour'] >= 12 && $array['hour'] < 24) ? 'pm' : 'am';
  329. $array['hour'] = ($array['hour'] > 12 || $array['hour'] == 0) ? abs($array['hour'] - 12) : (int) $array['hour'];
  330. }
  331. elseif ($format == '24-hour' && isset($array['ampm'])) {
  332. $array['hour'] = ($array['hour'] < 12 && $array['ampm'] == 'pm') ? $array['hour'] + 12 : (int) $array['hour'];
  333. }
  334. }
  335. if ($format == '12-hour' && !isset($array['ampm'])) {
  336. $array['ampm'] = '';
  337. }
  338. elseif ($format == '24-hour' && isset($array['ampm'])) {
  339. unset($array['ampm']);
  340. }
  341. return $array;
  342. }