You are here

function library_update_6201 in Library 6.2

File

./library.install, line 197
@author Jess Straatmann library.install Install and uninstall all required databases. Also do incremental database updates.

Code

function library_update_6201() {
  $ret = array();
  if (module_exists('patron')) {
    $empty_patrons = db_result(db_query("SELECT COUNT(*) FROM {library_patrons} WHERE patron_uid IS NULL OR patron_uid = 0"));

    //don't attempt to do the upgrade if patrons aren't users
    if (variable_get('patron_is_user', 0) == 0 || $empty_patrons > 0) {
      drupal_set_message(t('This module update eliminates the patron module and replaces patrons with Drupal users. Please return to <a href="@patron-settings">patron settings</a> and check Associate Library Patrons with Drupal Users. Then, create associated users for all existing patrons with the same e-mail address used in the patron node. After resaving the associated patrons, try this update again.', array(
        '@patron-settings' => base_path() . 'admin/settings/library/patrons',
      )), 'warning', FALSE);
      $ret['#abort'] = array(
        'success' => FALSE,
        'query' => t('Library module has updates, but updating would currently result in data loss.'),
      );
      return $ret;
    }
    $uids = db_result(db_query("SELECT COUNT(*) FROM {library_patrons} WHERE patron_uid IS NOT NULL"));

    // only import patron data if patrons exist
    if ($uids > 0) {
      if (!module_exists('profile')) {

        //enable the core profile module to store the patron information
        drupal_install_modules(array(
          'profile',
        ));
      }
      $ret[] = update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('First name','profile_patron_name_first','Imported from the patron module.', 'Library', 'textfield', '', 1)");
      $ret[] = update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('Last name','profile_patron_name_last','Imported from the patron module.', 'Library', 'textfield', '', 1)");
      $ret[] = update_sql("INSERT INTO {profile_fields} (title, name, explanation, category, type, page, visibility) VALUES ('Barcode','profile_patron_barcode', 'Imported from the patron module.', 'Library', 'textfield', '', 1)");
    }
    cache_clear_all();
    menu_rebuild();
  }
  return $ret;
}