You are here

function basic_cart_admin_content_type_submit in Basic cart 7.2

Same name and namespace in other branches
  1. 7.3 basic_cart.admin.inc \basic_cart_admin_content_type_submit()

Callback submit function for the settings page.

File

./basic_cart.admin.inc, line 199
Basic cart admin settings forms.

Code

function basic_cart_admin_content_type_submit($form, &$form_state) {

  // Remove internal Form API values.
  unset($form_state['values']['form_id'], $form_state['values']['form_token'], $form_state['values']['form_build_id'], $form_state['values']['op']);
  $product_types = array();

  // Setting up the price field for the selected content types.
  $content_types = $form_state['values']['basic_cart_content_types'];

  // unset to prevent 'double' save
  unset($form_state['values']['basic_cart_content_types']);
  if (!empty($content_types) && is_array($content_types)) {

    // Check to see if the price field already exists.
    $field = field_info_field('price');

    // If the price field does not exist then create it.
    if (empty($field)) {
      $field = array(
        'field_name' => 'price',
        'type' => 'number_decimal',
        'entity_types' => array(
          'node',
        ),
      );
      field_create_field($field);
    }
    foreach ($content_types as $type => $checked) {

      // If a node type is checked, then create the price field.
      if ($checked) {

        // save content_type as a product
        $product_types[$type] = $type;

        // Foreach checked content type, we must assign the price field to the content type.
        $instance = field_info_instance('node', 'price', $type);
        if (empty($instance)) {
          $instance = array(
            'field_name' => 'price',
            'label' => t('Price'),
            'description' => t('Please enter the price for this item.'),
            'entity_type' => 'node',
            'bundle' => $type,
          );

          // It doesn't exist. Create it.
          field_create_instance($instance);
        }
      }
      else {
        $instance = field_info_instance('node', 'price', $type);
        if (!empty($instance)) {
          field_delete_instance($instance);
        }
      }
    }
  }
  variable_set('basic_cart_content_types', $product_types);

  // Set VAT to nothing if the checkbox is unchecked.
  if (empty($form_state['values']['basic_cart_vat_state'])) {
    $form_state['values']['basic_cart_vat_value'] = '';

    // Check to see if the VAT instance exists and if so, delete it.
    if (module_exists('basic_cart_order')) {
      $instance = field_info_instance('node', 'vat', 'order');
      if (!empty($instance)) {
        field_delete_instance($instance);
      }
    }
  }
  else {

    // If the enable VAT checkbox is checked and if basic_cart_order is enabled,
    // then create the vat field for the Order content type.
    if (module_exists('basic_cart_order')) {

      // Check to see if the vat field already exists.
      $field = field_info_field('vat');

      // If the vat field does not exist then create it.
      if (empty($field)) {
        $field = array(
          'field_name' => 'vat',
          'type' => 'number_decimal',
          'entity_types' => array(
            'node',
          ),
        );
        field_create_field($field);

        // Assign the vat field to the Order content type.
        $instance = field_info_instance('node', 'vat', 'order');
        if (empty($instance)) {
          $instance = array(
            'field_name' => 'vat',
            'label' => t('VAT'),
            'description' => t('The VAT tax.'),
            'entity_type' => 'node',
            'bundle' => 'order',
          );

          // It doesn't exist. Create it.
          field_create_instance($instance);
        }
      }
    }
  }

  // Save other variables.
  foreach ($form_state['values'] as $key => $value) {
    if (is_array($value) && isset($form_state['values']['array_filter'])) {
      $value = array_keys(array_filter($value));
    }
    variable_set($key, $value);
  }
  drupal_set_message(t('The configuration options have been saved.'));
}