You are here

function basic_cart_admin_content_type_submit in Basic cart 7.3

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

Callback submit function for the settings page.

File

./basic_cart.admin.inc, line 123
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)) {
    foreach (basic_cart_get_fields() as $field_name => $field_) {

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

      // If the field does not exist then create it.
      if (empty($field)) {
        $field = array(
          'field_name' => $field_name,
          'type' => $field_['type'],
          '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 (basic_cart_get_fields() as $field_name => $field_) {

          // Foreach checked content type, we must assign the price field to the content type.
          $instance = field_info_instance('node', $field_name, $type);
          if (empty($instance)) {
            $instance = array(
              'field_name' => $field_name,
              'label' => $field_['title'],
              'description' => $field_['description'],
              'entity_type' => 'node',
              'bundle' => $type,
            );

            // It doesn't exist. Create it.
            field_create_instance($instance);
          }
        }
      }
      else {
        foreach (basic_cart_get_fields() as $field_name => $field_) {
          $instance = field_info_instance('node', $field_name, $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.
      $vat = field_info_field('vat');

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

        // 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.'));
}