diff -urp sites/all/modules/availability_calendars.orig/availability_calendars.css sites/all/modules/availability_calendars/availability_calendars.css
--- sites/all/modules/availability_calendars.orig/availability_calendars.css 2009-11-23 17:33:50.000000000 +0100
+++ sites/all/modules/availability_calendars/availability_calendars.css 2009-11-24 09:38:49.000000000 +0100
@@ -53,4 +53,13 @@ div.month_title{
div.calmonth-wrapper {
float:left;
+}
+
+input.delete-label {
+ background-color: #c0c0c0;
+}
+
+input.new-label {
+ border: 2px solid #4685c6;
+ background-color: #cbe5ff;
}
\ No newline at end of file
diff -urp sites/all/modules/availability_calendars.orig/availability_calendars.info sites/all/modules/availability_calendars/availability_calendars.info
--- sites/all/modules/availability_calendars.orig/availability_calendars.info 2009-11-24 09:01:41.000000000 +0100
+++ sites/all/modules/availability_calendars/availability_calendars.info 2009-11-24 09:04:35.000000000 +0100
@@ -2,6 +2,7 @@
name = Availability Calendars
description = Allows for availability information to be displayed using calendars on specified content types.
core = 6.x
+dependencies[] = ahah_helper
; Information added by drupal.org packaging script on 2009-09-01
version = "6.x-1.2"
diff -urp sites/all/modules/availability_calendars.orig/availability_calendars.install sites/all/modules/availability_calendars/availability_calendars.install
--- sites/all/modules/availability_calendars.orig/availability_calendars.install 2009-11-21 17:44:44.000000000 +0100
+++ sites/all/modules/availability_calendars/availability_calendars.install 2009-11-24 09:04:42.000000000 +0100
@@ -83,6 +83,15 @@ function availability_calendars_schema()
)
)
);
+
+ $schema['availability_calendars_labels'] = array(
+ 'description' => 'Store custom labels for Availability Calendars',
+ 'fields' => array(
+ 'lid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+ 'label' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 255)
+ ),
+ 'primary key' => array('lid')
+ );
return $schema;
}
@@ -107,3 +116,22 @@ function availability_calendars_uninstal
drupal_set_message(t('Availability Calendars module uninstalled successfully.'), 'warning');
}
+
+/**
+ * Implementation of hook_update_version
+ */
+function availability_calendars_update_6112() {
+ $ret = array();
+
+ $table = array(
+ 'description' => 'Store custom labels for Availability Calendars',
+ 'fields' => array(
+ 'lid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+ 'label' => array('type' => 'varchar', 'not null' => TRUE, 'length' => 255)
+ ),
+ 'primary key' => array('lid')
+ );
+
+ db_create_table($ret, 'availability_calendars_labels', $table);
+ return $ret;
+}
\ No newline at end of file
diff -urp sites/all/modules/availability_calendars.orig/availability_calendars.module sites/all/modules/availability_calendars/availability_calendars.module
--- sites/all/modules/availability_calendars.orig/availability_calendars.module 2009-11-21 17:27:39.000000000 +0100
+++ sites/all/modules/availability_calendars/availability_calendars.module 2009-11-24 09:40:20.000000000 +0100
@@ -52,29 +52,209 @@ function availability_calendars_perm() {
/**
* Settings page callback.
*/
-function availability_calendars_admin_settings() {
- $form = array();
+function availability_calendars_admin_settings(&$form_state) {
+ if (!module_exists('ahah_helper')){
+ drupal_set_message(t("Availability Calendars module requires the AHAH Helper module. Please install and enable the AHAH Helper module from the module administration page before setting up Availability Calendars."), 'warning');
+ return;
+ }
+
+ drupal_add_css(drupal_get_path('module', 'availability_calendars') .'/availability_calendars.css');
+
+ $form = array(
+ '#submit' => array('availability_calendars_admin_submit')
+ );
+ ahah_helper_register($form, $form_state);
+
+ $form['availability_calendars'] = array(
+ '#prefix' => '
',
+ '#suffix' => '
',
+ '#type' => 'fieldset',
+ '#title' => 'Availability Calendars Settings',
+ '#tree' => TRUE
+ );
- $form['display'] = array(
+ $form['availability_calendars']['display'] = array(
'#type' => 'fieldset',
'#title' => t('View settings'),
'#description' => t('Check where you want availability calendars to be displayed. If you choose none of the suggested places below you will need to output it manually using %func function.', array('%func' => 'theme_availability_calendars_node()'))
);
- $form['display']['availability_calendars_display_nodeview'] = array(
+ $form['availability_calendars']['display']['availability_calendars_display_nodeview'] = array(
'#type' => 'checkbox',
'#title' => t('Node view page'),
'#default_value' => variable_get('availability_calendars_display_nodeview', TRUE)
);
- $form['display']['availability_calendars_display_monthcount'] = array(
+ $form['availability_calendars']['display']['availability_calendars_display_monthcount'] = array(
'#type' => 'textfield',
'#title' => t('Number of months to display'),
'#default_value' => variable_get('availability_calendars_display_monthcount', 12),
'#description' => t("Your calendars will show this number of months to all users except those with the 'edit own availability calendars' or 'edit availability calendars' who will always see 3 extra months on the calendars they can edit. This is to allow them to enter information into future calendars before it is made publicly available.")
);
- //TODO: add status codes and css classes in here
+ // labels from the database, if this is the first load, otherwise, use the form input
+ $labels = array();
+ $names = array(); // for sorting later
+ $db_labels = availability_calendars_custom_options();
+ if (!$form_state['storage']){
+ foreach ($db_labels as $id => $label){
+ $labels[] = array('id' => $id, 'label' => $label, 'title' => $label);
+ $names[$label] = count($labels) - 1;
+ }
+ } else {
+ $new_id = -1;
+ foreach ($form_state['storage']['availability_calendars']['display_labels'] as $k => $v){
+ if ($k == 'add_label')
+ continue;
+
+ $new = ((int) $k < 1);
+ $label = NULL;
+ if (!$v) { // user has emptied this field to indicate removal
+ if (!$new) { // skip newly added ones that never made it to the database
+ $label = array('id' => $k, 'label' => $v, 'delete' => TRUE, 'title' => '[deleted] ' . $db_labels[$k]);
+ }
+ } else {
+ $id = ($new)? $new_id-- : $k;
+ $label = array('id' => $id, 'label' => $v, 'new' => $new, 'title' => (($new)? '[new] ' : '') . $v);
+ }
- return system_settings_form($form);
+ if ($label){
+ $labels[] = $label;
+ $names[$v] = count($labels) - 1;
+ }
+ }
+ }
+
+ $a = array_keys($names);
+ $sorted_labels = array();
+ sort($a);
+
+ foreach ($a as $name){
+ $sorted_labels[] = $labels[$names[$name]];
+ }
+
+ $form['availability_calendars']['display_labels'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Custom Availability Key'),
+ '#collapsible' => TRUE,
+ '#description' => t("You can create custom availability labels here. Add a new item and enter it's label. A CSS class will automatically be generated from this label. If you define no labels, the default set will be used. If you define at least one label, the default set will be ignored, and you must re-define any labels you wish to use from the default set.")
+ );
+
+ foreach ($sorted_labels as $label){
+ $form['availability_calendars']['display_labels'][$label['id']] = availability_calendars_label_form_field($label);
+ }
+ // one for new labels
+ $form['availability_calendars']['display_labels']['new'] = availability_calendars_label_form_field(array(
+ 'id' => 'new',
+ 'name' => ''
+ ));
+
+ $form['availability_calendars']['display_labels']['add_label'] = array(
+ '#type' => 'button',
+ '#value' => t('Add new label'),
+ '#ahah' => array(
+ 'path' => ahah_helper_path(array('availability_calendars')),
+ 'wrapper' => 'availability-calendars-wrapper'
+ )
+ );
+
+ if ($sorted_labels){
+ $options = array();
+ foreach ($sorted_labels as $label){
+ $options[$label['id']] = $label['label'];
+ }
+
+ $form['availability_calendars']['default_label'] = array(
+ '#type' => 'fieldset',
+ '#title' => 'Default Option',
+ 'default_label' => array(
+ '#type' => 'select',
+ '#options' => $options,
+ '#default_value' => variable_get('availability_calendars_default_option', '')
+ )
+ );
+ }
+
+ //TODO: add css classes in here
+
+ $form['save'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save'),
+ );
+
+ return $form;
+}
+
+function availability_calendars_admin_submit($form_id, &$form) {
+ if ($form['clicked_button']['#id'] != 'edit-save')
+ return;
+
+ $values = $form['values']['availability_calendars'];
+ $nkey = 'availability_calendars_display_nodeview';
+ $mkey = 'availability_calendars_display_monthcount';
+ $dkey = 'availability_calendars_default_option';
+ variable_set($nkey, $values['display'][$nkey]);
+ variable_set($mkey, $values['display'][$mkey]);
+ variable_set($dkey, $values['default_label']['default_label']);
+
+ $delete_ids = array();
+ $new_labels = array();
+ $labels = array();
+ $existing = availability_calendars_custom_options('label');
+
+ foreach ($values['display_labels'] as $id => $label){
+ if ($id == 'add_label')
+ continue;
+
+ $new = ((int) $id < 1);
+
+ if (!$label) { // user has emptied this field to indicate removal
+ if ((int) $id)
+ $delete_ids[] = $id;
+ } else {
+ if ($new && !in_array($label, $existing))
+ $new_labels[] = $label;
+ else
+ $labels[] = array('lid' => $id, 'label' => $label);
+ }
+ }
+
+ if ($delete_ids)
+ db_query("DELETE FROM {availability_calendars_labels} WHERE lid IN(" . join(',', $delete_ids) . ")");
+
+ foreach ($new_labels as $label) {
+ db_query("INSERT INTO {availability_calendars_labels} (label) VALUES ('%s')", $label);
+ }
+ foreach ($labels as $label) {
+ db_query("UPDATE {availability_calendars_labels} SET label = '%s' WHERE lid = %d", $label['label'], $label['lid']);
+ }
+}
+
+/**
+ * Get a field definition for a calendar key label.
+ *
+ * @param $label
+ * An array containing label info, as: array('id' => $id, 'name' => $name, ['delete' => TRUE], ['new' => TRUE | FALSE])
+ *
+ * @return
+ * An array with a Form field definition
+ */
+function availability_calendars_label_form_field($label) {
+ $class = '';
+ if (isset($label['delete']) && $label['delete'] == TRUE) {
+ $class = 'delete-label';
+ }
+ if (isset($label['new']) && $label['new'] == TRUE) {
+ $class = 'new-label';
+ }
+
+ $field = array(
+ '#type' => 'textfield',
+ '#title' => $label['title'],
+ '#default_value' => $label['label'],
+ '#description' => t("You may change the name of this label, or delete it. To delete this label, clear this field, and then save. The CSS class for this label is: '%css'", array('%css' => availability_calendars_option_css($label['label']))),
+ '#attributes' => array('class' => $class)
+ );
+
+ return $field;
}
/**
@@ -184,12 +364,23 @@ function availability_calendars_node_edi
else {
$daysinweekremaining = 7;
}
+
+ $options = array('-1' => 'select availability');
+ $db_options = availability_calendars_options();
+ foreach ($db_options as $id => $label)
+ $options[$id] = $label;
+
+ $day_state = $day_status[$day];
+ if (!$day_state)
+ $day_state = variable_get('availability_calendars_default_option', 0);
+ error_log($day_state);
+
while ($daysinweekremaining > 0 && $day <= $month_meta['daysinmonth']) {
$form['week-'. $week]['day-'. $day] = array(
'#type' => 'select',
'#title' => date('l d F', strtotime("$year-$month-$day")),
- '#options' => availability_calendars_options(),
- '#default_value' => $day_status[$day]
+ '#options' => $options,
+ '#default_value' => $day_state
);
$day++;
$daysinweekremaining--;
@@ -225,6 +416,7 @@ function availability_calendars_node_edi
// save $days
$day = 1;
$nomoredays = FALSE;
+ $days = array();
while (!$nomoredays) {
if (isset($form_state['values']['day-'. $day])) {
$days[$day] = $form_state['values']['day-'. $day];
@@ -297,14 +489,18 @@ function theme_availability_calendars_no
t('Color'),
t('Availability')
);
- $rows = array( // keystatus class used to allow better styling of the status column
- array(array('data' => ' ', 'class' => 'calavailable'),
- array('data' => t('Available'), 'class' => 'keystatus')),
- array(array('data' => ' ', 'class' => 'calnotavailable'),
- array('data' => t('Fully booked'), 'class' => 'keystatus')),
- array(array('data' => ' ', 'class' => 'calnotavailableprov'),
- array('data' => t('Provisionally booked'), 'class' => 'keystatus'))
- );
+
+ $rows = array();
+ $options = availability_calendars_options();
+
+ foreach ($options as $id => $label){
+ $css = availability_calendars_option_css($label);
+ $rows[] = array(
+ array('data' => ' ', 'class' => $css),
+ array('data' => t($label), 'class' => 'keystatus')
+ );
+ }
+
$key = theme_table($headers, $rows, array('class' => 'cal'));
$output .= ''.$keytitle.$key.'
';
}
@@ -372,6 +568,9 @@ function theme_availability_calendars_mo
1 => t('Sat'),
0 => t('Sun'),
);
+
+ $states = availability_calendars_custom_options();
+
for ($j=0;$j<$month_meta['weeksinmonth'];$j++) {
for ($i=0;$i<7;$i++) {
$counter++;
@@ -416,15 +615,12 @@ function theme_availability_calendars_mo
for ($i=0; $i<7; $i++) {
// if there's a date, it's part of this month
if ($week[$key][$i]) {
- if ($day_status[$week[$key][$i]] == 1) { // booked
- array_push($cells, array('data' => $week[$key][$i], 'class' => 'calnotavailable'));
- }
- elseif ($day_status[$week[$key][$i]] == 2) { // provisionally booked
- array_push($cells, array('data' => $week[$key][$i], 'class' => 'calnotavailableprov'));
- }
- else { // available
- array_push($cells, array('data' => $week[$key][$i], 'class' => 'calavailable'));
- }
+ $day_state = $day_status[$week[$key][$i]];
+ if (!$day_state)
+ $day_state = variable_get('availability_calendars_default_option', 0);
+
+ $class = availability_calendars_option_css($states[$day_state]);
+ array_push($cells, array('data' => $week[$key][$i], 'class' => $class));
}
else { // empty, typically row 1 or 5 in a month
array_push($cells, array('data' => $number, 'class' => 'calother'));
@@ -442,16 +638,74 @@ function theme_availability_calendars_mo
return ''.$month_title.$output.'
';
}
+/**
+ * Get labels for calendars. If none have been defined by the administrator, return the default set.
+ *
+ * @return
+ * An array of labels, in the form array(label_id => t('label name'))
+ */
function availability_calendars_options() {
- // TODO: make these configurable
- return array(
- 0 => t('Available'),
- 1 => t('Fully booked'),
- 2 => t('Provisionally booked')
- );
+ $labels = availability_calendars_custom_options();
+
+ if (!$labels) {
+ $labels = array(
+ 0 => t('Available'),
+ 1 => t('Fully booked'),
+ 2 => t('Provisionally booked')
+ );
+ }
+
+ return $labels;
+}
+
+/**
+ * Get custom labels for calendars, or an empty array if none have been set.
+ *
+ * @param $field
+ * Optionally, the field you'd like to retrieve
+ *
+ * @return
+ * An array of labels, in the form array(label_id => t('label name'))
+ * If $field is set to 'label' or 'lid', returns an array of those field values
+ */
+function availability_calendars_custom_options($field = 'all') {
+ $result = db_query("SELECT * FROM availability_calendars_labels ORDER BY label");
+
+ $labels = array();
+ $row = NULL;
+ while ($row = db_fetch_array($result)) {
+ if ($field != 'all'){
+ $labels[] = ($field == 'label')? t($row['label']) : $row[$field];
+ } else {
+ $labels[$row['lid']] = t($row['label']);
+ }
+ }
+
+ return $labels;
}
/**
+ * Get the CSS class for this option label
+ *
+ * @param $label
+ * The label for this option
+ *
+ * @return
+ * A string representing a CSS class. For backwards compatibility, default values get their historical css class
+ */
+function availability_calendars_option_css($label) {
+ if ($label == 'Available')
+ return 'calavailable';
+ if ($label == 'Fully booked')
+ return 'calnotavailable';
+ if ($lable == 'Provisionally booked')
+ return 'calnotavailableprov';
+
+ return str_replace(" ", "-", strtolower($label));
+}
+
+
+/**
* Implementation of hook_form_alter();
*/
function availability_calendars_form_alter(&$form, &$form_state, $form_id) {
Only in sites/all/modules/availability_calendars: LICENSE.txt
Only in sites/all/modules/availability_calendars: README.txt