You are here

function realname_update_6102 in Real Name 6

Implementation of hook_update_N().

File

./realname.install, line 118
Handles installation and updates for the RealName module.

Code

function realname_update_6102() {
  $ret = array();
  $table = array(
    'module' => 'RealName',
    'description' => 'Computed RealNames to reduce overhead.',
    'fields' => array(
      'uid' => array(
        'description' => 'User ID, links to User table.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'realname' => array(
        'description' => 'Saved computed RealName.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
    ),
  );

  // Create new table.
  if (db_table_exists('realname')) {

    // Update is being re-run.
    drupal_uninstall_schema('realname');
  }
  db_create_table($ret, 'realname', $table);
  db_add_primary_key($ret, 'realname', array(
    'uid',
  ));
  db_add_index($ret, 'realname', 'realname', array(
    'realname',
  ));

  // Populate table using current settings.
  // @TODO: User processing can take quite long. We probably need to use BATCH API
  $module = variable_get('realname_profile_module', NULL);
  if ($module) {
    drupal_load('module', 'realname');
    $result = db_query("SELECT u.uid, u.name FROM {users} u");
    while ($row = db_fetch_object($result)) {
      $realname = _realname_make_name($row);
      db_query("INSERT INTO {realname} (uid, realname) VALUES(%d, '%s')", $row->uid, $realname);
    }
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Realnames have been recalculated'),
    );
  }
  else {
    drupal_set_message(t('The Realname module has been updated. In order to start using it, you have to select a profile module to be used.'), 'warning');
  }
  return $ret;
}