You are here

commerce_registration.checkout_pane.inc in Commerce Registration 7.3

Checkout pane callback functions.

File

includes/commerce_registration.checkout_pane.inc
View source
<?php

/**
 * @file
 * Checkout pane callback functions.
 */

/**
 * Commerce checkout pane form builder callback.
 * @TODO: don't create registration objects if the host entity isn't allowing
 *        registrations.
 */
function commerce_registration_information_checkout_form($form, &$form_state, $checkout_pane, $order) {
  global $user;
  $pane_form = array();
  $show_form = FALSE;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  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) {
      $show_form = TRUE;

      // Registration objects are stored as a serialized string in the database,
      // so we need to unserialize it to get the full registration object.
      $registrations = $line_item_wrapper->registrations
        ->value();
      if (!is_array($registrations)) {
        $registrations = unserialize($registrations);
      }
      if (empty($registrations)) {
        $registrations = array();
      }
      $product = $line_item_wrapper->commerce_product;
      $quantity = (int) $line_item_wrapper->quantity
        ->value();
      $prodkey = 'lineitem-' . $line_item_wrapper->line_item_id
        ->value();
      $bundle = registration_get_entity_registration_type('commerce_product', $product
        ->value());
      $base_title = '!title - Registrant #!count';
      $entity = entity_get_controller('registration')
        ->create(array(
        'type' => $bundle,
      ));
      $entity->registration_id = NULL;
      $entity->entity_type = 'commerce_product';
      $entity->entity_id = $product->product_id
        ->value();
      $entity->created = REQUEST_TIME;
      $entity->updated = REQUEST_TIME;
      $entity->author_uid = $user->uid;
      $options = registration_access_people($entity);
      if (count($options) == 0 || !registration_register_page_access('commerce_product', $product
        ->value())) {
        $pane_form[$prodkey][$i]['no_reg'] = array(
          '#markup' => t('You do not have permission to register for this item.'),
        );
        $pane_form[$prodkey][$i]['no_reg_permission'] = array(
          '#type' => 'hidden',
          '#value' => TRUE,
        );
        continue;
      }
      $settings = registration_entity_settings('commerce_product', $product->product_id
        ->value());
      $limit_amount = $settings['settings'][$settings['settings']['limit_registrations'] . '_per_product'];
      for ($i = 0; $i < $quantity; $i++) {
        switch ($settings['settings']['limit_registrations']) {
          case 'slots':

            // A single quantity of the product only requires a one registration
            // but takes up multiple slots.
            $label = $i + 1;
            $title = t($base_title, array(
              '!title' => $product->title
                ->value(),
              '!count' => $label,
            ));
            if (isset($override_title)) {
              $data = array(
                'commerce-line-item' => $line_item_wrapper
                  ->value(),
                'commerce-product' => $product
                  ->value(),
              );
              $title = token_replace($override_title, $data);
              $tokens = array(
                '[title]' => $product->title
                  ->value(),
                '[index]' => $label,
              );
              $title = strtr($title, $tokens);
            }
            $pane_form[$prodkey][$i] = array(
              '#type' => 'fieldset',
              '#title' => $title,
              '#collapsible' => TRUE,
              '#tree' => TRUE,
              '#description' => variable_get('commerce_registration_registrant_help', ''),
            );
            $entity = NULL;

            // Determine if we should create a new entity or if the user has
            // gone back in the checkout process and we should load previously
            // stored values in the line item.
            if (isset($registrations[$i])) {
              $entity = $registrations[$i];
              if (!$settings['settings']['allow_user_slots_entry']) {
                $entity->count = $limit_amount;
              }
            }
            else {
              $entity = commerce_registration_create_product_registration($line_item_wrapper->commerce_product->product_id
                ->value(), $bundle, $order->order_id, $limit_amount);
            }

            // -------------------------------------------------------------------------
            // All the following code can be deleted if we use the form builder from ER.
            // -------------------------------------------------------------------------
            $blank = array();
            $register_form = registration_form($blank, $blank, $entity);
            unset($register_form['actions']);
            unset($register_form['state']);
            unset($register_form['count']);
            $pane_form[$prodkey][$i] += $register_form;

            // Default value for select list if user has saved registration.
            if (!is_null($entity
              ->registrant_type($user))) {
              $pane_form[$prodkey][$i]['who_is_registering']['#default_value'] = $entity
                ->registrant_type($user);
            }
            $find_name = "registration_information[{$prodkey}][{$i}][who_is_registering]";

            // Default value for user autocomplete if user has saved regitration by
            // proceeding forward/backward in the checkout process.
            if (!empty($entity->user_uid)) {
              $referenced_user = user_load($entity->user_uid);
              $pane_form[$prodkey][$i]['user']['#default_value'] = $referenced_user->name;
            }
            $pane_form[$prodkey][$i]['user']['#states'] = array(
              'visible' => array(
                ':input[name="' . $find_name . '"]' => array(
                  'value' => REGISTRATION_REGISTRANT_TYPE_USER,
                ),
              ),
            );
            $pane_form[$prodkey][$i]['anon_mail']['#states'] = array(
              'visible' => array(
                ':input[name="' . $find_name . '"]' => array(
                  'value' => REGISTRATION_REGISTRANT_TYPE_ANON,
                ),
              ),
            );
            $pane_form[$prodkey][$i]['#parents'] = array(
              'registration_information',
              $prodkey,
              $i,
            );
            if ($settings['settings']['allow_user_slots_entry']) {
              $pane_form[$prodkey][$i]['slots'] = array(
                '#type' => 'select',
                '#title' => t('Number of Slots'),
                '#options' => drupal_map_assoc(range(1, $settings['settings']['user_slots_maximum'])),
                '#default_value' => $entity->count,
              );
            }

            // Attach the entity's fields to the form.
            field_attach_form('registration', $entity, $pane_form[$prodkey][$i], $form_state);
            $registrations[$i] = $entity;
            break;
          case 'registrations':

            // A single quantity of the product requires multiple registrations.
            for ($j = 0; $j < $limit_amount; $j++) {

              // A single quantity of the product only requires a one registration
              // but takes up multiple slots.
              $label = $j + 1;
              $title = t($base_title, array(
                '!title' => $product->title
                  ->value(),
                '!count' => $label,
              ));
              if (isset($override_title)) {
                $data = array(
                  'commerce-line-item' => $line_item_wrapper
                    ->value(),
                  'commerce-product' => $product
                    ->value(),
                );
                $title = token_replace($override_title, $data);
                $tokens = array(
                  '[title]' => $product->title
                    ->value(),
                  '[index]' => $label,
                );
                $title = strtr($title, $tokens);
              }
              $pane_form[$prodkey][$i][$j] = array(
                '#type' => 'fieldset',
                '#title' => $title,
                '#collapsible' => TRUE,
                '#tree' => TRUE,
                '#description' => variable_get('commerce_registration_registrant_help', ''),
              );
              $entity = NULL;

              // Determine if we should create a new entity or if the user has
              // gone back in the checkout process and we should load previously
              // stored values in the line item.
              if (isset($registrations[$i][$j])) {
                $entity = $registrations[$i][$j];
              }
              else {
                $entity = commerce_registration_create_product_registration($line_item_wrapper->commerce_product->product_id
                  ->value(), $bundle, $order->order_id, 1);
              }

              // -------------------------------------------------------------------------
              // All the following code can be deleted if we use the form builder from ER.
              // -------------------------------------------------------------------------
              $blank = array();
              $register_form = registration_form($blank, $blank, $entity);
              unset($register_form['actions']);
              unset($register_form['state']);
              unset($register_form['count']);
              $pane_form[$prodkey][$i][$j] += $register_form;

              // Default value for select list if user has saved registration.
              if (!is_null($entity
                ->registrant_type($user))) {
                $pane_form[$prodkey][$i][$j]['who_is_registering']['#default_value'] = $entity
                  ->registrant_type($user);
              }
              $find_name = "registration_information[{$prodkey}][{$i}][{$j}][who_is_registering]";

              // Default value for user autocomplete if user has saved regitration by
              // proceeding forward/backward in the checkout process.
              if (!empty($entity->user_uid)) {
                $referenced_user = user_load($entity->user_uid);
                $pane_form[$prodkey][$i][$j]['user']['#default_value'] = $referenced_user->name;
              }
              $pane_form[$prodkey][$i][$j]['user']['#states'] = array(
                'visible' => array(
                  ':input[name="' . $find_name . '"]' => array(
                    'value' => REGISTRATION_REGISTRANT_TYPE_USER,
                  ),
                ),
              );
              $pane_form[$prodkey][$i][$j]['anon_mail']['#states'] = array(
                'visible' => array(
                  ':input[name="' . $find_name . '"]' => array(
                    'value' => REGISTRATION_REGISTRANT_TYPE_ANON,
                  ),
                ),
              );
              $pane_form[$prodkey][$i][$j]['#parents'] = array(
                'registration_information',
                $prodkey,
                $i,
                $j,
              );

              // Attach the entity's fields to the form.
              field_attach_form('registration', $entity, $pane_form[$prodkey][$i][$j], $form_state);
              $registrations[$i][$j] = $entity;
            }
            break;
        }
      }
      $line_item_wrapper->registrations = $registrations;
      $line_item_wrapper
        ->save();
    }
  }
  if (!$show_form) {

    // No register enabled products, so go to next step of checkout process.
    $pane_form['no_reg_products'] = array(
      '#markup' => t('No information required.'),
    );
  }
  return $pane_form;
}

