You are here

ac_update.module in Availability Calendars 7.2

Availability Calendars update: admin update code

  • form to perform the checks
  • form to clean-up the database (split day states)
  • form to copy the data

@author Erwin Derksen (http://drupal.org/user/750928)

File

ac_update/ac_update.module
View source
<?php

/**
 * @file
 * Availability Calendars update: admin update code
 * - form to perform the checks
 * - form to clean-up the database (split day states)
 * - form to copy the data
 *
 * @author Erwin Derksen (http://drupal.org/user/750928)
 */
function ac_update_init() {

  //   $result = array();
  //   ac_update_create_fields($result);
}

/**
 * Implements hook_permission().
 * @see http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_permission/7
 */
function ac_update_permission() {
  return array(
    'update availability calendars' => array(
      'title' => t('Update the Availability Calendars'),
    ),
  );
}
function ac_update_current_step_state($state = NULL) {
  static $curent_state = 'acu_active';
  if ($state !== NULL) {
    $curent_state = $state;
  }
  return $curent_state;
}
function ac_update_menu() {
  $items = array();
  $items['admin/config/content/availability-calendars/update'] = array(
    'title' => 'Update Availability Calendars',
    'description' => 'Update Availability Calendars to the fields based Availability Calendar version',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ac_update_form0',
    ),
    'access arguments' => array(
      'update availability calendars',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/content/availability-calendars/update/1'] = array(
    'title' => 'Update Availability Calendars',
    'description' => 'Update Availability Calendars to the fields based Availability Calendar version',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ac_update_form1',
    ),
    'access arguments' => array(
      'update availability calendars',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/content/availability-calendars/update/2'] = array(
    'title' => 'Update Availability Calendars',
    'description' => 'Update Availability Calendars to the fields based Availability Calendar version',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ac_update_form2',
    ),
    'access arguments' => array(
      'update availability calendars',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/content/availability-calendars/update/3'] = array(
    'title' => 'Update Availability Calendars',
    'description' => 'Update Availability Calendars to the fields based Availability Calendar version',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ac_update_form3',
    ),
    'access arguments' => array(
      'update availability calendars',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/content/availability-calendars/update/4'] = array(
    'title' => 'Update Availability Calendars',
    'description' => 'Update Availability Calendars to the fields based Availability Calendar version',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ac_update_form4',
    ),
    'access arguments' => array(
      'update availability calendars',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Retrieve form callback for the admin/config/content/availability-calendars/update page.
 *
 * @return array The form
 */
function ac_update_form0($form) {
  $language = new stdClass();
  $language->language = '';
  $text_file_link = l('UPGRADE.txt', drupal_get_path('module', 'ac_update') . '/UPGRADE.txt', array(
    'language' => $language,
  ));
  $form['intro'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Availability Calendars update</h2>
<p>
This module will update Availability Calendars to the new field based storage.
It will do so in a number of steps and checks.
But please, first make sure you have read {<span class="php-variable">$text_file_link</span>}.
</p>
LITERAL
,
  );

  // Add form submit button.
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t("I've read UPGRADE.txt ..."),
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ac_update') . '/ac_update.css',
  );
  $form['#method'] = 'POST';
  $form['#submit'] = array(
    'ac_update_form_submit',
  );
  $form['#submit-redirect'] = 'admin/config/content/availability-calendars/update/1';
  return $form;
}

/**
 * Retrieve form callback for the admin/config/content/availability-calendars/update1 page.
 *
 * @return array The form
 */
function ac_update_form1($form) {
  $state = ac_update_current_step_state();
  $form['intro'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Availability Calendars update</h2>
<p>
This module will update Availability Calendars to the new field based storage.
It will do so in a number of steps and checks.
But please, first <strong>backup your current database</strong>.
</p>
<ol class="acu_list">
<li class="{<span class="php-variable">$state</span>}">Check prerequisites.</li>
<li class="acu_inactive">Clean split day states.</li>
<li class="acu_inactive">Copy data.</li>
</ol>
LITERAL
,
  );
  $form['next'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Next step: Check prerequisites</h2>
<p>
The schema version of the current module will be checked, as well as some other
checks that look for incompatibilities between your current settings and the new
version. If settings are found that are not currently supported, you will get a
warning, but you may continue.
</p>
LITERAL
,
  );

  // Add form submit button.
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Execute next step ...'),
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ac_update') . '/ac_update.css',
  );
  $form['#method'] = 'POST';
  $form['#submit'] = array(
    'ac_update_form_submit',
  );
  $form['#submit-callback'] = 'ac_update_check_prerequisites';
  $form['#submit-redirect'] = 'admin/config/content/availability-calendars/update/2';
  return $form;
}

/**
 * Retrieve form callback for the admin/config/content/availability-calendars/update/2 page.
 *
 * @return array The form
 */
function ac_update_form2($form) {
  $state = ac_update_current_step_state();
  $form['intro'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Availability Calendars update</h2>
<p>
This module will update Availability Calendars to the new field based storage.
It will do so in a number of steps and checks.
</p>
<ol class="acu_list">
<li class="acu_ok">Check prerequisites.</li>
<li class="{<span class="php-variable">$state</span>}">Clean split day states.</li>
<li class="acu_inactive">Copy data.</li>
</ol>
LITERAL
,
  );
  $form['next'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Next step: Clean split day states</h2>
<p>
The fields based version does not store split day states, though it does allow
to show availability split over 2 days: a state for a given day will be shown on
the pm part of that day and on the am part of the next day.
Part of the update is to clean up split states in the database.
All days having a split state will get the pm state of that day.
</p>
<p>
Even when your current setting is to not show split day states, they may still be
present in the database. This because changing the setting did not clean the
database.
When you have never used this setting, this step just checks for unknown states
in the database.
</p>
<p>
Note: it will not be checked whether the pm and am states of two subsequent days
are equal. If the am state differs, that information will get lost!
</p>
LITERAL
,
  );

  // Add form submit button.
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Execute next step ...'),
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ac_update') . '/ac_update.css',
  );
  $form['#method'] = 'POST';
  $form['#submit'] = array(
    'ac_update_form_submit',
  );
  $form['#submit-callback'] = 'ac_update_remove_split_states';
  $form['#submit-redirect'] = 'admin/config/content/availability-calendars/update/3';
  return $form;
}

