You are here

function commerce_registration_line_item_registrations_sync in Commerce Registration 7.2

Submit callback for line item registration sync checkboxes.

1 string reference to 'commerce_registration_line_item_registrations_sync'
commerce_registration_form_commerce_order_ui_order_form_alter in ./commerce_registration.module
Implements hook_form_FORM_ID_alter().

File

./commerce_registration.module, line 1337
Commerce Registration module code.

Code

function commerce_registration_line_item_registrations_sync($form, &$form_state) {
  if (isset($form['commerce_registration']['line_item_registrations'])) {

    // If the user is editing an order, load a fresh copy.
    if ($form_state['commerce_order']->order_id) {
      $form_state['commerce_order'] = commerce_order_load($form_state['commerce_order']->order_id);
    }
    $order = $form_state['commerce_order'];

    // Did we trigger this with an ajax callback?
    $triggered = FALSE;
    if (!empty($form_state['triggering_element'])) {
      $trigger_parents = array_reverse($form_state['triggering_element']['#array_parents']);
      if ($triggered = (bool) ($trigger_parents[0] === 'sync_registrations')) {
        $triggering_line_item_id = $trigger_parents[1];
        $form_state['rebuild'] = TRUE;
      }
    }

    // Loop through the registration enabled line items and sync the number
    // of registrations.
    foreach (element_children($form['commerce_registration']['line_item_registrations']) as $line_item_id) {
      if (!$triggered || $triggering_line_item_id == $line_item_id) {
        if (isset($form['commerce_registration']['line_item_registrations'][$line_item_id]['registrations'])) {
          if ($line_item = commerce_line_item_load($line_item_id)) {
            $update_line_item = FALSE;
            $element = $form['commerce_registration']['line_item_registrations'][$line_item_id];
            $prodkey = $element['registrations']['#prodkey'];
            $sync = $element['sync_registrations']['#value'];
            if (!isset($line_item->data['sync_registrations']) || $sync != $line_item->data['sync_registrations']) {
              $line_item->data['sync_registrations'] = $sync;
              $update_line_item = TRUE;
            }

            // Sync the line item quantity if there are registrations attached.
            if (isset($order->data) && !empty($order->data['register_entities'][$prodkey])) {
              $quantity = count($order->data['register_entities'][$prodkey]);
              if ($sync && $quantity != $line_item->quantity) {

                // Update line item quantity with the number of registrations.
                $line_item->quantity = $quantity;
                if (isset($form_state['input']['commerce_line_items'][$form['commerce_line_items']['#language']]['line_items'][$line_item_id]['quantity'])) {
                  $form_state['input']['commerce_line_items'][$form['commerce_line_items']['#language']]['line_items'][$line_item_id]['quantity'] = $quantity;
                }
                if (isset($form_state['values']['commerce_line_items'][$form['commerce_line_items']['#language']]['line_items'][$line_item_id]['quantity'])) {
                  $form_state['values']['commerce_line_items'][$form['commerce_line_items']['#language']]['line_items'][$line_item_id]['quantity'] = $quantity;
                }
                $update_line_item = TRUE;
              }
            }
            else {
              $line_item->data['sync_registrations'] = FALSE;
              $update_line_item = TRUE;
            }

            // Update the line item with changes.
            if ($update_line_item) {
              commerce_line_item_save($line_item);
            }
          }
        }
      }
    }
  }
}