Projet

Général

Profil

Paste
Télécharger (2,55 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas / cas.batch.inc @ 6ae446a4

1 e9f59589 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Provides CAS batch functions.
6
 */
7
8
/**
9
 * CAS user creation batch callback.
10
 *
11
 * @param $cas_name
12
 *   The name of the CAS user.
13
 */
14
function cas_batch_user_add($cas_name, &$context) {
15
  $options = array(
16
    'invoke_cas_user_presave' => TRUE,
17
  );
18
19
  // Remove any whitespace around usernames.
20
  $cas_name = trim($cas_name);
21
22
  // Check if the account already exists.
23
  $existing_account = cas_user_load_by_name($cas_name);
24
25
  if ($existing_account == TRUE){
26
    $uri = entity_uri('user', $existing_account);
27
    $context['results']['messages']['already_exist'][] = t('<a href="@url">%name</a>', array('@url' => url($uri['path'], $uri['options']), '%name' => $existing_account->name));;
28
    return;
29
  }
30
31
  $account = cas_user_register($cas_name, $options);
32
33
  // Display error if user creation fails.
34
  if (!$account) {
35
    $context['results']['messages']['error'][] = t("@cas_name", array('@cas_name' => $cas_name));
36
  }
37
  else {
38
    $uri = entity_uri('user', $account);
39
    $context['results']['messages']['newly_created'][] = t('<a href="@url">%name</a>', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name));
40
    return $account;
41
  }
42
}
43
44
/**
45
 * CAS user creation 'finished' callback.
46
 * Consolidates message output.
47
 */
48
function cas_batch_user_finished($success, $results, $operations) {
49
  if ($success) {
50
    if (!empty($results['messages']['error'])) {
51
      drupal_set_message(t('Error occurred during account creation of %count CAS username(s): !usernames', array('%count' => count($results['messages']['error']), '!usernames' => implode(', ', $results['messages']['error']))), 'warning');
52
    }
53
    if (!empty($results['messages']['already_exist'])) {
54
      drupal_set_message(t('The following %count CAS username(s) are already in use on this site: !usernames', array('%count' => count($results['messages']['already_exist']), '!usernames' => implode(', ', $results['messages']['already_exist']))), 'warning');
55
    }
56
    if (!empty($results['messages']['newly_created'])) {
57
      drupal_set_message(t('The following %count CAS usernames were created: !usernames', array('%count' => count($results['messages']['newly_created']), '!usernames' => implode(', ', $results['messages']['newly_created']))));
58
    }
59
  }
60
  else {
61
    // An error occurred.
62
    // $operations contains the operations that remained unprocessed.
63
    $error_operation = reset($operations);
64
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
65
  }
66
}