/**
 * Retrieve form callback for the admin/config/content/availability-calendars/update/3 page.
 *
 * @return array The form
 */
function ac_update_form3($form) {
  $state = ac_update_current_step_state();
  $form['intro'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Availability Calendars update</h2>
<p>
This module will update Availability Calendars to the new field based storage.
It will do so in a number of steps and checks.
</p>
<ol class="acu_list">
<li class="acu_ok">Check prerequisites.</li>
<li class="acu_ok">Clean split day states.</li>
<li class="{<span class="php-variable">$state</span>}">Copy data.</li>
</ol>
LITERAL
,
  );
  $form['next'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Next step: Copy data</h2>
<p>The following data will be copied or created:</p>
<dl>
<dt>States</dt>
<dd>
The fields based version also stores 1 global set of states.
Each field can define a subset of this set as the set of allowed states for that field.
The global set of states is extended with the current set of states.
</dd>
<dt>Calendars</dt>
<dd>
For each calendar a record will be created that keeps track of the created and last updated time.
The conversion will fetch these timestamps from the main node referring to the calendar.
</dd>
<dt>Availability</dt>
<dd>The availability data wll be copied to the new calendar tables.</dd>
<dt>Field and field instances</dt>
<dd>
For each supported content type an Availability Calendar field and instance will be created.
</dd>
<dt>Field data</dt>
<dd>
Field data will be created to set up the links between the nodes and the created calendars.
</dd>
</dl>
LITERAL
,
  );

  // Add form submit button.
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Execute next step ...'),
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ac_update') . '/ac_update.css',
  );
  $form['#method'] = 'POST';
  $form['#submit'] = array(
    'ac_update_form_submit',
  );
  $form['#submit-callback'] = 'ac_update_copy_data';
  $form['#submit-redirect'] = 'admin/config/content/availability-calendars/update/4';
  return $form;
}
function ac_update_form4($form) {
  $state = ac_update_current_step_state();
  $form['intro'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Availability Calendars update</h2>
<p>
This module will update Availability Calendars to the new field based storage.
It will do so in a number of steps and checks.
</p>
<ol class="acu_list">
<li class="acu_ok">Clean split day states.</li>
<li class="acu_ok">Check prerequisites.</li>
<li class="acu_ok">Copy data.</li>
</ol>
LITERAL
,
  );
  $settings_link = l(t('Availability Calendar') . ' ' . t('Settings'), 'admin/config/content/availability-calendar/settings');
  $style_link = l(t('Availability Calendar') . ' ' . t('Styling'), 'admin/config/content/availability-calendar/styling');
  $module_link = l(t('modules'), 'admin/modules/list');
  $form['next'] = array(
    '#type' => 'markup',
    '#markup' => <<<LITERAL
<h2>Finished</h2>
<p>
The conversion has been finished.
If you don't see any error messages the conversion was succesful.
</p>
<p>
Next steps:
</p>
<ul>
<li>Backup the results of this conversion.</li>
<li>Change the (global) set of availability states: {<span class="php-variable">$settings_link</span>}.</li>
<li>Define styles like the colors for the availability states: {<span class="php-variable">$style_link</span>}.</li>
<li>Define the allowed states and the default state per field (admin/structure/types/manage/{type}/fields).</li>
<li>Define the way the calendars are displayed (admin/structure/types/manage/{type}/display) and edited (admin/structure/types/manage/{type}/fields).</li>
<li>Clean-up {<span class="php-variable">$module_link</span>}:
  <ul>
\t\t<li>Disable "Availability Calendars Update".</li>
    <li>Disable "Availability Calendars".</li>
\t\t<li>Uninstall "Availability Calendars Update".</li>
\t\t<li>Uninstall "Availability Calendars".</li>
\t</ul>
</li>
</ul>
LITERAL
,
  );

  // Add form submit button.
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Execute next step ...'),
      '#disabled' => TRUE,
    ),
  );
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ac_update') . '/ac_update.css',
  );
  $form['#method'] = 'POST';
  return $form;
}
function ac_update_form_submit($form, &$form_state) {
  $op = isset($form_state['clicked_button']) ? $form_state['clicked_button']['#id'] : '';
  if ($op == 'edit-submit') {
    $result = array(
      'success' => TRUE,
    );
    if (isset($form['#submit-callback'])) {
      $result = call_user_func($form['#submit-callback']);
      foreach ($result['messages'] as $message) {
        drupal_set_message($message['message'], $message['type'], FALSE);
      }
    }
    if ($result['success']) {
      $form_state['redirect'] = $form['#submit-redirect'];
      drupal_redirect_form($form_state);
    }
    else {
      ac_update_current_step_state('acu_error');
    }
  }
}
function ac_update_check_prerequisites() {
  $result = array(
    'success' => TRUE,
    'messages' => array(),
  );
  $current_version = drupal_get_installed_schema_version('availability_calendars');
  if ($current_version < 7204) {
    $result['success'] = FALSE;
    $result['messages'][] = array(
      'message' => "The current schema version of Availability Calendars is {$current_version}, which is too old. First update the current module to its latest version by running update.php.",
      'type' => 'error',
    );
  }
  module_load_include('inc', 'availability_calendars', 'availability_calendars');
  $settings = availability_calendars_get_settings();
  if ($settings->pernodeoverride == 1) {
    $result['messages'][] = array(
      'message' => "The current setting for 'Allow to override settings per node' cannot be mimicked in the new version.",
      'type' => 'warning',
    );
  }
  if ($settings->showweeknotes == 1) {
    $result['messages'][] = array(
      'message' => "The new version does not (yet) support the setting 'Show a note before each week of the availability calendars'. This data will not be copied and will get lost on uninstall of the old version.",
      'type' => 'warning',
    );
  }
  if ($settings->hideold == 0) {
    $result['messages'][] = array(
      'message' => "In contrast with your current setting for 'Do not show availability state of dates in the past', the new version always hides availability information from the past.",
      'type' => 'warning',
    );
  }
  if ($settings->splitday == 1) {
    $result['messages'][] = array(
      'message' => "Split day states will be removed from the database, but you can continue to show availablity as split days.",
      'type' => 'warning',
    );
  }
  return $result;
}

