You are here

function commerce_gc_product_attach_giftcard_to_user_recipient in Commerce GC 7

File

modules/commerce_gc_product/commerce_gc_product.rules.inc, line 163
Provides Rules module support for buying giftcards.

Code

function commerce_gc_product_attach_giftcard_to_user_recipient($mail, $coupon_wrapper, $email_type) {
  $mail = trim($mail);
  $user = user_load_by_mail($mail);
  if (!$user && variable_get('commerce_gc_product_create_new_recipient_users', TRUE)) {

    // If the specified user does not yet exist, create it.
    $user = new stdClass();
    $user->mail = $mail;

    // Generate a unique non-email based username for the new user.
    $username = preg_replace('/[^\\x{80}-\\x{F7} a-z0-9@_.\'-]/i', '-', trim($mail));
    $username = preg_replace('/@.*$/', '', $username);
    $username = substr($username, 0, USERNAME_MAX_LENGTH);
    $user->name = commerce_order_unique_username($username);
    $user->pass = drupal_random_key();
    $user->status = 1;
    user_save($user);

    // TODO: let admins define what role(s) these users get.
    _user_mail_notify($email_type, $user);

    // Allow other modules to alter the user before it is saved.
    drupal_alter('commerce_gc_product_new_recipient_user', $user, $coupon);
  }
  if ($user) {

    // Save the coupon with a reference to this user.
    $coupon_wrapper->commerce_coupon_recipient = $user->uid;
    $coupon_wrapper
      ->save();
    return array(
      'user_recipient' => $user,
    );
  }
}