View source
<?php
function calendar_systems_help($path, $arg) {
switch ($path) {
case 'admin/settings/calendar_systems':
$output = '<p>' . t('Using the following form you can assign a different calendar system to each language and also select the default calendar system.') . '</p>';
return $output;
}
}
function calendar_systems_load_dependenies() {
global $_calendar_systems_load_dependenies_called;
if ($_calendar_systems_load_dependenies_called !== TRUE) {
if (!class_exists('cmfcClassesCoreStandAlone') and !module_exists('cml_')) {
require_once dirname(__FILE__) . '/calendar/lib/exception.class.inc.php';
require_once dirname(__FILE__) . '/calendar/lib/classesCore.class.inc.php';
}
if (!class_exists('cmfcCalendar')) {
require_once dirname(__FILE__) . '/calendar/calendar.class.inc.php';
}
$_calendar_systems_load_dependenies_called = TRUE;
}
}
function calendar_systems_perm() {
return array(
'administer calendar_systems',
);
}
function calendar_systems_menu() {
$items['admin/settings/calendar_systems'] = array(
'title' => 'Calendar systems',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'calendar_systems_profile_overview',
),
'description' => 'Assign calendar systems.',
'access arguments' => array(
'administer calendar_systems',
),
'file' => 'calendar_systems.admin.inc',
);
$items['admin/settings/calendar_systems/profile'] = array(
'title' => 'Profiles',
'description' => 'Assign calendar systems.',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/calendar_systems/profile/%/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'calendar_systems_profile_form',
4,
),
'access arguments' => array(
'administer calendar_systems',
),
'file' => 'calendar_systems.admin.inc',
'description' => 'Assign calendar systems.',
'tab_root' => 'admin/settings/calendar_systems/profile',
'tab_parent' => 'admin/settings/calendar_systems/profile/%',
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/calendar_systems/profile/%/delete'] = array(
'title' => 'Remove',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'calendar_systems_profile_delete_confirm',
4,
),
'access arguments' => array(
'administer calendar_systems',
),
'description' => 'Assign calendar systems.',
'file' => 'calendar_systems.admin.inc',
'tab_root' => 'admin/settings/calendar_systems/profile',
'tab_parent' => 'admin/settings/calendar_systems/profile/%calendar_systems_profile',
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
function calendar_systems_theme() {
return array(
'calendar_systems_profile_overview' => array(
'arguments' => array(
'form' => NULL,
),
),
'calendar_systems_admin_button_table' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function calendar_systems_get_all_plugins() {
$result = array(
'default' => array(
'title' => t('Drupal\'s Default'),
'installed' => TRUE,
'installed version' => 2,
),
'iranian' => array(
'title' => t('Iranian'),
'installed' => TRUE,
'installed version' => 2,
),
'arabic' => array(
'title' => t('Arabic'),
'installed' => TRUE,
'installed version' => 2,
),
'thai' => array(
'title' => t('Thai'),
'installed' => TRUE,
'installed version' => 3,
),
);
return $result;
}
function calendar_systems_get_all_langauges() {
if (function_exists('locale_language_list')) {
$langs = locale_language_list('name', TRUE);
}
else {
$langs = array();
}
$result['default'] = array(
'name' => 'Default',
);
if (is_array($langs)) {
foreach ($langs as $name => $title) {
$result[$name] = array(
'name' => $title,
);
}
}
return $result;
}
function calendar_systems_profile_load_all() {
static $profiles;
if (!isset($profiles)) {
$profiles = array();
$result = db_query('SELECT language, calendar_system, settings FROM {calendar_systems}');
while ($profile = db_fetch_object($result)) {
$profile->settings = unserialize($profile->settings);
$profiles[$profile->language] = $profile;
}
}
return $profiles;
}
function calendar_systems_get_active_lang() {
global $language;
$result = 'default';
if ($language) {
if ($language->language) {
$result = $language->language;
}
}
return $result;
}
function calendar_systems_get_calendar_instance() {
calendar_systems_load_dependenies();
$profiles = calendar_systems_profile_load_all();
$lang = calendar_systems_get_active_lang();
if (isset($profiles[$lang])) {
$calendar_system = $profiles[$lang]->calendar_system;
}
else {
if ($profiles['default']) {
$calendar_system = $profiles['default']->calendar_system;
}
}
if (empty($calendar_system)) {
$calendar_system = 'default';
}
if ($calendar_system == 'iranian') {
$calendar = cmfcCalendar::factory('v1', array(
'name' => 'iranian',
));
}
elseif ($calendar_system == 'arabic') {
$calendar = cmfcCalendar::factory('v1', array(
'name' => 'arabic',
));
}
elseif ($calendar_system == 'default') {
$calendar = cmfcCalendar::factory('v1', array(
'name' => 'gregorian',
));
}
elseif ($calendar_system == 'default') {
$calendar = cmfcCalendar::factory('v1', array(
'name' => 'thai',
));
}
return $calendar;
}
function calendar_systems_format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
$calendar = calendar_systems_get_calendar_instance();
$calendar
->setTimeZoneOffset('0:0');
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);
}
}
if (!is_null($timezone)) {
$calendar
->setTimeZoneOffset($timezone);
}
$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':
break;
case 'medium':
default:
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
}
$max = strlen($format);
$date = '';
for ($i = 0; $i < $max; $i++) {
$c = $format[$i];
if (strpos('AaDlM', $c) !== FALSE) {
$date .= t($calendar
->timestampToStr($c, $timestamp), array(), $langcode);
}
elseif ($c == 'F') {
$date .= trim(t('!long-month-name ' . $calendar
->timestampToStr($c, $timestamp), array(
'!long-month-name' => '',
), $langcode));
}
elseif (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
$date .= $calendar
->timestampToStr($c, $timestamp);
}
elseif ($c == 'r') {
$date .= calendar_systems_format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode);
}
elseif ($c == 'O') {
$date .= sprintf('%s%02d%02d', $timezone < 0 ? '-' : '+', abs($timezone / 3600), abs($timezone % 3600) / 60);
}
elseif ($c == 'Z') {
$date .= $timezone;
}
elseif ($c == '\\') {
$date .= $format[++$i];
}
else {
$date .= $c;
}
}
return $date;
}
function calendar_systems_date_validate($element, &$form_state) {
if (!empty($element['#value'])) {
$v = $element['#value'];
$calendar = calendar_systems_get_calendar_instance();
$a = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$1', $v);
$b = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$2', $v);
$v = $calendar
->strToTimestamp("{$a} 10:10:10");
$v = date("Y-m-d {$b}", $v);
form_set_value($element, $v, $form_state);
}
}
function calendar_systems_form_alter(&$form, $form_state, $form_id) {
if (isset($form['author']['date'])) {
$form['author']['date']['#element_validate'] = array(
'calendar_systems_date_validate',
);
}
if (isset($form['admin']['date'])) {
$form['admin']['date']['#element_validate'] = array(
'calendar_systems_date_validate',
);
}
}
function calendar_systems_patch() {
$patches = array();
$patches[] = array(
'title' => t('Allows drupal format_date function to be hooked'),
'patch' => 'core_format_date_hook.patch',
'module' => 'core',
'issue' => '',
'description' => t('Allows drupal format_date function to be hooked'),
'patchdir' => 'patch',
);
return $patches;
}
function calendar_systems_preprocess_content_field(&$variables) {
if ($variables['field_type'] == 'date') {
foreach ($variables['items'] as $key => $item) {
$display = 'full';
if ($variables['teaser']) {
$display = 'teaser';
}
$type_info = date_get_format_types($variables['field']['display_settings'][$display]['format']);
if (!$type_info) {
$_format = $variables['field']['default_format'];
}
else {
$_format = $type_info['title'];
}
if (!isset($variables['field']['granularity'])) {
$granularity = FALSE;
}
else {
$granularity = $variables['field']['granularity'];
}
if (isset($variables['items'][$key]['value2'])) {
$format = _calendar_systems_get_format($_format, $granularity);
$variables['items'][$key]['view'] = format_date(strtotime($variables['items'][$key]['value']), 'custom', $format) . ' - ' . format_date(strtotime($variables['items'][$key]['value2']), 'custom', $format);
}
else {
$format = _calendar_systems_get_format($_format, $granularity);
$variables['items'][$key]['view'] = format_date(strtotime($variables['items'][$key]['value']), 'custom', $format);
}
}
}
}
function _calendar_systems_get_format($format, $granularity) {
$new_format = '';
switch ($format) {
case 'small':
case 'short':
$new_format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
case 'large':
$new_format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'medium':
$new_format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
break;
default:
$new_format = $format;
break;
}
if ($granularity) {
$new_format = _calendar_systems_fix_by_granularity($new_format, $granularity);
$new_format = _calendar_systems_fix_whitespace($new_format, $granularity);
}
return $new_format;
}
function _calendar_systems_fix_by_granularity($format, $granularity) {
$new_format = '';
$list = _calendar_systems_get_date_format();
for ($i = 0; $i < strlen($format); $i++) {
$c = $format[$i];
if (isset($list[$c])) {
if (isset($granularity[$list[$c]])) {
$new_format .= $c;
}
}
else {
$new_format .= $c;
}
}
return $new_format;
}
function _calendar_systems_fix_whitespace($format, $granularity) {
$new_format = '';
$list = _calendar_systems_get_date_format();
$i = strlen($format) - 1;
for (; $i >= 0; $i--) {
$c = $format[$i];
if (isset($list[$c])) {
break;
}
}
$format = substr($format, 0, $i + 1);
$i = 0;
for (; $i < strlen($format); $i++) {
$c = $format[$i];
if (isset($list[$c])) {
break;
}
}
$new_format = substr($format, $i, strlen($format) - $i);
return $new_format;
}
function _calendar_systems_get_date_format() {
$chars = array(
'L' => 'year',
'o' => 'year',
'Y' => 'year',
'y' => 'year',
'F' => 'month',
'm' => 'month',
'M' => 'month',
'n' => 'month',
't' => 'month',
'd' => 'day',
'D' => 'day',
'j' => 'day',
'l' => 'day',
'N' => 'day',
'S' => 'day',
'w' => 'day',
'z' => 'day',
'a' => 'hour',
'A' => 'hour',
'B' => 'hour',
'g' => 'hour',
'G' => 'hour',
'h' => 'hour',
'H' => 'hour',
'i' => 'minute',
's' => 'second',
'u' => 'second',
'e' => 'hour',
'O' => 'hour',
'P' => 'hour',
'T' => 'hour',
'Z' => 'second',
'I' => 'day',
'c' => '',
'r' => '',
'U' => '',
);
return $chars;
}