Newer
Older
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
<?php
// $Id$
/**
* gallery.module : gallery_user_admin.inc
* Gallery User Administration
*/
define(G2BATCH_INTERVAL, 25);
require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
/**
* Gallery Users Page - view list of users
*/
function _gallery_user_users($args) {
// sync the selected user
if ($args && is_numeric($args)) {
_gallery_user_sync($args);
drupal_goto('admin/user/gallery/users');
}
// generate user status overview
$header = array(
array('data' => t('ID'), 'field' => 'u.uid', 'sort' => 'asc'),
array('data' => t('G2ID')),
array('data' => t('Username'), 'field' => 'u.name'),
array('data' => t('Status'), 'field' => 'u.status'),
array('data' => t('Sync Status')),
t('Operations')
);
$sql = 'SELECT u.uid, u.name, u.status, u.mail, u.pass FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
$destination = drupal_get_destination();
if (!_gallery_init(TRUE)) {
return '';
}
list($ret, $admin) = GalleryCoreApi::isUserInSiteAdminGroup();
if ($ret) {
gallery_error(t('Error calling \'GalleryCoreApi::isUserInSiteAdminGroup\'.'), $ret);
}
while ($user = db_fetch_object($result)) {
$g2_userinfo = gallery_user_map_info($user);
$g2_id = ($g2_userinfo['g2_id'] >= 0) ? $g2_userinfo['g2_id'] : t('N/A');
$operations = array(l(t('edit'), "user/$user->uid/edit", array(), $destination));
if ($admin && ($g2_userinfo['g2_id'] > 0)) {
$link_url = gallery_generate_url(array('view' => 'core.SiteAdmin',
'subView' => 'core.AdminEditUser',
'userId' => $g2_userinfo['g2_id']), FALSE);
$operations[] = l(t('edit G2'), $link_url);
}
if (!$g2_userinfo['sync_ok']) {
$operations[] = l(t('sync'), 'admin/user/gallery/users/sync/'. $user->uid);
}
$rows[] = array($user->uid,
$g2_id,
theme_username($user),
$status[$user->status],
implode(',<br />', $g2_userinfo['error_msg']),
implode(' | ', $operations));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', array(), 50);
GalleryEmbed::done();
return $output;
}
/**
* Advanced User Administration
*/
function _gallery_user_advanced() {
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced Sync (Batch operations)'),
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
$form['advanced']['gallery_user_advanced_import'] = array(
'#type' => 'checkbox',
'#title' => t('Import users from Gallery2'),
'#return_value' => 1,
'#default_value' => FALSE,
'#description' => t('Use this option to import users from an existing Gallery2 install into Drupal.')
);
$form['advanced']['gallery_user_advanced_sync'] = array(
'#type' => 'checkbox',
'#title' => t('Synchronize (export) all existing users'),
'#return_value' => 1,
'#default_value' => FALSE,
'#description' => t('Use this option to sync all users between Drupal and Gallery2.')
);
$form['advanced']['gallery_user_advanced_offline'] = array(
'#type' => 'checkbox',
'#title' => t('Switch Drupal to \'offline mode\' for operation'),
'#return_value' => 1,
'#default_value' => FALSE,
'#disabled' => (user_access('administer site configuration') && !variable_get('site_offline', 0)) ? FALSE : TRUE,
'#prefix' => '<br />'
);
$form['buttons']['start'] = array('#type' => 'submit', '#value' => t('Start'));
$form['#base'] = '_gallery_user_advanced';
return $form;
}
/**
* function _gallery_user_advanced_validate().
*/
function _gallery_user_advanced_validate($form_id, $form_data) {
$num = $form_data['gallery_user_advanced_import']
+ $form_data['gallery_user_advanced_sync'];
if ($num > 1) {
form_set_error('', t('More than one option selected.'));
}
else if ($num < 1) {
form_set_error('', t('No option selected.'));
}
}
/**
* function _gallery_user_advanced_submit().
*/
function _gallery_user_advanced_submit($form_id, $form_data) {
if ($form_data['op'] == t('Start')) {
if ($form_data['gallery_user_progress_offline']) {
variable_set('site_offline', 1);
$_SESSION['gallery_user_progress_offline'] = TRUE;
}
if ($form_data['gallery_user_advanced_import']) {
_gallery_user_advanced_start('import');
}
elseif ($form_data['gallery_user_advanced_sync']) {
_gallery_user_advanced_start('sync');
}
}
}
/**
* function _gallery_user_advanced_import().
*/
function _gallery_user_advanced_import(&$position) {
// get number of G2 users
list($ret, $total) = GalleryCoreApi::fetchUserCount();
if ($ret || !$total) {
gallery_error(t('Error getting number of G2 users'), $ret);
return 100;
}
// first pass
if (!$position) {
// empty externalIdMap
$ret = GalleryCoreApi::removeAllMapEntries('ExternalIdMap');
if ($ret) {
gallery_error(t('Error emptying \'ExternalIdMap\''), $ret);
return 0;
}
// and import Gallery2 groups
if (variable_get('gallery_user_import_groups', 1)) {
if (!_gallery_groups_import()) {
return 0;
}
}
}
// fetch a list of G2 users
list($ret, $g2_users) = GalleryCoreApi::fetchUsernames(G2BATCH_INTERVAL, $position);
if ($ret) {
gallery_error(t('Error fetching G2 usernames'), $ret);
return 0;
}
else {
_gallery_user_import($g2_users);
}
$position += G2BATCH_INTERVAL;
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
return ( 100 * $position ) / $total;
}
/**
* function _gallery_user_advanced_sync().
*/
function _gallery_user_advanced_sync(&$position) {
$total = db_fetch_object(db_query("SELECT COUNT(*) AS users FROM {users} WHERE uid > 0"));
if (!$total->users) {
return 100;
}
// empty externalIdMap in first pass
if (!$position && variable_get('gallery_user_sync_remap', 0)) {
$ret = GalleryCoreApi::removeAllMapEntries('ExternalIdMap');
if ($ret) {
gallery_error(t('Error emptying \'ExternalIdMap\''), $ret);
return 0;
}
}
// sync users
$result = db_query_range("SELECT uid FROM {users} WHERE uid > 0", $position, G2BATCH_INTERVAL);
while ($user = db_fetch_object($result)) {
if ($account = user_load(array('uid' => $user->uid))) {
gallery_user_modify($account, 'update');
}
}
$position += G2BATCH_INTERVAL;
return ( 100 * $position ) / $total->users;
}
/**
* function _gallery_user_advanced_start().
*/
function _gallery_user_advanced_start($mode) {
$_SESSION['gallery_user_progress_mode'] = $mode;
$_SESSION['gallery_user_progress_position'] = 0;
drupal_goto('admin/user/gallery/advanced_progress');
}
/**
* function _gallery_user_advanced_progress().
*/
function _gallery_user_advanced_progress() {
$mode = $_SESSION['gallery_user_progress_mode'];
$position = $_SESSION['gallery_user_progress_position'];
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (!_gallery_init(TRUE)) {
$percent = 0;
}
else {
switch ($mode) {
case 'import':
$msg = t('Importing Gallery2 users into Drupal ...');
$percent = _gallery_user_advanced_import($position);
break;
case 'sync':
$msg = t('Synchronizing Drupal and Gallery2 users ...');
$percent = _gallery_user_advanced_sync($position);
break;
default:
$msg = t('Performing batch operation ...');
$percent = $position++;
}
GalleryEmbed::done();
// operation finished or error occured
if ($percent >= 100 || !$percent) {
if ($_SESSION['gallery_user_progress_offline']) {
variable_set('site_offline', 0);
unset($_SESSION['gallery_user_progress_offline']);
}
unset($_SESSION['gallery_user_progress_mode']);
unset($_SESSION['gallery_user_progress_position']);
if ($percent) {
drupal_set_message(t('Operation successfully completed.'));
}
else {
drupal_set_message(t('Operation failed.'), 'error');
}
drupal_goto('admin/user/gallery/advanced');
return;
}
$_SESSION['gallery_user_progress_position'] = $position;
}
drupal_set_title('Drupal <> Gallery2');
drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL='. base_path() .'index.php?q=admin/user/gallery/advanced_progress">');
print theme('gallery_user_progress_page', theme('progress_bar', round($percent, 1), $msg), FALSE);
exit();
}
function theme_gallery_user_progress_page($content, $messages = TRUE, $partial = FALSE) {
drupal_set_header('Content-Type: text/html; charset=utf-8');
drupal_set_html_head('<link rel="shortcut icon" href="'. base_path() .'misc/favicon.ico" type="image/x-icon" />');
$css = array('all' => array('module' => array('misc/maintenance.css' => FALSE, 'modules/system/system.css' => FALSE)));
$output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
$output .= '<html xmlns="http://www.w3.org/1999/xhtml">';
$output .= '<head>';
$output .= ' <title>'. strip_tags(drupal_get_title()) .'</title>';
$output .= drupal_get_html_head();
$output .= drupal_get_css($css);
$output .= drupal_get_js();
$output .= '</head>';
$output .= '<body>';
$output .= '<h2>'. drupal_get_title() .'</h2>';
$output .= "\n<!-- begin content -->\n";
$output .= $content;
$output .= "\n<!-- end content -->\n";
$output .= '</body></html>';
return $output;
}
function _gallery_user_settings() {
$form['user'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
$roles = array(0 => t('none'));
$roles += user_roles(TRUE);
unset($roles[DRUPAL_AUTHENTICATED_RID]);
$form['user']['gallery_user_admin_role'] = array(
'#type' => 'select',
'#title' => t('Drupal \'admin\' role'),
'#default_value' => variable_get('gallery_user_admin_role', 0),
'#options' => $roles,
'#description' => t('Select the Drupal role equivalent to Gallery2\'s \'Site Admin\' group (or \'none\' to disable this feature). The roles \'anonymous\' and \'authenticated\' are not available for selection.'),
);
$form['user']['gallery_user_hide_profile'] = array(
'#type' => 'checkbox',
'#title' => t('Hide Gallery2 section in profiles'),
'#default_value' => variable_get('gallery_user_hide_profile', 0),
'#description' => t('Hide the Gallery2 section (i.e. Gallery2-Drupal Sync Status) on the user profile pages.'),
$form['user']['sync'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced Sync - Options'),
'#collapsible' => TRUE,
);
// import behaviour
$form['user']['sync']['import'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced Sync - Import'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
$form['user']['sync']['import']['gallery_user_import_groups'] = array(
'#type' => 'checkbox',
'#title' => t('Import Gallery2 groups'),
'#default_value' => variable_get('gallery_user_import_groups', 1),
'#description' => t('Import Gallery2 groups into Drupal roles (in addition to users).'),
);
$form['user']['sync']['import']['gallery_user_import_override'] = array(
'#type' => 'checkbox',
'#title' => t('Override existing Drupal users'),
'#default_value' => variable_get('gallery_user_import_override', 1),
'#description' => t('Replaces user details (password, email, ...) of existing Drupal users with Gallery2 imported values.'),
);
// sync behaviour
$form['user']['sync']['export'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced Sync - Synchronize / Export'),
'#collapsible' => FALSE,
'#collapsed' => FALSE
);
$form['user']['sync']['export']['gallery_user_sync_remap'] = array(
'#type' => 'checkbox',
'#title' => t('Remap all users'),
'#default_value' => variable_get('gallery_user_sync_remap', 0),
'#description' => t('Remaps all users instead of missing or mismatching ones only. This will completely flush Gallery2\'s \'externalIdMap\'.'),
);
return system_settings_form($form);
}
?>