/[drupal]/contributions/modules/gallery/gallery_user_admin.inc
ViewVC logotype

Contents of /contributions/modules/gallery/gallery_user_admin.inc

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


Revision 1.3 - (show annotations) (download) (as text)
Fri Nov 23 11:20:33 2007 UTC (2 years ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.2: +165 -180 lines
File MIME type: text/x-php
- very early D6 version (Oct-01, 2007)
1 <?php
2 // $Id: gallery_user_admin.inc,v 1.1.2.19 2007/09/10 20:11:16 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 gallery_error(t('Unable to initialize embedded Gallery.'));
249 $context['finished'] = $context['results']['error'] = 1;
250 return;
251 }
252 // First pass
253 if (empty($context['sandbox'])) {
254 // Initialize batch variables
255 $context['finished'] = 0;
256 $context['sandbox']['progress'] = 0;
257 // Total number of G2 users
258 list($ret, $context['sandbox']['max']) = GalleryCoreApi::fetchUserCount();
259 if ($ret || !$context['sandbox']['max']) {
260 gallery_error(t('Error getting number of G2 users'), $ret);
261 $context['finished'] = $context['results']['error'] = 1;
262 return;
263 }
264 // Flush entity cache
265 gallery_flush_entity_cache();
266 // Import Gallery2 groups
267 if (variable_get('gallery_user_import_groups', 1)) {
268 _gallery_groups_import();
269 }
270 }
271 // Fetch a list of G2 users
272 list($ret, $g2_users) = GalleryCoreApi::fetchUsernames(GALLERY_BATCH_INTERVAL, $context['sandbox']['progress']);
273 if ($ret) {
274 gallery_error(t('Error fetching G2 usernames'), $ret);
275 $context['finished'] = $context['results']['error'] = 1;
276 return;
277 }
278 else {
279 if (!_gallery_user_import($g2_users, $context['results']['messages'])) {
280 $context['finished'] = $context['results']['error'] = 1;
281 return;
282 }
283 }
284 $context['sandbox']['progress'] += GALLERY_BATCH_INTERVAL;
285 $context['finished'] = 1;
286 if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
287 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
288 }
289 }
290
291 /**
292 * Function _gallery_user_advanced_sync().
293 */
294 function _gallery_user_advanced_sync(&$context) {
295 // Skip operation if an error occured
296 if (isset($context['results']['error']) && $context['results']['error']) {
297 $context['finished'] = $context['results']['error'] = 1;
298 return;
299 }
300 // Initialize G2
301 if (!_gallery_init(TRUE, NULL, FALSE)) {
302 gallery_error(t('Unable to initialize embedded Gallery.'));
303 $context['finished'] = $context['results']['error'] = 1;
304 return;
305 }
306 // First pass
307 if (empty($context['sandbox'])) {
308 // Initialize batch variables
309 $context['finished'] = 0;
310 $context['sandbox']['progress'] = 0;
311 // Total number of users
312 $total = db_fetch_object(db_query("SELECT COUNT(*) AS users FROM {users} WHERE uid > 0"));
313 if (!$total->users) {
314 $context['finished'] = $context['results']['error'] = 1;
315 return;
316 }
317 $context['sandbox']['max'] = $total->users;
318 // Empty externalIdMap in first pass
319 if (variable_get('gallery_user_sync_remap', 0)) {
320 $ret = GalleryCoreApi::removeAllMapEntries('ExternalIdMap');
321 if ($ret) {
322 gallery_error(t('Error emptying \'ExternalIdMap\''), $ret);
323 $context['finished'] = $context['results']['error'] = 1;
324 return;
325 }
326 }
327 }
328 // Sync users
329 $result = db_query_range("SELECT uid FROM {users} WHERE uid > 0", $context['sandbox']['progress'], GALLERY_BATCH_INTERVAL);
330 while ($user = db_fetch_object($result)) {
331 if ($account = user_load(array('uid' => $user->uid))) {
332 if (!gallery_user_modify($account, 'update', !$context['sandbox']['progress'])) {
333 $context['finished'] = $context['results']['error'] = 1;
334 return;
335 }
336 }
337 }
338 $context['sandbox']['progress'] += GALLERY_BATCH_INTERVAL;
339 $context['finished'] = 1;
340 if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
341 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
342 }
343 }
344
345 /**
346 * Function _gallery_user_advanced_finished().
347 */
348 function _gallery_user_advanced_finished($success, $results, $operations) {
349 if (isset($_SESSION['gallery_user_batch_offline'])) {
350 variable_set('site_offline', 0);
351 unset($_SESSION['gallery_user_batch_offline']);
352 }
353 //
354 if ($success) {
355 if (isset($results['error']) && $results['error']) {
356 drupal_set_message(t('User synchronization (partially) failed.'), 'error');
357 }
358 else {
359 if (isset($results['messages']) && count($results['messages'])) {
360 drupal_set_message(theme('item_list', $results['messages'], t('The following messages occured:')), 'notice');
361 drupal_set_message('<strong>'. t('Invalid user items were skipped.') .'</strong>', 'notice');
362 watchdog('gallery', theme('item_list', $results['messages'], t('The following messages occured:')), WATCHDOG_NOTICE);
363 }
364 else {
365 drupal_set_message(t('User synchronization successfully completed.'));
366 }
367 }
368 }
369 else {
370 $error_operation = reset($operations);
371 drupal_set_message(t('An error occurred while processing @operation', array('@operation' => $error_operation[0])), 'error');
372 }
373 }
374
375 /**
376 * Function _gallery_user_settings().
377 */
378 function _gallery_user_settings() {
379 require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
380
381 $form['user'] = array(
382 '#type' => 'fieldset',
383 '#title' => t('Settings'),
384 '#collapsible' => FALSE,
385 '#collapsed' => FALSE
386 );
387
388 // General (not sync related) settings
389 $form['user']['gallery_user_hide_profile'] = array(
390 '#type' => 'checkbox',
391 '#title' => t('Hide Gallery2 section in profiles'),
392 '#default_value' => variable_get('gallery_user_hide_profile', 0),
393 '#description' => t('Hide the Gallery2 section (i.e. Gallery2-Drupal Sync Status) on the user profile pages.'),
394 );
395
396 // Sync settings
397 $form['user']['sync'] = array(
398 '#type' => 'fieldset',
399 '#title' => t('User Synchronization'),
400 '#collapsible' => TRUE,
401 '#collapsed' => FALSE
402 );
403
404 $roles = array(0 => t('none'));
405 $roles += user_roles(TRUE);
406 unset($roles[DRUPAL_AUTHENTICATED_RID]);
407 $form['user']['sync']['gallery_user_admin_role'] = array(
408 '#type' => 'select',
409 '#title' => t('Drupal \'admin\' role'),
410 '#default_value' => variable_get('gallery_user_admin_role', 0),
411 '#options' => $roles,
412 '#description' => t('Select the Drupal role equivalent to Gallery2\'s \'Site Admin\' group (or \'none\' to disable this
413 feature). The roles \'anonymous\' and \'authenticated\' are not available for selection.'),
414 );
415
416 $form['user']['sync']['gallery_user_locked'] = array(
417 '#type' => 'checkbox',
418 '#title' => t('Lock G2 accounts'),
419 '#default_value' => variable_get('gallery_user_locked', 0),
420 '#description' => t('Locking G2 account prevents users from changing their details (password, email, ...) in G2.'),
421 );
422
423 // Fullname settings
424 $profile_status = module_exists('profile');
425 $profile_status_str = theme('gallery_module_status_message', $profile_status);
426 $desc = t('Full names in Gallery2 can be supported by using the profile module
427 (!profile_status) with a \'Full Name\' profile field as defined below.',
428 array('!profile_status' => $profile_status_str)
429 );
430 if (!$profile_status) {
431 $desc .= t(' However the profile module is disabled, so this functionality is
432 not available and the options are disabled.');
433 }
434 $form['user']['sync']['fullname'] = array(
435 '#type' => 'fieldset',
436 '#title' => t('Full Name settings'),
437 '#collapsible' => TRUE,
438 '#collapsed' => !$profile_status,
439 '#description' => $desc,
440 );
441 if ($profile_status) {
442 $usefullname = variable_get('gallery_use_fullname', 0);
443 $form['user']['sync']['fullname']['gallery_use_fullname'] = array(
444 '#type' => 'checkbox',
445 '#title' => t('Enable Full Name support'),
446 '#return_value' => 1,
447 '#default_value' => $usefullname,
448 '#disabled' => !$profile_status,
449 '#description' => t('Use full name from profile module in Gallery2 user data. Note that
450 enabling/disabling this only updates Gallery2 user data when the
451 Drupal user is updated or if a user sync is performed.'),
452 );
453
454 if ($usefullname) {
455 $categories = array();
456 $result = db_query('SELECT DISTINCT(category) FROM {profile_fields}');
457 while ($category = db_fetch_object($result)) {
458 $categories[$category->category] = $category->category;
459 }
460 $default_category = variable_get('gallery_profile_fullname_category', 'Personal Information');
461 $form['user']['sync']['fullname']['gallery_profile_fullname_category'] = array(
462 '#type' => 'select',
463 '#title' => t('Full name profile category'),
464 '#default_value' => $default_category,
465 '#options' => $categories,
466 '#description' => t('Name of the category containing the \'Full Name\' field in profile module.'),
467 );
468 $fields = array();
469 $result = _profile_get_fields($default_category);
470 while ($field = db_fetch_object($result)) {
471 $fields[$field->name] = $field->title;
472 }
473 $form['user']['sync']['fullname']['gallery_profile_fullname_field'] = array(
474 '#type' => 'select',
475 '#title' => t('Full name profile field'),
476 '#default_value' => variable_get('gallery_profile_fullname_field', 'profile_fullname'),
477 '#options' => $fields,
478 '#description' => t('Name of \'Full Name\' field in profile module.'),
479 );
480 }
481 }
482
483 // Import behaviour
484 $form['user']['sync']['import'] = array(
485 '#type' => 'fieldset',
486 '#title' => t('Advanced Sync - Import'),
487 '#collapsible' => TRUE,
488 '#collapsed' => TRUE,
489 );
490
491 $form['user']['sync']['import']['gallery_user_import_groups'] = array(
492 '#type' => 'checkbox',
493 '#title' => t('Import Gallery2 groups'),
494 '#default_value' => variable_get('gallery_user_import_groups', 1),
495 '#description' => t('Import Gallery2 groups into Drupal roles (in addition to users).'),
496 );
497
498 $form['user']['sync']['import']['gallery_user_import_override'] = array(
499 '#type' => 'checkbox',
500 '#title' => t('Override existing Drupal users'),
501 '#default_value' => variable_get('gallery_user_import_override', 0),
502 '#description' => t('Replaces user details (password, email, groups, ...) of existing Drupal users with Gallery2 imported values.'),
503 );
504
505 $form['user']['sync']['import']['gallery_user_import_conflict'] = array(
506 '#type' => 'checkboxes',
507 '#title' => t('Auto-resolve email address conflicts'),
508 '#default_value' => variable_get('gallery_user_import_conflict', array()),
509 '#options' => array(
510 GALLERY_IMPORT_CONFLICT_DUPLICATE => t('Duplicate e-mail addresses'),
511 GALLERY_IMPORT_CONFLICT_INVALID => t('Invalide e-mail addresses')
512 ),
513 '#description' => t('Renames duplicate/invalid e-mail addresses to username@drupaldomain.
514 If this option is disabled you get a list of users to handle conflicts yourself.')
515 );
516
517 // Export behaviour
518 $form['user']['sync']['export'] = array(
519 '#type' => 'fieldset',
520 '#title' => t('Advanced Sync - Export (Synchronize)'),
521 '#collapsible' => TRUE,
522 '#collapsed' => TRUE,
523 );
524
525 $form['user']['sync']['export']['gallery_user_sync_remap'] = array(
526 '#type' => 'checkbox',
527 '#title' => t('Remap all users'),
528 '#default_value' => variable_get('gallery_user_sync_remap', 0),
529 '#description' => t('Remaps all users instead of missing or mismatching ones only.
530 This will completely flush Gallery2\'s \'externalIdMap\'.'),
531 );
532
533 $form = system_settings_form($form);
534 $form['#submit'] = array('_gallery_user_settings_submit', 'system_settings_form_submit');
535 return $form;
536 }
537
538 /**
539 * Function _gallery_user_settings_submit().
540 */
541 function _gallery_user_settings_submit($form, &$form_state) {
542 if ($form_state['values']['op'] == t('Reset to defaults')) {
543 $fullname_changed = variable_get('gallery_use_fullname', 0);
544 }
545 else {
546 $fullname_changed = (isset($form_state['values']['gallery_use_fullname'])
547 && ($form_state['values']['gallery_use_fullname'] != variable_get('gallery_use_fullname', 0)));
548 }
549 if ($fullname_changed) {
550 drupal_set_message(t('Full Name settings have changed. You should now synchronize
551 your users on the <a href="@user-admin">Gallery users</a> page.',
552 array('@user-admin' => url('admin/user/gallery'))));
553 }
554 }

  ViewVC Help
Powered by ViewVC 1.1.2