date_popup.module 30 KB

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