availability_calendar.install in Availability Calendars 7.3
Same filename and directory in other branches
Install, update and uninstall functions for the Availability Calendar module.
@author Erwin Derksen (http://drupal.org/user/750928)
File
availability_calendar.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the Availability Calendar module.
*
* @author Erwin Derksen (http://drupal.org/user/750928)
*/
/**
* Implenments hook_schema()
* @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_schema/7
*/
function availability_calendar_schema() {
$schema = array();
// Singular table names (though [#140860] isn't clear about the conclusion).
$schema['availability_calendar_calendar'] = array(
'description' => 'Stores availability calendars.',
'fields' => array(
'cid' => array(
'description' => 'The primary identifier for an availability calendar.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'created' => array(
'description' => 'The Unix timestamp when the calendar was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' => 'The Unix timestamp when the calendar was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'cid',
),
);
$schema['availability_calendar_availability'] = array(
'description' => 'Stores availability per availability calendar per day.',
'fields' => array(
'cid' => array(
'description' => 'The primary identifier for an availability calendar.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
// [#747036]: using date instead of separate fields allows for better querying.
// [#1083198]: Mysql, pgsql, mssql, and oracle support the DATE type, but
// this is not a type supported by Drupal, so specifiy per supported
// database. Sqlite uses TEXT as type but has a lot of date functions.
// 'yyyy-mm-dd' (iso 8601) is accepted by mysql, pgsql, mssql, and sqlite
// to specify DATE's. For oracle I could not find this information.
// The 'between' operator is inclusive on both sides on all databases:
// date between from and to <=> from <= date and date <= to.
'date' => array(
'description' => 'Date of availability state.',
'mysql_type' => 'DATE',
'pgsql_type' => 'DATE',
'type' => 'TEXT',
'not null' => TRUE,
),
'sid' => array(
'description' => 'The availability state.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
),
),
'primary key' => array(
'cid',
'date',
),
'indexes' => array(
'availability_calendar_availability_date' => array(
'date',
'sid',
),
),
'foreign keys' => array(
'availability_calendar_calendar' => array(
'table' => 'availability_calendar_calendar',
'columns' => array(
'cid' => 'cid',
),
),
'availability_calendar_state' => array(
'table' => 'availability_calendar_state',
'columns' => array(
'sid' => 'sid',
),
),
),
);
$schema['availability_calendar_state'] = array(
'description' => 'Stores defined availability calendar states.',
'fields' => array(
'sid' => array(
'description' => 'The identifier for a state.',
'type' => 'serial',
'size' => 'tiny',
'not null' => TRUE,
),
'css_class' => array(
'description' => 'The CSS class used for this state.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'label' => array(
'description' => 'The label for this state.',
'type' => 'varchar',
'length' => 64,
),
'weight' => array(
'description' => 'The weight of this state.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'is_available' => array(
'description' => 'Boolean indicating whether this state is to be treated as available.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'sid',
),
);
return $schema;
}
/**
* Implements hook_install().
* @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_install/7
*/
function availability_calendar_install() {
$t = get_t();
// Fill schema: add a default set of states to the database.
$states = array(
array(
'sid' => 1,
'css_class' => 'cal-nc',
'label' => 'Not communicated',
'weight' => 1,
'is_available' => 0,
),
array(
'sid' => 2,
'css_class' => 'cal-av',
'label' => 'Available',
'weight' => 2,
'is_available' => 1,
),
array(
'sid' => 3,
'css_class' => 'cal-na',
'label' => 'Fully booked',
'weight' => 3,
'is_available' => 0,
),
array(
'sid' => 4,
'css_class' => 'cal-opt',
'label' => 'Provisionally booked',
'weight' => 4,
'is_available' => 0,
),
);
foreach ($states as $state) {
db_insert('availability_calendar_state')
->fields($state)
->execute();
}
// Define a set of default style settings.
variable_set('availability_calendar_styles_generate', 1);
$styles = array(
'table' => array(
'font-size' => 'smaller',
'color' => '#000000',
'background-color' => '',
'border-width' => '1px',
'border-color' => '#000000',
),
'caption' => array(
'font-weight' => 'bold',
'font-style' => 'inherit',
'font-size' => 'smaller',
),
'header' => array(
'height' => '',
'font-weight' => 'bold',
'font-style' => 'inherit',
'font-size' => 'inherit',
'text-align' => 'center',
),
'week_notes' => array(
'width' => '90px',
),
'days' => array(
'width' => '28px',
'height' => '28px',
'text-align' => 'center',
'vertical-align' => 'middle',
),
'states' => array(
'split-day' => '/',
),
);
// Fill default states
$styles['states']['cal-nc'] = '#909090';
$styles['states']['cal-av'] = '#90ee90';
$styles['states']['cal-na'] = '#ffb6c1';
$styles['states']['cal-opt'] = '#ffffe0';
variable_set('availability_calendar_styles', $styles);
module_load_include('inc', 'availability_calendar', 'availability_calendar.styles');
availability_calendar_styles_generate();
$link = l($t('Availability Calendar') . ' ' . $t('Settings'), 'admin/config/content/availability-calendar/settings');
drupal_set_message($t("Please visit the '!link' page to define the set of availability states.", array(
'!link' => $link,
)), 'warning');
$link = l($t('Availability Calendar') . ' ' . $t('Styling'), 'admin/config/content/availability-calendar/styling');
drupal_set_message($t("Please visit the '!link' page to generate your own custom CSS file for Availability Calendar.", array(
'!link' => $link,
)), 'warning');
}
/**
* Implements hook_field_schema().
* @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_schema/7
*/
function availability_calendar_field_schema($field) {
if ($field['type'] == 'availability_calendar') {
return array(
'columns' => array(
'enabled' => array(
'type' => 'int',
'size' => 'tiny',
'default' => 1,
'not null' => TRUE,
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'default' => '',
'not null' => TRUE,
),
'cid' => array(
'type' => 'int',
'not null' => TRUE,
),
),
'indexes' => array(),
'foreign_keys' => array(
'cid' => array(
'table' => 'availability_calendar_calendar',
'columns' => array(
'cid' => 'cid',
),
),
),
);
}
}
/**
* Implements hook_uninstall().
* @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_uninstall/7
*/
function availability_calendar_uninstall() {
availability_calendar_variable_del_multiple('availability_calendar_');
file_unmanaged_delete_recursive('public://availability_calendar');
}
/**
* Utility function that is an altered version of variable_del, it will delete
* a set of variables set by a module.
*
* @param string $name
* The variables' namespace to delete.
*/
function availability_calendar_variable_del_multiple($name) {
db_delete('variable')
->condition('name', addcslashes($name, '%_') . '%', 'LIKE')
->execute();
cache_clear_all('variables', 'cache');
}
/**
* Regenerate custom styles file.
*/
function availability_calendar_update_7301(&$sandbox) {
module_load_include('inc', 'availability_calendar', 'availability_calendar.styles');
availability_calendar_styles_generate();
}
Functions
Name | Description |
---|---|
availability_calendar_field_schema | Implements hook_field_schema(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/... |
availability_calendar_install | Implements hook_install(). @link http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... |
availability_calendar_schema | Implenments hook_schema() @link http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... |
availability_calendar_uninstall | Implements hook_uninstall(). @link http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... |
availability_calendar_update_7301 | Regenerate custom styles file. |
availability_calendar_variable_del_multiple | Utility function that is an altered version of variable_del, it will delete a set of variables set by a module. |