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')
);
$query = 'SELECT u.uid, u.name, u.status FROM {users} u WHERE uid != 0';
$status = array(t('blocked'), t('active'));
$destination = drupal_get_destination();
$filter = $_SESSION['gallery_user_filter'];
if (!_gallery_init(TRUE)) {
return '';
}
list($ret, $g2_admin) = GalleryCoreApi::isUserInSiteAdminGroup();
if ($ret) {
gallery_error(t('Error calling \'GalleryCoreApi::isUserInSiteAdminGroup\'.'), $ret);
}
if (isset($filter)) {
$result = db_query($query);
}
else {
$query .= tablesort_sql($header);
$result = pager_query($query, 50);
}
while ($user = db_fetch_object($result)) {
$g2_userinfo = gallery_user_map_info(user_load(array('uid' => $user->uid)), FALSE);
$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 ($g2_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 (count($g2_userinfo['status'])) {
$operations[] = l(t('sync'), 'admin/user/gallery/users/sync/'. $user->uid, array(), drupal_get_destination());
}
if (isset($filter)) {
if ($filter == GALLERY_USERINFO_ERROR) {
if (!count($g2_userinfo['status'])) {
continue;
}
}
elseif (!in_array($filter, $g2_userinfo['status'])) {
continue;
}
}
$rows[] = array($user->uid,
$g2_id,
theme_username($user),
$status[$user->status],
implode(',
', gallery_user_map_info_status($g2_userinfo['status'])),
implode(' | ', $operations));
}
if ($filter && !count($rows)) {
$rows[] = array(array('data' => t('There are no users with the selected status.'), 'colspan' => '6', 'align' => 'center', 'class' => 'message'));
}
$output = drupal_get_form('_gallery_user_filter_form', $filter);
$output .= theme('table', $header, $rows);
$output .= theme('pager', array(), 50);
GalleryEmbed::done();
return $output;
}
/**
* Function _gallery_user_filter_form().
* (filter form for user status)
*/
function _gallery_user_filter_form($filter) {
$form['filter'] = array(
'#type' => 'fieldset',
'#title' => t('Filter by status'),
'#collapsible' => TRUE,
'#collapsed' => !$filter,
);
$filter_options = gallery_user_map_info_status(array(), FALSE);
unset($filter_options[GALLERY_USERINFO_NOERROR]);
$form['filter']['filter_status'] = array(
'#type' => 'select',
'#title' => t('Show only users with status'),
'#options' => $filter_options,
'#default_value' => $filter,
);
$form['filter']['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
if ($filter) {
$form['filter']['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
}
$form['#submit']['_gallery_user_filter_submit'] = array();
return $form;
}
/**
* Function _gallery_user_filter_submit().
* (submit handler of the filter form)
*/
function _gallery_user_filter_submit($form_id, $form_values) {
unset($_SESSION['gallery_user_filter']);
if ($form_values['op'] == t('Filter')) {
$_SESSION['gallery_user_filter'] = $form_values['filter_status'];
}
}
/**
* Function _gallery_user_advanced().
* (advanced user administration)
*/
function _gallery_user_advanced() {
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced Sync (Batch operations)'),
'#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' => '
',
);
$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_values) {
if (($form_values['gallery_user_advanced_import'] + $form_values['gallery_user_advanced_sync']) < 1) {
form_set_error('', t('No option selected.'));
}
}
/**
* Function _gallery_user_advanced_submit().
*/
function _gallery_user_advanced_submit($form_id, $form_values) {
if ($form_values['op'] == t('Start')) {
if ($form_values['gallery_user_progress_offline']) {
variable_set('site_offline', 1);
$_SESSION['gallery_user_progress_offline'] = TRUE;
}
$tasks = array();
if ($form_values['gallery_user_advanced_import']) {
$tasks[] = 'import';
}
if ($form_values['gallery_user_advanced_sync']) {
$tasks[] = 'sync';
}
_gallery_user_advanced_start($tasks);
}
}
/**
* 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) {
// Flush entity cache
gallery_flush_entity_cache();
// Import Gallery2 groups
if (variable_get('gallery_user_import_groups', 1)) {
_gallery_groups_import();
}
}
// Fetch a list of G2 users
list($ret, $g2_users) = GalleryCoreApi::fetchUsernames(GALLERY_BATCH_INTERVAL, $position);
if ($ret) {
gallery_error(t('Error fetching G2 usernames'), $ret);
return 0;
}
else {
if (!_gallery_user_import($g2_users)) {
return 0;
}
}
$position += GALLERY_BATCH_INTERVAL;
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, GALLERY_BATCH_INTERVAL);
while ($user = db_fetch_object($result)) {
if ($account = user_load(array('uid' => $user->uid))) {
if (!gallery_user_modify($account, 'update', !$position)) {
return 0;
}
}
}
$position += GALLERY_BATCH_INTERVAL;
return ( 100 * $position ) / $total->users;
}
/**
* Function _gallery_user_advanced_start().
*/
function _gallery_user_advanced_start($mode, $redirect = NULL) {
$_SESSION['gallery_user_progress_mode'] = $mode;
$_SESSION['gallery_user_progress_position'] = 0;
$_SESSION['gallery_user_progress_messages'] = array();
$_SESSION['gallery_user_progress_redirect'] = $redirect;
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'];
$messages = $_SESSION['gallery_user_progress_messages'];
$redirect = $_SESSION['gallery_user_progress_redirect'];
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (!_gallery_init(TRUE, NULL, FALSE)) {
$percent = 0;
drupal_set_message(t('Unable to initialize embedded Gallery.'), 'error');
}
else {
switch (reset($mode)) {
case 'import':
$status = t('Importing Gallery2 users into Drupal ...');
$percent = _gallery_user_advanced_import($position);
break;
case 'sync':
$status = t('Synchronizing Drupal and Gallery2 users ...');
$percent = _gallery_user_advanced_sync($position);
break;
default:
$status = t('Performing batch operation ...');
$percent = $position++;
}
GalleryEmbed::done();
}
// Operation finished or error occured
if ($percent >= 100 || !$percent) {
array_shift($mode);
if (count($mode) && $percent) {
_gallery_user_advanced_start($mode, $redirect);
}
if ($_SESSION['gallery_user_progress_offline']) {
variable_set('site_offline', 0);
unset($_SESSION['gallery_user_progress_offline']);
}
if (count($_SESSION['gallery_user_progress_messages'])) {
drupal_set_message(theme('item_list', $_SESSION['gallery_user_progress_messages'], t('The following messages occured:')), 'notice');
drupal_set_message(''. t('Invalid user items were skipped.') .'', 'notice');
watchdog('gallery', theme('item_list', $_SESSION['gallery_user_progress_messages'], t('The following messages occured:')), WATCHDOG_NOTICE);
}
unset($_SESSION['gallery_user_progress_mode']);
unset($_SESSION['gallery_user_progress_position']);
unset($_SESSION['gallery_user_progress_messages']);
if ($percent) {
drupal_set_message(t('User synchronization successfully completed.'));
}
else {
drupal_set_message(t('User synchronization (partially) failed.'), 'error');
}
if ($redirect) {
unset($_SESSION['gallery_user_progress_redirect']);
drupal_goto($redirect);
}
drupal_goto('admin/user/gallery/advanced');
}
$_SESSION['gallery_user_progress_position'] = $position;
}
drupal_set_title('Drupal <> Gallery2');
drupal_set_html_head('');
print theme('gallery_user_progress_page', theme('progress_bar', round($percent, 1), $status), FALSE);
exit();
}
/**
* Theme function : theme_gallery_user_progress_page().
*/
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('');
$css = array('all' => array('module' => array('misc/maintenance.css' => FALSE, 'modules/system/system.css' => FALSE)));
$output = "\n";
$output .= '';
$output .= '