You are here

datex_api.module in Datex 7.2

Same filename and directory in other branches
  1. 7 datex_api/datex_api.module

File

datex_api/datex_api.module
View source
<?php

/**
 * @file
 *  Main module file for php library datex. All library files are in 
 *  datex_api.class file.
 */
require_once 'datex_api.class.inc';
require_once 'DatexJalali.inc';

/**
 * Available calendar plugins in datex API.
 *
 * Gregorian needs not to be used since PHP has sufficant support for it.
 * API itself is built upon PHP DateTime, A Gregorian datex plugin is added to 
 * datex just for more compatibility from an OO point of view.
 */
function _datex_api_available_calendars() {
  return array(
    'jalali',
  );
}

/**
 * Calls all functions containing dummy names.
 *
 * Called in order to register names with translations API.
 */
function datex_get_all_dummy_names() {
  foreach (_datex_api_available_calendars() as $cal) {
    $function = '_datex_dummy_translation_function_' . $cal;
    $function();
  }
}

/**
 * List of css files which jquery calendar has.
 */
function _datex_api_css_files_list() {
  $css = array(
    'ui-black-tie.calendars.picker.css',
    'ui-blitzer.calendars.picker.css',
    'ui-cupertino.calendars.picker.css',
    'ui-dark-hive.calendars.picker.css',
    'ui-dot-luv.calendars.picker.css',
    'ui-eggplant.calendars.picker.css',
    'ui-excite-bike.calendars.picker.css',
    'ui-flick.calendars.picker.css',
    'ui-hot-sneaks.calendars.picker.css',
    'ui-humanity.calendars.picker.css',
    'ui-le-frog.calendars.picker.css',
    'ui-mint-choc.calendars.picker.css',
    'ui-overcast.calendars.picker.css',
    'ui-pepper-grinder.calendars.picker.css',
    'ui-redmond.calendars.picker.css',
    'ui-smoothness.calendars.picker.css',
    'ui-south-street.calendars.picker.css',
    'ui-start.calendars.picker.css',
    'ui-sunny.calendars.picker.css',
    'ui-swanky-purse.calendars.picker.css',
    'ui-trontastic.calendars.picker.css',
    'ui-ui-darkness.calendars.picker.css',
    'ui-ui-lightness.calendars.picker.css',
    'ui-vader.calendars.picker.css',
    'ui.calendars.picker.css',
    'flora.calendars.picker.css',
    'humanity.calendars.picker.css',
    'jquery.calendars.picker.css',
    'redmond.calendars.picker.css',
    'smoothness.calendars.picker.css',
  );
  return $css;
}

/**
 * Implements hook_libraries_info().
 *
 * Utilizes libararies module hooks to add support of jQuery World Calendar
 * written by keith wood. Check the library at keith-wood.info, He has written
 * some very cool stuff, Which I will add to datex later. 
 */
function datex_api_libraries_info() {
  $libraries['jquery.calendars'] = array(
    'vendor url' => 'http://keith-wood.info',
    'download url' => 'http://keith-wood.name/zip/jquery.calendars.package-1.2.1.zip',
    'name' => t('jQuery calendars'),
    'version arguments' => array(
      'file' => 'jquery.calendars.all.js',
      'pattern' => '#v(\\d\\.\\d\\.\\d)\\.#',
      'lines' => 2,
    ),
    'files' => array(
      'js' => array(
        'jquery.calendars.all.min.js',
        'jquery.calendars.lang.min.js',
        'jquery.calendars.picker.lang.min.js',
        'jquery.calendars.persian.min.js',
        'jquery.calendars.persian-fa.js',
      ),
    ),
    'variants' => array(
      'normal' => array(
        'files' => array(
          'js' => array(
            'jquery.calendars.all.js',
            'jquery.calendars.lang.js',
            'jquery.calendars.picker.lang.js',
            'jquery.calendars.persian.js',
            'jquery.calendars.persian-fa.js',
          ),
        ),
      ),
    ),
  );
  return $libraries;
}

/**
 * Finds suitable jquery calendar localization for a datex calendar.
 *
 * Datex and jquery calendars locale names might be different, Sush as
 * Datex: Jalali and jQuery Calendars: Persian.
 *
 * @TODO complete the list.
 */
function _datex_api_get_language_locale_calendar($language) {
  if ($language === TRUE) {
    $language = $GLOBALS['language']->language;
  }
  $calendars = array(
    'persian' => array(
      'fa',
      'jalali',
      'persian',
      'iranian',
    ),
    'gregorian' => array(
      'en',
      'gregorian',
    ),
  );
  foreach ($calendars as $calendar => $langs) {
    if (in_array($language, $langs)) {
      return $calendar;
    }
  }

  // Not found.
  return FALSE;
}

/**
 * Helper to add date widgets.
 *
 * $l10n, $c_translation, $l_translation can all be TRUE, so current site 
 * language will be used.
 */
function _datex_api_add_l10n($l10n, $translation = NULL, $zip = 'pack') {
  static $loaded = array();
  if (!module_exists('libraries')) {
    return;
  }
  if ($zip === 'min' || $zip === 'pack') {
    $zip = '.' . $zip . '.js';
  }
  else {
    $zip = '.js';
  }
  $base_path = libraries_get_path('jquery.calendars') . '/';
  $name = 'jquery.calendars.' . $l10n;
  if (!isset($loaded['l10n'][$l10n])) {

    // Base file name for jquery.$component.
    drupal_add_js($base_path . $name . $zip);
    $loaded['l10n'][$l10n] = TRUE;
  }
  if ($translation && !isset($loaded['translation'][$translation])) {
    $name = "{$name}-{$translation}.js";
    drupal_add_js($base_path . $name);
    $loaded['translation'][$translation] = TRUE;
  }
}

/**
 * php->date() localalized version. With default values for Drupal.
 */
function datex_format_date($locale, $datetime, $format, $tz = NULL, $intl = NULL) {
  if (!$tz) {
    $tz = drupal_get_user_timezone();
  }
  if (!$intl) {
    $intl = _datex_cfg('useintl');
  }
  return datex_format($locale, $datetime, $format, $tz, $intl);
}

/**
 * Check to see if a calendar is available and valid in datex API.
 */
function _datex_calendar_is_valid($calendar) {
  switch (_datex_get_calendar_name_from_name($calendar)) {
    case 'jalali':
      return TRUE;
      break;
    default:
      return FALSE;
  }
}

/**
 * Always returns same name for a calendar.
 *
 * A calendar could be called with many names, Such as Jalali and Persian,
 * This function is for easing the pain.
 */
function _datex_get_calendar_name_from_name($name) {
  switch (strtolower($name)) {
    case 'fa':
    case 'farsi':
    case 'parsi':
    case 'persian':
    case 'jalali':
    case 'shamsi':
    case 'iran':
    case 'iranian':
      return 'jalali';
      break;
    default:
      return '';
  }
}

Functions

Namesort descending Description
datex_api_libraries_info Implements hook_libraries_info().
datex_format_date php->date() localalized version. With default values for Drupal.
datex_get_all_dummy_names Calls all functions containing dummy names.
_datex_api_add_l10n Helper to add date widgets.
_datex_api_available_calendars Available calendar plugins in datex API.
_datex_api_css_files_list List of css files which jquery calendar has.
_datex_api_get_language_locale_calendar Finds suitable jquery calendar localization for a datex calendar.
_datex_calendar_is_valid Check to see if a calendar is available and valid in datex API.
_datex_get_calendar_name_from_name Always returns same name for a calendar.