[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/gallery/ -> gallery_user_admin.inc (source)

   1  <?php
   2  // $Id: gallery_user_admin.inc,v 1.3.2.3 2008/02/11 01:11:12 profix898 Exp $
   3  
   4  require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
   5  
   6  /**
   7   * gallery.module : gallery_user_admin.inc
   8   * Gallery User Administration
   9   */
  10   
  11  define('GALLERY_BATCH_INTERVAL', 10);
  12  
  13  /**
  14   * Function _gallery_user_users().
  15   * (gallery users page - view list of users)
  16   */
  17  function _gallery_user_users() {
  18    // Generate user status overview
  19    $header = array(
  20      array('data' => t('ID'), 'field' => 'u.uid', 'sort' => 'asc'),
  21      array('data' => t('G2ID')),
  22      array('data' => t('Username'), 'field' => 'u.name'),
  23      array('data' => t('Status'), 'field' => 'u.status'),
  24      array('data' => t('Sync Status')),
  25      t('Operations')
  26    );
  27    $query = 'SELECT u.uid, u.name, u.status FROM {users} u WHERE uid != 0';
  28  
  29    $status = array(t('blocked'), t('active'));
  30    $destination = drupal_get_destination();
  31    $filter = isset($_SESSION['gallery_user_filter']) ? $_SESSION['gallery_user_filter'] : FALSE;
  32  
  33    if (!_gallery_init(TRUE)) {
  34      return '';
  35    }
  36    list($ret, $g2_admin) = GalleryCoreApi::isUserInSiteAdminGroup();
  37    if ($ret) {
  38      gallery_error(t('Error calling \'GalleryCoreApi::isUserInSiteAdminGroup\'.'), $ret);
  39    }
  40    
  41    if ($filter) {
  42      $result = db_query($query);
  43    }
  44    else {
  45      $query .= tablesort_sql($header);
  46      $result = pager_query($query, 50);
  47    }
  48    
  49    $rows = array();
  50    while ($user = db_fetch_object($result)) {
  51      $g2_userinfo = gallery_user_map_info(user_load(array('uid' => $user->uid)), FALSE);
  52      
  53      $g2_id = ($g2_userinfo['g2_id'] >= 0) ? $g2_userinfo['g2_id'] : t('N/A');
  54  
  55      $operations = array(l(t('edit'), "user/$user->uid/edit", array(), $destination));
  56  
  57      if ($g2_admin && ($g2_userinfo['g2_id'] > 0)) {
  58        $link_url = gallery_generate_url(array('view' => 'core.SiteAdmin',
  59                                                 'subView' => 'core.AdminEditUser',
  60                                                 'userId' => $g2_userinfo['g2_id']), FALSE);
  61        $operations[] = l(t('edit G2'), $link_url);
  62      }
  63  
  64      if (count($g2_userinfo['status'])) {
  65        $operations[] = l(t('sync'), 'admin/user/gallery/users/sync/'. $user->uid, array(), drupal_get_destination());
  66      }
  67      
  68      if ($filter) {
  69        if ($filter == GALLERY_USERINFO_ERROR) {
  70          if (!count($g2_userinfo['status'])) {
  71            continue;
  72          }
  73        }
  74        elseif (!in_array($filter, $g2_userinfo['status'])) {
  75          continue;
  76        }    
  77      }
  78  
  79      $rows[] = array($user->uid,
  80                      $g2_id,
  81                      theme_username($user),
  82                      $status[$user->status],
  83                      implode(',<br />', gallery_user_map_info_status($g2_userinfo['status'])),
  84                      implode(' | ', $operations));
  85    }
  86    
  87    if ($filter && !count($rows)) {
  88      $rows[] = array(array('data' => t('There are no users with the selected status.'), 'colspan' => '6', 'align' => 'center', 'class' => 'message'));
  89    }
  90    
  91    $output  = drupal_get_form('_gallery_user_filter_form', $filter);
  92    $output .= theme('table', $header, $rows);
  93    $output .= theme('pager', array(), 50);
  94  
  95    GalleryEmbed::done();
  96    return $output;
  97  }
  98  
  99  /**
 100   * Function _gallery_user_filter_form().
 101   * (filter form for user status)
 102   */
 103  function _gallery_user_filter_form($form_state, $filter) {
 104    $form['filter'] = array(
 105      '#type' => 'fieldset',
 106      '#title' => t('Filter by status'),
 107      '#collapsible' => TRUE,
 108      '#collapsed' => !$filter,
 109    );
 110    $filter_options = gallery_user_map_info_status(array(), FALSE);
 111    unset($filter_options[GALLERY_USERINFO_NOERROR]);
 112    $form['filter']['filter_status'] = array(
 113      '#type' => 'select',
 114      '#title' => t('Show only users with status'),
 115      '#options' => $filter_options,
 116      '#default_value' => $filter,
 117    );
 118    $form['filter']['buttons']['submit'] = array(
 119      '#type' => 'submit',
 120      '#value' => t('Filter'),
 121      '#submit' => array('_gallery_user_filter_submit')
 122    );
 123    if ($filter) {
 124      $form['filter']['buttons']['reset'] = array(
 125        '#type' => 'submit',
 126        '#value' => t('Reset'),
 127        '#submit' => array('_gallery_user_filter_reset')
 128      );
 129    }
 130    
 131    return $form;
 132  }
 133  
 134  /**
 135   * Function _gallery_user_filter_submit().
 136   */
 137  function _gallery_user_filter_submit($form, &$form_state) {
 138    $_SESSION['gallery_user_filter'] = $form_state['values']['filter_status'];
 139  }
 140  
 141  /**
 142   * Function _gallery_user_filter_reset().
 143   */
 144  function _gallery_user_filter_reset($form, &$form_state) {
 145    unset($_SESSION['gallery_user_filter']);
 146  }
 147  
 148  /**
 149   * Function _gallery_user_users_sync().
 150   */
 151  function _gallery_user_users_sync($uid = NULL) {
 152    // Sync the selected user
 153    if (isset($uid) && is_numeric($uid)) {
 154      _gallery_user_sync($uid);
 155    }
 156  
 157    drupal_goto('admin/user/gallery/users');
 158  }
 159  
 160  /**
 161   * Function _gallery_user_advanced().
 162   * (advanced user administration)
 163   */
 164  function _gallery_user_advanced() {
 165    $form['advanced'] = array(
 166      '#type' => 'fieldset',
 167      '#title' => t('Advanced Sync (Batch operations)'),
 168      '#collapsible' => FALSE,
 169      '#collapsed' => FALSE,
 170    );
 171    $form['advanced']['gallery_user_advanced_import'] = array(
 172      '#type' => 'checkbox',
 173      '#title' => t('Import users from Gallery2'),
 174      '#return_value' => 1,
 175      '#default_value' => FALSE,
 176      '#description' => t('Use this option to import users from an existing Gallery2 install into Drupal.'),
 177    );
 178    $form['advanced']['gallery_user_advanced_sync'] = array(
 179      '#type' => 'checkbox',
 180      '#title' => t('Synchronize (export) all existing users'),
 181      '#return_value' => 1,
 182      '#default_value' => FALSE,
 183      '#description' => t('Use this option to sync all users between Drupal and Gallery2.'),
 184    );
 185    
 186    $form['advanced']['gallery_user_advanced_offline'] = array(
 187      '#type' => 'checkbox',
 188      '#title' => t('Switch Drupal to \'offline mode\' for operation'),
 189      '#return_value' => 1,
 190      '#default_value' => FALSE,
 191      '#disabled' => (user_access('administer site configuration') && !variable_get('site_offline', 0)) ? FALSE : TRUE,
 192      '#prefix' => '<br />',
 193    );
 194    
 195    $form['buttons']['start'] = array('#type' => 'submit', '#value' => t('Start'));
 196    $form['#validate'] = array('_gallery_user_advanced_validate');
 197    $form['#submit'] = array('_gallery_user_advanced_submit');
 198    return $form;
 199  }
 200  
 201  /**
 202   * Function _gallery_user_advanced_validate().
 203   */
 204  function _gallery_user_advanced_validate($form, &$form_state) {
 205    if (($form_state['values']['gallery_user_advanced_import'] + $form_state['values']['gallery_user_advanced_sync']) < 1) {
 206      form_set_error('', t('No option selected.'));
 207    }
 208  }
 209  
 210  /**
 211   * Function _gallery_user_advanced_submit().
 212   */
 213  function _gallery_user_advanced_submit($form, &$form_state) {
 214    if ($form_state['values']['gallery_user_advanced_offline']) {
 215      variable_set('site_offline', 1);
 216      $_SESSION['gallery_user_batch_offline'] = TRUE;
 217    }
 218  
 219    $operations = array();
 220    if ($form_state['values']['gallery_user_advanced_import']) {
 221      $operations[] = array('_gallery_user_advanced_import', array());
 222    }
 223    if ($form_state['values']['gallery_user_advanced_sync']) {
 224      $operations[] = array('_gallery_user_advanced_sync', array());
 225    }
 226    
 227    $batch = array(
 228      'title' => t('User Synchronization'),
 229      'operations' => $operations,
 230      'file' => drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc',
 231      'finished' => '_gallery_user_advanced_finished'
 232    );
 233    
 234    batch_set($batch);
 235  }
 236  
 237  /**
 238   * Function _gallery_user_advanced_import().
 239   */
 240  function _gallery_user_advanced_import(&$context) {
 241    // Skip operation if an error occured
 242    if (isset($context['results']['error']) && $context['results']['error']) {
 243      $context['finished'] = $context['results']['error'] = 1;
 244      return;
 245    }
 246    // Initialize G2
 247    if (!_gallery_init(TRUE, NULL, FALSE)) {
 248      $context['finished'] = $context['results']['error'] = 1;
 249      return;
 250    }
 251    // First pass
 252    if (empty($context['sandbox'])) {
 253      // Initialize batch variables
 254      $context['finished'] = 0;
 255      $context['sandbox']['progress'] = 0;
 256      // Total number of G2 users
 257      list($ret, $context['sandbox']['max']) = GalleryCoreApi::fetchUserCount();
 258      if ($ret || !$context['sandbox']['max']) {
 259        gallery_error(t('Error getting number of G2 users'), $ret);
 260        $context['finished'] = $context['results']['error'] = 1;
 261        return;
 262      }
 263      // Flush entity cache
 264      gallery_flush_entity_cache();
 265      // Import Gallery2 groups
 266      if (variable_get('gallery_user_import_groups', 1)) {
 267        _gallery_groups_import();
 268      }
 269    }
 270    // Fetch a list of G2 users
 271    list($ret, $g2_users) = GalleryCoreApi::fetchUsernames(GALLERY_BATCH_INTERVAL, $context['sandbox']['progress']);
 272    if ($ret) {
 273      gallery_error(t('Error fetching G2 usernames'), $ret);
 274      $context['finished'] = $context['results']['error'] = 1;
 275      return;
 276    }
 277    else {
 278      if (!_gallery_user_import($g2_users, $context['results']['messages'])) {
 279        $context['finished'] = $context['results']['error'] = 1;
 280        return;
 281      }
 282    }
 283    $context['sandbox']['progress'] += GALLERY_BATCH_INTERVAL;
 284    $context['finished'] = 1;
 285    if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
 286      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
 287    }
 288  }
 289  
 290  /**
 291   * Function _gallery_user_advanced_sync().
 292   */
 293  function _gallery_user_advanced_sync(&$context) {
 294    // Skip operation if an error occured
 295    if (isset($context['results']['error']) && $context['results']['error']) {
 296      $context['finished'] = $context['results']['error'] = 1;
 297      return;
 298    }
 299    // Initialize G2
 300    if (!_gallery_init(TRUE, NULL, FALSE)) {
 301      $context['finished'] = $context['results']['error'] = 1;
 302      return;
 303    }
 304    // First pass
 305    if (empty($context['sandbox'])) {
 306      // Initialize batch variables
 307      $context['finished'] = 0;
 308      $context['sandbox']['progress'] = 0;
 309      // Total number of users
 310      $total = db_fetch_object(db_query("SELECT COUNT(*) AS users FROM {users} WHERE uid > 0"));
 311      if (!$total->users) {
 312        $context['finished'] = $context['results']['error'] = 1;
 313        return;
 314      }
 315      $context['sandbox']['max'] = $total->users;
 316      // Empty externalIdMap in first pass
 317      if (variable_get('gallery_user_sync_remap', 0)) {
 318        $ret = GalleryCoreApi::removeAllMapEntries('ExternalIdMap');
 319        if ($ret) {
 320          gallery_error(t('Error emptying \'ExternalIdMap\''), $ret);
 321          $context['finished'] = $context['results']['error'] = 1;
 322          return;
 323        }
 324      }
 325    }
 326    // Sync users
 327    $result = db_query_range("SELECT uid FROM {users} WHERE uid > 0", $context['sandbox']['progress'], GALLERY_BATCH_INTERVAL);
 328    while ($user = db_fetch_object($result)) {
 329      if ($account = user_load(array('uid' => $user->uid))) {
 330        if (!gallery_user_modify($account, 'update', !$context['sandbox']['progress'])) {
 331          $context['finished'] = $context['results']['error'] = 1;
 332          return;
 333        }
 334      }
 335    }
 336    $context['sandbox']['progress'] += GALLERY_BATCH_INTERVAL;
 337    $context['finished'] = 1;
 338    if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
 339      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
 340    }
 341  }
 342  
 343  /**
 344   * Function _gallery_user_advanced_finished().
 345   */
 346  function _gallery_user_advanced_finished($success, $results, $operations) {
 347    if (isset($_SESSION['gallery_user_batch_offline'])) {
 348      variable_set('site_offline', 0);
 349      unset($_SESSION['gallery_user_batch_offline']);
 350    }
 351    //
 352    if ($success) {
 353      if (isset($results['error']) && $results['error']) {
 354        drupal_set_message(t('User synchronization (partially) failed.'), 'error');
 355      }
 356      else {
 357        if (isset($results['messages']) && count($results['messages'])) {
 358          drupal_set_message(theme('item_list', $results['messages'], t('The following messages occured:')), 'notice');
 359          drupal_set_message('<strong>'. t('Invalid user items were skipped.') .'</strong>', 'notice');
 360          watchdog('gallery', theme('item_list', $results['messages'], t('The following messages occured:')));
 361        }
 362        else {
 363          drupal_set_message(t('User synchronization successfully completed.'));
 364        }
 365      }
 366    }
 367    else {
 368      $error_operation = reset($operations);
 369      drupal_set_message(t('An error occurred while processing @operation', array('@operation' => $error_operation[0])), 'error');
 370    }
 371  }
 372  
 373  /**
 374   * Function _gallery_user_settings().
 375   */
 376  function _gallery_user_settings() {
 377    require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
 378    
 379    $form['user'] = array(
 380      '#type' => 'fieldset',
 381      '#title' => t('Settings'),
 382      '#collapsible' => FALSE,
 383      '#collapsed' => FALSE
 384    );
 385    
 386    // General settings
 387    $form['user']['gallery_user_profile_hide'] = array(
 388      '#type' => 'checkbox',
 389      '#title' => t('Hide Gallery2 section in profiles'),
 390      '#default_value' => variable_get('gallery_user_profile_hide', 0),
 391      '#description' => t('Hide the Gallery2 section (i.e. Gallery2-Drupal Sync Status) on the user profile pages.'),
 392    );
 393    
 394    // Sync settings
 395    $form['user']['sync'] = array(
 396      '#type' => 'fieldset',
 397      '#title' => t('User Synchronization'),
 398      '#collapsible' => TRUE,
 399      '#collapsed' => FALSE
 400    );
 401    
 402    $roles  = array(0 => t('none'));
 403    $roles += user_roles(TRUE);
 404    unset($roles[DRUPAL_AUTHENTICATED_RID]);
 405    $form['user']['sync']['gallery_user_admin_role'] = array(
 406      '#type' => 'select',
 407      '#title' => t('Drupal \'admin\' role'),
 408      '#default_value' => variable_get('gallery_user_admin_role', 0),
 409      '#options' => $roles,
 410      '#description' => t('Select the Drupal role equivalent to Gallery2\'s \'Site Admin\' group (or \'none\' to disable this
 411                           feature). The roles \'anonymous\' and \'authenticated\' are not available for selection.'),
 412    );
 413    
 414    $form['user']['sync']['gallery_user_locked'] = array(
 415      '#type' => 'checkbox',
 416      '#title' => t('Lock G2 accounts'),
 417      '#default_value' => variable_get('gallery_user_locked', 0),
 418      '#description' => t('Locking G2 account prevents users from changing their details (password, email, ...) in G2.'),
 419    );
 420    
 421    // Fullname settings
 422    $profile_status = module_exists('profile');
 423    $profile_status_str = theme('gallery_module_status_message', $profile_status);
 424    $desc = t('Full names in Gallery2 can be supported by using the profile module
 425              (!profile_status) with a \'Full Name\' profile field as defined below.', 
 426              array('!profile_status' => $profile_status_str)
 427    );
 428    if (!$profile_status) {
 429      $desc .= t(' However the profile module is disabled, so this functionality is
 430        not available and the options are disabled.');
 431    }
 432    $form['user']['sync']['fullname'] = array(
 433      '#type' => 'fieldset',
 434      '#title' => t('Full Name settings'),
 435      '#collapsible' => TRUE,
 436      '#collapsed' => !$profile_status,
 437      '#description' => $desc,
 438    );
 439    if ($profile_status) {
 440      $usefullname = variable_get('gallery_use_fullname', 0);
 441      $form['user']['sync']['fullname']['gallery_use_fullname'] = array(
 442        '#type' => 'checkbox',
 443        '#title' => t('Enable Full Name support'),
 444        '#return_value' => 1,
 445        '#default_value' => $usefullname,
 446        '#disabled' => !$profile_status,
 447        '#description' => t('Use full name from profile module in Gallery2 user data. Note that
 448                             enabling/disabling this only updates Gallery2 user data when the
 449                             Drupal user is updated or if a user sync is performed.'),
 450      );
 451      
 452      if ($usefullname) {
 453        $categories = array();
 454        $result = db_query('SELECT DISTINCT(category) FROM {profile_fields}');
 455        while ($category = db_fetch_object($result)) {
 456          $categories[$category->category] = $category->category;
 457        }
 458        $default_category = variable_get('gallery_profile_fullname_category', 'Personal Information');
 459        $form['user']['sync']['fullname']['gallery_profile_fullname_category'] = array(
 460          '#type' => 'select',
 461          '#title' => t('Full name profile category'),
 462          '#default_value' => $default_category,
 463          '#options' => $categories,
 464          '#description' => t('Name of the category containing the \'Full Name\' field in profile module.'),
 465        );
 466        $fields = array();
 467        $result = _profile_get_fields($default_category);
 468        while ($field = db_fetch_object($result)) {
 469          $fields[$field->name] = $field->title;
 470        }
 471        $form['user']['sync']['fullname']['gallery_profile_fullname_field'] = array(
 472          '#type' => 'select',
 473          '#title' => t('Full name profile field'),
 474          '#default_value' => variable_get('gallery_profile_fullname_field', 'profile_fullname'),
 475          '#options' => $fields,
 476          '#description' => t('Name of \'Full Name\' field in profile module.'),
 477        );
 478      }
 479    }
 480    
 481    // Import behaviour
 482    $form['user']['sync']['import'] = array(
 483      '#type' => 'fieldset',
 484      '#title' => t('Advanced Sync - Import'),
 485      '#collapsible' => TRUE,
 486      '#collapsed' => TRUE,
 487    );
 488    
 489    $form['user']['sync']['import']['gallery_user_import_groups'] = array(
 490      '#type' => 'checkbox',
 491      '#title' => t('Import Gallery2 groups'),
 492      '#default_value' => variable_get('gallery_user_import_groups', 1),
 493      '#description' => t('Import Gallery2 groups into Drupal roles (in addition to users).'),
 494    );
 495    
 496    $form['user']['sync']['import']['gallery_user_import_override'] = array(
 497      '#type' => 'checkbox',
 498      '#title' => t('Override existing Drupal users'),
 499      '#default_value' => variable_get('gallery_user_import_override', 0),
 500      '#description' => t('Replaces user details (password, email, groups, ...) of existing Drupal users with Gallery2 imported values.'),
 501    );
 502    
 503    $form['user']['sync']['import']['gallery_user_import_conflict'] = array(
 504      '#type' => 'checkboxes',
 505      '#title' => t('Auto-resolve email address conflicts'),
 506      '#default_value' => variable_get('gallery_user_import_conflict', array()),
 507      '#options' => array(
 508        GALLERY_IMPORT_CONFLICT_DUPLICATE   => t('Duplicate e-mail addresses'),
 509        GALLERY_IMPORT_CONFLICT_INVALID     => t('Invalide e-mail addresses')
 510      ),
 511      '#description' => t('Renames duplicate/invalid e-mail addresses to username@drupaldomain.
 512                           If this option is disabled you get a list of users to handle conflicts yourself.')
 513    );
 514    
 515    // Export behaviour
 516    $form['user']['sync']['export'] = array(
 517      '#type' => 'fieldset',
 518      '#title' => t('Advanced Sync - Export (Synchronize)'),
 519      '#collapsible' => TRUE,
 520      '#collapsed' => TRUE,
 521    );
 522    
 523    $form['user']['sync']['export']['gallery_user_sync_remap'] = array(
 524      '#type' => 'checkbox',
 525      '#title' => t('Remap all users'),
 526      '#default_value' => variable_get('gallery_user_sync_remap', 0),
 527      '#description' => t('Remaps all users instead of missing or mismatching ones only.
 528                           This will completely flush Gallery2\'s \'externalIdMap\'.'),
 529    );
 530    
 531    $form = system_settings_form($form);
 532    $form['#submit'] = array('_gallery_user_settings_submit', 'system_settings_form_submit');
 533    return $form;
 534  }
 535  
 536  /**
 537   * Function _gallery_user_settings_submit().
 538   */
 539  function _gallery_user_settings_submit($form, &$form_state) {
 540    if ($form_state['values']['op'] == t('Reset to defaults')) {
 541      $fullname_changed = variable_get('gallery_use_fullname', 0);
 542    } 
 543    else {
 544      $fullname_changed = (isset($form_state['values']['gallery_use_fullname'])
 545        && ($form_state['values']['gallery_use_fullname'] != variable_get('gallery_use_fullname', 0)));
 546    }
 547    if ($fullname_changed) {
 548        drupal_set_message(t('Full Name settings have changed. You should now synchronize
 549                              your users on the <a href="@user-admin">Gallery users</a> page.',
 550                              array('@user-admin' => url('admin/user/gallery'))));
 551    }
 552  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7