You are here

calendar_systems.module in Calendar Systems 5

File

calendar_systems.module
View source
<?php

function calendar_systems_init() {

  //error_reporting(E_ALL^E_NOTICE);
  @(include_once 'PEAR.php');
  if (!class_exists('PEAR')) {
    require_once dirname(__FILE__) . '/calendar/lib/PEAR.php';
  }
  require_once dirname(__FILE__) . '/calendar/lib/classesCore.class.inc.php';
  require_once dirname(__FILE__) . '/calendar/calendar.class.inc.php';
}
function calendar_systems_get_active_lang() {
  if (module_exists('path')) {
    $pathAlias = drupal_get_path_alias($_GET['q']);
  }
  else {
    $pathAlias = $_GET['q'];
  }
  if (preg_match("/(\\/|^)(fa)(\\/|\$)/i", $pathAlias)) {
    return 'fa';
  }
  else {
    return 'en';
  }
}
function calendar_systems_get_calendar_instance() {
  if (calendar_systems_get_active_lang() == 'fa') {
    $calendar = cmfcCalendar::factory('v1', array(
      'name' => 'iranian',
    ));
  }
  else {
    $calendar = cmfcCalendar::factory('v1', array(
      'name' => 'gregorian',
    ));
  }
  return $calendar;
}

/**
 * Format a date with the given configured format or a custom format string.
 *
 * Drupal allows administrators to select formatting strings for 'small',
 * 'medium' and 'large' date formats. This function can handle these formats,
 * as well as any custom format.
 *
 * @param $timestamp
 *   The exact date to format, as a UNIX timestamp.
 * @param $type
 *   The format to use. Can be "small", "medium" or "large" for the preconfigured
 *   date formats. If "custom" is specified, then $format is required as well.
 * @param $format
 *   A PHP date format string as required by date(). A backslash should be used
 *   before a character to avoid interpreting the character as part of a date
 *   format.
 * @param $timezone
 *   Time zone offset in seconds; if omitted, the user's time zone is used.
 * @return
 *   A translated date string in the requested format.
 */
function calendar_systems_format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
  $calendar = calendar_systems_get_calendar_instance();
  if (!isset($timezone)) {
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
  }
  $timestamp += $timezone;
  switch ($type) {
    case 'small':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    case 'large':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
      break;
    case 'custom':

      // No change to format
      break;
    case 'medium':
    default:
      $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
  }

  //$format=strrev($format);
  $max = strlen($format);
  $date = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    if (strpos('AaDFlM', $c) !== FALSE) {
      $date .= t($calendar
        ->timestampToStr($c, $timestamp));
    }
    else {
      if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
        $date .= $calendar
          ->timestampToStr($c, $timestamp);
      }
      else {
        if ($c == 'r') {
          $date .= calendar_systems_format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone);
        }
        else {
          if ($c == 'O') {
            $date .= sprintf('%s%02d%02d', $timezone < 0 ? '-' : '+', abs($timezone / 3600), abs($timezone % 3600) / 60);
          }
          else {
            if ($c == 'Z') {
              $date .= $timezone;
            }
            else {
              if ($c == '\\') {
                $date .= $format[++$i];
              }
              else {
                $date .= $c;
              }
            }
          }
        }
      }
    }
  }
  return $date;
}

/**
 * Implementation of hook_form_alter().
 * Optionally record all form submissions, for later use in building distributions
 */
function calendar_systems_form_alter($form_id, &$form) {
  if (!empty($form['#post']['date'])) {
    $calendar = calendar_systems_get_calendar_instance();
    $form['#post']['date'] = $calendar
      ->strToTimestamp($form['#post']['date']);
    $form['#post']['date'] = date('Y-m-d H:i:s', $form['#post']['date']);
  }
}

Functions

Namesort descending Description
calendar_systems_format_date Format a date with the given configured format or a custom format string.
calendar_systems_form_alter Implementation of hook_form_alter(). Optionally record all form submissions, for later use in building distributions
calendar_systems_get_active_lang
calendar_systems_get_calendar_instance
calendar_systems_init