You are here

function uc_shipping_zones_admin_validate in Ubercart Global Quote 7

Same name and namespace in other branches
  1. 6 uc_shipping_zones/uc_shipping_zones_admin.inc \uc_shipping_zones_admin_validate()

File

uc_shipping_zones/uc_shipping_zones_admin.inc, line 165
Administration pages for uc_shipping_zones

Code

function uc_shipping_zones_admin_validate($form, &$form_state) {

  // Cancel edit
  if ($form_state['input']['op'] == t('Cancel')) {
    $form_state['redirect'] = 'admin/store/settings/quotes/methods/zones';
    return;
  }
  $cerror = NULL;
  $rerror = NULL;
  $values = $form_state['values'];
  if (!isset($values['zones']) || !is_array($values['zones']) || !array_search(array(
    'delete' => 1,
  ), $values['zones'])) {
    if (!$values['zone']['name']) {
      form_set_error('name', t('Enter a name for the zone'));
    }
    if (!is_array($values['zone']['country']) || count($values['zone']['country']) < 1) {
      form_set_error('country', t('You must select a country for your zone'));
    }
    else {

      // Check overlapping zones.
      $zones = uc_shipping_zones_get();
      $cerror = array();
      $rerror = array();
      if (isset($values['zone']['region'][''])) {
        $values['zone']['region'] = NULL;
      }
      foreach ($zones as $zone) {

        // Not the same outdated zone
        if (!$values['zone']['zid'] || $values['zone']['zid'] != $zone->zid) {
          $check_regions = FALSE;

          // Check for overlapping countries
          foreach ($values['zone']['country'] as $vc) {

            // Country defined in another zone
            if (in_array($vc, explode('|', $zone->countries))) {

              // all regions
              if ($values['zone']['region'] == NULL || $zone->regions == NULL) {
                $cerror[] = $vc;
              }
              else {
                $check_regions = TRUE;
              }
            }
          }

          // Check for region overlapping
          if ($check_regions) {
            foreach ($values['zone']['region'] as $vr) {
              if (in_array($vr, explode('|', $zone->regions))) {
                $rerror[] = $vr;
              }
            }
          }
        }
      }
      if (count($cerror)) {
        form_set_error('country', t('The following countries are selected in other zones: %list', array(
          '%list' => implode(', ', $cerror),
        )));
      }
      if (count($rerror)) {
        form_set_error('region', t('The following regions are selected in other zones: %list', array(
          '%list' => implode(', ', $rerror),
        )));
      }
    }
  }
}