/**
 * Commerce checkout pane form validation callback.
 */
function commerce_registration_information_checkout_form_validate($form, &$form_state, $checkout_pane, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $ret = TRUE;
  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 (registration_status('commerce_product', $id, TRUE) == 1) {
      $settings = registration_entity_settings('commerce_product', $id);
      $product = $line_item_wrapper->commerce_product;
      $emails = array();
      $self_products = array();

      // Registration objects are stored as a serialized string in the database,
      // so we need to unserialize it to get the full registration object.
      $registrations = $line_item_wrapper->registrations
        ->value();
      if (!is_array($registrations)) {
        $registrations = unserialize($registrations);
      }
      $validate = array();
      $prodkey = 'lineitem-' . $line_item_wrapper->line_item_id
        ->value();
      switch ($settings['settings']['limit_registrations']) {
        case 'registrations':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $quantity_reg) {
            foreach ($quantity_reg as $j => $registration) {
              $registration['form_index'] = "{$i}][{$j}";
              $registration['entity'] = $registrations[$i][$j];
              $registration['form_structure'] = $form['registration_information'][$prodkey][$i][$j];
              $validate[] = $registration;
            }
          }
          break;
        case 'slots':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $registration) {
            $registration['form_index'] = "{$i}";
            $registration['entity'] = $registrations[$i];
            $registration['form_structure'] = $form['registration_information'][$prodkey][$i];
            $validate[] = $registration;
          }
          break;
      }
      foreach ($validate as $registration_data) {

        // Delete the following when we use ER's validation handler.
        $index = $registration_data['form_index'];
        if (isset($registration_data['no_reg_permission'])) {
          form_set_error("registration_information][{$prodkey}][{$index}][no_reg", t("Unable to continue, you are unable to register for this item. Please remove it from your cart."));
          $ret = FALSE;
          continue;
        }
        $entity = $registration_data['entity'];
        $type = $registration_data['who_is_registering'];
        switch ($type) {
          case REGISTRATION_REGISTRANT_TYPE_ANON:
            $mail = $registration_data['anon_mail'];
            $mail_is_required = $registration_data['anon_mail']['#required'];
            if (registration_is_registered($entity, $mail)) {
              form_set_error("registration_information][{$prodkey}][{$index}][anon_mail", t("The email address entered is already registered for this item."));
              $ret = FALSE;
            }
            else {
              if ($mail_is_required) {
                if (empty($mail) || !valid_email_address($mail)) {
                  form_set_error("registration_information][{$prodkey}][{$index}][anon_mail", t("You must provide a valid email address."));
                  $ret = FALSE;
                }
              }
              elseif (in_array($mail, $emails)) {
                form_set_error("registration_information][{$prodkey}][{$index}][anon_mail", t("Email addresses must be unique for each registrant."));
                $ret = FALSE;
              }
              else {
                if ($mail_is_required) {
                  $emails[] = $mail;
                }
              }
            }
            break;
          case REGISTRATION_REGISTRANT_TYPE_USER:
            $account = user_load_by_name($registration_data['user']);
            if ($account !== FALSE) {
              if (registration_is_registered($entity, NULL, $account->uid)) {
                form_set_error("registration_information][{$prodkey}][{$index}][user", t("That user is already registered for this item."));
                $ret = FALSE;
              }
            }
            else {
              form_set_error("registration_information][{$prodkey}][{$index}][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}][{$index}][who_is_registering", t("You are already registered for this item."));
              $ret = FALSE;
            }
            if (isset($self_products[$id]) && $self_products[$id] == $user->uid) {
              form_set_error("registration_information][{$prodkey}][{$index}][who_is_registering", 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[$id] = $user->uid;
            }
            break;
        }

        // Let other modules do any field API validation if necessary.
        field_attach_form_validate('registration', $entity, $registration_data['form_structure'], $form_state);
      }
    }
  }
  return $ret;
}

