You are here

hijri.module in Hijri 8

Same filename and directory in other branches
  1. 8.2 hijri.module
  2. 7 hijri.module
  3. 3.0.x hijri.module
  4. 1.0.x hijri.module

hijri.module This module convert to Hijri date in nodes,comments and a block.

File

hijri.module
View source
<?php

/**
 * @file hijri.module
 * 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>';
  }
}

/**
 * Retrive Hijri date from given format and timestamp.
 */
function hijri($format, $timestamp = NULL, $correction = 0) {
  $timestamp = $timestamp == NULL ? time() : $timestamp;
  $hijri_month_name = hijri_month_names();
  $hijri_day_name = hijri_day_names();

  // 0. Preparing timestamp if there is increments?
  if ($correction != 0) {
    $timestamp = $timestamp + 60 * 60 * 24 * $correction;
  }

  // 1. $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_calc($year, $month, $day);

  // 2. Reserve constants that able to convert to Hijri.
  $replacements = array(
    'x01',
    'x02',
    'x03',
    'x04',
    'x05',
    'x06',
    'x07',
    'x08',
    'x09',
    'x10',
    'x11',
  );
  $patterns = array(
    'Y',
    'y',
    'M',
    'F',
    'm',
    'n',
    'L',
    'l',
    'D',
    'd',
    'j',
  );
  $format = str_replace($patterns, $replacements, $format);

  // 3. Converting user given date.
  $date_result = format_date($timestamp, 'custom', $format);

  // 4. Replacing reserved constants with Hijri results.
  $patterns = $replacements = array();

  // Y
  $patterns[] = 'x01';
  $replacements[] = $calc_hijri_date[0];

  // y
  $patterns[] = 'x02';
  $replacements[] = substr($calc_hijri_date[0], -2);

  // M .. There is no shortname in Hijri month names.
  $patterns[] = 'x03';
  $replacements[] = $hijri_month_name[$calc_hijri_date[1]];

  // F
  $patterns[] = 'x04';
  $replacements[] = $hijri_month_name[$calc_hijri_date[1]];

  // m
  $patterns[] = 'x05';
  $replacements[] = sprintf('%02d', $calc_hijri_date[1]);

  // n
  $patterns[] = 'x06';
  $replacements[] = $calc_hijri_date[1];

  // L
  $patterns[] = 'x07';
  $replacements[] = $hijri_day_name[format_date($timestamp, 'custom', 'N')];

  // l .. There is no lowercase in Arabic day names.
  $patterns[] = 'x08';
  $replacements[] = $hijri_day_name[format_date($timestamp, 'custom', 'N')];

  // D .. There is no shortnames in Arabic day names.
  $patterns[] = 'x09';
  $replacements[] = $hijri_day_name[format_date($timestamp, 'custom', 'N')];

  // d
  $patterns[] = 'x10';
  $replacements[] = sprintf('%02d', $calc_hijri_date[2]);

  // j
  $patterns[] = 'x11';
  $replacements[] = $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 day names to replace the N in date function.
 */
function hijri_day_names() {
  return array(
    '1' => t('Monday'),
    '2' => t('Tuesday'),
    '3' => t('Wednesday'),
    '4' => t('Thursday'),
    '5' => t('Friday'),
    '6' => t('Saturday'),
    '7' => t('Sunday'),
  );
}

/**
 * Calculate Hijri date from the 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,
  );
}

/**
 * * @file hijri.inc
 * 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');
      $format = 'l, F j, Y - H:i';
      break;
    case 'medium':

      //$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
      $format = 'D, m/d/Y - H:i';
      break;
    case 'short':

      //$format = variable_get('date_format_short', 'm/d/Y - H:i');
      $format = 'm/d/Y - H:i';
      break;
    case 'custom':

      // No change to format.
      break;
  }
  return hijri($format, $timestamp, $correction);
}

/**
 * Prepares variables for node templates.
 *
 * Default template: node.html.twig.
 *
 * Most themes use their own copy of node.html.twig. The default is located
 * inside "/core/modules/node/templates/node.html.twig". Look in there for the
 * full list of variables.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An array of elements to display in view mode.
 *   - node: The node object.
 *   - view_mode: View mode; e.g., 'full', 'teaser', etc.
 */
function hijri_preprocess_node(&$variables) {
  $node = $variables['node'];
  $node_types = \Drupal::config('hijri.config')
    ->get('hijri_types');
  $correction = \Drupal::config('hijri.config')
    ->get('correction_value');
  $hijri_display = \Drupal::config('hijri.config')
    ->get('hijri_display');
  if (isset($node_types[$node
    ->getType()]) && (string) $node_types[$node
    ->getType()] == $node
    ->getType()) {
    switch ($hijri_display) {
      case 'full':
      case 'long':
      case 'medium':
      case 'short':
        $format = hijri_format_date($node
          ->getCreatedTime(), $hijri_display, NULL, $correction);
        break;
      default:
        $format = $node
          ->getCreatedTime();
        break;
    }
    $node_type = $node->type->entity;

    // Display post information only on certain node types.
    if ($node_type
      ->displaySubmitted()) {
      $variables['display_submitted'] = TRUE;
      $variables['date'] = $format;
    }
  }
}

/**
 * Prepares variables for comment templates.
 *
 * Default template: comment.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the comment and entity objects.
 *     Array keys: #comment, #commented_entity.
 */
function hijri_preprocess_comment(&$variables) {
  $comment = $variables['elements']['#comment'];
  $node = $comment
    ->getCommentedEntity();
  $variables['comment'] = $comment;
  $node_types = \Drupal::config('hijri.config')
    ->get('hijri_types');
  $correction = \Drupal::config('hijri.config')
    ->get('correction_value');
  $hijri_display = \Drupal::config('hijri.config')
    ->get('hijri_comment_display');
  if (isset($node_types[$node
    ->getType()]) && (string) $node_types[$node
    ->getType()] == $node
    ->getType()) {
    switch ($hijri_display) {
      case 'full':
      case 'long':
      case 'medium':
      case 'short':
        $format = hijri_format_date($comment
          ->getCreatedTime(), $hijri_display, NULL, $correction);
        break;
      default:
        $format = $variables['created'];
        break;
    }
    $variables['submitted'] = t('Submitted by @username on @datetime', array(
      '@username' => $variables['author'],
      '@datetime' => $format,
    ));
    $variables['created'] = $format;
  }
}

Functions

Namesort descending Description
hijri Retrive Hijri date from given format and timestamp.
hijri_calc Calculate Hijri date from the 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_day_names Retrive day names to replace the N in date function.
hijri_format_date @file hijri.inc Retrive Hijri date like Drupal format_date function.
hijri_help Implements hook_help().
hijri_month_names Retrive Hijri months names.
hijri_preprocess_comment Prepares variables for comment templates.
hijri_preprocess_node Prepares variables for node templates.