hijri.module in Hijri 7
Same filename and directory in other branches
This module convert to Hijri date in nodes,comments and a block.
File
hijri.moduleView source
<?php
/**
* @file
* This module convert to Hijri date in nodes,comments and a block.
* */
/**
* Implements hook_help().
*/
function hijri_help($path, $arg) {
switch ($path) {
case 'admin/config/regional/date-time/hijri':
return '<p>' . t('Hijri module helps Islamic community to use the Islamic date') . '</p>';
}
}
/**
* Implements hook_block_info().
*/
function hijri_block_info() {
// This example comes from node.module.
$blocks['hijri'] = array(
'info' => t('Hijri Date Block'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function hijri_block_view($delta = '') {
// This example is adapted from node.module.
$block = array();
switch ($delta) {
case 'hijri':
$block['subject'] = t('Hijri Date');
$block['content'] = _hijri_date_block();
break;
}
return $block;
}
/**
* The callback of Hijri date block to display the type of date selected.
*/
function _hijri_date_block() {
$correction = variable_get('hijri_correction_value', 0);
$hijri_value = variable_get('hijri_display_block', 'full');
$format = hijri_format_date(time(), $hijri_value, NULL, $correction);
$content = theme('hijri_date', array(
'data' => $format,
));
return $content;
}
/**
* Implements hook_theme().
*/
function hijri_theme($existing, $type, $theme, $path) {
$item = array();
$item = array(
'hijri_date' => array(
'variables' => array(
'data' => NULL,
),
'template' => 'hijri-date',
'path' => drupal_get_path('module', 'hijri') . '/theme',
),
);
return $item;
}
/**
* Implements hook_init().
*/
function hijri_init() {
if (variable_get('hijri_datetype_value') == 'hijir_solar') {
variable_set('hijri_correction_value', 0);
}
}
/**
* Form builder.
*/
function hijri_settings_form($form, &$form_state) {
$correction = variable_get('hijri_correction_value', 0);
$hijri_types = array(
'full' => t('Hijri full format: @date', array(
'@date' => t('@hijri on @gregorian', array(
'@hijri' => hijri('l j F Y', time(), $correction),
'@gregorian' => format_date(time(), 'custom', 'F j, Y'),
)),
)),
'long' => t('Hijri long format: @date', array(
'@date' => hijri_format_date(time(), 'long', NULL, $correction),
)),
'medium' => t('Hijri medium format: @date', array(
'@date' => hijri_format_date(time(), 'medium', NULL, $correction),
)),
'short' => t('Hijri short format: @date', array(
'@date' => hijri_format_date(time(), 'short', NULL, $correction),
)),
);
// Content types settings.
$form['hijri_settings'] = array(
'#type' => 'vertical_tabs',
);
$form['hijri_settings']['hijridatetype'] = array(
'#type' => 'fieldset',
'#title' => t('Hijri Type'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['hijridatetype']['hijri_datetype_value'] = array(
'#type' => 'radios',
'#title' => t('Hijri Date Type'),
'#options' => array(
'hijir_moon' => t('Islamic Hijir'),
'hijir_solar' => t('Solar Hijri (Shamsi Hijri)'),
),
'#default_value' => variable_get('hijri_datetype_value', 'hijir_moon'),
);
$form['hijri_settings']['correction'] = array(
'#type' => 'fieldset',
'#title' => t('Hijri Correction'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['correction']['hijri_correction_value'] = array(
'#type' => 'select',
'#title' => t('Correction days'),
'#options' => array(
-2 => -2,
-1 => -1,
0 => 0,
+1 => +1,
+2 => +2,
),
'#default_value' => variable_get('hijri_correction_value', 0),
);
// Per-path visibility.
$form['hijri_settings']['node_type'] = array(
'#type' => 'fieldset',
'#title' => t('Content types'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['node_type']['hijri_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Hijri date Correction field for specific content types'),
'#default_value' => variable_get('hijri_types', array()),
'#options' => node_type_get_names(),
'#description' => t('Add/Remove the Correction field for content type.'),
);
$form['hijri_settings']['node_display'] = array(
'#type' => 'fieldset',
'#title' => t('Node Display'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['node_display']['hijri_display'] = array(
'#type' => 'radios',
'#title' => t('Hijri date on the node view'),
'#default_value' => variable_get('hijri_display', 'full'),
'#options' => $hijri_types,
'#description' => t('Select the display type you want to be in the node view page'),
);
$form['hijri_settings']['hijri_comment_display'] = array(
'#type' => 'fieldset',
'#title' => t('Comment Display'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['hijri_comment_display']['hijri_comment_display'] = array(
'#type' => 'radios',
'#title' => t('Hijri date on the comment area'),
'#default_value' => variable_get('hijri_comment_display', 'full'),
'#options' => $hijri_types,
'#description' => t('Select the display type you want to be in the comment area'),
);
$form['hijri_settings']['block_display'] = array(
'#type' => 'fieldset',
'#title' => t('Block Display'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'hijri_settings',
'#weight' => 0,
);
$form['hijri_settings']['block_display']['hijri_display_block'] = array(
'#type' => 'radios',
'#title' => t('Hijri date on Hijri block'),
'#default_value' => variable_get('hijri_display_block', 'full'),
'#options' => $hijri_types,
'#description' => t('Select the display type you want in Hijri block'),
);
$form['#submit'][] = 'hijri_settings_form_submit';
return system_settings_form($form);
}
/**
* Form submit handler.
*/
function hijri_settings_form_submit($form, &$form_state) {
// Check if our field is not already created.
if (!field_info_field('field_hijri_correction')) {
// Create the field base.
$field = array(
'field_name' => 'field_hijri_correction',
'type' => 'number_integer',
);
field_create_field($field);
}
foreach ($form_state['values']['hijri_types'] as $key => $type) {
// Create the instance.
$instance = array(
'field_name' => 'field_hijri_correction',
'entity_type' => 'node',
'bundle' => $key,
'label' => t('Hijri Date Correction'),
'description' => t('This field will save the Correction and different between months'),
'required' => FALSE,
);
if ((string) $type == (string) $key) {
$prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
if (empty($prior_instance)) {
field_create_instance($instance);
}
}
else {
$prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
if (!empty($prior_instance)) {
field_delete_instance($instance, FALSE);
}
}
}
}
/**
* Implements hook_node_presave().
*/
function hijri_node_presave($node) {
$node_types = variable_get('hijri_types', array());
foreach ($node_types as $type) {
if ($node->type == $type) {
$node->field_hijri_correction[$node->language][0]['value'] = variable_get('hijri_correction_value', 0);
}
}
}
/**
* Implements hook_menu().
*/
function hijri_menu() {
$items = array();
$items['admin/config/regional/date-time/hijri'] = array(
'title' => 'Hijri Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hijri_settings_form',
),
'access arguments' => array(
'administer hijri',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_views_api().
*/
function hijri_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'hijri') . '/views',
);
}
/**
* Processes variables for node.tpl.php.
*
* The $variables array contains the following arguments:
* - $node
* - $view_mode
* - $page.
*
* @see node.tpl.php
*/
function hijri_preprocess_node(&$variables) {
$node = $variables['node'];
$node_types = variable_get('hijri_types', array());
if (isset($node_types[$node->type]) && (string) $node_types[$node->type] == $node->type) {
$correction = isset($node->field_hijri_correction[$node->language][0]['value']) ? $node->field_hijri_correction[$node->language][0]['value'] : 0;
switch (variable_get('hijri_display')) {
case 'full':
case 'long':
case 'medium':
case 'short':
$format = hijri_format_date($node->created, variable_get('hijri_display'), NULL, $correction);
break;
default:
$format = $variables['date'];
break;
}
// Display post information only on certain node types.
if (variable_get('node_submitted_' . $node->type, TRUE)) {
$variables['display_submitted'] = TRUE;
$variables['submitted'] = t('Submitted by !username on !datetime', array(
'!username' => $variables['name'],
'!datetime' => $format,
));
}
else {
$variables['display_submitted'] = FALSE;
$variables['submitted'] = '';
}
}
}
/**
* Process variables for comment.tpl.php.
*
* @see comment.tpl.php
*/
function hijri_preprocess_comment(&$variables) {
$comment = $variables['elements']['#comment'];
$node = $variables['elements']['#node'];
$variables['comment'] = $comment;
$variables['author'] = theme('username', array(
'account' => $comment,
));
$variables['created'] = format_date($comment->created);
$correction = isset($node->field_hijri_correction[$node->language][0]['value']) ? $node->field_hijri_correction[$node->language][0]['value'] : 0;
switch (variable_get('hijri_comment_display')) {
case 'full':
case 'long':
case 'medium':
case 'short':
$format = hijri_format_date($node->created, variable_get('hijri_comment_display'), NULL, $correction);
break;
default:
$format = $variables['created'];
break;
}
$node_types = variable_get('hijri_types', array());
if ((string) $node_types[$node->type] == $node->type) {
$variables['submitted'] = t('Submitted by !username on !datetime', array(
'!username' => $variables['author'],
'!datetime' => $format,
));
$variables['created'] = $format;
}
}
/**
* Implements hook_permission().
*/
function hijri_permission() {
return array(
'administer hijri' => array(
'title' => t('administer hijri'),
'description' => t('Perform administration to Hijri Settings.'),
),
);
}
/**
* Implements hook_node_view().
*/
function hijri_node_view($node) {
unset($node->content['field_hijri_correction']);
}
/**
* @file
* Retrive Hijri date like Drupal format_date function.
*/
function hijri_format_date($timestamp, $type = 'medium', $format = '', $correction = 0) {
switch ($type) {
case 'full':
return t('@hijri on @gregorian', array(
'@hijri' => hijri('l j F Y', $timestamp, $correction),
'@gregorian' => format_date($timestamp, 'custom', 'F j, Y'),
));
case 'long':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'medium':
$format = variable_get('date_format_medium', 'D, d/m/Y - H:i');
break;
case 'short':
$format = variable_get('date_format_short', 'd/m/Y - H:i');
break;
case 'custom':
// No change to format.
break;
}
return hijri($format, $timestamp, $correction);
}
/**
* Retrive Hijri date from given format and timestamp.
*/
function hijri($format, $timestamp = NULL, $correction = 0) {
$timestamp = $timestamp == NULL ? time() : $timestamp;
$patterns = array();
$replacements = array();
array_push($patterns, 'Y');
array_push($replacements, 'x1');
array_push($patterns, 'y');
array_push($replacements, 'x2');
array_push($patterns, 'M');
array_push($replacements, 'x3');
array_push($patterns, 'F');
array_push($replacements, 'x3');
array_push($patterns, 'n');
array_push($replacements, 'x4');
array_push($patterns, 'm');
array_push($replacements, 'x5');
array_push($patterns, 'j');
array_push($replacements, 'x6');
array_push($patterns, 'd');
array_push($replacements, 'x7');
array_push($patterns, 'S');
array_push($replacements, '');
$format = str_replace($patterns, $replacements, $format);
// 1. Converting user given date before any increment.
$date_result = format_date($timestamp, 'custom', $format);
// 2. Preparing timestamp if there is increments?
if ($correction != 0) {
$timestamp = $timestamp + 60 * 60 * 24 * $correction;
}
// 3. $calc_hijri_date to retrive Hijri Year.
// Hijri Month and Hijri day in numbers.
$year = format_date($timestamp, 'custom', 'Y');
$month = format_date($timestamp, 'custom', 'n');
$day = format_date($timestamp, 'custom', 'j');
$calc_hijri_date = "";
$hijri_month_name = "";
switch (variable_get('hijri_datetype_value')) {
case 'hijir_solar':
$calc_hijri_date = hijri_shamsi_calc($year, $month, $day);
$hijri_month_name = hijri_shamsi_month_names();
break;
default:
case 'hijir_moon':
$calc_hijri_date = hijri_calc($year, $month, $day);
$hijri_month_name = hijri_month_names();
break;
}
// 4. Replacing reserved constants with Hijri results.
$patterns = $replacements = array();
// Y.
$patterns[] = 'x1';
$replacements[] = $calc_hijri_date[0];
// Y.
$patterns[] = 'x2';
$replacements[] = substr($calc_hijri_date[0], -2);
// M .. There is no shortname in Hijri month names.
$patterns[] = 'x3';
$replacements[] = $hijri_month_name[$calc_hijri_date[1]];
// N.
$patterns[] = 'x4';
$replacements[] = $calc_hijri_date[1];
// M.
$patterns[] = 'x5';
$replacements[] = sprintf('%02d', $calc_hijri_date[1]);
// J.
$patterns[] = 'x6';
$replacements[] = $calc_hijri_date[2];
// D.
$patterns[] = 'x7';
$replacements[] = sprintf('%02d', $calc_hijri_date[2]);
return str_replace($patterns, $replacements, $date_result);
}
/**
* Retrive Hijri months names.
*/
function hijri_month_names() {
// This Arabic names should written in English with t() function.
return array(
'1' => t('Muharram'),
'2' => t('Safar'),
'3' => t('Rabia al-Awwal'),
'4' => t('Rabia ath-Thani'),
'5' => t('Jumada al-Ula'),
'6' => t('Jumada ath-Thaniya'),
'7' => t('Rajab'),
'8' => t('Shaban'),
'9' => t('Ramadan'),
'10' => t('Shawwal'),
'11' => t('Dhu al-Qada'),
'12' => t('Dhu al-Hijja'),
);
}
/**
* Retrive Hijri Shamsi months names.
*
* Https://en.wikipedia.org/wiki/Solar_Hijri_calendar#Month_names.
*/
function hijri_shamsi_month_names() {
// This Arabic names should written in English with t() function.
return array(
'1' => t('Hamal'),
'2' => t('Sawr'),
'3' => t('Jawzā'),
'4' => t('Saratān'),
'5' => t('Asad'),
'6' => t('Sonbola'),
'7' => t('Mizān'),
'8' => t('‘Aqrab'),
'9' => t('Qaws'),
'10' => t('Jadi'),
'11' => t('Dalvæ'),
'12' => t('Hūt'),
);
}
/**
* Calculate Hijri date from given Gregorian by converting through Julian day.
* For more checkout these links:
* http://php.net/manual/en/ref.calendar.php#54234.
* http://sourceforge.net/p/ar-php/code/47/tree/I18N/Arabic/Date.php#l442.
*/
function hijri_calc($y, $m, $d) {
$jd = cal_to_jd(CAL_GREGORIAN, $m, $d, $y);
$jd = $jd - 1948440 + 10632;
$n = (int) (($jd - 1) / 10631);
$jd = $jd - 10631 * $n + 354;
$j = (int) ((10985 - $jd) / 5316) * (int) (50 * $jd / 17719) + (int) ($jd / 5670) * (int) (43 * $jd / 15238);
$jd = $jd - (int) ((30 - $j) / 15) * (int) (17719 * $j / 50) - (int) ($j / 16) * (int) (15238 * $j / 43) + 29;
$m = (int) (24 * $jd / 709);
$d = $jd - (int) (709 * $m / 24);
$y = 30 * $n + $j - 30;
return array(
$y,
$m,
$d,
);
}
/**
* Calculate Hijri Shamsi (Jalali-Iranian) date
* From the given Gregorian by converting through Julian day.
* For more checkout these links:
* http://php.net/manual/en/ref.calendar.php#54234
* https://github.com/mohamadnorouzi20/JalaliGregorianConverter.
*/
function hijri_shamsi_calc($y, $m, $d) {
$g_d_m = [
0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334,
];
$j_year = $y <= 1600 ? 0 : 979;
$y -= $y <= 1600 ? 621 : 1600;
$gy2 = $m > 2 ? $y + 1 : $y;
$days = 365 * $y + (int) (($gy2 + 3) / 4) - (int) (($gy2 + 99) / 100) + (int) (($gy2 + 399) / 400) - 80 + $d + $g_d_m[$m - 1];
$j_year += 33 * (int) ($days / 12053);
$days %= 12053;
$j_year += 4 * (int) ($days / 1461);
$days %= 1461;
$j_year += (int) (($days - 1) / 365);
if ($days > 365) {
$days = ($days - 1) % 365;
}
$j_month = $days < 186 ? 1 + (int) ($days / 31) : 7 + (int) (($days - 186) / 30);
$j_day = 1 + ($days < 186 ? $days % 31 : ($days - 186) % 30);
return array(
$j_year,
$j_month,
$j_day,
);
}
Functions
Name | Description |
---|---|
hijri | Retrive Hijri date from given format and timestamp. |
hijri_block_info | Implements hook_block_info(). |
hijri_block_view | Implements hook_block_view(). |
hijri_calc | Calculate Hijri date from given Gregorian by converting through Julian day. For more checkout these links: http://php.net/manual/en/ref.calendar.php#54234. http://sourceforge.net/p/ar-php/code/47/tree/I18N/Arabic/Date.php#l442. |
hijri_format_date | @file Retrive Hijri date like Drupal format_date function. |
hijri_help | Implements hook_help(). |
hijri_init | Implements hook_init(). |
hijri_menu | Implements hook_menu(). |
hijri_month_names | Retrive Hijri months names. |
hijri_node_presave | Implements hook_node_presave(). |
hijri_node_view | Implements hook_node_view(). |
hijri_permission | Implements hook_permission(). |
hijri_preprocess_comment | Process variables for comment.tpl.php. |
hijri_preprocess_node | Processes variables for node.tpl.php. |
hijri_settings_form | Form builder. |
hijri_settings_form_submit | Form submit handler. |
hijri_shamsi_calc | Calculate Hijri Shamsi (Jalali-Iranian) date From the given Gregorian by converting through Julian day. For more checkout these links: http://php.net/manual/en/ref.calendar.php#54234 https://github.com/mohamadnorouzi20/JalaliGregorianConverter. |
hijri_shamsi_month_names | Retrive Hijri Shamsi months names. |
hijri_theme | Implements hook_theme(). |
hijri_views_api | Implements hook_views_api(). |
_hijri_date_block | The callback of Hijri date block to display the type of date selected. |