date_popup.module 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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(),
  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' => theme('date_part_label_date', array('part_type' => 'date', 'element' => $element)),
  324. '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
  325. '#default_value' => $element['#value']['date'],
  326. '#id' => $id,
  327. '#input' => FALSE,
  328. '#size' => !empty($element['#size']) ? $element['#size'] : 20,
  329. '#maxlength' => !empty($element['#maxlength']) ? $element['#maxlength'] : 30,
  330. '#attributes' => $element['#attributes'],
  331. '#parents' => $parents,
  332. '#name' => array_shift($parents) . '['. implode('][', $parents) .']',
  333. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  334. );
  335. $sub_element['#value'] = $sub_element['#default_value'];
  336. // TODO, figure out exactly when we want this description. In many places it is not desired.
  337. $sub_element['#description'] = ' '. t('E.g., @date', array('@date' => date_format_date(date_example_date(), 'custom', date_popup_date_format($element))));
  338. return $sub_element;
  339. }
  340. /**
  341. * Process the time portion of the element.
  342. */
  343. function date_popup_process_time_part(&$element) {
  344. $granularity = date_format_order($element['#date_format']);
  345. $has_time = date_has_time($granularity);
  346. if (empty($has_time)) return array();
  347. switch ($element['#timepicker']) {
  348. case 'default':
  349. $func = 'timeEntry';
  350. $settings = array(
  351. 'show24Hours' => strpos($element['#date_format'], 'H') !== FALSE ? TRUE : FALSE,
  352. 'showSeconds' => (in_array('second', $granularity) ? TRUE : FALSE),
  353. 'timeSteps' => array(1, intval($element['#date_increment']), (in_array('second', $granularity) ? $element['#date_increment'] : 0)),
  354. 'spinnerImage' => '',
  355. 'fromTo' => isset($fromto),
  356. );
  357. if (strpos($element['#date_format'], 'a') !== FALSE) {
  358. // Then we are using lowercase am/pm.
  359. $settings['ampmNames'] = array('am', 'pm');
  360. }
  361. if (strpos($element['#date_format'], ' A') !== FALSE || strpos($element['#date_format'], ' a') !== FALSE) {
  362. $settings['ampmPrefix'] = ' ';
  363. }
  364. break;
  365. case 'wvega':
  366. $func = 'timepicker';
  367. $time_granularity = array_intersect($granularity, array('hour', 'minute', 'second'));
  368. $format = date_popup_format_to_popup_time(date_limit_format($element['#date_format'], $time_granularity), 'wvega');
  369. // The first value in the dropdown list should be the same as the element
  370. // default_value, but it needs to be in JS format (i.e. milliseconds since
  371. // the epoch).
  372. $start_time = new DateObject($element['#default_value'], $element['#date_timezone'], DATE_FORMAT_DATETIME);
  373. date_increment_round($start_time, $element['#date_increment']);
  374. $start_time = $start_time->format(DATE_FORMAT_UNIX) * 1000;
  375. $settings = array(
  376. 'timeFormat' => $format,
  377. 'interval' => $element['#date_increment'],
  378. 'startTime' => $start_time,
  379. 'scrollbar' => TRUE,
  380. );
  381. break;
  382. default:
  383. $func = '';
  384. $settings = array();
  385. break;
  386. }
  387. // Create a unique id for each set of custom settings.
  388. $id = date_popup_js_settings_id($element['#id'], $func, $settings);
  389. // Manually build this element and set the value - this will prevent corrupting
  390. // the parent value
  391. $parents = array_merge($element['#parents'], array('time'));
  392. $sub_element = array(
  393. '#type' => 'textfield',
  394. '#title' => theme('date_part_label_time', array('part_type' => 'time', 'element' => $element)),
  395. '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
  396. '#default_value' => $element['#value']['time'],
  397. '#id' => $id,
  398. '#size' => 15,
  399. '#maxlength' => 10,
  400. '#attributes' => $element['#attributes'],
  401. '#parents' => $parents,
  402. '#name' => array_shift($parents) . '['. implode('][', $parents) .']',
  403. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  404. );
  405. $sub_element['#value'] = $sub_element['#default_value'];
  406. // TODO, figure out exactly when we want this description. In many places it is not desired.
  407. $example_date = date_now();
  408. date_increment_round($example_date, $element['#date_increment']);
  409. $sub_element['#description'] = t('E.g., @date', array('@date' => date_format_date($example_date, 'custom', date_popup_time_format($element))));
  410. return ($sub_element);
  411. }
  412. /**
  413. * Massage the input values back into a single date.
  414. *
  415. * When used as a Views widget, the validation step always gets triggered,
  416. * even with no form submission. Before form submission $element['#value']
  417. * contains a string, after submission it contains an array.
  418. *
  419. */
  420. function date_popup_validate($element, &$form_state) {
  421. if (date_hidden_element($element)) {
  422. return;
  423. }
  424. if (is_string($element['#value'])) {
  425. return;
  426. }
  427. module_load_include('inc', 'date_api', 'date_api_elements');
  428. date_popup_add();
  429. $input_exists = NULL;
  430. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  431. drupal_alter('date_popup_pre_validate', $element, $form_state, $input);
  432. $granularity = date_format_order($element['#date_format']);
  433. $date_granularity = date_popup_date_granularity($element);
  434. $time_granularity = date_popup_time_granularity($element);
  435. $has_time = date_has_time($granularity);
  436. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  437. $label = t($label);
  438. $date = date_popup_input_date($element, $input);
  439. // If the date has errors, display them.
  440. // If something was input but there is no date, the date is invalid.
  441. // If the field is empty and required, set error message and return.
  442. $error_field = implode('][', $element['#parents']);
  443. if (empty($date) || !empty($date->errors)) {
  444. if (is_object($date) && !empty($date->errors)) {
  445. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  446. $message .= '<br />' . implode('<br />', $date->errors);
  447. form_set_error($error_field, $message);
  448. return;
  449. }
  450. if (!empty($input['date'])) {
  451. $message = t('The value input for field %field is invalid.', array('%field' => $label));
  452. form_set_error($error_field, $message);
  453. return;
  454. }
  455. if ($element['#required']) {
  456. $message = t('A valid date is required for %title.', array('%title' => $label));
  457. form_set_error($error_field, $message);
  458. return;
  459. }
  460. }
  461. // If the created date is valid, set it.
  462. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  463. form_set_value($element, $value, $form_state);
  464. }
  465. /**
  466. * Helper function for extracting a date value out of user input.
  467. *
  468. * @param autocomplete
  469. * Should we add a time value to complete the date if there is no time?
  470. * Useful anytime the time value is optional.
  471. */
  472. function date_popup_input_date($element, $input, $auto_complete = FALSE) {
  473. if (empty($input) || !is_array($input) || !array_key_exists('date', $input) || empty($input['date'])) {
  474. return NULL;
  475. }
  476. date_popup_add();
  477. $granularity = date_format_order($element['#date_format']);
  478. $has_time = date_has_time($granularity);
  479. $flexible = !empty($element['#date_flexible']) ? $element['#date_flexible'] : 0;
  480. $format = date_popup_date_format($element);
  481. $format .= $has_time ? ' ' . date_popup_time_format($element) : '';
  482. $datetime = $input['date'];
  483. $datetime .= $has_time ? ' ' . $input['time'] : '';
  484. $date = new DateObject($datetime, $element['#date_timezone'], $format);
  485. if (is_object($date)) {
  486. $date->limitGranularity($granularity);
  487. if ($date->validGranularity($granularity, $flexible)) {
  488. date_increment_round($date, $element['#date_increment']);
  489. }
  490. return $date;
  491. }
  492. return NULL;
  493. }
  494. /**
  495. * Allowable time formats.
  496. */
  497. function date_popup_time_formats($with_seconds = FALSE) {
  498. return array(
  499. 'H:i:s',
  500. 'h:i:sA',
  501. );
  502. }
  503. /**
  504. * Format options array.
  505. *
  506. * TODO Remove any formats not supported by the widget, if any.
  507. */
  508. function date_popup_formats() {
  509. $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
  510. $formats = drupal_map_assoc($formats);
  511. return $formats;
  512. }
  513. /**
  514. * Recreate a date format string so it has the values popup expects.
  515. *
  516. * @param string $format
  517. * a normal date format string, like Y-m-d
  518. * @return string
  519. * A format string in popup format, like YMD-, for the
  520. * earlier 'calendar' version, or m/d/Y for the later 'datepicker'
  521. * version.
  522. */
  523. function date_popup_format_to_popup($format) {
  524. if (empty($format)) {
  525. $format = 'Y-m-d';
  526. }
  527. $replace = date_popup_datepicker_format_replacements();
  528. return strtr($format, $replace);
  529. }
  530. /**
  531. * Recreate a time format string so it has the values popup expects.
  532. *
  533. * @param string $format
  534. * a normal time format string, like h:i (a)
  535. * @return string
  536. * a format string that the popup can accept like h:i a
  537. */
  538. function date_popup_format_to_popup_time($format, $timepicker = NULL) {
  539. if (empty($format)) {
  540. $format = 'H:i';
  541. }
  542. $format = str_replace(array('/', '-', ' .', ',', 'F', 'M', 'l', 'z', 'w', 'W', 'd', 'j', 'm', 'n', 'y', 'Y'), '', $format);
  543. $format = strtr($format, date_popup_timepicker_format_replacements($timepicker));
  544. return $format;
  545. }
  546. /**
  547. * Reconstruct popup format string into normal format string.
  548. *
  549. * @param string $format
  550. * a string in popup format, like YMD-
  551. * @return string
  552. * a normal date format string, like Y-m-d
  553. */
  554. function date_popup_popup_to_format($format) {
  555. $replace = array_flip(date_popup_datepicker_format_replacements());
  556. return strtr($format, $replace);
  557. }
  558. /**
  559. * Return a map of format replacements required for a given timepicker.
  560. *
  561. * Client-side time entry plugins don't support all possible date formats.
  562. * This function returns a map of format replacements required to change any
  563. * input format into one that the given timepicker can support.
  564. *
  565. * @param $timepicker
  566. * The time entry plugin being used: either 'wvega' or 'default'.
  567. * @return
  568. * A map of replacements.
  569. */
  570. function date_popup_timepicker_format_replacements($timepicker = 'default') {
  571. switch ($timepicker) {
  572. case 'wvega':
  573. return array(
  574. 'a' => 'A', // The wvega timepicker only supports uppercase AM/PM.
  575. );
  576. default:
  577. return array(
  578. 'G' => 'H', // The default timeEntry plugin requires leading zeros.
  579. 'g' => 'h',
  580. );
  581. }
  582. }
  583. /**
  584. * The format replacement patterns for the new datepicker.
  585. */
  586. function date_popup_datepicker_format_replacements() {
  587. return array(
  588. 'd' => 'dd',
  589. 'j' => 'd',
  590. 'l' => 'DD',
  591. 'D' => 'D',
  592. 'm' => 'mm',
  593. 'n' => 'm',
  594. 'F' => 'MM',
  595. 'M' => 'M',
  596. 'Y' => 'yy',
  597. 'y' => 'y',
  598. );
  599. }
  600. /**
  601. * Format a date popup element.
  602. *
  603. * Use a class that will float date and time next to each other.
  604. */
  605. function theme_date_popup($vars) {
  606. $element = $vars['element'];
  607. $attributes = !empty($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array());
  608. $attributes['class'][] = 'container-inline-date';
  609. // If there is no description, the floating date elements need some extra padding below them.
  610. $wrapper_attributes = array('class' => array('date-padding'));
  611. if (empty($element['date']['#description'])) {
  612. $wrapper_attributes['class'][] = 'clearfix';
  613. }
  614. // Add an wrapper to mimic the way a single value field works, for ease in using #states.
  615. if (isset($element['#children'])) {
  616. $element['#children'] = '<div id="' . $element['#id'] . '" ' . drupal_attributes($wrapper_attributes) .'>' . $element['#children'] . '</div>';
  617. }
  618. return '<div ' . drupal_attributes($attributes) .'>' . theme('form_element', $element) . '</div>';
  619. }
  620. /**
  621. * Implements hook_menu().
  622. */
  623. function date_popup_menu() {
  624. $items = array();
  625. // TODO Fix this later.
  626. $items['admin/config/date/date_popup'] = array(
  627. 'title' => 'Date Popup',
  628. 'description' => 'Configure the Date Popup settings.',
  629. 'page callback' => 'drupal_get_form',
  630. 'page arguments' => array('date_popup_settings'),
  631. 'access callback' => 'user_access',
  632. 'access arguments' => array('administer site configuration'),
  633. );
  634. return $items;
  635. }
  636. /**
  637. * General configuration form for controlling the Date Popup behaviour.
  638. */
  639. function date_popup_settings() {
  640. $wvega_available = date_popup_get_wvega_path();
  641. $preferred_timepicker = date_popup_get_preferred_timepicker();
  642. $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>');
  643. $form['date_popup_timepicker'] = array(
  644. '#type' => 'select',
  645. '#options' => array(
  646. 'default' => t('Use default jQuery timepicker'),
  647. 'wvega' => t('Use dropdown timepicker'),
  648. 'none' => t('Manual time entry, no jQuery timepicker')
  649. ),
  650. '#title' => t('Timepicker'),
  651. '#default_value' => variable_get('date_popup_timepicker', $preferred_timepicker),
  652. );
  653. if (!$wvega_available) {
  654. $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'));
  655. unset($form['date_popup_timepicker']['#options']['wvega']);
  656. }
  657. $css = <<<EOM
  658. /* ___________ IE6 IFRAME FIX ________ */
  659. .ui-datepicker-cover {
  660. display: none; /*sorry for IE5*/
  661. display/**/: block; /*sorry for IE5*/
  662. position: absolute; /*must have*/
  663. z-index: -1; /*must have*/
  664. filter: mask(); /*must have*/
  665. top: -4px; /*must have*/
  666. left: -4px; /*must have*/ /* LTR */
  667. width: 200px; /*must have*/
  668. height: 200px; /*must have*/
  669. }
  670. EOM;
  671. $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>';
  672. return system_settings_form($form);
  673. }