You are here

function uc_discounts_admin_discount_edit_form_submit in Ubercart Discounts (Alternative) 7.2

Submit handler for uc_discounts_admin_discount_edit_form().

See also

uc_discounts_admin_discount_edit_form()

uc_discounts_admin_discount_edit_form_validate()

File

uc_discounts/uc_discounts.admin.inc, line 803
Admin forms and functions for uc_discounts module.

Code

function uc_discounts_admin_discount_edit_form_submit($form, &$form_state) {
  $codes = explode("\n", $form_state['values']['codes']);

  // Set expiration to a valid value because it is not nullable.
  $expiration = isset($form_state['values']['expiration']) ? strtotime($form_state['values']['expiration']) : 0;

  //date_convert(date_make_date($form_state['values']['expiration']), DATE_OBJECT, DATE_UNIX) : 0;
  $activates_on = isset($form_state['values']['activates_on']) ? strtotime($form_state['values']['activates_on']) : 0;

  //date_convert(date_make_date($form_state['values']['activates_on']), DATE_OBJECT, DATE_UNIX) : 0;

  // Set discount_amount.
  if (($index = strpos($form_state['values']['discount_amount'], '%')) !== FALSE) {
    $discount_amount = floatval(substr($form_state['values']['discount_amount'], 0, $index)) / 100;
  }
  else {
    $discount_amount = floatval($form_state['values']['discount_amount']);
  }
  $discount = new stdClass();
  $discount->name = $form_state['values']['name'];
  $discount->short_description = $form_state['values']['short_description'];
  $discount->description = $form_state['values']['description'];
  $discount->qualifying_type = $form_state['values']['qualifying_type'];
  $discount->qualifying_amount = $form_state['values']['qualifying_amount'];
  $discount->has_qualifying_amount_max = $form_state['values']['has_qualifying_amount_max'];
  $discount->qualifying_amount_max = $form_state['values']['qualifying_amount_max'];
  $discount->discount_type = $form_state['values']['discount_type'];
  $discount->discount_amount = $discount_amount;
  $discount->requires_code = $form_state['values']['requires_code'];
  $discount->filter_type = $form_state['values']['filter_type'];
  $discount->has_role_filter = $form_state['values']['has_role_filter'];
  $discount->use_only_discounted_products_to_qualify = $form_state['values']['use_only_discounted_products_to_qualify'];
  $discount->requires_single_product_to_qualify = $form_state['values']['requires_single_product_to_qualify'];
  $discount->required_product_type = $form_state['values']['required_product_type'];
  $discount->add_to_cart = $form_state['values']['add_to_cart'];
  $discount->max_times_applied = $form_state['values']['max_times_applied'];
  $discount->limit_max_times_applied = $form_state['values']['limit_max_times_applied'];
  $discount->can_be_combined_with_other_discounts = $form_state['values']['can_be_combined_with_other_discounts'];
  $discount->max_uses = $form_state['values']['max_uses'];
  $discount->max_uses_per_user = $form_state['values']['max_uses_per_user'];
  $discount->max_uses_per_code = $form_state['values']['max_uses_per_code'];
  $discount->has_expiration = $form_state['values']['has_expiration'];
  $discount->expiration = $expiration;
  $discount->has_activation = $form_state['values']['has_activation'];
  $discount->activates_on = $activates_on;
  $discount->is_active = $form_state['values']['is_active'];
  $discount->weight = $form_state['values']['weight'];
  if (empty($form_state['values']['discount_id'])) {
    $discount->insert_timestamp = REQUEST_TIME;
    drupal_write_record('uc_discounts', $discount);
  }
  else {
    $discount->discount_id = $form_state['values']['discount_id'];
    drupal_write_record('uc_discounts', $discount, 'discount_id');

    // Delete existing related fields.
    uc_discounts_codes_delete($discount->discount_id);
    uc_discounts_products_delete($discount->discount_id);
    uc_discounts_terms_delete($discount->discount_id);
    uc_discounts_skus_delete($discount->discount_id);
    uc_discounts_classes_delete($discount->discount_id);
    uc_discounts_authors_delete($discount->discount_id);
    uc_discounts_roles_delete($discount->discount_id);
  }

  // Insert codes if necessary.
  if ($form_state['values']['requires_code']) {
    foreach ($codes as $code) {
      $code = trim($code);
      if (empty($code)) {
        continue;
      }
      $new_code = array(
        'discount_id' => $discount->discount_id,
        'code' => $code,
      );
      drupal_write_record('uc_discounts_codes', $new_code);
    }
  }

  // Insert required products.
  foreach (array(
    UC_DISCOUNTS_GROUPING_APPLICATION,
    UC_DISCOUNTS_GROUPING_QUALIFICATION,
  ) as $grouping) {
    $name = $grouping == UC_DISCOUNTS_GROUPING_APPLICATION ? 'filter_type' : 'required_product_type';
    if ($form_state['values'][$name] == UC_DISCOUNTS_FILTER_TYPE_PRODUCTS) {
      $ids = $form_state['values'][$grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_PRODUCTS];
      $ids = in_array(UC_DISCOUNTS_OPTION_ALL_PRODUCTS, $ids) ? array(
        UC_DISCOUNTS_OPTION_ALL_PRODUCTS,
      ) : array_filter($ids);
      foreach ($ids as $id) {
        $new_product = array(
          'discount_id' => $discount->discount_id,
          'product_id' => $id,
          'grouping' => $grouping,
        );
        drupal_write_record('uc_discounts_products', $new_product);
      }
    }
    elseif ($form_state['values'][$name] == UC_DISCOUNTS_FILTER_TYPE_TERMS) {
      $ids = $form_state['values'][$grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_TERMS];
      $ids = in_array(UC_DISCOUNTS_OPTION_ALL_TERMS, $ids) ? array(
        UC_DISCOUNTS_OPTION_ALL_TERMS,
      ) : array_filter($ids);
      foreach ($ids as $id) {
        $new_term = array(
          'discount_id' => $discount->discount_id,
          'term_id' => $id,
          'grouping' => $grouping,
        );
        drupal_write_record('uc_discounts_terms', $new_term);
      }
    }
    elseif ($form_state['values'][$name] == UC_DISCOUNTS_FILTER_TYPE_SKUS) {
      $ids = $form_state['values'][$grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_SKUS];

      // Empty SKU is UC_DISCOUNTS_OPTION_ALL_SKUS.
      $ids = in_array(UC_DISCOUNTS_OPTION_ALL_SKUS, $ids) ? array() : array_filter($ids);
      foreach ($ids as $id) {
        $new_sku = array(
          'discount_id' => $discount->discount_id,
          'sku' => $id,
          'grouping' => $grouping,
        );
        drupal_write_record('uc_discounts_skus', $new_sku);
      }
    }
    elseif ($form_state['values'][$name] == UC_DISCOUNTS_FILTER_TYPE_CLASS) {
      $ids = $form_state['values'][$grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_CLASS];

      // Empty Class is UC_DISCOUNTS_OPTION_ALL_CLASSES.
      $ids = in_array(UC_DISCOUNTS_OPTION_ALL_CLASSES, $ids) ? array() : array_filter($ids);
      foreach ($ids as $id) {
        $new_class = array(
          'discount_id' => $discount->discount_id,
          'class' => $id,
          'grouping' => $grouping,
        );
        drupal_write_record('uc_discounts_classes', $new_class);
      }
    }
    if ($form_state['values'][$name] == UC_DISCOUNTS_FILTER_TYPE_AUTHORS) {
      $ids = $form_state['values'][$grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_AUTHORS];
      $ids = in_array(UC_DISCOUNTS_OPTION_ALL_AUTHORS, $ids) ? array(
        UC_DISCOUNTS_OPTION_ALL_AUTHORS,
      ) : array_filter($ids);
      foreach ($ids as $id) {
        $new_author = array(
          'discount_id' => $discount->discount_id,
          'author_id' => $id,
          'grouping' => $grouping,
        );
        drupal_write_record('uc_discounts_authors', $new_author);
      }
    }
  }

  // Insert roles.
  if ($form_state['values']['has_role_filter']) {
    $role_ids = $form_state['values']['has_role_filter'] ? $form_state['values']['role_ids'] : array();
    $role_ids = in_array(UC_DISCOUNTS_OPTION_ALL_ROLES, $role_ids) ? array(
      UC_DISCOUNTS_OPTION_ALL_ROLES,
    ) : array_filter($role_ids);
    foreach ($role_ids as $role_id) {
      $new_role = array(
        'discount_id' => $discount->discount_id,
        'role_id' => $role_id,
      );
      drupal_write_record('uc_discounts_roles', $new_role);
    }
  }
  foreach (module_implements('uc_discount') as $module) {
    $function = $module . '_uc_discount';
    $function('save', $discount);
  }
  drupal_set_message(t('%name settings have been saved.', array(
    '%name' => $form_state['values']['name'],
  )));
  $form_state['redirect'] = 'admin/store/uc_discounts/edit/' . $discount->discount_id;
}