You are here

function availability_calendars_update_6202 in Availability Calendars 6.2

Same name and namespace in other branches
  1. 7.2 availability_calendars.install \availability_calendars_update_6202()

Allow to define custom colors using administration interface (issue #660502) The upgrade should mimic the current availability_calendars.css as much as possible.

File

./availability_calendars.install, line 403
Install, update and uninstall functions for the Availability Calendars module.

Code

function availability_calendars_update_6202(&$sandbox) {
  $ret = array();

  // Update statuses: shorten them
  $state_updates = array(
    'calavailable' => 'calav',
    'calnotavailable' => 'calna',
    'calnotavailableprov' => 'calopt',
  );
  $update_states_query = "UPDATE {availability_calendars_states} SET class = '%s' WHERE class = '%s'";
  $update_days_query = "UPDATE {availability_calendars_day} SET status = '%s' WHERE status = '%s'";
  $success = TRUE;
  foreach ($state_updates as $state_update_old => $state_update_new) {
    $success = db_query($update_states_query, $state_update_new, $state_update_old) !== FALSE && $success;
    $success = db_query($update_days_query, $state_update_new, $state_update_old) !== FALSE && $success;
  }
  $ret[] = array(
    'success' => $success,
    'query' => 'Shortened default states',
  );

  // Update statuses: redefine storage for split day states
  // - get unique split day statuses
  // - for each of these: define an update entry/query (taking into account above renamings)
  // - execute querys
  $split_state_updates = array();
  $result = db_query("SELECT distinct status FROM {availability_calendars_day} WHERE status LIKE 'calsplit %'");
  while ($state = db_fetch_array($result)) {
    $state = $state['status'];

    // state = 'calsplit cal-<am>_<pm>'
    // algorithm will fail with underscores in class names (not likely as 6200 to 6203 will be released at the same time)
    $underscore = strpos($state, '_');
    $am_state = substr($state, strlen('calsplit cal-'), $underscore - strlen('calsplit cal-'));
    if (array_key_exists($am_state, $state_updates)) {
      $am_state = $state_updates[$am_state];
    }
    $pm_state = substr($state, $underscore + 1);
    if (array_key_exists($pm_state, $state_updates)) {
      $pm_state = $state_updates[$pm_state];
    }
    $split_state_updates[$state] = "{$am_state}-am {$pm_state}-pm";
  }
  $update_query = "UPDATE {availability_calendars_day} SET status = '%s' WHERE status = '%s'";
  $success = TRUE;
  foreach ($split_state_updates as $state_update_old => $state_update_new) {
    $success = db_query($update_query, $state_update_new, $state_update_old) !== FALSE && $success;
  }
  $ret[] = array(
    'success' => $success,
    'query' => 'Redefined storage for split day states',
  );
  variable_set('availability_calendars_settings_system_generate', 1);
  $styles = array(
    'table' => array(
      'font-size' => 'smaller',
      'color' => '#000000',
      'background-color' => '#ffffff',
      'border-width' => '0px',
      'border-color' => '#000000',
    ),
    'caption' => array(
      'text-align' => 'left',
      'font-weight' => 'bold',
      'font-style' => 'inherit',
      'font-size' => 'smaller',
    ),
    'header' => array(
      'height' => '',
      'font-weight' => 'bold',
      'font-style' => 'inherit',
      'font-size' => '',
      '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']['calav'] = '#90ee90';
  $styles['states']['calna'] = '#ffb6c1';
  $styles['states']['calopt'] = '#ffffe0';
  variable_set('availability_calendars_styles', $styles);
  $link = l(st('Availability Calendars') . ' ' . st('Styling'), 'admin/settings/availability-calendars/styling');
  drupal_set_message(st("Please visit the '!link' page to generate a CSS file.", array(
    '!link' => $link,
  )), 'warning');
  $ret[] = array(
    'success' => true,
    'query' => 'Added new variables.',
  );
  return $ret;
}