<?php
/**************************************************
  CPG Photo Shop Plugin for Coppermine Photo Gallery
  *************************************************
  Copyright (c) 2006 Thomas Lange <stramm@gmx.net>
  *************************************************
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  *************************************************
  Coppermine version: 1.4.5
  Photo Shop version: 1.0
  $Revision: 1.0 $
  $Author: stramm $
***************************************************/

function item_price($cd_counter) { // all totals!! return[0] = photo price, return[1] = cd price, return[2] = to many items in cd error (if set)
	
	global $SHOP_CONFIG, $lang_photoshop;
	$return = array();
	foreach($_SESSION['photoshop']['cart'] as $key => $temp){//foreach count photo items

		$item_id = (isset($_SESSION['photoshop']['cart'][$key]['id'])) ? $_SESSION['photoshop']['cart'][$key]['id'] : '';
		$amount = (isset($_SESSION['photoshop']['cart'][$key]['amount'])) ? $_SESSION['photoshop']['cart'][$key]['amount'] : '';
		
		//get price from config
		$price = $SHOP_CONFIG[$item_id]['price'];
		$return[0] += $amount*$price;
	}	
	
	if ($cd_counter > 0) { //if we have an item added to cd
		$pricelist = array();
		foreach($SHOP_CONFIG as $key => $value){//foreach
			if ($value['type']=="cd") $pricelist[$key]=$value['max_items'];
		}
		arsort($pricelist);
		foreach ($pricelist as $key => $val) {
			if ($val >= $cd_counter){
				$pointer=$key;
				$check=true;
			} 
			
		}
		$return[3] = true;
		$return[2] = '';
		$return[1] = $SHOP_CONFIG[$pointer]['price'];
		if(!$check==true) {
			$return[3] = false;
			$return[2] = "<font color=\"red\">".$lang_photoshop['to_many_pics']."</font>&nbsp;<br />";
			$return[1] = '';
		}
	} else {
		$return[1] = '';//end if we have an item added to cd
		$return[3] = true;
	}
	return $return;
}

function photoshop_count_items() { // return['cd'] = number of cd items, return['photo'] = number of print photo items
	global $SHOP_CONFIG;
	$cd_counter = array();
	foreach($_SESSION['photoshop']['cart'] as $key => $temp){//foreach
		$amount = (isset($_SESSION['photoshop']['cart'][$key]['amount'])) ? $_SESSION['photoshop']['cart'][$key]['amount'] : '';
		$item_id = (isset($_SESSION['photoshop']['cart'][$key]['id'])) ? $_SESSION['photoshop']['cart'][$key]['id'] : '';
		if($item_id == 'CD') {
			$cd_counter['cd']++;
		} else {
			$cd_counter['photo'] = $cd_counter['photo'] + $amount;
		}
	
	}//end foreach 
	return $cd_counter;
}


function photoshop_refresh($redirect) {
			global $lang_info;
			
            pageheader($lang_info, "<META http-equiv=\"refresh\" content=\"0;url={$redirect}\">");
            pagefooter();
            ob_end_flush();
			exit;
}

function photoshop_pricelist(){

	global $lang_photoshop, $SHOP_CONFIG;
	starttable('100%', $lang_photoshop['pricelist'], 2);
	$cd_pricelist="";
	$photo_pricelist="";
	foreach($SHOP_CONFIG as $key => $value) {
		if ((is_numeric($key))) {
			if($SHOP_CONFIG[$key]['type']=="photo"){
				$photo_pricelist .= $SHOP_CONFIG[$key]['name']." - ".$lang_photoshop['USD']." ".$SHOP_CONFIG[$key]['price'] ."<br />";
			} else {
				$cd_pricelist .= $SHOP_CONFIG[$key]['name']." - ".$lang_photoshop['USD']." ".$SHOP_CONFIG[$key]['price'] ."<br />";
			}
		}
	}
	echo <<<EOT
					<tr style="font-weight:bold;">
						<td><p>{$lang_photoshop['photos']}</p></td>
						<td><p>{$lang_photoshop['cds']}</p></td>
					</tr>
					<tr style="font-size:10px;">
						<td align="left" width="50%" valign="top">
						{$photo_pricelist}
						</td>
						<td align="left" width="50%" valign="top">
						{$cd_pricelist}
						</td>
					</tr>
EOT;

endtable();
}

function photoshop_get_post_var($var)
{
    global $lang_errors;

    if (!isset($_POST[$var])) cpg_die(CRITICAL_ERROR, $lang_errors['param_missing'] . " ($var)", __FILE__, __LINE__);
    return $_POST[$var];
}

function photoshop_format_price($temp_price, $cd_counter, $error=''){
	global $lang_photoshop;

	$cd_price = number_format($temp_price[1],2);
	$photo_price = number_format($temp_price[0],2);
	$tot_price = number_format(($cd_price+$photo_price),2);
	if ($cd_price > 0) {
		$cd_price = "{$lang_photoshop['on_cd']}: {$cd_counter['cd']} {$lang_photoshop['pics']} :: $cd_price{$lang_photoshop['USD']}&nbsp;<br />";
	} else $cd_price = '';
	if ($photo_price > 0) {
		$photo_price = "{$lang_photoshop['for_print']}: {$cd_counter['photo']} {$lang_photoshop['pics']} :: $photo_price{$lang_photoshop['USD']}&nbsp;<br />";
	} else $photo_price = '';
	if ($tot_price > 0) {
		$total_price = "<hr size=\"1\" width=\"200\" align=\"right\" />&nbsp;";
		$total_price .= "{$lang_photoshop['total']}: {$tot_price}{$lang_photoshop['USD']}&nbsp;";
	} else $tot_price = '';

	echo <<<EOT
		<tr>
			<td colspan="6" align="center">
			<hr size="1" />
			</td>
		</tr>
		<tr>
			<td colspan="6" align="right">
				{$photo_price}
				{$cd_price}{$error}
				{$total_price}
				
			</td>
		</tr>
EOT;
}
?>
