<?php
/**
 * @file Provides shipcountryrestriction options
 */

/**
 * Implementation of hook_help().
 *
 * @param string $section
 * @return string
 */
function uc_shipcountryrestriction_help($path, $arg) {
  switch ($section) {
  case 'admin/store/settings/shipcountryrestriction':
    $output = '<p>'. t('This module will ensure that shippable products can only be sold if they are being delivered to a specific country(s).') .'</p>';
    return $output;
}
}

/**
 * Implementation of hook_menu().
 *
 * Called when Drupal is building menus.  Cache parameter lets module know
 * if Drupal intends to cache menu or not - different results may be
 * returned for either case.
 *
 * @param may_cache true when Drupal is building menus it will cache
 *
 * @return An array with the menu path, callback, and parameters.
 */
function uc_shipcountryrestriction_menu() {
  $items = array();

  $items['admin/store/settings/shipcountryrestriction'] = array(
    'title'              => 'Ship Country Restriction',
    'access arguments'   => array('administer store'),
    'page callback'      => 'drupal_get_form',
    'description'        => 'Allow products to be delivered to select countries only.',
    'page arguments'     => array('uc_shipcountryrestriction_admin_settings'),
    'type'               => MENU_NORMAL_ITEM,
  );
  return $items;
}


/**
 * Admin Settings
 *
 * @return Forms for store administrator to set configuration options.
 */
function uc_shipcountryrestriction_admin_settings() {
  $countries = uc_country_select(uc_get_field_name('country'));

  $product_classes = uc_product_types();
  foreach($product_classes as $class){
    $country_selected = variable_get('uc_shipcountryrestriction_country_list_'.$class, $settings['countries']);
    $form[$class]['uc_shipcountryrestriction_country_list_'.$class] = $countries;
    $form[$class]['uc_shipcountryrestriction_country_list_'.$class]['#title']=t('Product class "%class": select countries which you not permit products to be delivered to.', array('%class' => $class));
    $form[$class]['uc_shipcountryrestriction_country_list_'.$class]['#type']='checkboxes';
    $form[$class]['uc_shipcountryrestriction_country_list_'.$class]['#default_value'] = ($country_selected == '' ? array() : $country_selected);
  }

  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter().
 */
function uc_shipcountryrestriction_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'uc_cart_checkout_form') {
    $form['#validate'][] = 'uc_shipcountryrestriction_validate';
  }
}

// Validate the order against the shippable countries
function uc_shipcountryrestriction_validate($form, &$form_state) {
  if (($order_id = intval($_SESSION['cart_order'])) > 0) {
    $order = uc_order_load($order_id);
    $country_id = $form_state['values']['panes']['delivery']['delivery_country'];
    $country = db_fetch_object(db_query("SELECT country_name FROM {uc_countries} WHERE country_id = %d", $country_id));
    $items = array();

    // run through the cart
    foreach ($order->products as $product) {
      $product_node = node_load($product->nid);
      if ($product_node->shippable=='1') {
        $var_countries = variable_get('uc_shipcountryrestriction_country_list_'.$product_node->type, '');
        //Check if the delivery address is a shippable country
        if ($var_countries[$country_id] != 0) {
          $items[] = $product_node->title;
        }
      }
    }

    if(count($items) > 0){
      form_set_error('panes][delivery][delivery_country', t('Sorry, but we do not deliver the following products to <b>!country</b>', array('!country' => $country->country_name)). theme('item_list', $items));
    }
  }
}
