You are here

datex_popup.module in Datex 7.2

Same filename and directory in other branches
  1. 7.3 datex_popup/datex_popup.module

File

datex_popup/datex_popup.module
View source
<?php

/**
 * The format replacement patterns for the new datepicker.
 */
function _datex_popup_datepicker_format_replacements() {
  return array(
    'd' => 'dd',
    'j' => 'd',
    'l' => 'DD',
    'D' => 'D',
    'm' => 'mm',
    'n' => 'm',
    'F' => 'MM',
    'M' => 'M',
    'Y' => 'yyyy',
    'y' => 'yy',
  );
}
function _datex_popup_picker_format($format) {
  $replace = _datex_popup_datepicker_format_replacements();
  return strtr($format, $replace);
}
function _datex_popup_php_format($format) {
  $replace = array_flip(_datex_popup_datepicker_format_replacements());
  return strtr($format, $replace);
}

/**
 * Helper function to extract schema and granularitis.
 */
function _datex_popup_get_element_info(array $element) {
  if (isset($element['#instance']['widget']['settings']['datex_schema'])) {
    $field_schema = $element['#instance']['widget']['settings']['datex_schema'];
  }
  else {
    $field_schema = 'default';
  }
  if (isset($element['#granularity'])) {
    $g = $element['#granularity'];
  }
  elseif (isset($element['#field']['settings']['granularity'])) {
    $g = $element['#field']['settings']['granularity'];
  }
  else {
    $g = array(
      'year',
      'month',
      'day',
      'minute',
      'hour',
      'second',
    );
  }
  return array(
    $field_schema,
    $g,
  );
}

// _______________________________________________________________________MAIN.

/**
 * Imlements hook_js_alter().
 *
 * Whenever date_popup.js is loaded, Remove it and inject datex library.
 */
function datex_popup_js_alter(&$js) {
  if (!libraries_load('jquery.calendars')) {
    return;
  }
  foreach (preg_grep('#(date_popup.js)#', array_keys($js)) as $component) {
    unset($js[$component]);
  }
  static $loaded = FALSE;
  if ($loaded) {
    return;
  }
  $path = drupal_get_path('module', 'datex_popup');
  drupal_add_js("{$path}/datex_popup.js");
  drupal_add_css("{$path}/datex_popup.css");

  // @TODO Make configurable.
  $path = libraries_get_path('jquery.calendars') . '/';
  $name = 'smoothness.calendars.picker.css';
  drupal_add_css($path . $name);
  $loaded = TRUE;
}

/**
 * Implements hook_date_popup_process_alter().
 */
function datex_popup_date_popup_process_alter(&$element, &$form_state, $context) {
  $schema = variable_get('datex_schema');
  $langcode = $GLOBALS['language']->language;
  list($field_schema, $g) = _datex_popup_get_element_info($element);
  if (!($calendar = _datex_field_calendar($g, $field_schema))) {
    $calendar = new DateTime();
  }
  static $global_settings_not_added = TRUE;
  if ($global_settings_not_added) {
    $settings['datex']['global']['langcode'] = $GLOBALS['language']->language;
    $settings['datex']['global']['direction'] = $GLOBALS['language']->direction;
    $global_settings_not_added = FALSE;
  }
  switch (_datex_get_calendar_name_from_name($field_schema)) {
    case 'jalali':
      _datex_api_add_l10n('persian', $langcode == 'fa' ? 'fa' : NULL);
      break;
    default:
      break;
  }
  $range = explode(':', $element['#date_year_range']);
  $y = $calendar
    ->format('Y');
  $timestamp = strtotime(empty($element['#default_value']) ? 'now' : $element['#default_value']);
  $limited_format = date_limit_format($element['#date_format'], date_popup_date_granularity($element));
  $calendar
    ->setTimestamp($timestamp);
  if (!empty($element['date']['#default_value'])) {
    $date = $calendar
      ->format($limited_format);
    if (!empty($element['date']['#value']) && $element['date']['#value'] == $element['date']['#default_value']) {
      $element['date']['#value'] = $date;
    }
    $element['date']['#default_value'] = $date;
  }
  $name = 'datex_' . str_replace('-', '_', $element['#id']);
  $settings['datex']['fields'][$element['#id']] = array(
    'calendar' => isset($schema[$field_schema][$langcode]) ? $schema[$field_schema][$langcode] : 'default',
    'defaultValue' => $timestamp,
    'yearRange' => $y + $range[0] . ':' . ($y + $range[1]),
    'format' => _datex_popup_picker_format($limited_format),
    'defaultYear' => $calendar
      ->format('Y'),
    'defaultMonth' => $calendar
      ->format('m'),
    'defaultDay' => $calendar
      ->format('j'),
    'altField' => $element['#id'] . '-' . str_replace('_', '-', $name),
    'altFormat' => 'yyyy-mm-dd',
  );
  drupal_add_js($settings, 'setting');

  // So we can easily track input time by user without depending on the format of field.
  if (!isset($element[$name])) {
    $element[$name] = array(
      '#type' => 'textfield',
      '#default_value' => $calendar
        ->format('Y-m-d'),
      '#prefix' => '<div class="datex_hidden_element">',
      '#suffix' => '</div>',
    );
  }
}

/**
 * Form element validation callback.
 *
 * Converts a Jalali date to Gregorian date just before actual validation of
 * date element happens.
 */
function _datex_popup_element_validate_callback(&$element, &$form_state) {

  // If there are already error on form let them be fixed first.
  if (empty($element['date']['#value']) || form_get_errors()) {
    return;
  }
  list($schema, $g) = _datex_popup_get_element_info($element);
  $tz = $element['#date_timezone'];
  if (!($calendar = _datex_field_calendar($g, $schema, $tz))) {
    return;
  }
  $datex = $element['datex_' . str_replace('-', '_', $element['#id'])];
  $values = explode('-', $datex['#value']);
  if (count($values) == 3) {
    $calendar
      ->setDate($values[0], $values[1], $values[2]);
    $format = date_limit_format($element['#date_format'], date_popup_date_granularity($element));
    $element['date']['#value'] = $calendar
      ->xformat(_datex_popup_php_format($format));
  }
  form_set_value($element['date'], $element['date']['#value'], $form_state);
  _datex_form_set_input($element['date'], $element['date']['#value'], $form_state);
}

Functions

Namesort descending Description
datex_popup_date_popup_process_alter Implements hook_date_popup_process_alter().
datex_popup_js_alter Imlements hook_js_alter().
_datex_popup_datepicker_format_replacements The format replacement patterns for the new datepicker.
_datex_popup_element_validate_callback Form element validation callback.
_datex_popup_get_element_info Helper function to extract schema and granularitis.
_datex_popup_php_format
_datex_popup_picker_format