/**
 * Commerce checkout pane form submit callback.
 */
function commerce_registration_information_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  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;
    }
    $product_id = (int) $line_item_wrapper->commerce_product->product_id
      ->value();
    if (registration_status('commerce_product', $product_id, TRUE) == 1) {
      $settings = registration_entity_settings('commerce_product', $product_id);

      // Registration objects are stored as a serialized string in the database,
      // so we need to unserialize it to get the full registration object.
      $registrations = $line_item_wrapper->registrations
        ->value();
      if (!is_array($registrations)) {
        $registrations = unserialize($registrations);
      }
      $submit = array();
      $prodkey = 'lineitem-' . $line_item_wrapper->line_item_id
        ->value();
      switch ($settings['settings']['limit_registrations']) {
        case 'registrations':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $quantity_reg) {
            foreach ($quantity_reg as $j => $registration) {
              $registration['form_index'] = "{$i}][{$j}";
              $registration['entity'] =& $registrations[$i][$j];
              $registration['form_structure'] = $form['registration_information'][$prodkey][$i][$j];
              $submit[] = $registration;
            }
          }
          break;
        case 'slots':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $registration) {
            $registration['form_index'] = "{$i}";
            $registration['entity'] =& $registrations[$i];
            $registration['form_structure'] = $form['registration_information'][$prodkey][$i];
            $submit[] = $registration;
          }
          break;
      }
      foreach ($submit as $registration_data) {
        if (isset($registration_data['no_reg_permission'])) {
          continue;
        }
        $entity =& $registration_data['entity'];
        $entity->count = $registration_data['slots'];
        $type = $registration_data['who_is_registering'];
        switch ($type) {
          case REGISTRATION_REGISTRANT_TYPE_ANON:
            $entity->anon_mail = $registration_data['anon_mail'];
            break;
          case REGISTRATION_REGISTRANT_TYPE_USER:
            $account = user_load_by_name($registration_data['user']);
            $entity->user_uid = $account->uid;
            break;
          case REGISTRATION_REGISTRANT_TYPE_ME:
            global $user;
            $entity->user_uid = $user->uid;
            break;
        }

        // Notify the field API that values were submitted.
        field_attach_submit('registration', $entity, $registration_data['form_structure'], $form_state);
        $entity->updated = REQUEST_TIME;
        registration_save($entity);
        $registrations[$registration_data['form_index']] = $entity;
      }
      $line_item_wrapper->registrations = $registrations;
      $line_item_wrapper
        ->save();
    }
  }
}

