You are here

function commerce_ss_admin_form_submit in Commerce Stock 7.2

Add or remove the Stock field from product types.

File

modules/commerce_ss/includes/commerce_ss.admin.inc, line 101
Administrative callbacks and form builder functions for Commerce Stock.

Code

function commerce_ss_admin_form_submit($form, &$form_state) {
  $form_state['commerce_stock']['delete_instances'] = array();

  // Prepare a batch in case we need it for enabling stock on product types.
  $batch = array(
    'operations' => array(),
    'file' => drupal_get_path('module', 'commerce_ss') . '/includes/commerce_ss.admin.inc',
    'finished' => 'commerce_ss_batch_product_type_init_finished',
    'title' => t('Product stock initialization'),
    'init_message' => t('Product stock initialization is starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Product stock initialization has encountered an error.'),
  );
  foreach ($form_state['values']['product_types'] as $type => $enable) {
    $instance = field_info_instance('commerce_product', 'commerce_stock', $type);
    $currently_enabled = commerce_ss_product_type_enabled($type);

    // If they want us to enable it and it doesn't currently exist, do the work.
    if ($enable && !$currently_enabled) {

      // Create the instance.
      commerce_ss_admin_create_instance('commerce_stock', 'number_decimal', TRUE, 'commerce_product', $type, t('Stock'));
      drupal_set_message(t('Stock field has been added to the %type product type.', array(
        '%type' => $type,
      )));

      // Add the operation to process this type to the batch.
      $batch['operations'][] = array(
        'commerce_ss_batch_product_type_init_process',
        array(
          $type,
          0,
        ),
      );
    }
    elseif (!$enable && $currently_enabled) {

      // If they haven't clicked the "confirm" checkbox, rebuild and get them
      // to do it.
      if (empty($form_state['values']['confirmation'])) {
        $form_state['commerce_stock']['delete_instances'][] = $type;
        $form_state['rebuild'] = TRUE;
      }
      else {

        // Remove the instance.
        field_delete_instance($instance);

        // Remove override if enabled.
        if (commerce_ss_product_type_override_enabled($type)) {
          $override = field_info_instance('commerce_product', 'commerce_stock_override', $type);
          field_delete_instance($override);
        }
        drupal_set_message(t('Stock management has been disabled on the %type product type', array(
          '%type' => $type,
        )));
      }
    }
  }
  if (!empty($form_state['values']['product_types_override'])) {
    foreach ($form_state['values']['product_types_override'] as $type => $enable) {
      $instance = field_info_instance('commerce_product', 'commerce_stock_override', $type);
      $currently_enabled = commerce_ss_product_type_override_enabled($type);
      $stock_enabled = commerce_ss_product_type_enabled($type);

      // If they want us to enable it and it doesn't currently exist,
      // do the work.
      if ($enable && $stock_enabled && !$currently_enabled) {
        commerce_ss_admin_create_instance('commerce_stock_override', 'list_boolean', FALSE, 'commerce_product', $type, t('Disable stock for this product'));
        drupal_set_message(t('Stock management override has been enabled on the %type product type', array(
          '%type' => $type,
        )));
      }
      elseif (!$enable && $currently_enabled) {

        // If they haven't clicked the "confirm" checkbox, rebuild and get them
        // to do it.
        if (empty($form_state['values']['confirmation'])) {
          $form_state['commerce_stock']['delete_instances'][] = $type;
          $form_state['rebuild'] = TRUE;
        }
        else {

          // Remove the instance.
          field_delete_instance($instance);
          drupal_set_message(t('Stock management override has been disabled on the %type product type', array(
            '%type' => $type,
          )));
        }
      }
    }
  }

  // If our batch has operations, run it now.
  if (count($batch['operations'])) {
    batch_set($batch);
  }
}