You are here

import_user.install in Import 6

The install file for the import_user example. This module should not be used on a production installation of Drupal ... it is for illustration purposes only. The install hook removes all traces of what was created by this module and could potentially be destructive on a production site.

File

examples/import_user/import_user.install
View source
<?php

/**
 * @file
 * The install file for the import_user example.  This module should not be used
 * on a production installation of Drupal ... it is for illustration purposes
 * only.  The install hook removes all traces of what was created by this module
 * and could potentially be destructive on a production site.
 */

/**
 * Implementation of hook_install().
 *
 * Remove the variables, nodes and schema corresponding to the module.
 */
function import_user_install() {

  // Create this variable to indicate during cron that our import has not been
  // staged.
  define('IMPORT_USER_STAGED', FALSE);

  // Create tables.
  drupal_install_schema('import_user');
  _import_user_insert_data();
}

/**
 * Implementation of hook_uninstall().
 *
 * Remove the variables, nodes and schema corresponding to the module.
 */
function import_user_uninstall() {
  drupal_uninstall_schema('import_user');
  _import_user_delete_data();
}

/**
 * Implementation of hook_schema().
 */
function import_user_schema() {
  $schema['import_example_user'] = array(
    'description' => t('Example table for a user import.'),
    'fields' => array(
      'id' => array(
        'description' => t('Primary key: ID'),
        'type' => 'serial',
      ),
      'email' => array(
        'description' => t('The users email address.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'name' => array(
        'description' => t('The users name.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'random_username' => array(
        'description' => t('A random user name.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'random_password' => array(
        'description' => t('A random password.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}
function _import_user_insert_data() {
  $sql_profile_field = "INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, autocomplete, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', '%s')";

  // insert 100 random users to be imported
  _import_user_generate_random_user(10);

  // Create example profile fields
  db_query($sql_profile_field, 'Name', 'profile_name', '', 'Personal Information', 'textfield', 0, 0, 0, 2, 0, '', '');
  db_query($sql_profile_field, 'Original ID', 'profile_original_id', '', 'Hidden Information', 'textfield', 0, 0, 0, 4, 0, '', '');
}
function _import_user_random_string() {
  return preg_replace("/([0-9])/e", "chr((\\1+112))", rand());
}

/**
 * Helper function to insert random users
 * @param
 *   $count integer The number of random users to generate
 * @param
 *   $fail boolean Indicates if account should be valid or invalid for purposes
 *   of testing import_pass and import_fail
 */
function _import_user_generate_random_user($count) {
  $sql_insert = "INSERT INTO {import_example_user} (email, name, random_username, random_password) VALUES('%s', '%s', '%s', '%s')";
  $sql_username_check = "SELECT COUNT(id) AS total FROM {import_example_user} WHERE name = '%s'";
  while ($count > 0) {
    $name = ucwords(_import_user_random_string() . ' ' . _import_user_random_string());
    $email = _import_user_random_string() . '@' . _import_user_random_string() . '.com';
    $username = _import_user_random_string();
    $password = _import_user_random_string();
    $user_count = db_fetch_object(db_query($sql_username_check, $username));
    if ($user_count->total == 0) {
      db_query($sql_insert, $email, $name, $username, $password);
      $count--;
    }
  }
}

/**
 * Helper function to clean up data created by this module
 */
function _import_user_delete_data() {

  // Clean up the example information that was used for this import.
  $sql_profile_field = "SELECT fid FROM {profile_fields} WHERE title = '%s' AND name = '%s' AND category = '%s' AND type = '%s'";
  $name_field = db_fetch_object(db_query($sql_profile_field, 'Name', 'profile_name', 'Personal Information', 'textfield'));
  $original_id_field = db_fetch_object(db_query($sql_profile_field, 'Original ID', 'profile_original_id', 'Hidden Information', 'textfield'));
  db_query("DELETE FROM {profile_fields} WHERE fid = %d", $name_field->fid);
  db_query("DELETE FROM {profile_fields} WHERE fid = %d", $original_id_field->fid);
  db_query("DELETE FROM {profile_values} WHERE fid = %d", $name_field->fid);
  db_query("DELETE FROM {profile_values} WHERE fid = %d", $original_id_field->fid);

  // Clean up users that were created during this import
  $imported_users = db_query("SELECT uid FROM {users} WHERE data LIKE '%%%s%%'", 'profile_original_id');
  while ($account = db_fetch_object($imported_users)) {
    user_delete(array(), $account->uid);
  }

  // delete from import tables
  db_query("DELETE FROM {import} WHERE type = '%s'", 'user');
  db_query("DELETE FROM {import_pass} WHERE type = '%s'", 'user');
  db_query("DELETE FROM {import_fail} WHERE type = '%s'", 'user');

  // remove the variable used for staging
  variable_del('import_user_staged');
}

Functions

Namesort descending Description
import_user_install Implementation of hook_install().
import_user_schema Implementation of hook_schema().
import_user_uninstall Implementation of hook_uninstall().
_import_user_delete_data Helper function to clean up data created by this module
_import_user_generate_random_user Helper function to insert random users
_import_user_insert_data
_import_user_random_string