diff -urp availability_calendars/availability_calendars.css availability_calendars.css --- availability_calendars.css 2011-01-18 05:10:01.000000000 +0100 +++ availability_calendars.css 2011-01-25 11:52:52.000000000 +0100 @@ -2,7 +2,7 @@ table.sticky-header{font-size:80%;z-index:2;} table.cal{width:200px;display:inline;display:inline-table;/* for Safari */float:left;font-size:80%;margin:0 6px 6px;} table.cal tr.odd,table.cal tr.even{background:#FFFFFF;height:29px;} -div.month_title{margin:6px 6px 0;border-bottom:1px solid #DDDDDD;} +div.month_title{margin:6px 6px 0;border-bottom:1px solid #DDDDDD;width:200px} div.calmonth-wrapper{float:left;} tr.caldays,tr.odd.caldays,tr.even.caldays{color:#CCCCCC;} th.dayofweek{text-align:center;} @@ -31,3 +31,6 @@ td.cal-notavailableprov_available,tr.odd td.cal-notavailableprov_available > .cal-split-bg{border-color:#FFFFE0 #90EE90 #90EE90 #FFFFE0;} td.cal-notavailableprov_notavailable,tr.odd td.cal-notavailableprov_notavailable,tr.even td.cal-notavailableprov_notavailable{background:#FFB6C1;} td.cal-notavailableprov_notavailable > .cal-split-bg{border-color:#FFFFE0 #FFB6C1 #FFB6C1 #FFFFE0;} +/* custom label form elements */ +input.delete-label {background-color: #c0c0c0;} +input.new-label {border: 2px solid #4685c6;background-color: #cbe5ff;} diff -urp availability_calendars/availability_calendars.install availability_calendars.install --- availability_calendars.install 2011-01-21 08:16:11.000000000 +0100 +++ availability_calendars.install 2011-01-25 12:47:33.000000000 +0100 @@ -17,39 +17,39 @@ function availability_calendars_schema() { $schema = array(); $schema['availability_calendars_day'] = array( - 'description' => 'The table for calendar days.', + 'description' => t('The table for calendar days.'), 'fields' => array( 'nid' => array( - 'description' => 'The primary identifier for a node.', + 'description' => t('The primary identifier for a node.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'year' => array( - 'description' => 'The number of the year.', + 'description' => t('The number of the year.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'month' => array( - 'description' => 'The number of the month.', + 'description' => t('The number of the month.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'day' => array( - 'description' => 'The number of the day.', + 'description' => t('The number of the day.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'status' => array( - 'description' => 'The status.', + 'description' => t('The status.'), 'type' => 'text', 'size' => 'medium', ), 'date' => array( - 'description' => 'Datetime representation of availability', + 'description' => t('Datetime representation of availability'), 'type' => 'datetime' ) ) @@ -59,36 +59,46 @@ function availability_calendars_schema() 'description' => 'The table for calendar days.', 'fields' => array( 'nid' => array( - 'description' => 'The primary identifier for a node.', + 'description' => t('The primary identifier for a node.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'year' => array( - 'description' => 'The number of the year.', + 'description' => t('The number of the year.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'month' => array( - 'description' => 'The number of the month.', + 'description' => t('The number of the month.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'week' => array( - 'description' => 'The number of the week.', + 'description' => t('The number of the week.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE ), 'note' => array( - 'description' => 'The status.', + 'description' => t('The status.'), 'type' => 'varchar', 'length' => 64 ) ) ); + + $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; } @@ -198,6 +208,26 @@ function availability_calendars_update_6 return $ret; } + +/** + * Add custom labels + */ +function availability_calendars_update_6104(&$sandbox) { + $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; +} + /** * Implementation of hook_update_N(). * Change statuses to now use class strings instead of integers and alter defaultstatus setting if set. diff -urp availability_calendars/availability_calendars.module availability_calendars.module --- availability_calendars.module 2011-01-23 05:35:46.000000000 +0100 +++ availability_calendars.module 2011-01-26 10:03:19.000000000 +0100 @@ -101,9 +101,13 @@ function availability_calendars_perm() { * * @return array */ -function availability_calendars_admin_settings() { - $form = array(); +function availability_calendars_admin_settings(&$form_state) { $settings = availability_calendar_getsettings(); + + $form = array( + '#submit' => array('availability_calendars_admin_submit') + ); + $form['display'] = array( '#type' => 'fieldset', '#title' => t('View settings'), @@ -146,8 +150,97 @@ function availability_calendars_admin_se '#title' => t('Show availability calendars within teasers.'), '#default_value' => $settings->showteaser, ); - //TODO: add status codes and css classes in here - return system_settings_form($form); + + $db_labels = availability_calendars_custom_options(); + $form['display_labels'] = array( + '#type' => 'fieldset', + '#title' => t('Custom Availability Labels'), + '#collapsible' => TRUE, + '#collapsed' => (count($db_labels) == 0), + '#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 ($db_labels as $lid => $label){ + $form['display_labels']['custom_label-' . $lid] = availability_calendars_label_form_field(array('id' => $lid, 'label' => $label)); + } + // one for new labels + $form['display_labels']['custom_label-new'] = availability_calendars_label_form_field(array('id' => 'new', 'name' => '')); + + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + + return $form; +} + +function availability_calendars_admin_submit($form, &$form_state) { + if ($form_state['clicked_button']['#id'] != 'edit-save') + return; + + $delete_ids = array(); + $new_labels = array(); + $labels = array(); + $existing = availability_calendars_custom_options('label'); + + foreach ($form_state['values'] as $k => $v){ + $matches = array(); + if (preg_match("/^availability_calendars_settings_system/", $k)) { + variable_set($k, $v); + } + elseif (preg_match("/custom_label-(.+)/", $k, $matches)) { + // casting as int means the 'new' label will result in $id == 0 + $id = (int) $matches[1]; + if (!$v) { // user has emptied this field to indicate removal + if ($id) + $delete_ids[] = $id; + } else { + if (!$id && !in_array($v, $existing)) + $new_labels[] = $v; + else + $labels[$id] = $v; + } + } + } + + 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 $lid => $label) { + db_query("UPDATE {availability_calendars_labels} SET label = '%s' WHERE lid = %d", $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; } /** @@ -246,11 +339,13 @@ function availability_calendars_node_edi $month_meta = availability_calendars_month_meta($year, $month, $settings); // find all entries in database for this month ($availability, $notes) and pre-populate + $notes = array(); $notes_result = db_query('SELECT week, note FROM {availability_calendars_week} WHERE nid = %d AND year = %d and month = %d', $nid, $year, $month); while ($note = db_fetch_array($notes_result)) { $notes[$note['week']] = $note['note']; } + $day_status = array(); $status_result = db_query('SELECT day, status FROM {availability_calendars_day} WHERE nid = %d AND year = %d AND month = %d', $nid, $year, $month); while ($status = db_fetch_array($status_result)) { $day_status[$status['day']] = $status['status']; @@ -274,14 +369,22 @@ function availability_calendars_node_edi else { $daysinweekremaining = 7; } + + $db_options = availability_calendars_options(TRUE); 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] === NULL) ? $settings->defaultstatus : $day_status[$day] + $day_state = $day_status[$day]; + + if (!$day_state) { + $day_state = variable_get('availability_calendars_default_option', 0); + } + + $form['week-'. $week]['day-'. $day] = array( + '#type' => 'select', + '#title' => date('l d F', strtotime("$year-$month-$day")), + '#options' => $db_options, + '#default_value' => "$day_state" ); - $day++; + $day++; $daysinweekremaining--; } } @@ -323,16 +426,15 @@ function availability_calendars_node_edi } // save $days - $day = 1; - $nomoredays = FALSE; - while (!$nomoredays) { - if (isset($form_state['values']['day-' . $day])) { - $days[$day] = $form_state['values']['day-' . $day]; - $day++; - } - else { - $nomoredays = TRUE; - } + $days = array(); + $day = 1; + while (TRUE) { + if (!isset($form_state['values']['day-' . $day])) { + break; + } + + $days[$day] = $form_state['values']['day-' . $day]; + $day++; } db_query('DELETE FROM {availability_calendars_week} WHERE nid = %d AND year = %d AND month = %d', $nid, $year, $month); @@ -434,10 +536,28 @@ function theme_availability_calendars_no $output = ''; // Create our key for the availability calendar if the node has it set to do so - if ($settings->showkey === 1) { - $output .= availability_calendars_key(); - } + if($showkey == 1) { //use all the same classes for cells and table, so it styles the same as the calendars + $keytitle = '