You are here

function commerce_stock_admin_form in Commerce Stock 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_stock.admin.inc \commerce_stock_admin_form()

Commerce Stock admin form.

1 string reference to 'commerce_stock_admin_form'
commerce_stock_menu in ./commerce_stock.module
Implements hook_menu().

File

includes/commerce_stock.admin.inc, line 11
Administrative callbacks and form builder functions for Commerce Stock.

Code

function commerce_stock_admin_form($form, &$form_state) {

  // Find out what our status is. Use both the state of existing fields
  // and the state of variables to determine what's right.
  $field_name = 'commerce_stock';
  $field = field_info_field($field_name);
  $form['#tree'] = TRUE;
  $form['product_types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Enable stock management for these product types'),
    '#description' => t('Note that disabling stock management removes the Stock field from the product type, deleting any existing stock data for that product type.'),
  );
  $form['product_types_override'] = array(
    '#type' => 'fieldset',
    '#title' => t('Enable stock management override for these product types'),
    '#description' => t('Note that disabling stock management override removes the Stock override field from the product type, deleting any existing stock override data for that product type.'),
  );

  // Create a checkbox for each product type, set with the current stock-
  // enabled state.
  foreach (commerce_product_types() as $type => $product_type) {
    $instance[$type] = field_info_instance('commerce_product', 'commerce_stock', $type);
    $enabled[$type] = !empty($instance[$type]);
    $form['product_types'][$type] = array(
      '#type' => 'checkbox',
      '#default_value' => $enabled[$type],
      '#title' => t('@name (@machine_name)', array(
        '@name' => $product_type['name'],
        '@machine_name' => $type,
      )),
    );
    if ($enabled[$type]) {
      $instance[$type] = field_info_instance('commerce_product', 'commerce_stock_override', $type);
      $enabled[$type] = !empty($instance[$type]);
      $form['product_types_override'][$type] = array(
        '#type' => 'checkbox',
        '#default_value' => $enabled[$type],
        '#title' => t('Allow stock override for @name (@machine_name)', array(
          '@name' => $product_type['name'],
          '@machine_name' => $type,
        )),
      );
    }
  }

  // Add a checkbox that requires them to say "I do", but don't show it
  // (#access == FALSE) unless they're deleting.
  if (!empty($form_state['commerce_stock']['delete_instances'])) {
    $type_plural = format_plural(count($form_state['commerce_stock']['delete_instances']), 'type', 'types');
    $affirmation = t('I understand that all stock data will be permanently removed from the product @type_plural %product_types.', array(
      '@type_plural' => $type_plural,
      '%product_types' => implode(', ', $form_state['commerce_stock']['delete_instances']),
    ));
  }
  $form['confirmation'] = array(
    '#type' => 'checkbox',
    '#title' => !empty($affirmation) ? $affirmation : '',
    '#default_value' => FALSE,
    '#access' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  // If they're deleting, show the confirmation checkbox.
  if (!empty($form_state['commerce_stock']['delete_instances'])) {
    $form['confirmation']['#access'] = TRUE;
    drupal_set_message(t('You must click the confirmation checkbox to confirm that you want to delete stock data'), 'warning');
  }
  return $form;
}