View source
<?php
module_load_include('inc', 'availability_calendars', 'availability_calendars');
function availability_calendars_node_edit_form_alter(&$form, &$form_state, $form_id) {
$node = node_load($form['nid']['#value']);
$calendar_nid = $node->tnid && $node->tnid != $node->nid ? $node->tnid : $node->nid;
$settings = availability_calendars_get_settings('node', $calendar_nid);
$form['availability_calendars'] = array(
'#type' => 'fieldset',
'#title' => t('Availability calendar settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['availability_calendars']['showteaser'] = array(
'#type' => 'checkbox',
'#title' => t('Show availability calendars within teasers'),
'#default_value' => $settings->showteaser,
);
$form['availability_calendars']['showkey'] = array(
'#type' => 'checkbox',
'#title' => t('Show a key for the availability calendars'),
'#default_value' => $settings->showkey,
);
$form['availability_calendars']['showweeknotes'] = array(
'#type' => 'checkbox',
'#title' => t('Show a note before each week of the availability calendars'),
'#default_value' => $settings->showweeknotes,
);
$form['availability_calendars']['firstletter'] = array(
'#type' => 'checkbox',
'#title' => t('Use only the first letter from the day of the week'),
'#default_value' => $settings->firstletter,
);
$form['availability_calendars']['hideold'] = array(
'#type' => 'checkbox',
'#title' => t('Do not show availability state of dates in the past'),
'#default_value' => $settings->hideold,
);
$form['availability_calendars']['splitday'] = array(
'#type' => 'checkbox',
'#title' => t('Allow split day statuses'),
'#default_value' => $settings->splitday,
);
$form['availability_calendars']['startofweek'] = array(
'#type' => 'select',
'#title' => t('First day of week'),
'#default_value' => $settings->startofweek,
'#options' => array(
6 => t('Monday'),
5 => t('Tuesday'),
4 => t('Wednesday'),
3 => t('Thursday'),
2 => t('Friday'),
1 => t('Saturday'),
0 => t('Sunday'),
),
);
$form['availability_calendars']['monthcount'] = array(
'#type' => 'textfield',
'#title' => t('Number of months to display'),
'#default_value' => $settings->monthcount,
'#description' => t('The number of months to display to normal visitors.'),
);
$form['availability_calendars']['editormonthcount'] = array(
'#type' => 'textfield',
'#title' => t('Number of months to display to editors'),
'#default_value' => $settings->editormonthcount,
'#description' => t("The number of months to display to visitors who are allowed to edit the calendar to be displayed. Setting this larger than the previous value allows editors to enter information into future calendars before it is made publicly available."),
);
$form['availability_calendars']['defaultstatus'] = array(
'#type' => 'select',
'#title' => t('Default status for each day'),
'#default_value' => $settings->defaultstatus,
'#options' => availability_calendars_options(),
);
}
function availability_calendars_save_node_settings($node) {
variable_set('availability_calendars_settings_node_' . $node->nid, (array) $node->availability_calendars);
}
function availability_calendars_edit_calendar($node = NULL, $year = NULL, $month = NULL, $edit = FALSE) {
if (is_object($node)) {
drupal_set_title(t('Availability for !name', array(
'!name' => $node->title,
)));
$calendar_node = $node->tnid && $node->tnid != $node->nid ? node_load($node->tnid) : $node;
$settings = availability_calendars_get_settings('node', $calendar_node->nid);
if (is_numeric($year) && is_numeric($month)) {
$settings->monthcount = 1;
}
elseif (is_numeric($year)) {
$month = 1;
$settings->monthcount = 12;
}
else {
$year = date('Y');
$month = date('m');
}
if ($edit == 'edit') {
drupal_set_title(t('Availability for !name in !date', array(
'!name' => $node->title,
'!date' => format_date(mktime(12, 0, 0, $month, 1, $year), 'custom', 'F Y'),
)));
return drupal_get_form('availability_calendars_node_edit_calendar_month_form', $node, $year, $month, $settings);
}
else {
return theme('availability_calendars_node', $calendar_node, $year, $month, $settings);
}
}
else {
print drupal_not_found();
}
}
function availability_calendars_node_edit_calendar_month_form($form_state, $node, $year, $month, $settings) {
$form = array();
$nid = $node->nid;
$form['nid'] = array(
'#type' => 'hidden',
'#default_value' => $nid,
);
$form['year'] = array(
'#type' => 'hidden',
'#default_value' => $year,
);
$form['month'] = array(
'#type' => 'hidden',
'#default_value' => $month,
);
$month_meta = availability_calendars_month_meta($year, $month, $settings);
$notes = availability_calendars_get_node_notes($nid, $year, $month);
$states = availability_calendars_get_node_states($nid, $year, $month, $settings);
$day = 1;
for ($week = 1; $week <= $month_meta['weeksinmonth']; $week++) {
$form['week-' . $week] = array(
'#type' => 'fieldset',
'#title' => t('Week @week', array(
'@week' => $week,
)),
);
if ($settings->showweeknotes) {
$form['week-' . $week]['note-' . $week] = array(
'#type' => 'textfield',
'#title' => t('Note'),
'#default_value' => $notes[$week],
'#description' => t('This will be displayed beside the week in the calendar. It could include, for example, a weekly price.'),
);
}
if ($week == 1) {
$daysinweekremaining = 7 - $month_meta['firstday'];
}
else {
$daysinweekremaining = 7;
}
while ($daysinweekremaining > 0 && $day <= $month_meta['daysinmonth']) {
$date = mktime(12, 0, 0, $month, $day, $year);
$form['week-' . $week]['day-' . $day] = array(
'#type' => 'select',
'#title' => format_date($date, 'custom', 'l j F'),
'#options' => availability_calendars_options(),
'#default_value' => $states[date(AC_ISODATE, $date)],
);
$day++;
$daysinweekremaining--;
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
function availability_calendars_node_edit_calendar_month_form_submit($form, &$form_state) {
$nid = $form_state['values']['nid'];
$year = $form_state['values']['year'];
$month = $form_state['values']['month'];
$notes = array();
$week = 1;
$nomoreweeks = FALSE;
while (!$nomoreweeks) {
if (isset($form_state['values']['note-' . $week])) {
$notes[$week] = $form_state['values']['note-' . $week];
$week++;
}
else {
$nomoreweeks = TRUE;
}
}
$states = array();
$day = 1;
$nomoredays = FALSE;
while (!$nomoredays) {
if (isset($form_state['values']['day-' . $day])) {
$states[$day] = $form_state['values']['day-' . $day];
$day++;
}
else {
$nomoredays = TRUE;
}
}
if (!empty($notes)) {
availability_calendars_update_node_notes($nid, $year, $month, $notes);
}
availability_calendars_update_node_states($nid, $year, $month, $states);
cache_clear_all();
drupal_set_message(t('Availability information saved.'));
}