<?php
/*
  $Id: catalog/includes/functions/proforma.php, 20:57:52 NCB Exp $
 	Proforma v1.0 PLUS (Shareware Version)
 	COPYRIGHT (c) 2008 OPEN SOURCE SERVICES. ALL RIGHTS RESERVED.
  Support and Forum at http://www.open-source-services.com/Forum/Proforma-PF-v1.0-PLUS-Shareware-Version-osCommerce/
  Upgrade at http://www.open-source-services.com/Products/OSS-Premium-Add-Ons/Proforma-v1.0-PLUS.html?flypage=0
*/
require(DIR_WS_FUNCTIONS . 'proforma_data_access.php');

function doProformaContent($pf_pos){
	
	notifyDeployed($pf_pos);
	$content = getContentRows($pf_pos);
	$content = filterContent($content);
	
	if(count($content > 0 )) {
		$content = sortByPlacements($content);
	} else {
		$content = '';
	}
	
	for($b=0; $b < count($content); $b++){
		echo stripslashes($content[$b]['content']);
	}
}

function filterContent($content){
	global $language;
	include_once(DIR_WS_LANGUAGES . $language . '/proforma.php');
	
	$filtered = array();
		
	while($unfiltered = tep_db_fetch_array($content)) {
		$pages = explode(',', $unfiltered['pages']);
		
		if($unfiltered['active'] == PF_YES) {
			for($a=0; $a < count($pages); $a++){
				if(strpos(($_SERVER['SCRIPT_FILENAME'] . $_SERVER['QUERY_STRING']), $pages[$a])){
					$filtered[] = $unfiltered;
				}
			}
		}
	}
	return $filtered;
}

function sortByPlacements($content){

	$placements = array();
	$sorted = array();
	
	for($i=0; $i < count($content); $i++) {
		$placements[] = $content[$i]['placement'];
	}
	
	array_multisort($placements, SORT_ASC);
	
	for($x=0; $x < count($placements); $x++) {
		for($a=0; $a < count($content); $a++) {
			if($placements[$x] == $content[$a]['placement']){
				$sorted[] = $content[$a];
			}
		}		
	}
	
	return $sorted;
}
?>