You are here

datex_api.module in Datex 7

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

File

datex_api/datex_api.module
View source
<?php

/**
 * @file
 * Helper functions for converting date formats.
 * Required by all datex module.
 */
require_once 'datex_api_classes.inc';

/**
 * Implements hook_help().
 */
function datex_api_help($path, $arg) {
  if ($path == 'admin/config/regional/date-time/datex_api' || $path == 'admin/help#datex_api') {
    $output = '<p>';
    $output .= t('PHP-Intl is a php library available at !link', array(
      '!link' => l(t('Here'), 'http://pecl.php.net/intl'),
    ));
    $output .= '</p>';
    if (class_exists('IntlDateFormatter')) {
      $output .= t('It is currently available for this Drupal installation environment and can be used.');
    }
    else {
      $output .= t('It is currently NOT available for this Drupal installation environment and enabling this option will not affect the way %module works.', array(
        '%module' => 'Datex API',
      ));
    }
    $output .= '</p><p>';
    $output .= t('You can read more about it at !link', array(
      '!link' => l(t('Here'), 'http://www.drupalion.com/blog/123'),
    ));
    $output .= '</p>';
    return $output;
  }
}

/**
 * Implements hook_menu().
 */
function datex_api_menu() {

  // Use PHP-Intl or not.
  $items['admin/config/regional/date-time/datex_api'] = array(
    'title' => 'Datex API',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'datex_api_admin_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Provides administration form for datex API module (menu callback).
 */
function datex_api_admin_form() {
  $options = array(
    DATEX_USE_INTL => t('Use PHP-INTL'),
    !DATEX_USE_INTL => t('Use datex internal methods.'),
  );

  // Use PHP-Intl or not.
  $form['datex_api_intl'] = array(
    '#type' => 'radios',
    '#title' => t('Date conversion method'),
    '#default_value' => variable_get('datex_api_intl', 0),
    '#description' => t('Which method datex should use. Intl is experimental'),
    '#options' => $options,
  );

  // JQuery widgets in admin forms.
  $options = array(
    FALSE => t('Do not use widgets'),
    TRUE => t('Add widgets'),
  );
  $form['datex_jquery'] = array(
    '#type' => 'radios',
    '#title' => t('Use jQuery widgets in forms.'),
    '#default_value' => variable_get('datex_jquery', 0),
    '#description' => t('If a jQuery date widget should be added to forms, Such as node add/edit form. This option does not affect date module.'),
    '#options' => $options,
  );
  if (!module_exists('libraries')) {
    $status = t('Can not use datex jquery, Libraries module is missing.');
    $ok = 0;
  }
  elseif (TRUE) {
    $status = t('Can not use datex jquery, Library files are missing from sites/all/libararies.');
    $ok = 0;
  }
  else {
    $status = t('Everything is OK and datex widget will work.');
    $ok = 1;
  }
  $form['datex_status'] = array(
    '#type' => 'markup',
    '#markup' => t('Current datex jquery status: ') . '<h4>' . $status . '</h4>',
  );
  if (!$ok) {
    $form['datex_jquery']['#disabled'] = TRUE;
    $form['datex_jquery']['#default_value'] = 0;
  }
  $options = array();
  foreach (language_list() as $lang) {
    $options[$lang->language] = $lang->name . '(' . $lang->native . ')';
  }
  $form['datex_api_enabled_langs'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled languages'),
    '#default_value' => variable_get('datex_api_enabled_langs', array(
      'fa' => 'fa',
    )),
    '#description' => t("Drupal's date will be displayed in Jalali only in selected languages"),
    '#options' => $options,
  );
  return system_settings_form($form);
}

/**
 * Alias of DatexFormater::format().
 */
function datex_format($date, $format, $tz = NULL, $use_intl = 'check first', &$intl_args = array()) {
  if ($use_intl === 'check first') {
    $use_intl == _datex_intl();
  }
  if (!$tz) {
    $tz = drupal_get_user_timezone();
  }
  return DatexFormatter::format($date, $format, $tz, $use_intl, $intl_args);
}

/**
 * Alias of DatexFormatter::date().
 */
function datex_date($format, $timestamp = NULL) {
  return DatexObjectUtils::date($format, $timestamp);
}

/**
 * Helper function to determine whether PHP_Intl should be used or not.
 */
function _datex_intl() {
  return variable_get('datex_api_intl', 0);
}

// ____________________________________________________________________ jQuery.

/**
 * Implements hook_libraries_info().
 */
function datex_libraries_info() {
  $libraries = array();
  $libraries['jquery.datepick'] = array(
    'name' => 'jQuery Datepick',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/datepick.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.datepick.min.js',
      ),
      'css' => array(
        'ui.datepick.css',
        'jquery.datepick.css',
      ),
    ),
  );
  $libraries['jquery.datepick.fa'] = array(
    'name' => ' jQuery Datepicker Persian',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/calendars.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.datepicker.fa.js',
      ),
    ),
  );
  $libraries['jquery.dateentry'] = array(
    'name' => 'jQuery Date Entry',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/dateEntry.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.dateentry.pack.js',
      ),
      'css' => array(
        'jquery.dateentry.css',
      ),
    ),
  );
  $libraries['jquery.dateentry.fa'] = array(
    'name' => ' jQuery Datepicker Persian',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/calendars.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.datepicker.fa.js',
      ),
    ),
  );
  $libraries['jquery.timeentry'] = array(
    'name' => ' jQuery Time Entry',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/timeEntry.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.timeentry.pack.js',
      ),
      'css' => array(
        'jquery.timeentry.css',
      ),
    ),
  );
  $libraries['jquery.timeentry.fa'] = array(
    'name' => ' jQuery DateTime Entry Persian',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/calendars.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.timeentry.fa.js',
      ),
    ),
  );
  $libraries['jquery.datetimeentry'] = array(
    'name' => ' jQuery DateTime Entry',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/datetimeEntry.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.datetimeentry.min.js',
      ),
      'css' => array(
        'jquery.datetimeentry.css',
      ),
    ),
  );
  $libraries['jquery.datetimeentry.fa'] = array(
    'name' => ' jQuery DateTime Entry Persian',
    'vendor url' => 'http://keith-wood.name/',
    'download url' => 'http://keith-wood.name/calendars.html',
    'version callback' => '_datex_jquery_intl_version_callback',
    'files' => array(
      'js' => array(
        'jquery.datetimeentry.fa.js',
      ),
    ),
  );
  return $libraries;
}

