-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_import.drush.inc
117 lines (98 loc) · 3.62 KB
/
user_import.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* @file
* Drush integration for the User Import module.
*/
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_drush_help($command) {
switch ($command) {
case 'drush:user-import':
return dt('Queue an import of users from a CSV file');
case 'error:template-not-found':
return dt('Template not found');
case 'error:file-not-found':
return dt('File not found');
case 'error:no-default-template':
return dt('There is no default template configured');
}
}
/**
* Implements hook_drush_command().
*
* In this hook, you specify which commands your
* drush module makes available, what it does and
* description.
*
* Notice how this structure closely resembles how
* you define menu hooks.
*
* See `drush topic docs-commands` for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function user_import_drush_command() {
$commands = array();
$commands['user-import'] = array(
'description' => dt('Queue an import of users'),
'arguments' => array(
'file' => dt('The CSV file with user data'),
'template' => dt('Name of the template to use (optional, if not provided the default template is used)'),
),
'examples' => array(
dt('standard example') => 'drush user-import users.csv',
),
);
return $commands;
}
function drush_user_import($original_file = NULL, $template_name = NULL) {
if (!file_exists($original_file)) {
return drush_set_error('file-not-found');
}
$original_file = realpath($original_file);
if ($template_name) {
$template = db_query("SELECT * FROM {user_import} WHERE name = :name AND setting = 'template'", array(':name' => $template_name))->fetchObject();
if (!$template) {
return drush_set_error('template-not-found');
}
}
else {
$template_id = config_get('user_import.settings', 'default_settings');
if (!$template_id) {
return drush_set_error('no-default-template');
}
$template = db_query("SELECT * FROM {user_import} WHERE import_id = :import_id AND setting = 'template'", array(':import_id' => $template_id))->fetchObject();
if (!$template) {
return drush_set_error('template-not-found');
}
drush_print(dt('Using default settings template "!template"', array('!template' => $template->name)));
}
$template->options = unserialize($template->options);
$template->field_match = unserialize($template->field_match);
$template->roles = unserialize($template->roles);
$file = new stdClass();
$file->filepath = $original_file;
$file->filename = basename($original_file);
$destination = 'private://user_import/processing';
$filepath = file_unmanaged_copy($file->filepath, $destination, FILE_EXISTS_RENAME);
// initialize import from template
$import = new stdClass();
$import->oldfilename = basename($original_file);
$import->filename = $file->filename;
$import->filepath = $filepath;
$import->started = time();
$import->field_match = $template->field_match;
$import->roles = $template->roles;
$import->options = $template->options;
$import->setting = 'import';
$result = backdrop_write_record('user_import', $import);
if ($result == SAVED_NEW) {
drush_print(dt('Successfully queued user import. The original CSV file !file has been copied to the Backdrop installation and can be removed.', array('!file' => $original_file)));
}
else {
drush_set_error('import_failed', dt('Unable to queue the user import.'));
}
}