| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id$
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Demonstrates the Holidays module functionality by drawing a calendar.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_menu().
|
| 12 |
*/
|
| 13 |
function holidays_menu($may_cache) {
|
| 14 |
$items = array();
|
| 15 |
if ($may_cache) {
|
| 16 |
$items[] = array(
|
| 17 |
'path' => 'holidays',
|
| 18 |
'title' => t('Holidays module demonstration'),
|
| 19 |
'callback' => 'holidays_demo',
|
| 20 |
'access' => TRUE,
|
| 21 |
'type' => MENU_NORMAL_ITEM,
|
| 22 |
);
|
| 23 |
}
|
| 24 |
return $items;
|
| 25 |
}
|
| 26 |
|
| 27 |
// Builds the selection and settings form. Note that, for usability reasons, this
|
| 28 |
// is a GET form. Since FAPI doens't process GET forms, we will have to so some
|
| 29 |
// chores ourselves later on.
|
| 30 |
function holidays_calendar_selection_form($cal) {
|
| 31 |
global $year, $month;
|
| 32 |
$form['calendar-selection'] = array(
|
| 33 |
'#type' => 'fieldset'
|
| 34 |
);
|
| 35 |
$form['calendar-selection']['calendar'] = array(
|
| 36 |
'#type' => 'select',
|
| 37 |
'#title' => t('Select the calendar'),
|
| 38 |
'#options' => holidays_get_calendars(),
|
| 39 |
'#default_value' => $cal->name,
|
| 40 |
);
|
| 41 |
$form['calendar-selection']['language'] = array(
|
| 42 |
'#type' => 'radios',
|
| 43 |
'#title' => t('Select the language in which to print the calendar'),
|
| 44 |
'#options' => array(
|
| 45 |
'foreign' => t("The website's language"),
|
| 46 |
'native' => t("The calendar's native language"),
|
| 47 |
),
|
| 48 |
'#default_value' => ($cal->language == CAL_LANG_NATIVE) ? 'native' : 'foreign',
|
| 49 |
);
|
| 50 |
$form['calendar-selection']['submit'] = array(
|
| 51 |
'#type' => 'submit',
|
| 52 |
'#value' => t('Submit'),
|
| 53 |
);
|
| 54 |
if ($cal && ($settings_form = $cal->settings_get_form())) {
|
| 55 |
$form['calendar-selection']['settings'] = $settings_form;
|
| 56 |
$form['calendar-selection']['settings'] += array(
|
| 57 |
'#type' => 'fieldset',
|
| 58 |
'#title' => t('Settings for this calendar'),
|
| 59 |
'#collapsible' => TRUE,
|
| 60 |
'#collapsed' => FALSE,
|
| 61 |
);
|
| 62 |
$form['calendar-selection']['settings_are_for'] = array(
|
| 63 |
'#type' => 'hidden',
|
| 64 |
'#value' => $cal->name,
|
| 65 |
);
|
| 66 |
}
|
| 67 |
if (!variable_get('clean_url', '0')) {
|
| 68 |
$form['q'] = array(
|
| 69 |
'#type' => 'hidden',
|
| 70 |
'#value' => $_GET['q'],
|
| 71 |
);
|
| 72 |
}
|
| 73 |
$form['year'] = array(
|
| 74 |
'#type' => 'hidden',
|
| 75 |
'#value' => $year,
|
| 76 |
);
|
| 77 |
$form['month'] = array(
|
| 78 |
'#type' => 'hidden',
|
| 79 |
'#value' => $month,
|
| 80 |
);
|
| 81 |
$form['#method'] = 'GET';
|
| 82 |
$form['#process'] = array('_holidays_form_process' => array());
|
| 83 |
return $form;
|
| 84 |
}
|
| 85 |
|
| 86 |
// Makes the URL more aesthetic by removing some interval FAPI variables.
|
| 87 |
function _holidays_form_process($form) {
|
| 88 |
unset($form['form_id']);
|
| 89 |
unset($form['form_token']);
|
| 90 |
return $form;
|
| 91 |
}
|
| 92 |
|
| 93 |
// Extracts the chosen calendar's settings from the URL.
|
| 94 |
function _holidays_get_url_settings($cal) {
|
| 95 |
$settings = array();
|
| 96 |
if (isset($_GET['settings_are_for']) && $_GET['settings_are_for'] == $cal->name) {
|
| 97 |
// Settings are available in the URL.
|
| 98 |
foreach ($cal->settings_save() as $name) {
|
| 99 |
if (isset($_GET[$name])) {
|
| 100 |
$settings[$name] = $_GET[$name];
|
| 101 |
} else {
|
| 102 |
// We assume a mising setting stands for an unchecked checkbox.
|
| 103 |
$settings[$name] = 0;
|
| 104 |
}
|
| 105 |
}
|
| 106 |
}
|
| 107 |
return $settings;
|
| 108 |
}
|
| 109 |
|
| 110 |
// The main function.
|
| 111 |
function holidays_demo() {
|
| 112 |
global $year, $month;
|
| 113 |
$year = isset($_GET['year']) ? $_GET['year'] : NULL;
|
| 114 |
$month = isset($_GET['month']) ? $_GET['month'] : NULL;
|
| 115 |
|
| 116 |
$today = getdate(time());
|
| 117 |
|
| 118 |
if (!isset($year) || !isset($month)) {
|
| 119 |
$year = $today['year'];
|
| 120 |
$month = $today['mon'];
|
| 121 |
}
|
| 122 |
|
| 123 |
drupal_add_css(drupal_get_path('module', 'holidays'). '/holidays_demo.css');
|
| 124 |
require_once drupal_get_path('module', 'holidays'). '/holidays_demo.theme';
|
| 125 |
|
| 126 |
if (empty($_GET['calendar'])) {
|
| 127 |
// Get the first one in the list
|
| 128 |
$list = holidays_get_calendars();
|
| 129 |
$_GET['calendar'] = current($list);
|
| 130 |
}
|
| 131 |
|
| 132 |
$cal = holidays_factory($_GET['calendar']);
|
| 133 |
if (!$cal) {
|
| 134 |
drupal_set_message(t("Cannot create calendar of type '%s'", array('%s' => $_GET['calendar'])), 'error');
|
| 135 |
return '';
|
| 136 |
}
|
| 137 |
|
| 138 |
$cal->settings(_holidays_get_url_settings($cal));
|
| 139 |
|
| 140 |
if (isset($_GET['language']) && $_GET['language'] == 'native') {
|
| 141 |
$cal->language(CAL_LANG_NATIVE);
|
| 142 |
} else {
|
| 143 |
$cal->language(CAL_LANG_FOREIGN);
|
| 144 |
}
|
| 145 |
|
| 146 |
$output = drupal_get_form('holidays_calendar_selection_form', $cal);
|
| 147 |
|
| 148 |
$output .= '<div class="calendar '. _holidays_calculate_dir($cal) .'">';
|
| 149 |
$output .= theme('holidays_navigation', $cal);
|
| 150 |
$output .= $cal->printCal($year, $month);
|
| 151 |
$output .= '</div>';
|
| 152 |
|
| 153 |
return $output;
|
| 154 |
}
|
| 155 |
|
| 156 |
// Figures out the directionality of the table.
|
| 157 |
function _holidays_calculate_dir($cal) {
|
| 158 |
if ($cal->language == CAL_LANG_NATIVE && $cal->is_rtl()) {
|
| 159 |
return 'rtl';
|
| 160 |
} else {
|
| 161 |
return 'ltr';
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|