/**
 * Required by datex_popup_libraries_info().
 *
 * Intl file has no version so just return one.
 */
function _datex_jquery_intl_version_callback() {
  return 1;
}

/**
 * Loads requested js libraries.
 */
function _datex_jquery_add_libraries($libraries = NULL, $intl = NULL) {
  if (!function_exists('libraries_load')) {
    return;
  }

  // Let's assume TRUE means fa.
  $intl = $intl === TRUE ? 'fa' : $intl;

  // Loading multiple libraries in an arraya.
  if (is_array($libraries)) {
    unset($libraries['jquery.calendars']);
    foreach ($libraries as $library) {
      libraries_load($library);
      if ($intl) {
        libraries_load($library . ".{$intl}");
      }
    }
  }
  elseif (is_string($libraries)) {
    libraries_load($libraries);
    if ($intl) {
      libraries_load($libraries . ".{$intl}");
    }
  }
}

/**
 * Helper to determine if datex should skip the given language.
 *
 * If user has enabled for the given language, This function will return FALSE,
 * Which means the language should not be skipped, In other words, If datex is
 * not enabled for the given language it should be skiped and dates should not
 * be represented in Jalali.
 */
function _datex_skip_language($language = NULL) {
  if (!$language) {
    $language = $GLOBALS['language']->language;
  }
  $enabled_languages = variable_get('datex_api_enabled_langs', array(
    'fa' => 'fa',
  ));
  $enabled = isset($enabled_languages[$language]) ? $enabled_languages[$language] : 0;
  return $enabled === 0;
}

Functions

Namesort descending Description
datex_api_admin_form Provides administration form for datex API module (menu callback).
datex_api_help Implements hook_help().
datex_api_menu Implements hook_menu().
datex_date Alias of DatexFormatter::date().
datex_format Alias of DatexFormater::format().
datex_libraries_info Implements hook_libraries_info().
_datex_intl Helper function to determine whether PHP_Intl should be used or not.
_datex_jquery_add_libraries Loads requested js libraries.
_datex_jquery_intl_version_callback Required by datex_popup_libraries_info().
_datex_skip_language Helper to determine if datex should skip the given language.