--- availability_calendars.install Fri Jan 21 08:16:11 2011 +++ availability_calendars.install.new Mon Feb 07 14:30:02 2011 @@ -45,8 +45,8 @@ ), 'status' => array( 'description' => 'The status.', - 'type' => 'text', - 'size' => 'medium', + 'type' => 'varchar', + 'length' => 64, // status = class or (split day) calsplit cal-_ = 14 + 2 * length of a class ), 'date' => array( 'description' => 'Datetime representation of availability', @@ -89,6 +89,26 @@ ) ) ); + + $schema['availability_calendars_states'] = array( + 'description' => 'Store classes and labels for the possible states in availability calendars', + 'fields' => array( + 'class' => array( + 'description' => 'The class used for this state', + 'type' => 'varchar', + 'length' => 24, + 'not null' => TRUE, + ), + 'label' => array( + 'description' => 'The label as displayed to users for this state', + 'type' => 'varchar', + 'length' => 64, // should not be too long: will give display problems + 'not null' => TRUE, + ), + ), + 'primary key' => array('class'), + ); + return $schema; } @@ -96,8 +116,37 @@ * Implementation of hook_install(). */ function availability_calendars_install() { + // Install schema drupal_install_schema('availability_calendars'); - drupal_set_message(t('Availability Calendars module installed successfully.'), 'warning'); + + // Fill schema: add a default (starter, example) set of states to the database + // @TODO: shorten the classes if no css refers to these classes anymore + $states = array( + array( + 'class' => 'calavailable', + 'label' => 'Available', + ), + array( + 'class' => 'calnotavailable', + 'label' => 'Fully booked', + ), + array( + 'class' => 'calnotavailableprov', + 'label' => 'Provisionally booked', + ), + ); + $insert_query = "INSERT INTO {availability_calendars_states} (class, label) VALUES ('%s', '%s')"; + $success = true; + foreach ($states as $state) { + $success = db_query($insert_query, $state['class'], $state['label']) !== false && $success; + } + + if (!$success) { + drupal_set_message(t('Availability Calendars module not installed successfully.'), 'error'); + } + else { + drupal_set_message(t('Availability Calendars module installed successfully.'), 'warning'); + } } /** @@ -197,7 +246,7 @@ ); return $ret; } - + /** * Implementation of hook_update_N(). * Change statuses to now use class strings instead of integers and alter defaultstatus setting if set. @@ -253,5 +302,49 @@ 'success' => TRUE, 'query' => 'Fixed variables for ' . $sandbox['max'] . ' availability calendar variable settings.', ); + return $ret; +} + +/** + * Implementation of hook_update_N(). + * Add custom states + * + * @return array + */ +function availability_calendars_update_6104(&$sandbox) { + $ret = array(); + + // Change type of field status of table availability_calendars_day, + // as it (kind of) refers to the class field of the new states table + db_change_field($ret, 'availability_calendars_day', 'status', 'status', array('type' => 'varchar', 'length' => 64)); + + // Add table to store configurable statuses + $tables = availability_calendars_schema(); //DRY: get table def from schema + $table_name = 'availability_calendars_states'; + db_create_table($ret, $table_name, $tables[$table_name]); + + // Add existing (hard-coded) states to the database + // Note: We can never shorten in an update situation, unless we update the database *contents* as well. + $states = array( + array( + 'class' => 'calavailable', + 'label' => 'Available', + ), + array( + 'class' => 'calnotavailable', + 'label' => 'Fully booked', + ), + array( + 'class' => 'calnotavailableprov', + 'label' => 'Provisionally booked', + ), + ); + $insert_query = "INSERT INTO {{$table_name}} (class, label) VALUES ('%s', '%s')"; + $success = true; + foreach ($states as $state) { + $success = db_query($insert_query, $state['class'], $state['label']) !== false && $success; + } + $ret[] = array('success' => $success, 'query' => $insert_query); + return $ret; }