You are here

uc_shipping_zones.module in Ubercart Global Quote 7

Same filename and directory in other branches
  1. 6 uc_shipping_zones/uc_shipping_zones.module

Provides zone definitions for uc_global_quote

File

uc_shipping_zones/uc_shipping_zones.module
View source
<?php

/**
 * @file
 * Provides zone definitions for uc_global_quote
 */

/**
 * Implementation of hook_menu().
 */
function uc_shipping_zones_menu() {
  $items = array();
  $items['admin/store/settings/quotes/methods/zones'] = array(
    'title' => 'Shipping zones',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_shipping_zones_admin',
      6,
    ),
    'access arguments' => array(
      'configure quotes',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'uc_shipping_zones_admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function uc_shipping_zones_theme($form) {
  return array(
    'uc_shipping_zones_admin' => array(
      //'arguments' => array('form' => NULL, 'zid' => NULL),
      'render element' => 'form',
      'file' => 'uc_shipping_zones_admin.inc',
    ),
  );
}

/**
 * Get shipping zones
 *
 * @param $country
 * country name or code (optional)
 *
 * @param $region
 * region name or id (optional)
 *
 * @return
 * an array containing the shipping zones.
 */
function uc_shipping_zones_get($country = NULL, $region = NULL) {
  if (!$country) {
    $r = db_query("SELECT * FROM {uc_shipping_zones} ORDER BY name")
      ->fetchAll();
  }
  else {

    // Get country name or id
    $c = new stdClass();
    if (is_int($country)) {
      $c->name = uc_country_get_by_id($country);
      $c->id = $country;
    }
    else {
      $c->name = $country;
      $c->id = db_query("SELECT country_id FROM {uc_countries} WHERE country_name = :country", array(
        ':country' => $country,
      ))
        ->fetchObject()->country_id;
    }

    // Get region name or id
    if (is_int($region)) {
      $r = new stdClass();
      $r->name = uc_zone_get_by_id($region);
      $r->id = $region;
    }
    else {
      $r->name = $region;
      $r->id = db_query("SELECT zone_id FROM {uc_zones} WHERE zone_name = :region", array(
        ':region' => $region,
      ))
        ->fetchObject()->zone_id;
    }

    // Name + id
    $cnid = '%' . $c->name . ',' . $c->id . '%';
    $rnid = '%' . $r->name . ',' . $r->id . '%';

    // Willcard countries without regions
    if ($region < 1 && !db_query("SELECT zone_id FROM {uc_zones} WHERE zone_country_id = :cid", array(
      ':cid' => $c->id,
    ))
      ->rowCount()) {
      $row = db_query("SELECT * FROM {uc_shipping_zones} WHERE countries LIKE :cnid", array(
        ':cnid' => $cnid,
      ))
        ->fetchObject();
    }
    else {
      $row = db_query("SELECT * FROM {uc_shipping_zones} WHERE countries LIKE :cnid AND (regions LIKE :rnid OR (regions IS NULL OR regions = ''))\n                             ORDER BY name", array(
        ':cnid' => $cnid,
        ':rnid' => $rnid,
      ))
        ->fetchObject();
    }
    return $row;
  }
  return $r;
}

/**
 * Returns shipping zones ready to feed a selectbox
 *
 * @return
 * an array ready to feed a selectbox.
 */
function uc_shipping_zones_get_select() {
  $rows = array();
  $r = db_query("SELECT zid,name FROM {uc_shipping_zones} ORDER BY name")
    ->fetchAll();
  foreach ($r as $row) {
    $rows[$row->zid] = $row->name;
  }
  return $rows;
}

Functions

Namesort descending Description
uc_shipping_zones_get Get shipping zones
uc_shipping_zones_get_select Returns shipping zones ready to feed a selectbox
uc_shipping_zones_menu Implementation of hook_menu().
uc_shipping_zones_theme Implementation of hook_theme().