View source
<?php
module_load_include('inc', 'availability_calendars', 'availability_calendars');
function availability_calendars_admin_settings() {
drupal_add_css(drupal_get_path('module', 'availability_calendars') . '/availability_calendars.admin.css', 'module', 'all', FALSE);
$form = array();
$form['#validate'][] = 'availability_calendars_admin_settings_validate';
$form['#submit'][] = 'availability_calendars_admin_settings_submit';
$settings = availability_calendars_get_settings();
$form['availability_calendars_settings_content_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Availability calendar enabled content types'),
'#description' => t('Availability calendars allows you to enable the calendar component for any content type. Choose the types on which you would like to associate availability calendar.'),
'#options' => node_get_types('names'),
'#default_value' => $settings->contenttypes,
);
$form['availability_calendars_settings_system_nodeview'] = array(
'#type' => 'checkbox',
'#title' => t('Show automatically on node page'),
'#default_value' => $settings->nodeview,
'#description' => t("Check whether you want availability calendars to be displayed on the node page. If you don't check this, you will need to output it manually using the %func function.", array(
'%func' => "theme('availability_calendars_node')",
)),
);
$form['availability_calendars_settings_system_pernodeoverride'] = array(
'#type' => 'checkbox',
'#title' => t('Allow to override settings per node'),
'#description' => t("If you check this setting, all settings in the '!block_name' block below will be overridden on a per node basis.", array(
'!block_name' => t('Global settings'),
)),
'#default_value' => $settings->pernodeoverride,
);
$form['availability_calendars_settings_system'] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#description' => t("The following settings define the look & feel of the calendars on a system wide basis. If '!setting_name' is enabled, these can be overridden on a per node basis.", array(
'!setting_name' => t('Allow to override settings per node'),
)),
'#tree' => TRUE,
);
$form['availability_calendars_settings_system']['showteaser'] = array(
'#type' => 'checkbox',
'#title' => t('Show availability calendars within teasers'),
'#default_value' => $settings->showteaser,
);
$form['availability_calendars_settings_system']['showkey'] = array(
'#type' => 'checkbox',
'#title' => t('Show a key for the availability calendars'),
'#default_value' => $settings->showkey,
);
$form['availability_calendars_settings_system']['showweeknotes'] = array(
'#type' => 'checkbox',
'#title' => t('Show a note before each week of the availability calendars'),
'#default_value' => $settings->showweeknotes,
);
$form['availability_calendars_settings_system']['firstletter'] = array(
'#type' => 'checkbox',
'#title' => t('Use only the first letter from the day of the week'),
'#default_value' => $settings->firstletter,
);
$form['availability_calendars_settings_system']['hideold'] = array(
'#type' => 'checkbox',
'#title' => t('Do not show availability state of dates in the past'),
'#default_value' => $settings->hideold,
);
$form['availability_calendars_settings_system']['splitday'] = array(
'#type' => 'checkbox',
'#title' => t('Allow split day statuses'),
'#default_value' => $settings->splitday,
);
$form['availability_calendars_settings_system']['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_settings_system']['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_settings_system']['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['states'] = array(
'#type' => 'fieldset',
'#title' => t('States'),
'#tree' => TRUE,
'#description' => t('<p>You can modify the availability states here.</p>
<ul>
<li>The label is what visitors will see in the legend and what editors will see when editing the calendar.</li>
<li>The class must be unique and will be used for the css.</li>
<li>Default defines the status to use for days that have no state yet set.</li>
<li>The weight defines the order in the legend and in the dropdown on the edit form.</li>
<li>The is available checkbox defines whether this state should be treated as available in searches.</li>
<li>Make a label empty to remove the row.</li>
<li>If there are no more empty lines to add new states, save the form and you will be able to add another state.</li>
<li><strong>Changes made to existing classes will not be updated in existing calendars!</strong> So do not change on live sites.</li>
<li>If you define new classes, visit the styling page to define its colors.</li>
</ul>
'),
'#attributes' => array(
'class' => 'state-list',
),
);
$element =& $form['states'];
$states = availability_calendars_get_states();
$i = 0;
foreach ($states as $state) {
availability_calendars_admin_settings_add_state($element, $i, $state, $settings);
$i++;
}
do {
availability_calendars_admin_settings_add_state($element, $i, array(
'class' => '',
'label' => '',
'weight' => 0,
'is_available' => 0,
), $settings);
$i++;
} while ($i < 4);
return system_settings_form($form);
}
function availability_calendars_admin_settings_add_state(&$element, $i, $state, $settings) {
static $max_weight = 0;
$element[$i]['label'] = array(
'#type' => 'textfield',
'#title' => $i == 0 ? t('Label') : '',
'#default_value' => $state['label'],
'#size' => 24,
'#prefix' => '<div class="state-item">',
);
$element[$i]['class'] = array(
'#type' => 'textfield',
'#title' => $i == 0 ? t('Class') : '',
'#default_value' => $state['class'],
'#size' => 24,
);
$element[$i]['defaultstatus'] = array(
'#type' => 'radio',
'#tree' => FALSE,
'#prefix' => $i == 0 ? '<div class="cal-state-label form-item"><label for="edit-defaultstatus">' . t('Default') . ': </label>' : '',
'#suffix' => $i == 0 ? '</div>' : '',
'#return_value' => $i,
'#default_value' => !empty($state['class']) && $state['class'] == $settings->defaultstatus ? $i : 0,
'#attributes' => array(
'name' => 'defaultstatus',
),
);
$element[$i]['is_available'] = array(
'#type' => 'checkbox',
'#prefix' => $i == 0 ? '<div class="cal-state-label form-item"><label for="edit-defaultstatus">' . t('Is available?') . ': </label>' : '',
'#suffix' => $i == 0 ? '</div>' : '',
'#default_value' => $state['is_available'],
);
$element[$i]['weight'] = array(
'#type' => 'select',
'#title' => $i == 0 ? t('Weight') : '',
'#default_value' => $state['weight'] > 0 ? $state['weight'] : ++$max_weight,
'#options' => array_combine(range(1, 20, 1), range(1, 20, 1)),
'#suffix' => '</div>',
);
if ($state['weight'] > $max_weight) {
$max_weight = $state['weight'];
}
}
function availability_calendars_admin_settings_validate($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
if ($op == t('Save configuration')) {
$element = $form_state['values']['states'];
$all_empty = TRUE;
foreach ($element as $i => $state_fields) {
if (!empty($state_fields['label'])) {
$all_empty = FALSE;
}
if (!empty($state_fields['class']) && !availability_calendars_is_safe_id($state_fields['class'])) {
form_set_error("states][{$i}][class", t('Class should be usable as a valid css class.'));
}
}
if ($all_empty) {
form_set_error('states][0][label', t('At least 1 state should be defined.'));
}
$default = $form_state['values']['defaultstatus'];
if (empty($element[$default]['label'])) {
form_set_error("states][{$default}][label", t('The default state may not be empty or deleted.'));
}
}
}
function availability_calendars_admin_settings_submit($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
if ($op == t('Save configuration')) {
$element = $form_state['values']['states'];
unset($form_state['values']['states']);
$states = array();
foreach ($element as $i => $state_fields) {
if (!empty($state_fields['label'])) {
if (empty($state_fields['class'])) {
$state_fields['class'] = availability_calendars_string_to_safe_id($state_fields['label']);
}
$states[$state_fields['class']] = array(
'class' => $state_fields['class'],
'label' => $state_fields['label'],
'weight' => $state_fields['weight'],
'is_available' => $state_fields['is_available'],
);
}
}
$default = $form_state['values']['defaultstatus'];
if (!empty($element[$default]['class'])) {
$form_state['values']['availability_calendars_settings_system']['defaultstatus'] = $element[$default]['class'];
}
availability_calendars_update_states($states);
if ($form_state['values']['availability_calendars_settings_system_pernodeoverride'] == 0) {
$settings = availability_calendars_get_settings();
if ($settings->pernodeoverride) {
availability_calendars_delete_node_settings();
}
}
$form_state['values']['availability_calendars_settings_content_types'] = array_keys(array_filter($form_state['values']['availability_calendars_settings_content_types']));
}
}
function availability_calendars_string_to_safe_id($string) {
return 'cal' . strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
}
function availability_calendars_is_safe_id($string) {
return preg_match('/^[a-zA-Z][a-zA-Z0-9_-]*$/', $string) > 0;
}