<?php
// $Id$
 
/*
 * @file
 * Drupal Module: uc_radio_images
 * Adds ability to have images displayed with radio selection product attributes in Ubercart
 */

 /**
 * Implementation of hook_form_alter().
 */
 
function uc_radio_images_form_alter(&$form, &$form_state, $form_id){
	if (strstr($form_id, 'uc_product_add_to_cart_form_')) {
		
	    //first, loop over each attribute on the product
		if ($form['attributes']) {
		foreach ($form['attributes'] as $att_key => &$attribute) {
		  //if the attribute is type "radios" and the admin has enabled radio images, loop over the options in the attribute.
		  if ($attribute['#type'] == 'radios' && variable_get('uc_radio_images_attr' . $att_key , NULL) == 1) {
			foreach ($attribute['#options'] as $opt_key => &$option) {
			    //grab the image path for this attribute and option pair
			    $result = db_query("SELECT image_path FROM {uc_radio_images} WHERE aid = %d AND oid = %d", $att_key, $opt_key);
				if ($radio_image = db_result($result)) {
					//if image_path has a value, add the image to the option
					if ($radio_image != '') {
					    //insert the correct imagecache path for the preset radio_thumb
						$full_path = file_directory_path();
						$imagecache_path = '/' . $full_path . '/imagecache/radio_thumb';
						$option = '<img src="' . $imagecache_path . str_replace($full_path, '', $radio_image) . '" class="radio_image"/> <div class="radio_image_text">' . $option . '</div>';
					}
				}
			}
		  }
		}
		}
	}
	//Add the checkbox to the "edit attribute screen"
	if ($form_id == 'uc_attribute_form') {
	  $form['radio_images'] = array (
		'#type' => 'checkbox',
		'#title' => t('Display images for radio selections.'),
		'#description' => t('If you check this, an image is required for every option under this attribute.'),
		'#weight' => 1,
		'#default_value' => (int)variable_get('uc_radio_images_attr' . $form['aid']['#value'] , NULL),
	  );
	  $form['submit']['#weight'] = 5;
	  $form['#submit'][] = 'uc_radio_images_attribute_form_submit';
	}
	
	if ($form_id == 'uc_attribute_option_form') {
	  if (variable_get('uc_radio_images_attr' . $form['aid']['#value'], NULL) == 1) {
	  	$option = db_result(db_query("SELECT image_path FROM {uc_radio_images} WHERE oid = %d AND aid = %d", $form['oid']['#value'], $form['aid']['#value']));		
		if ($option) {
			$full_path = file_directory_path();
			$imagecache_path = '/' . $full_path . '/imagecache/radio_thumb';
			$form['delete_image'] = array (
				'#type' => 'checkbox',
				'#title' => t('Delete this image'),
				'#prefix' => '<img src="'. $imagecache_path . str_replace($full_path, '', $option) .'" />',
			);
		}
	    $form['radio_image'] = array (
		  '#type' => 'file',
		  '#title' => t('Select the image you want to appear for this option'),
		);

		$form['#attributes'] = array('enctype' => "multipart/form-data");
		$form['#submit'][] = 'uc_radio_images_option_form_submit';
	  }
	}
}
 
function uc_radio_images_attribute_form_submit($form, &$form_state) {
  //if radio button display type not selected, disable images no matter what.
  if ($form_state['values']['display'] != 2 || $form_state['values']['radio_images'] == 0) {
    variable_set('uc_radio_images_attr' . $form_state['values']['aid'] , 0);
  } elseif($form_state['values']['radio_images'] == 1) {
	variable_set('uc_radio_images_attr' . $form_state['values']['aid'] , 1);
  }    
}

function uc_radio_images_option_form_submit ($form, &$form_state) {
	$attribute_id = $form_state['values']['aid'];
	$option_id = $form_state['values']['oid'];	
	
	$validators = array(
	  'file_validate_extensions' => array('jpg', 'gif', 'png'),
	);
	
	if ($form_state['values']['delete_image'] == 1) {
	  db_query("UPDATE {uc_radio_images} SET image_path = '' WHERE aid = %d AND oid = %d", $attribute_id, $option_id);
	  drupal_set_message('The image has been deleted.');
	}
	
		if ($image_file = file_save_upload('radio_image', $validators, file_directory_path())) { 
			$result = db_query("SELECT oid FROM {uc_radio_images} WHERE oid = %d AND aid = %d", $option_id, $attribute_id);
			if (db_result($result)) {
				db_query("UPDATE {uc_radio_images} SET image_path = '%s' WHERE aid = %d AND oid = %d", $image_file->filepath, $attribute_id, $option_id);
			} else {
				db_query("INSERT INTO {uc_radio_images} (aid, oid, image_path) VALUES(%d, %d, '%s')", $attribute_id, $option_id, $image_file->filepath);
			}
		    drupal_set_message('Image saved.','status');
		} else {
			drupal_set_message('Couldn\'t save file.','error');
		}

}

function uc_radio_images_theme() {
    return array(
        'insert_radio_images' => array(
            'arguments' => array($attribut => NULL),
        ),
    );
}