date_popup.module

Tracking 7.x-2.x branch
  1. drupal
    1. 5 contributions/date/date_popup/date_popup.module
    2. 6 contributions/date/date_popup/date_popup.module
    3. 7 contributions/date/date_popup/date_popup.module

A module to enable jquery calendar and time entry popups. Requires the Date API.

Add a type of #date_popup to any date, time, or datetime field that will use this popup. Set #date_format to the way the date should be presented to the user in the form. Set #default_value to be a date in the local timezone, and note the timezone name in #date_timezone.

The element will create two textfields, one for the date and one for the time. The date textfield will include a jQuery popup calendar date picker, and the time textfield uses a jQuery timepicker.

If no time elements are included in the format string, only the date textfield will be created. If no date elements are included in the format string, only the time textfield, will be created.

Functions & methods

NameDescription
date_popup_addLoad needed files.
date_popup_datepicker_format_replacementsThe format replacement patterns for the new datepicker.
date_popup_date_format
date_popup_date_granularity
date_popup_element_infoImplements hook_element_info().
date_popup_element_processJavascript popup element processing. Add popup attributes to $element.
date_popup_element_value_callbackElement value callback for date_popup element.
date_popup_formatsFormat options array.
date_popup_format_to_popupRecreate a date format string so it has the values popup expects.
date_popup_format_to_popup_timeRecreate a time format string so it has the values popup expects.
date_popup_get_preferred_timepickerGet the name of the preferred default timepicker.
date_popup_get_wvega_pathGet the location of the Willington Vega timepicker library.
date_popup_input_dateHelper function for extracting a date value out of user input.
date_popup_js_settings_idCreate a unique CSS id name and output a single inline JS block for each startup function to call and settings array to pass it. This used to create a unique CSS class for each unique combination of function and settings, but using classes requires a…
date_popup_libraryImplements hook_library().
date_popup_menuImplements hook_menu().
date_popup_popup_to_formatReconstruct popup format string into normal format string.
date_popup_process_date_partProcess the date portion of the element.
date_popup_process_time_partProcess the time portion of the element.
date_popup_settingsGeneral configuration form for controlling the Date Popup behaviour.
date_popup_theme
date_popup_timepicker_format_replacementsReturn a map of format replacements required for a given timepicker.
date_popup_time_format
date_popup_time_formatsAllowable time formats.
date_popup_time_granularity
date_popup_validateMassage the input values back into a single date.
theme_date_popupFormat a date popup element.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * A module to enable jquery calendar and time entry popups.
  5. * Requires the Date API.
  6. *
  7. * Add a type of #date_popup to any date, time, or datetime field that will
  8. * use this popup. Set #date_format to the way the date should be presented
  9. * to the user in the form. Set #default_value to be a date in the local
  10. * timezone, and note the timezone name in #date_timezone.
  11. *
  12. * The element will create two textfields, one for the date and one for the
  13. * time. The date textfield will include a jQuery popup calendar date picker,
  14. * and the time textfield uses a jQuery timepicker.
  15. *
  16. * If no time elements are included in the format string, only the date
  17. * textfield will be created. If no date elements are included in the format
  18. * string, only the time textfield, will be created.
  19. *
  20. */
  21. /**
  22. * Load needed files.
  23. *
  24. * Play nice with jQuery UI.
  25. */
  26. function date_popup_add() {
  27. static $loaded = FALSE;
  28. if ($loaded) {
  29. return;
  30. }
  31. drupal_add_library('system', 'ui.datepicker');
  32. drupal_add_library('date_popup', 'timeentry');
  33. // Add the wvega-timepicker library if it's available.
  34. $wvega_path = date_popup_get_wvega_path();
  35. if ($wvega_path) {
  36. drupal_add_js($wvega_path . '/jquery.timepicker.js');
  37. drupal_add_css($wvega_path . '/jquery.timepicker.css');
  38. }
  39. $loaded = TRUE;
  40. }
  41. /**
  42. * Get the location of the Willington Vega timepicker library.
  43. *
  44. * @return
  45. * The location of the library, or FALSE if the library isn't installed.
  46. */
  47. function date_popup_get_wvega_path() {
  48. $path = FALSE;
  49. if (function_exists('libraries_get_path')) {
  50. $path = libraries_get_path('wvega-timepicker');
  51. if (!file_exists($path)) {
  52. $path = FALSE;
  53. }
  54. }
  55. elseif (file_exists('sites/all/libraries/wvega-timepicker/jquery.timepicker.js')) {
  56. $path = 'sites/all/libraries/wvega-timepicker';
  57. }
  58. return $path;
  59. }
  60. /**
  61. * Get the name of the preferred default timepicker.
  62. *
  63. * If the wvega timepicker is available on the system, default to using that,
  64. * unless the administrator has specifically chosen otherwise.
  65. */
  66. function date_popup_get_preferred_timepicker() {
  67. $wvega_available = date_popup_get_wvega_path();
  68. return $wvega_available ? 'wvega' : 'default';
  69. }
  70. /**
  71. * Implements hook_library().
  72. */
  73. function date_popup_library() {
  74. $libraries = array();
  75. $path = drupal_get_path('module', 'date_popup');
  76. $libraries['timeentry'] = array(
  77. 'title' => 'Time Entry',
  78. 'website' => 'http://plugins.jquery.com/project/timeEntry',
  79. 'version' => '1.4.7',
  80. 'js' => array(
  81. $path . '/jquery.timeentry.pack.js' => array(),
  82. ),
  83. 'css' => array(
  84. $path . '/themes/jquery.timeentry.css' => array('preprocess' => FALSE),
  85. ),
  86. );
  87. return $libraries;
  88. }
  89. /**
  90. * Create a unique CSS id name and output a single inline JS block for
  91. * each startup function to call and settings array to pass it. This
  92. * used to create a unique CSS class for each unique combination of
  93. * function and settings, but using classes requires a DOM traversal
  94. * and is much slower than an id lookup. The new approach returns to
  95. * requiring a duplicate copy of the settings/code for every element
  96. * that uses them, but is much faster. We could combine the logic by
  97. * putting the ids for each unique function/settings combo into
  98. * Drupal.settings and searching for each listed id.
  99. *
  100. * @param $pfx
  101. * The CSS class prefix to search the DOM for.
  102. * TODO : unused ?
  103. * @param $func
  104. * The jQuery function to invoke on each DOM element containing the
  105. * returned CSS class.
  106. * @param $settings
  107. * The settings array to pass to the jQuery function.
  108. * @returns
  109. * The CSS id to assign to the element that should have
  110. * $func($settings) invoked on it.
  111. */
  112. function date_popup_js_settings_id($id, $func, $settings) {
  113. static $js_added = FALSE;
  114. static $id_count = array();
  115. // Make sure popup date selector grid is in correct year.
  116. if (!empty($settings['yearRange'])) {
  117. $parts = explode(':', $settings['yearRange']);
  118. // Set the default date to 0 or the lowest bound if the date ranges do not include the current year
  119. // Necessary for the datepicker to render and select dates correctly
  120. $defaultDate = ($parts[0] > 0 || 0 > $parts[1]) ? $parts[0] : 0;
  121. $settings += array('defaultDate' => (string) $defaultDate . 'y');
  122. }
  123. if (!$js_added) {
  124. drupal_add_js(drupal_get_path('module', 'date_popup') .'/date_popup.js');
  125. $js_added = TRUE;
  126. }
  127. // We use a static array to account for possible multiple form_builder()
  128. // calls in the same request (form instance on 'Preview').
  129. if (!isset($id_count[$id])) {
  130. $id_count[$id] = 0;
  131. }
  132. // It looks like we need the additional id_count for this to
  133. // work correctly when there are multiple values.
  134. // $return_id = "$id-$func-popup";
  135. $return_id = "$id-$func-popup-". $id_count[$id]++;
  136. $js_settings['datePopup'][$return_id] = array(
  137. 'func' => $func,
  138. 'settings' => $settings
  139. );
  140. drupal_add_js($js_settings, 'setting');
  141. return $return_id;
  142. }
  143. function date_popup_theme() {
  144. return array(
  145. 'date_popup' => array('render element' => 'element'),
  146. );
  147. }
  148. /**
  149. * Implements hook_element_info().
  150. *
  151. * Set the #type to date_popup and fill the element #default_value with
  152. * a date adjusted to the proper local timezone in datetime format (YYYY-MM-DD HH:MM:SS).
  153. *
  154. * The element will create two textfields, one for the date and one for the
  155. * time. The date textfield will include a jQuery popup calendar date picker,
  156. * and the time textfield uses a jQuery timepicker.
  157. *
  158. * NOTE - Converting a date stored in the database from UTC to the local zone
  159. * and converting it back to UTC before storing it is not handled by this
  160. * element and must be done in pre-form and post-form processing!!
  161. *
  162. * #date_timezone
  163. * The local timezone to be used to create this date.
  164. *
  165. * #date_format
  166. * Unlike earlier versions of this popup, most formats will work.
  167. *
  168. * #date_increment
  169. * Increment minutes and seconds by this amount, default is 1.
  170. *
  171. * #date_year_range
  172. * The number of years to go back and forward in a year selector,
  173. * default is -3:+3 (3 back and 3 forward).
  174. *
  175. * #datepicker_options
  176. * An associative array representing the jQuery datepicker options you want
  177. * to set for this element. Use the jQuery datepicker option names as keys.
  178. * Hard coded defaults are:
  179. * - changeMonth => TRUE
  180. * - changeYear => TRUE
  181. * - autoPopUp => 'focus'
  182. * - closeAtTop => FALSE
  183. * - speed => 'immediate'
  184. */
  185. function date_popup_element_info() {
  186. $timepicker = date_popup_get_preferred_timepicker();
  187. $type['date_popup'] = array(
  188. '#input' => TRUE,
  189. '#tree' => TRUE,
  190. '#date_timezone' => date_default_timezone(),
  191. '#date_flexible' => 0,
  192. '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
  193. '#datepicker_options' => array(),
  194. '#timepicker' => variable_get('date_popup_timepicker', $timepicker),
  195. '#date_increment' => 1,
  196. '#date_year_range' => '-3:+3',
  197. '#date_label_position' => 'above',
  198. '#process' => array('date_popup_element_process'),
  199. '#value_callback' => 'date_popup_element_value_callback',
  200. '#theme_wrappers' => array('date_popup'),
  201. );
  202. if (module_exists('ctools')) {
  203. $type['date_popup']['#pre_render'] = array('ctools_dependent_pre_render');
  204. }
  205. return $type;
  206. }
  207. function date_popup_date_granularity($element) {
  208. $granularity = date_format_order($element['#date_format']);
  209. return array_intersect($granularity, array('month', 'day', 'year'));
  210. }
  211. function date_popup_time_granularity($element) {
  212. $granularity = date_format_order($element['#date_format']);
  213. return array_intersect($granularity, array('hour', 'minute', 'second'));
  214. }
  215. function date_popup_date_format($element) {
  216. return (date_limit_format($element['#date_format'], date_popup_date_granularity($element)));
  217. }
  218. function date_popup_time_format($element) {
  219. return date_popup_format_to_popup_time(date_limit_format($element['#date_format'], date_popup_time_granularity($element)), $element['#timepicker']);
  220. }
  221. /**
  222. * Element value callback for date_popup element.
  223. */
  224. function date_popup_element_value_callback($element, $input = FALSE, &$form_state) {
  225. $granularity = date_format_order($element['#date_format']);
  226. $has_time = date_has_time($granularity);
  227. $date = NULL;
  228. $return = $has_time ? array('date' => '', 'time' => '') : array('date' => '');
  229. // Normal input from submitting the form element.
  230. // Check is_array() to skip the string input values created by Views pagers.
  231. // Those string values, if present, should be interpreted as empty input.
  232. if ($input !== FALSE && is_array($input)) {
  233. $return = $input;
  234. $date = date_popup_input_date($element, $input);
  235. }
  236. // No input? Try the default value.
  237. elseif (!empty($element['#default_value'])) {
  238. $date = date_default_date($element);
  239. }
  240. // Date with errors won't re-display.
  241. if (date_is_date($date)) {
  242. $return['date'] = !$date->timeOnly ? date_format_date($date, 'custom', date_popup_date_format($element)) : '';
  243. $return['time'] = $has_time ? date_format_date($date, 'custom', date_popup_time_format($element)) : '';
  244. }
  245. elseif (!empty($input)) {
  246. $return = $input;
  247. }
  248. return $return;
  249. }
  250. /**
  251. * Javascript popup element processing.
  252. * Add popup attributes to $element.
  253. */
  254. function date_popup_element_process($element, &$form_state, $form) {
  255. if (date_hidden_element($element)) {
  256. return $element;
  257. }
  258. date_popup_add();
  259. module_load_include('inc', 'date_api', 'date_api_elements');
  260. $element['#tree'] = TRUE;
  261. $element['#theme_wrappers'] = array('date_popup');
  262. if (!empty($element['#ajax'])) {
  263. $element['#ajax'] += array(
  264. 'trigger_as' => array('name' =>$element['#name']),
  265. 'event' => 'change',
  266. );
  267. }
  268. $element['date'] = date_popup_process_date_part($element);
  269. $element['time'] = date_popup_process_time_part($element);
  270. if (isset($element['#element_validate'])) {
  271. array_push($element['#element_validate'], 'date_popup_validate');
  272. }
  273. else {
  274. $element['#element_validate'] = array('date_popup_validate');
  275. }
  276. $context = array(
  277. 'form' => $form,
  278. );
  279. drupal_alter('date_popup_process', $element, $form_state, $context);
  280. return $element;
  281. }
  282. /**
  283. * Process the date portion of the element.
  284. */
  285. function date_popup_process_date_part(&$element) {
  286. $granularity = date_format_order($element['#date_format']);
  287. $date_granularity = date_popup_date_granularity($element);
  288. if (empty($date_granularity)) return array();
  289. // The datepicker can't handle zero or negative values like 0:+1
  290. // even though the Date API can handle them, so rework the value
  291. // we pass to the datepicker to use defaults it can accept (such as +0:+1)
  292. // date_range_string() adds the necessary +/- signs to the range string.
  293. $this_year = date_format(date_now(), 'Y');
  294. $date = '';
  295. if (!empty($element['#value']['date'])) {
  296. $date = new DateObject($element['#value']['date'], $element['#date_timezone'], date_popup_date_format($element));
  297. }
  298. $range = date_range_years($element['#date_year_range'], $date);
  299. $year_range = date_range_string($range);
  300. // Add the dynamic datepicker options. Allow element-specific datepicker
  301. // preferences to override these options for whatever reason they see fit.
  302. $settings = $element['#datepicker_options'] + array(
  303. 'changeMonth' => TRUE,
  304. 'changeYear' => TRUE,
  305. 'autoPopUp' => 'focus',
  306. 'closeAtTop' => FALSE,
  307. 'speed' => 'immediate',
  308. 'firstDay' => intval(variable_get('date_first_day', 0)),
  309. //'buttonImage' => base_path() . drupal_get_path('module', 'date_api') ."/images/calendar.png",
  310. //'buttonImageOnly' => TRUE,
  311. 'dateFormat' => date_popup_format_to_popup(date_popup_date_format($element), 'datepicker'),
  312. 'yearRange' => $year_range,
  313. // Custom setting, will be expanded in Drupal.behaviors.date_popup()
  314. 'fromTo' => isset($fromto),
  315. );
  316. // Create a unique id for each set of custom settings.
  317. $id = date_popup_js_settings_id($element['#id'], 'datepicker', $settings);
  318. // Manually build this element and set the value - this will prevent corrupting
  319. // the parent value
  320. $parents = array_merge($element['#parents'], array('date'));
  321. $sub_element = array(
  322. '#type' => 'textfield',
  323. '#title' => $element['#date_label_position'] == 'above' ? theme('date_part_label_date', array('part_type' => 'date', 'element' => $element)) : '',
  324. '#default_value' => $element['#value']['date'],
  325. '#id' => $id,
  326. '#input' => FALSE,
  327. '#size' => !empty($element['#size']) ? $element['#size'] : 20,
  328. '#maxlength' => !empty($element['#maxlength']) ? $element['#maxlength'] : 30,
  329. '#attributes' => $element['#attributes'],
  330. '#parents' => $parents,
  331. '#name' => array_shift($parents) . '['. implode('][', $parents) .']',
  332. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  333. );
  334. $sub_element['#value'] = $sub_element['#default_value'];
  335. // TODO, figure out exactly when we want this description. In many places it is not desired.
  336. $sub_element['#description'] = ' '. t('E.g., @date', array('@date' => date_format_date(date_example_date(), 'custom', date_popup_date_format($element))));
  337. return $sub_element;
  338. }
  339. /**
  340. * Process the time portion of the element.
  341. */
  342. function date_popup_process_time_part(&$element) {
  343. $granularity = date_format_order($element['#date_format']);
  344. $has_time = date_has_time($granularity);
  345. if (empty($has_time)) return array();
  346. switch ($element['#timepicker']) {
  347. case 'default':
  348. $func = 'timeEntry';
  349. $settings = array(
  350. 'show24Hours' => strpos($element['#date_format'], 'H') !== FALSE ? TRUE : FALSE,
  351. 'showSeconds' => (in_array('second', $granularity) ? TRUE : FALSE),
  352. 'timeSteps' => array(1, intval($element['#date_increment']), (in_array('second', $granularity) ? $element['#date_increment'] : 0)),
  353. 'spinnerImage' => '',
  354. 'fromTo' => isset($fromto),
  355. );
  356. if (strpos($element['#date_format'], 'a') !== FALSE) {
  357. // Then we are using lowercase am/pm.
  358. $settings['ampmNames'] = array('am', 'pm');
  359. }
  360. if (strpos($element['#date_format'], ' A') !== FALSE || strpos($element['#date_format'], ' a') !== FALSE) {
  361. $settings['ampmPrefix'] = ' ';
  362. }
  363. break;
  364. case 'wvega':
  365. $func = 'timepicker';
  366. $time_granularity = array_intersect($granularity, array('hour', 'minute', 'second'));
  367. $format = date_popup_format_to_popup_time(date_limit_format($element['#date_format'], $time_granularity), 'wvega');
  368. // The first value in the dropdown list should be the same as the element
  369. // default_value, but it needs to be in JS format (i.e. milliseconds since
  370. // the epoch).
  371. $start_time = new DateObject($element['#default_value'], $element['#date_timezone'], DATE_FORMAT_DATETIME);
  372. date_increment_round($start_time, $element['#date_increment']);
  373. $start_time = $start_time->format(DATE_FORMAT_UNIX) * 1000;
  374. $settings = array(
  375. 'timeFormat' => $format,
  376. 'interval' => $element['#date_increment'],
  377. 'startTime' => $start_time,
  378. 'scrollbar' => TRUE,
  379. );
  380. break;
  381. default:
  382. $func = '';
  383. $settings = array();
  384. break;
  385. }
  386. // Create a unique id for each set of custom settings.
  387. $id = date_popup_js_settings_id($element['#id'], $func, $settings);
  388. // Manually build this element and set the value - this will prevent corrupting
  389. // the parent value
  390. $parents = array_merge($element['#parents'], array('time'));
  391. $sub_element = array(
  392. '#type' => 'textfield',
  393. '#title' => $element['#date_label_position'] == 'above' ? theme('date_part_label_time', array('part_type' => 'time', 'element' => $element)) : '',
  394. '#default_value' => $element['#value']['time'],
  395. '#id' => $id,
  396. '#size' => 15,
  397. '#maxlength' => 10,
  398. '#attributes' => $element['#attributes'],
  399. '#parents' => $parents,
  400. '#name' => array_shift($parents) . '['. implode('][', $parents) .']',
  401. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  402. );
  403. $sub_element['#value'] = $sub_element['#default_value'];
  404. // TODO, figure out exactly when we want this description. In many places it is not desired.
  405. $example_date = date_now();
  406. date_increment_round($example_date, $element['#date_increment']);
  407. $sub_element['#description'] = t('E.g., @date', array('@date' => date_format_date($example_date, 'custom', date_popup_time_format($element))));
  408. return ($sub_element);
  409. }
  410. /**
  411. * Massage the input values back into a single date.
  412. *
  413. * When used as a Views widget, the validation step always gets triggered,
  414. * even with no form submission. Before form submission $element['#value']
  415. * contains a string, after submission it contains an array.
  416. *
  417. */
  418. function date_popup_validate($element, &$form_state) {
  419. if (date_hidden_element($element)) {
  420. return;
  421. }
  422. if (is_string($element['#value'])) {
  423. return;
  424. }
  425. module_load_include('inc', 'date_api', 'date_api_elements');
  426. date_popup_add();
  427. $input_exists = NULL;
  428. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  429. drupal_alter('date_popup_pre_validate', $element, $form_state, $input);
  430. $granularity = date_format_order($element['#date_format']);
  431. $date_granularity = date_popup_date_granularity($element);
  432. $time_granularity = date_popup_time_granularity($element);
  433. $has_time = date_has_time($granularity);
  434. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  435. $label = t($label);
  436. $date = date_popup_input_date($element, $input);
  437. // If the date has errors, display them.
  438. // If something was input but there is no date, the date is invalid.
  439. // If the field is empty and required, set error message and return.
  440. $error_field = implode('][', $element['#parents']);
  441. if (empty($date) || !empty($date->errors)) {
  442. if (is_object($date) && !empty($date->errors)) {
  443. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  444. $message .= '<br />' . implode('<br />', $date->errors);
  445. form_set_error($error_field, $message);
  446. return;
  447. }
  448. if (!empty($input['date'])) {
  449. $message = t('The value input for field %field is invalid.', array('%field' => $label));
  450. form_set_error($error_field, $message);
  451. return;
  452. }
  453. if ($element['#required']) {
  454. $message = t('A valid date is required for %title.', array('%title' => $label));
  455. form_set_error($error_field, $message);
  456. return;
  457. }
  458. }
  459. // If the created date is valid, set it.
  460. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  461. form_set_value($element, $value, $form_state);
  462. }
  463. /**
  464. * Helper function for extracting a date value out of user input.
  465. *
  466. * @param autocomplete
  467. * Should we add a time value to complete the date if there is no time?
  468. * Useful anytime the time value is optional.
  469. */
  470. function date_popup_input_date($element, $input, $auto_complete = FALSE) {
  471. if (empty($input) || !is_array($input) || !array_key_exists('date', $input) || empty($input['date'])) {
  472. return NULL;
  473. }
  474. date_popup_add();
  475. $granularity = date_format_order($element['#date_format']);
  476. $has_time = date_has_time($granularity);
  477. $flexible = !empty($element['#date_flexible']) ? $element['#date_flexible'] : 0;
  478. $format = date_popup_date_format($element);
  479. $format .= $has_time ? ' ' . date_popup_time_format($element) : '';
  480. $datetime = $input['date'];
  481. $datetime .= $has_time ? ' ' . $input['time'] : '';
  482. $date = new DateObject($datetime, $element['#date_timezone'], $format);
  483. if (is_object($date)) {
  484. $date->limitGranularity($granularity);
  485. if ($date->validGranularity($granularity, $flexible)) {
  486. date_increment_round($date, $element['#date_increment']);
  487. }
  488. return $date;
  489. }
  490. return NULL;
  491. }
  492. /**
  493. * Allowable time formats.
  494. */
  495. function date_popup_time_formats($with_seconds = FALSE) {
  496. return array(
  497. 'H:i:s',
  498. 'h:i:sA',
  499. );
  500. }
  501. /**
  502. * Format options array.
  503. *
  504. * TODO Remove any formats not supported by the widget, if any.
  505. */
  506. function date_popup_formats() {
  507. $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
  508. $formats = drupal_map_assoc($formats);
  509. return $formats;
  510. }
  511. /**
  512. * Recreate a date format string so it has the values popup expects.
  513. *
  514. * @param string $format
  515. * a normal date format string, like Y-m-d
  516. * @return string
  517. * A format string in popup format, like YMD-, for the
  518. * earlier 'calendar' version, or m/d/Y for the later 'datepicker'
  519. * version.
  520. */
  521. function date_popup_format_to_popup($format) {
  522. if (empty($format)) {
  523. $format = 'Y-m-d';
  524. }
  525. $replace = date_popup_datepicker_format_replacements();
  526. return strtr($format, $replace);
  527. }
  528. /**
  529. * Recreate a time format string so it has the values popup expects.
  530. *
  531. * @param string $format
  532. * a normal time format string, like h:i (a)
  533. * @return string
  534. * a format string that the popup can accept like h:i a
  535. */
  536. function date_popup_format_to_popup_time($format, $timepicker = NULL) {
  537. if (empty($format)) {
  538. $format = 'H:i';
  539. }
  540. $format = str_replace(array('/', '-', ' .', ',', 'F', 'M', 'l', 'z', 'w', 'W', 'd', 'j', 'm', 'n', 'y', 'Y'), '', $format);
  541. $format = strtr($format, date_popup_timepicker_format_replacements($timepicker));
  542. return $format;
  543. }
  544. /**
  545. * Reconstruct popup format string into normal format string.
  546. *
  547. * @param string $format
  548. * a string in popup format, like YMD-
  549. * @return string
  550. * a normal date format string, like Y-m-d
  551. */
  552. function date_popup_popup_to_format($format) {
  553. $replace = array_flip(date_popup_datepicker_format_replacements());
  554. return strtr($format, $replace);
  555. }
  556. /**
  557. * Return a map of format replacements required for a given timepicker.
  558. *
  559. * Client-side time entry plugins don't support all possible date formats.
  560. * This function returns a map of format replacements required to change any
  561. * input format into one that the given timepicker can support.
  562. *
  563. * @param $timepicker
  564. * The time entry plugin being used: either 'wvega' or 'default'.
  565. * @return
  566. * A map of replacements.
  567. */
  568. function date_popup_timepicker_format_replacements($timepicker = 'default') {
  569. switch ($timepicker) {
  570. case 'wvega':
  571. return array(
  572. 'a' => 'A', // The wvega timepicker only supports uppercase AM/PM.
  573. );
  574. default:
  575. return array(
  576. 'G' => 'H', // The default timeEntry plugin requires leading zeros.
  577. 'g' => 'h',
  578. );
  579. }
  580. }
  581. /**
  582. * The format replacement patterns for the new datepicker.
  583. */
  584. function date_popup_datepicker_format_replacements() {
  585. return array(
  586. 'd' => 'dd',
  587. 'j' => 'd',
  588. 'l' => 'DD',
  589. 'D' => 'D',
  590. 'm' => 'mm',
  591. 'n' => 'm',
  592. 'F' => 'MM',
  593. 'M' => 'M',
  594. 'Y' => 'yy',
  595. 'y' => 'y',
  596. );
  597. }
  598. /**
  599. * Format a date popup element.
  600. *
  601. * Use a class that will float date and time next to each other.
  602. */
  603. function theme_date_popup($vars) {
  604. $element = $vars['element'];
  605. $attributes = !empty($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array());
  606. $attributes['class'][] = 'container-inline-date';
  607. // If there is no description, the floating date elements need some extra padding below them.
  608. $wrapper_attributes = array('class' => array('date-padding'));
  609. if (empty($element['date']['#description'])) {
  610. $wrapper_attributes['class'][] = 'clearfix';
  611. }
  612. // Add an wrapper to mimic the way a single value field works, for ease in using #states.
  613. if (isset($element['#children'])) {
  614. $element['#children'] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($wrapper_attributes) .'>' . $element['#children'] . '</div>';
  615. }
  616. return '<div ' . drupal_attributes($attributes) .'>' . theme('form_element', $element) . '</div>';
  617. }
  618. /**
  619. * Implements hook_menu().
  620. */
  621. function date_popup_menu() {
  622. $items = array();
  623. // TODO Fix this later.
  624. $items['admin/config/date/date_popup'] = array(
  625. 'title' => 'Date Popup',
  626. 'description' => 'Configure the Date Popup settings.',
  627. 'page callback' => 'drupal_get_form',
  628. 'page arguments' => array('date_popup_settings'),
  629. 'access callback' => 'user_access',
  630. 'access arguments' => array('administer site configuration'),
  631. );
  632. return $items;
  633. }
  634. /**
  635. * General configuration form for controlling the Date Popup behaviour.
  636. */
  637. function date_popup_settings() {
  638. $wvega_available = date_popup_get_wvega_path();
  639. $preferred_timepicker = date_popup_get_preferred_timepicker();
  640. $form['#prefix'] = t('<p>The Date Popup module allows for manual time entry or use of a jQuery timepicker plugin. The Date module comes with a default jQuery timepicker which is already installed. The module also supports a dropdown timepicker that must be downloaded separately. The dropdown timepicker will not appear as an option until the code is available in the libraries folder. If you do not want to use a jQuery timepicker, you can choose the "Manual time entry" option below and users will get a regular textfield instead.</p>');
  641. $form['date_popup_timepicker'] = array(
  642. '#type' => 'select',
  643. '#options' => array(
  644. 'default' => t('Use default jQuery timepicker'),
  645. 'wvega' => t('Use dropdown timepicker'),
  646. 'none' => t('Manual time entry, no jQuery timepicker')
  647. ),
  648. '#title' => t('Timepicker'),
  649. '#default_value' => variable_get('date_popup_timepicker', $preferred_timepicker),
  650. );
  651. if (!$wvega_available) {
  652. $form['#prefix'] .= t('<p>To install the dropdown timepicker, create a <code>!directory</code> directory in your site installation. Then visit <a href="@download">@download</a>, download the latest copy and unzip it. You will see files with names like jquery.timepicker-1.1.2.js and jquery.timepicker-1.1.2.css. Rename them to jquery.timepicker.js and jquery.timepicker.css and copy them into <code>!directory</code>.</p>', array('!directory' => 'sites/all/libraries/wvega-timepicker', '@download' => 'https://github.com/wvega/timepicker/archives/master'));
  653. unset($form['date_popup_timepicker']['#options']['wvega']);
  654. }
  655. $css = <<<EOM
  656. /* ___________ IE6 IFRAME FIX ________ */
  657. .ui-datepicker-cover {
  658. display: none; /*sorry for IE5*/
  659. display/**/: block; /*sorry for IE5*/
  660. position: absolute; /*must have*/
  661. z-index: -1; /*must have*/
  662. filter: mask(); /*must have*/
  663. top: -4px; /*must have*/
  664. left: -4px; /*must have*/ /* LTR */
  665. width: 200px; /*must have*/
  666. height: 200px; /*must have*/
  667. }
  668. EOM;
  669. $form['#suffix'] = t('<p>The Date Popup calendar includes some css for IE6 that breaks css validation. Since IE 6 is now superceded by IE 7, 8, and 9, the special css for IE 6 has been removed from the regular css used by the Date Popup. If you find you need that css after all, you can add it back in your theme. Look at the way the Garland theme adds special IE-only css in in its page.tpl.php file. The css you need is:</p>') .'<blockquote><PRE>' . $css .'</PRE></blockquote>';
  670. return system_settings_form($form);
  671. }