/**
 * @return array with keys:
 *   success: bool
 *   messages: array of array with keys:
 *     type: string
 *     message: string
 */
function ac_update_remove_split_states() {
  $result = array(
    'success' => TRUE,
    'messages' => array(),
  );

  // Get all stored states.
  $states = array_keys(availability_calendars_get_states());
  $stored_states = db_select('availability_calendars_day', 'acd')
    ->distinct()
    ->fields('acd', array(
    'status',
  ))
    ->execute()
    ->fetchCol();
  $non_default_states = array_diff($stored_states, $states);

  // Create an update map for the found values.
  $update_map = array();
  foreach ($non_default_states as $stored_state) {
    $haystack = " {$stored_state} ";
    $found = FALSE;
    foreach ($states as $state) {
      if (strpos($haystack, " {$state}-pm ") !== FALSE) {
        $update_map[$stored_state] = $state;
        $found = TRUE;
        break;
      }
    }
    if (!$found) {
      foreach ($states as $state) {
        if (strpos($haystack, " {$state} ") !== FALSE) {
          $update_map[$stored_state] = $state;
          $found = TRUE;
          break;
        }
      }
    }
    if (!$found) {
      $result['success'] = FALSE;
      $result['messages'][] = array(
        'message' => "Found a (split) state that cannot be mapped: {$stored_state}",
        'type' => 'error',
      );
    }
  }
  if ($result['success']) {
    $count = 0;
    foreach ($update_map as $current_state => $new_state) {
      $count += db_update('availability_calendars_day')
        ->fields(array(
        'status' => $new_state,
      ))
        ->condition('status', $current_state)
        ->execute();
    }
    if ($count === 0) {
      $result['messages'][] = array(
        'message' => t('No split days were found. Updates were not necessary'),
        'type' => 'status',
      );
    }
    else {
      $result['messages'][] = array(
        'message' => t("{$count} records were updated. Split days have been removed from the database."),
        'type' => 'status',
      );
    }
  }
  return $result;
}
function ac_update_copy_data() {
  module_load_include('inc', 'availability_calendars', 'availability_calendars');
  module_load_include('inc', 'availability_calendar', 'availability_calendar');
  $result = array(
    'success' => TRUE,
    'messages' => array(),
  );
  ac_update_copy_style_settings($result);
  if ($result['success']) {
    ac_update_add_field_nid($result);
  }
  if ($result['success']) {
    ac_update_copy_states($result);
  }
  if ($result['success']) {
    ac_update_create_calendars($result);
  }
  if ($result['success']) {
    ac_update_copy_availability($result);
  }
  if ($result['success']) {
    ac_update_create_field_instances($result);
  }
  if ($result['success']) {
    ac_update_create_field_data($result);
  }
  if ($result['success']) {

    //  We can now drop the temporary field.
    ac_update_drop_field_nid($result);
  }
  return $result;
}
function ac_update_copy_style_settings(&$result) {

  // Get old styles.
  $old_styles = variable_get('availability_calendars_styles', array());
  $old_styles['generate'] = variable_get('availability_calendars_styles_generate', variable_get('availability_calendars_settings_system_generate', 1));

  // Rename standard states that are defined by this module itself.
  if (array_key_exists('calav', $old_styles['states'])) {
    $old_styles['states']['cal-av'] = $old_styles['states']['calav'];
    unset($old_styles['states']['calav']);
  }
  if (array_key_exists('calna', $old_styles['states'])) {
    $old_styles['states']['cal-na'] = $old_styles['states']['calna'];
    unset($old_styles['states']['calna']);
  }
  if (array_key_exists('calopt', $old_styles['states'])) {
    $old_styles['states']['cal-opt'] = $old_styles['states']['calopt'];
    unset($old_styles['states']['calopt']);
  }

  // Get new styles.
  $new_styles = variable_get('availability_calendar_styles', array());
  $new_styles['generate'] = variable_get('availability_calendar_styles_generate', 1);

  // Merge old style into new styles.
  $new_styles = drupal_array_merge_deep($new_styles, $old_styles);

  // Save merged styles.
  variable_set('availability_calendar_styles_generate', $new_styles['generate']);
  unset($new_styles['generate']);
  variable_set('availability_calendar_styles', $new_styles);
  $result['messages'][] = array(
    'message' => "Copied style settings.",
    'type' => 'status',
  );
}
function ac_update_add_field_nid(&$result) {

  // We temporarily add a field 'nid' to the calendar table,
  // so we can relate cid's and nid's while converting the data.
  try {
    db_add_field('availability_calendar_calendar', 'nid', array(
      'type' => 'int',
      'unsigned' => TRUE,
    ));
  } catch (DatabaseSchemaObjectExistsException $e) {
  }
}
function ac_update_drop_field_nid(&$result) {
  db_drop_field('availability_calendar_calendar', 'nid');
}
function ac_update_copy_states(&$result) {
  $old_states = availability_calendars_get_states();
  $current_states = availability_calendar_get_states();
  $new_states = $current_states;
  $merged = 0;
  $renamed = 0;
  foreach ($old_states as $old_state) {

    // Get current state, based on css_class equivalence
    $found = FALSE;
    foreach ($current_states as $sid => $current_state) {
      if ($old_state['css_class'] === $current_state['css_class']) {
        $found = TRUE;
        $merged++;
        break;
      }
      else {
        if ($old_state['css_class'] === str_replace('-', '', $current_state['css_class'])) {
          $found = TRUE;
          $renamed++;
          break;
        }
      }
    }

    // If there is a equivalent, update the state, otherwise create a new.
    if ($found) {
      $new_states[$sid] = array(
        'sid' => $sid,
        'css_class' => $old_state['css_class'],
        'label' => $old_state['label'],
        'weight' => $old_state['weight'],
        'is_available' => $old_state['is_available'] ? 1 : 0,
      );
    }
    else {
      $new_states[] = array(
        'sid' => NULL,
        'css_class' => $old_state['css_class'],
        'label' => $old_state['label'],
        'weight' => $old_state['weight'],
        'is_available' => $old_state['is_available'] ? 1 : 0,
      );
    }
  }
  availability_calendar_update_states($new_states);
  $count = count($new_states);
  $result['messages'][] = array(
    'message' => "Copied {$count} states.",
    'type' => 'status',
  );
}
function ac_update_create_calendars(&$result) {

  // Copy all existing calendars, including their nid, to the new table.
  $query = "\nINSERT INTO {availability_calendar_calendar} (created, changed, nid)\nSELECT created, changed, nid\nFROM {node} n\nWHERE n.nid IN (SELECT DISTINCT nid FROM {availability_calendars_day})";
  $count = db_query($query)
    ->rowCount();
  $result['messages'][] = array(
    'message' => "Created {$count} calendars.",
    'type' => 'status',
  );
}
function ac_update_copy_availability(&$result) {

  // Copy the availability, replacing the nid with the cid and the availability
  // class with the sid. Unknown availability states are ignored.
  $query = "\nSELECT\n  acc.cid as cid,\n  acd.date as date,\n  acs.sid as sid\nFROM {availability_calendars_day} acd\n  INNER JOIN {availability_calendar_calendar} acc on acc.nid = acd.nid\n  INNER JOIN {availability_calendar_state} acs on acs.css_class = acd.status";
  $insert_query = "INSERT INTO {availability_calendar_availability} {$query}";
  $count = db_query($insert_query)
    ->rowCount();
  $result['messages'][] = array(
    'message' => "Copied {$count} availability records.",
    'type' => 'status',
  );
}
function ac_update_create_field_instances(&$result) {
  $old_states = availability_calendars_get_states();
  $current_states = availability_calendar_get_states();

  // Set up the allowed states. An empty array means that all states are allowed
  // which is the case if the count of old and new states are equal.
  $allowed_states = array();
  if (count($old_states) !== count($current_states)) {
    foreach ($current_states as $sid => $state) {
      if (array_key_exists($state['css_class'], $old_states)) {
        $allowed_states[$sid] = $state;
      }
    }
  }

  // Get old settings and copy as many settings as possible to the new fields.
  $settings = availability_calendars_get_settings();

  // Convert defaultstatus to sid.
  $default_state = 1;
  foreach ($current_states as $current_state) {
    if ($current_state['css_class'] === $settings->defaultstatus) {
      $default_state = $current_state['sid'];
      break;
    }
  }
  $c = 0;
  $supported_content_types = variable_get('availability_calendars_settings_content_types', array());
  foreach ($supported_content_types as $supported_content_type) {

    // Create field. We create 1 field per supported content type as this allows
    // the user to define different settings per bundle.
    // If a field name already exists, we skip it.
    do {
      $c++;
      $field_name = 'field_availability' . ($c === 1 ? '' : $c);
    } while (field_read_field($field_name) !== FALSE);
    $field = field_create_field(array(
      'field_name' => $field_name,
      'type' => 'availability_calendar',
      'settings' => array(
        'allocation_type' => $settings->splitday ? AC_ALLOCATION_TYPE_OVERNIGHT : AC_ALLOCATION_TYPE_FULLDAY,
        'allowed_states' => $allowed_states,
        'default_state' => $default_state,
      ),
    ));
    $result['messages'][] = array(
      'message' => "Created Availability Calendar field <em>{$field_name}</em>.",
      'type' => 'status',
    );

    // Create field instance based on created field.
    $instance = array(
      'label' => t('Availability Calendar'),
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $supported_content_type,
      'settings' => array(
        'allow_disable' => 1,
        'add_name' => 1,
      ),
      'widget' => array(
        'type' => 'availability_calendar',
        'settings' => array(
          'show_number_of_months' => $settings->editormonthcount,
          'first_day_of_week' => ($settings->startofweek + 4) % 7 + 1,
          // old: 1 = saturday, new: 1 = monday (ISO8601)
          'show_week_number' => 0,
          'show_only_first_letter' => $settings->firstletter,
          'show_split_day' => $settings->splitday,
        ),
      ),
      'display' => array(
        'default' => array(
          'type' => 'availability_calendar',
          'settings' => array(
            'show_number_of_months' => $settings->monthcount,
            'first_day_of_week' => ($settings->startofweek + 4) % 7 + 1,
            // old: 1 = saturday, new: 1 = monday (ISO8601)
            'show_week_number' => 0,
            'show_only_first_letter' => $settings->firstletter,
            'show_split_day' => $settings->splitday,
            'selectable' => 0,
          ),
        ),
      ),
    );
    if ($settings->showteaser) {
      $instance['display']['teaser'] = $instance['display']['default'];
      $instance['display']['teaser']['settings']['show_number_of_months'] = 2;
    }
    field_create_instance($instance);
    $result['messages'][] = array(
      'message' => "Created a field instance (of field <em>{$field_name}</em>) for content type <em>{$supported_content_type}</em>.",
      'type' => 'status',
    );
    $result['field instances'][$supported_content_type] = $field_name;
  }
}
function ac_update_create_field_data(&$result) {
  $supported_content_types = variable_get('availability_calendars_settings_content_types', array());
  foreach ($supported_content_types as $supported_content_type) {
    $field_name = $result['field instances'][$supported_content_type];
    $field_info = field_info_field($field_name);

    // Create the insert query for the field data table.
    $field_table_name = key($field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
    $field_cid = $field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT][$field_table_name]['cid'];
    $field_calendarname = $field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT][$field_table_name]['name'];
    $field_enabled = $field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT][$field_table_name]['enabled'];
    $language = field_is_translatable('node', $field_info) ? 'n.language' : "'und'";
    $query = "\nSELECT\n  'node' as entity_type,\n  n.type as bundle,\n  0 as deleted,\n  n.nid as entity_id,\n  n.nid as revision_id,\n  {$language} as language,\n  0 as delta,\n  1 as {$field_enabled},\n  '' as {$field_calendarname},\n  acc.cid as {$field_cid}\nFROM {node} n\n  INNER JOIN {availability_calendar_calendar} acc on acc.nid in (n.tnid, n.nid)\nWHERE n.type = '{$supported_content_type}'";
    $field_data_query = "INSERT INTO {{$field_table_name}} {$query}";
    $count = db_query($field_data_query)
      ->rowCount();
    $result['messages'][] = array(
      'message' => "Created {$count} fields (in table <em>{$field_table_name}</em>) for content type <em>{$supported_content_type}</em>.",
      'type' => 'status',
    );

    // Create the insert query for the field revision table: content is the same
    // as all fields start with 1 revision.
    $field_table_name = key($field_info['storage']['details']['sql'][FIELD_LOAD_REVISION]);
    $field_cid = $field_info['storage']['details']['sql'][FIELD_LOAD_REVISION][$field_table_name]['cid'];
    $field_enabled = $field_info['storage']['details']['sql'][FIELD_LOAD_REVISION][$field_table_name]['enabled'];
    $field_revision_query = "INSERT INTO {{$field_table_name}} {$query}";
    $count = db_query($field_revision_query)
      ->rowCount();
    $result['messages'][] = array(
      'message' => "Created {$count} revisions (in table <em>{$field_table_name}</em>) for content type <em>{$supported_content_type}</em>.",
      'type' => 'status',
    );
  }
}

Functions

Namesort descending Description
ac_update_add_field_nid
ac_update_check_prerequisites
ac_update_copy_availability
ac_update_copy_data
ac_update_copy_states
ac_update_copy_style_settings
ac_update_create_calendars
ac_update_create_field_data
ac_update_create_field_instances
ac_update_current_step_state
ac_update_drop_field_nid
ac_update_form0 Retrieve form callback for the admin/config/content/availability-calendars/update page.
ac_update_form1 Retrieve form callback for the admin/config/content/availability-calendars/update1 page.
ac_update_form2 Retrieve form callback for the admin/config/content/availability-calendars/update/2 page.
ac_update_form3 Retrieve form callback for the admin/config/content/availability-calendars/update/3 page.
ac_update_form4
ac_update_form_submit
ac_update_init @file Availability Calendars update: admin update code
ac_update_menu
ac_update_permission Implements hook_permission().
ac_update_remove_split_states