You are here

function party_user_user_insert in Party 8.2

Same name and namespace in other branches
  1. 7 modules/party_user/party_user.module \party_user_user_insert()

Implements hook_user_insert().

If we have set up Party Acquisition, perform a party match based off of the mail of the account being created. If it exists, this user will acquire the party, if not we'll create a party. We will also create a user if we don't use Party Acquisition but we have set that we want to create a party when a user registers.

File

modules/party_user/party_user.module, line 408
Support for linking users to parties

Code

function party_user_user_insert(&$edit, $account, $category) {
  if ($category == 'account') {
    $action = variable_get('party_user_on_registration', 'nothing');
    if ($action == 'acquire') {

      // Perform our matching query and load the party.
      $query = db_select('party');
      $query
        ->addField('party', 'pid');
      $query
        ->condition('party.email', $account->mail);
      $query
        ->range(0, 1);
      $party = $query
        ->execute()
        ->fetchField();

      // Attempt to load the party.
      if ($party) {
        $party = party_load($party);
      }
      else {

        // Indicate that we need to create a party.
        $action = 'create';
      }
    }

    // If we need to create a party, lets do it.
    if ($action == 'create') {
      $party = party_create();
      party_save($party);
    }
    if (!empty($party)) {
      if (module_exists('party_hat')) {

        // Add in our hats.
        $hats = array();
        foreach (variable_get('party_user_registration_hats', array()) as $hat_name) {
          $hats[] = $hat_name;
        }
        party_hat_hats_assign($party, $hats);
      }

      // Get our controller and attach the user.
      $controller = party_get_crm_controller($party, 'user');

      // Check there isn't already an account attached.
      $user_ids = $controller
        ->getEntityIds();
      if (empty($user_ids)) {
        $account_clone = clone $account;
        unset($account_clone->is_new);
        $controller
          ->attachEntity($account_clone);
        $controller
          ->save();
      }
      module_invoke_all('party_user_acquisition', $party, $user, $action);
    }
  }
}