datex_date.module in Datex 7
Provides Jalali support for date module.
File
datex_date/datex_date.moduleView source
<?php
/**
* @file
* Provides Jalali support for date module.
*/
/**
* Implements hook_help().
*/
function datex_date_help($path, $arg) {
if ($path == 'admin/help#datex_date') {
$output = '<p>';
$output .= t('By enabling Datex Date, Gregorian dates generates by');
$output .= t('<em> Date module </em>');
$output .= t('will be displayed in Jalali format.');
$output .= '<br/>';
$output .= t('When storing date fields, they will be converted back to Gregorian format');
$output .= t('to prevent problems caused by databases not supporting dates other than Gregorian,');
$output .= '</p><p>';
$output .= t('Please be noted that Datex Date will not change display of dates when UI language is not Persian.');
$output .= '</p>';
return $output;
}
}
/**
* Implemetns hook_date_formatter_dates_alter().
*
* Converts gregorian date to Jalali date before display (such as node view).
*/
function datex_date_date_formatter_dates_alter(&$dates, $context) {
if (_datex_date_skip_field($context)) {
return;
}
foreach ($dates as $name => &$date) {
$dates[$name]['formatted'] = DatexFormatter::format(strtotime($date['local']['datetime']), $context['format'], DatexFormatter::getTzObject($date['local']['timezone']), _datex_intl());
}
}
/**
* Implements hook date_select_process_alter().
*
* Converts Gregorian date to Jalali date before displaying it in Form API.
* (such as node edit form).
*/
function datex_date_date_select_process_alter(&$element, &$form_state, $context) {
if (_datex_skip_language()) {
return;
}
$persian_date_names = DatexFormatter::persianDateNames();
$y =& $element['year']['#default_value'];
$m =& $element['month']['#default_value'];
$d =& $element['day']['#default_value'];
$default = DatexFormatter::toJalali($y, $m, $d);
if (!empty($y)) {
$y = $default['year'];
}
if (!empty($m)) {
$m = $default['month'];
}
if (!empty($d)) {
$d = $default['day'];
}
// Generating Year Option (if field has year granularity):
if (in_array('year', $element['#granularity'])) {
list($year_before, $year_after) = explode(':', $element['#date_year_range']);
$element['year']['#options'] = array();
// If year field is not required, add an empty value as drupal does.
if (!$element['#required']) {
$element['year']['#options'][''] = '';
}
// Now add actual year options.
$year_now = DatexObjectUtils::getYear();
for ($year = $year_now + intval($year_before); $year <= $year_now + intval($year_after); $year++) {
$element['year']['#options'][$year] = $year;
}
}
// Generating month Option (if field has month granularity).
if (in_array('month', $element['#granularity'])) {
$element['month']['#options'] = array();
// If field is not required, add an empty value as drupal does.
if (!$element['#required']) {
$element['month']['#options'][''] = '';
}
// Now add month options.
for ($i = 1; $i < 13; $i++) {
$element['month']['#options'][$i] = $persian_date_names['months'][$i];
}
}
}
/**
* Adds datex as a date form element validator.
*
* So we get a chance to convert Jalali to Greg.
*/
function datex_date_element_info_alter(&$elements) {
if (isset($elements['date_select'])) {
if (isset($elements['date_select']['#element_validate'])) {
$validator = $elements['date_select']['#element_validate'];
}
else {
$validator = array();
}
$validator = array_merge(array(
'_datex_date_element_validate_callback',
), $validator);
$elements['date_select']['#element_validate'] = $validator;
}
}
/**
* Form element validation callback.
*
* Converts a Jalali date to Gregorian date just before actual validation of
* date element happens.
*/
function _datex_date_element_validate_callback(&$element, &$form_state) {
if (_datex_skip_language()) {
return;
}
$y = $element['year']['#value'];
$m = $element['month']['#value'];
$d = $element['day']['#value'];
$dates = DatexFormatter::toGregorian($y, $m, $d);
foreach ($dates as $granularity => $value) {
if (!$element[$granularity]['#value']) {
$dates[$granularity] = 0;
}
}
foreach (array(
'hour',
'minute',
'second',
) as $granularity) {
$dates[$granularity] = $element[$granularity]['#value'];
}
drupal_array_set_nested_value($form_state['values'], $element['#parents'], $dates);
drupal_array_set_nested_value($form_state['input'], $element['#parents'], $dates);
}
/**
* Implements hook_date_field_instance_settings_form_alter().
*
* For per-field control.
*/
function datex_date_date_field_instance_settings_form_alter(&$form, $context) {
$settings = $context['instance']['settings'];
$default = isset($settings['datex_enabled']) ? $settings['datex_enabled'] : 'site';
$form['datex_enabled'] = array(
'#title' => t('Enable datex on this field:'),
'#type' => 'select',
'#options' => array(
'enabled' => t('Force enable, Ignoring site-wide datex config'),
'disabled' => t('Force disable, Ignoring site-wide datex config'),
'site' => t('Follow site-wide configuration'),
),
'#default_value' => $default,
);
}
/**
* Helper function to check if a date field should pass through datex or not.
*/
function _datex_date_skip_field($context) {
$datex = $context['instance']['settings'];
$datex = isset($datex['datex_enabled']) ? $datex['datex_enabled'] : 'site';
if ($datex == 'disabled' || $datex == 'site' && _datex_skip_language()) {
// Field must be skipped;
return TRUE;
}
// Field must NOT be skipped;
return FALSE;
}
/**
* Implements hook_field_info_alter().
*/
function datex_date_field_info_alter(&$info) {
foreach (array(
'datetime',
'date',
'datestamp',
) as $type) {
if (isset($info[$type]['instance_settings'])) {
$info[$type]['instance_settings']['datex_enabled'] = 'site';
}
}
}
Functions
Name | Description |
---|---|
datex_date_date_field_instance_settings_form_alter | Implements hook_date_field_instance_settings_form_alter(). |
datex_date_date_formatter_dates_alter | Implemetns hook_date_formatter_dates_alter(). |
datex_date_date_select_process_alter | Implements hook date_select_process_alter(). |
datex_date_element_info_alter | Adds datex as a date form element validator. |
datex_date_field_info_alter | Implements hook_field_info_alter(). |
datex_date_help | Implements hook_help(). |
_datex_date_element_validate_callback | Form element validation callback. |
_datex_date_skip_field | Helper function to check if a date field should pass through datex or not. |