You are here

function commerce_registration_information_checkout_form_validate in Commerce Registration 7.2

Same name and namespace in other branches
  1. 7.3 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form_validate()
  2. 7 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form_validate()

Commerce checkout pane form validation callback.

File

includes/commerce_registration.checkout_pane.inc, line 178
Checkout pane callback functions.

Code

function commerce_registration_information_checkout_form_validate($form, &$form_state, $checkout_pane, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $ret = TRUE;
  $i = 0;

  // Main counter for all the registrations
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if (!in_array($line_item_wrapper->type
      ->value(), commerce_product_line_item_types())) {
      continue;
    }
    $id = (int) $line_item_wrapper->commerce_product->product_id
      ->value();
    if (commerce_registration_product_has_registration_field($id) && registration_status('commerce_product', $id, TRUE) == 1) {
      $product = $line_item_wrapper->commerce_product;
      $line_item_id = $line_item_wrapper->line_item_id
        ->value();
      $quantity = (int) $line_item_wrapper->quantity
        ->value();
      $emails = array();
      $self_products = array();
      for ($k = 0; $k < $quantity; $k++) {
        $prodkey = $line_item_id . 'prod-' . $product->sku
          ->value();

        // Delete the following when we use ER's validation handler.
        if (isset($form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['no_reg_permission'])) {
          form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][no_reg", t("Unable to continue, you are unable to register for this item. Please remove it from your cart."));
          $ret = FALSE;
          continue;
        }
        $entity = $form_state['values'][$checkout_pane['pane_id']]['register_entities'][$prodkey][$i];
        $type = $form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['register_for'];
        $is_registered = FALSE;
        switch ($type) {
          case REGISTRATION_REGISTRANT_TYPE_ANON:
            $mail = $form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['anon_mail'];
            $mail_is_required = $form['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['anon_mail']['#required'];
            if (registration_is_registered($entity, $mail)) {
              form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][anon_mail", t("The email address entered is already registered for this item."));
              $ret = FALSE;
            }
            elseif (in_array($mail, $emails)) {
              form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][anon_mail", t("Email addresses must be unique for each registrant."));
              $ret = FALSE;
            }
            else {

              // If email is not empty, or is set to required, then validate email address.
              if ((!empty($mail) || $mail_is_required) && !valid_email_address($mail)) {
                form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][anon_mail", t("You must provide a valid email address."));
                $ret = FALSE;
              }
              else {
                if (!empty($mail)) {
                  $emails[] = $mail;
                }
              }
            }
            break;
          case REGISTRATION_REGISTRANT_TYPE_USER:
            $account = user_load_by_name($form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['user']);
            if ($account !== FALSE) {
              if (registration_is_registered($entity, NULL, $account->uid)) {
                form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][user", t("That user is already registered for this item."));
                $ret = FALSE;
              }
            }
            else {
              form_set_error("registration_information][{$prodkey}][{$prodkey}-reg-{$i}][user", t("Cannot find a user by that username."));
              $ret = FALSE;
            }
            break;
          case REGISTRATION_REGISTRANT_TYPE_ME:
            global $user;
            if (registration_is_registered($entity, NULL, $user->uid)) {
              form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][register_for", t("You are already registered for this item."));
              $ret = FALSE;
            }
            if (isset($self_products[$product->product_id
              ->value()]) && $self_products[$product->product_id
              ->value()] == $user->uid) {
              form_set_error("registration_information][" . $prodkey . "][" . $prodkey . "-reg-" . $i . "][register_for", t("You cannot register yourself for the same item more than once."));
              $ret = FALSE;
            }
            else {

              // Set a flag that the current user is registering 'self' for
              // this product.
              $self_products[$product->product_id
                ->value()] = $user->uid;
            }
            break;
        }

        // Let other modules do any field API validation if necessary.
        field_attach_form_validate('registration', $entity, $form['registration_information'][$prodkey][$prodkey . '-reg-' . $i], $form_state);

        // Update the total registration counter
        $i++;
      }
    }
  }
  return $ret;
}