function commerce_registration_information_checkout_form in Commerce Registration 7.3
Same name and namespace in other branches
- 7 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form()
- 7.2 includes/commerce_registration.checkout_pane.inc \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.
File
- includes/
commerce_registration.checkout_pane.inc, line 12 - Checkout pane callback functions.
Code
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;
}