You are here

function mailchimp_lists_update_7205 in Mailchimp 7.2

Update list->settings with new values, remove list_type data and rebuild the registry.

File

modules/mailchimp_lists/mailchimp_lists.install, line 355
Install, update and uninstall functions for the mailchimp_lists module.

Code

function mailchimp_lists_update_7205() {

  // Rebuild the registry to reflect the updated module structure.
  registry_rebuild();
  $lists = db_select('mailchimp_lists', 'm')
    ->fields('m', array(
    'id',
    'list_type',
    'settings',
  ))
    ->execute()
    ->fetchAll();
  if (empty($lists)) {
    return t('There are no lists to update.');
  }
  $success = TRUE;
  foreach ($lists as $list) {
    $settings = unserialize($list->settings);
    $result = TRUE;
    if (isset($list->list_type)) {
      switch ($list->list_type) {
        case MAILCHIMP_LISTTYPE_FREEFORM:
          $settings['required'] = FALSE;
          $settings['allow_anonymous'] = TRUE;
          $settings['roles'][DRUPAL_ANONYMOUS_RID] = TRUE;
          if (isset($settings['mergefields'])) {
            $settings['mergefields_display'] = array();
            foreach ($settings['mergefields'] as $id => $field) {
              $settings['mergefields_display'][$id] = 'TRUE';
            }
          }
          break;
        case MAILCHIMP_LISTTYPE_OPTIONAL:
          $settings['required'] = FALSE;
          $settings['allow_anonymous'] = FALSE;
          break;
        case MAILCHIMP_LISTTYPE_REQUIRED:
          $settings['required'] = TRUE;
          $settings['allow_anonymous'] = FALSE;
          break;
      }

      // This is where we would delete the list_type variable if we deemed it a
      // good idea. It is minimal cruft, however, and allows for back-revving
      // one's Mailchimp install more easily.
      $settings = serialize($settings);
      $result = db_update('mailchimp_lists')
        ->fields(array(
        'settings' => $settings,
      ))
        ->condition('id', $list->id)
        ->execute();
    }
    $success = $result && $success;
  }
  unset($lists);
  if ($success) {
    db_drop_field('mailchimp_lists', 'list_type');
    drupal_get_schema('mailchimp_lists', TRUE);
    $t = get_t();
    return $t('MailChimp lists have been updated with new settings.');
  }
  else {
    throw new DrupalUpdateException('There was a problem updating your Mailchimp Lists configuration. Either roll back your database or go to the Mailchimp Lists configuration page and re-configure each list individually.');
  }
}