/**
 * Commerce checkout pane review callback.
 */
function commerce_registration_information_review($form, $form_state, $checkout_pane, $order) {
  $msg = '';
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  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 (registration_status('commerce_product', $id, TRUE) == 1) {
      $product = $line_item_wrapper->commerce_product;

      // Registration objects are stored as a serialized string in the database,
      // so we need to unserialize it to get the full registration object.
      $registrations = $line_item_wrapper->registrations
        ->value();
      if (!is_array($registrations)) {
        $registrations = unserialize($registrations);
      }
      $variables = array(
        'line_item' => $line_item_wrapper,
        'registrations' => $registrations,
        'product' => $product,
      );
      $msg .= theme('commerce_registration_review_pane', $variables);
    }
  }
  return $msg;
}

/**
 * Commerce checkout pane settings form builder.
 */
function commerce_registration_information_settings_form($checkout_pane) {
  $form = array();
  $form['commerce_registration_product_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Product Registration Title'),
    '#description' => t('The text to populate the fieldset label for a registrant\'s information.') . '<br>' . t('Available tokens are [title] for the product title, and [index] for a countable index value.'),
    '#default_value' => variable_get('commerce_registration_product_title', ''),
  );
  $form['commerce_registration_registrant_help'] = array(
    '#type' => 'textarea',
    '#title' => t('Product Registration Fieldset Help Text'),
    '#description' => t('The help text to display to the user entering registration information.'),
    '#default_value' => variable_get('commerce_registration_registrant_help', ''),
  );
  $form['commerce_registration_hide_email_logged_in'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hide the Email field for logged in users'),
    '#default_value' => variable_get('commerce_registration_hide_email_logged_in', ''),
  );
  return $form;
}

/**
 * Helper function to create a new registration for a product.
 *
 * @param int $product_id
 * @param string $bundle
 * @param int $order_id
 *
 * @return RegistrationEntity
 */
function commerce_registration_create_product_registration($product_id, $bundle, $lineitem_id, $slots = 1) {
  global $user;
  $entity = entity_get_controller('registration')
    ->create(array(
    'type' => $bundle,
  ));
  $entity->registration_id = NULL;
  $entity->entity_type = 'commerce_product';
  $entity->entity_id = $product_id;
  $entity->created = REQUEST_TIME;
  $entity->updated = REQUEST_TIME;
  $entity->author_uid = $user->uid;
  $entity->lineitem_id = $lineitem_id;
  $entity->count = $slots;
  registration_save($entity);
  return $entity;
}

Functions

Namesort descending Description
commerce_registration_create_product_registration Helper function to create a new registration for a product.
commerce_registration_information_checkout_form Commerce checkout pane form builder callback. @TODO: don't create registration objects if the host entity isn't allowing registrations.
commerce_registration_information_checkout_form_submit Commerce checkout pane form submit callback.
commerce_registration_information_checkout_form_validate Commerce checkout pane form validation callback.
commerce_registration_information_review Commerce checkout pane review callback.
commerce_registration_information_settings_form Commerce checkout pane settings form builder.