/[drupal]/contributions/modules/import/examples/import_user/import_user.install
ViewVC logotype

Diff of /contributions/modules/import/examples/import_user/import_user.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Fri Oct 31 17:51:23 2008 UTC revision 1.2, Wed Apr 8 21:42:11 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: import_user.install,v 1.1 2008/10/31 17:51:23 cyberswat Exp $
3    
4    /**
5     * @file
6     * The install file for the import_user example.  This module should not be used
7     * on a production installation of Drupal ... it is for illustration purposes
8     * only.  The install hook removes all traces of what was created by this module
9     * and could potentially be destructive on a production site.
10     */
11    
12  /**  /**
13   * Implementation of hook_install().   * Implementation of hook_install().
# Line 7  Line 15 
15   * Remove the variables, nodes and schema corresponding to the module.   * Remove the variables, nodes and schema corresponding to the module.
16   */   */
17  function import_user_install() {  function import_user_install() {
18      // Create this variable to indicate during cron that our import has not been
19      // staged.
20    define('IMPORT_USER_STAGED', FALSE);    define('IMPORT_USER_STAGED', FALSE);
21    
22      // Create tables.
23      drupal_install_schema('import_user');
24      _import_user_insert_data();
25    
26  }  }
27    
28  /**  /**
29   * Implementation of hook_uninstall().   * Implementation of hook_uninstall().
30   *   *
31   * Remove the variables, nodes and schema corresponding to the module.   * Remove the variables, nodes and schema corresponding to the module.
32   */   */
33  function import_user_uninstall() {  function import_user_uninstall() {
34      drupal_uninstall_schema('import_user');
35      _import_user_delete_data();
36    }
37    
38    /**
39     * Implementation of hook_schema().
40     */
41    function import_user_schema() {
42      $schema['import_example_user'] = array(
43        'description' => t('Example table for a user import.'),
44        'fields' => array(
45          'id' => array(
46            'description' => t('Primary key: ID'),
47            'type' => 'serial',
48          ),
49          'email' => array(
50            'description' => t('The users email address.'),
51            'type' => 'varchar',
52            'length' => 255,
53            'not null' => FALSE,
54            'default' => '',
55          ),
56          'name' => array(
57            'description' => t('The users name.'),
58            'type' => 'varchar',
59            'length' => 255,
60            'not null' => FALSE,
61            'default' => '',
62          ),
63          'random_username' => array(
64            'description' => t('A random user name.'),
65            'type' => 'varchar',
66            'length' => 255,
67            'not null' => FALSE,
68            'default' => '',
69          ),
70          'random_password' => array(
71            'description' => t('A random password.'),
72            'type' => 'varchar',
73            'length' => 255,
74            'not null' => FALSE,
75            'default' => '',
76          ),
77        ),
78        'primary key' => array('id'),
79      );
80      return $schema;
81    }
82    
83    function _import_user_insert_data() {
84      $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')";
85    
86      // insert 100 random users to be imported
87      _import_user_generate_random_user(100);
88    
89      // Create example profile fields
90      db_query($sql_profile_field, 'Name', 'profile_name', '', 'Personal Information', 'textfield', 0, 0, 0, 2, 0, '', '');
91      db_query($sql_profile_field, 'Original ID', 'profile_original_id', '', 'Hidden Information', 'textfield', 0, 0, 0, 4, 0, '', '');
92    
93    }
94    
95    function _import_user_random_string() {
96      return preg_replace("/([0-9])/e", "chr((\\1+112))", rand());
97    }
98    
99    /**
100     * Helper function to insert random users
101     * @param
102     *   $count integer The number of random users to generate
103     * @param
104     *   $fail boolean Indicates if account should be valid or invalid for purposes
105     *   of testing import_pass and import_fail
106     */
107    function _import_user_generate_random_user($count) {
108      $sql_insert = "INSERT INTO {import_example_user} (email, name, random_username, random_password) VALUES('%s', '%s', '%s', '%s')";
109      $sql_username_check = "SELECT COUNT(id) AS total FROM {import_example_user} WHERE name = '%s'";
110      while ($count > 0) {
111        $name = ucwords(_import_user_random_string() .' '. _import_user_random_string());
112        $email = _import_user_random_string() .'@'. _import_user_random_string() .'.com';
113        $username = _import_user_random_string();
114        $password = _import_user_random_string();
115        $user_count = db_fetch_object(db_query($sql_username_check, $username));
116        if ($user_count->total == 0) {
117          db_query($sql_insert, $email, $name, $username, $password);
118          $count--;
119        }
120      }
121    }
122    
123    /**
124     * Helper function to clean up data created by this module
125     */
126    function _import_user_delete_data() {
127      // Clean up the example information that was used for this import.
128      $sql_profile_field = "SELECT fid FROM {profile_fields} WHERE title = '%s' AND name = '%s' AND category = '%s' AND type = '%s'";
129      $name_field = db_fetch_object(db_query($sql_profile_field, 'Name', 'profile_name', 'Personal Information', 'textfield'));
130      $original_id_field = db_fetch_object(db_query($sql_profile_field, 'Original ID', 'profile_original_id', 'Hidden Information', 'textfield'));
131      db_query("DELETE FROM {profile_fields} WHERE fid = %d", $name_field->fid);
132      db_query("DELETE FROM {profile_fields} WHERE fid = %d", $original_id_field->fid);
133      db_query("DELETE FROM {profile_values} WHERE fid = %d", $name_field->fid);
134      db_query("DELETE FROM {profile_values} WHERE fid = %d", $original_id_field->fid);
135      db_query("DELETE FROM {import} WHERE type = '%s'", 'user');
136    
137      // Clean up users that were created during this import
138      $imported_users = db_query("SELECT uid FROM {users} WHERE data LIKE '%%%s%%'", 'profile_original_id');
139      while ($account = db_fetch_object($imported_users)) {
140        user_delete(array(), $account->uid);
141      }
142    
143      // delete from import tables
144      db_query("DELETE FROM {import_pass} WHERE type = '%s'", 'user');
145      db_query("DELETE FROM {import_pass} WHERE type = '%s'", 'user');
146      db_query("DELETE FROM {import_fail} WHERE type = '%s'", 'user');
147    
148      // remove the variable used for staging
149    variable_del('import_user_staged');    variable_del('import_user_staged');
150  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2