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

/*******************************************************************************
 * Hook Functions (Drupal)
 ******************************************************************************/

/**
 * Implementation of hook_form_alter().
 */
function uc_ship2country_form_alter(&$form, $form_state, $form_id) { 
	if ((uc_product_is_product_form($form)) || ($form['#node']->type == 'product_kit')){

		$form['shipping']['countries'] = array(
				'#type' => 'fieldset',
				'#title' => t('Shippable countries'),
				'#description' => t('Select the countries where this product can be shipped. All countries apply if no country is selected.'),
				'#tree' => TRUE,
				'#collapsible' => TRUE,
      	'#collapsed' => TRUE,
		);	
		
		// load data
		$result = db_fetch_object(db_query("SELECT * FROM {uc_ship2country} WHERE nid = %d", $form['#node']->nid));
		$data = unserialize($result->data);
		
		$result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
		while ($country = db_fetch_object($result)) {
			$form['shipping']['countries'][$country->country_id] = array(
			'#type' => 'checkbox',
			'#title' => $country->country_name,
			'#default_value' => $data[$country->country_id],
			);
		}		
 }
 
 if($form_id == 'uc_cart_checkout_form') {
		$form['#validate'][] = 'uc_ship2country_validate';
 }
 
 // check cart form if item can be shipped to this country
 /*
 if(strstr($form_id, 'uc_product_add_to_cart_form_')) {
		$result = db_fetch_object(db_query("SELECT * FROM {uc_ship2country} WHERE nid = %d", $form['nid']['#value']));
			if($result){
				$data = unserialize($result->data);
				if($data[$_SESSION['country_id']]==0){
					$form['submit']['#attributes'] = array('disabled' => 'disabled');	
				}		
			}
 }
 */
}

/**
 * Implementation of hook_nodeapi().
 */
function uc_ship2country_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
	if (($node->type == 'product') || ($node->type == 'product_kit')) {
		switch ($op) {
			case 'insert':
      case 'update':
        db_query("DELETE FROM {uc_ship2country} WHERE nid = %d", $node->nid);
        
				if(count($node->countries)==0)
					break;
				
				if (in_array(1, $node->countries)) {
          db_query("INSERT INTO {uc_ship2country} (nid, data) VALUES (%d, '%s')",
            $node->nid, serialize($node->countries)
          );
        }
      break;
		}
	}
}

// Validate the order against the shippable countries
function uc_ship2country_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'];
		$items = array();
		
		// run thought the cart
		foreach($order->products as $product){
			$result = db_fetch_object(db_query("SELECT * FROM {uc_ship2country} WHERE nid = %d", $product->nid));
			if($result){
				$data = unserialize($result->data);
				// check if is shippable
				if($data[$country_id]==0){
					$items[] = $product->title;
				}		
			}
		}

		if(count($items)!=0){
			$country = db_fetch_object(db_query("SELECT country_name FROM {uc_countries} WHERE country_id = %d", $country_id));
			form_set_error('', t('We are sorry to inform you that the next products are not shippable to your selected country: ') .
										 			$country->country_name .
													theme('item_list', $items));
		}

	}
}


