<?php

function pxmlCompany($query){
	global $xpath;
	global $firephp;
	$tabCompany = array();
	$items = $xpath->query($query);
	$countCompany = 0;
	$oldType ='';
	foreach ($items as $item){
		$type = $item->getAttribute('Type');
		if($type!=$oldType){$countCompany = 0;}
		//Name
		$name = $item->getElementsByTagName("Name")->item(0)->nodeValue;
		$tabCompany[$type][$countCompany]['Name'] = $item->getElementsByTagName("Name")->item(0)->nodeValue;
		//CompanyId
		$tabCompanyId = pxmlCompanyId($query.'[@Type="'.$type.'"]/CompanyId');
		if($tabCompanyId){ $tabCompany[$type][$countCompany]['CompanyId'] = $tabCompanyId;}
		//Country
		//@TODO
		//Address
		$tabAddress = pxmlAddress($query.'[@Type="'.$type.'"]['.($countCompany+1).']/Address');
		if($tabAddress){ $tabCompany[$type][$countCompany]['Address'] = $tabAddress;}
		//Amount
		//@TODO
		//FreeText
		//@TODO
		//Date
		$tabDate = pxmlDate($query.'[@Type="'.$type.'"]/Date');
		if($tabDate){ $tabCompany[$type][$countCompany]['Date'] = $tabDate;}
		
		$oldType = $type;
		$countCompany++;
	}
    return $tabCompany;
}

function pxmlCompanyId($query){
	global $xpath;
	global $firephp;
	$tabCompanyId = array();
	$items = $xpath->query($query);
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			$tabCompanyId[$type] = $item->nodeValue;
		}
		return $tabCompanyId;
	}else{
		return false;
	}
}

function pxmlDate($query){
	global $xpath;
	global $firephp;
	$tabDate = array();
	$items = $xpath->query($query);
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			$format = $item->getAttribute('Format');
			$value = $item->nodeValue;
			
			//Formattage de la date
			switch($format){
				case "CCYY":
					$date = $value;
					break;
				case "CCYYMMDD":
					$date = substr($value,6,2).'/'.substr($value,4,2).'/'.substr($value,0,4);
					break;		
				case "DD":
					$date = $value;
					break;
				case "DDMM":
					$date = $value;
					break;
				case "DDMMCCYY":
					$date = $value;
					break;
				case "MM":
					$timestamp = mktime(0, 0, 0, date("m")-$value, 1, date("Y"));
					$date = date('m/Y',$timestamp);
					break;
				case "MMCCYY":
					$date = $value;
					break;
				case "MMDDCCYY":
					$date = substr($value,2,2).'/'.substr($value,0,2).'/'.substr($value,4,4);
					break;
				case "DD/MM/CCYY":
					$date = $value;
					break;
				default:
					$date = $value;
			}
			$tabDate[$type] = $date;
		}
		return $tabDate;
	}else{
		return false;
	}
}

function pxmlName($query){
	global $xpath;
	global $firephp;
	$tabName = array();
	$items = $xpath->query($query);
	$countName = 0;
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type !='' ){ $index = $type; }else{$index = $countName;} 
			$tabName[$index] = $item->nodeValue;
			$countName++;
		}
	    return $tabName;
	}else{
		return false;
	}
}

function pxmlAddress($query){
	global $xpath;
	global $firephp;
	$tabAddress = array();
	$items = $xpath->query($query);
	$countAddress = 0;
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type !='' ){ $index = $type; }else{$index = $countAddress;} 
			//Liste des elements à parser
			$elements = array('EntireAddress', 'Building', 'Street', 'Town', 'City', 'County', 'PostCode', 'Country');
			foreach($elements as $elementName){
				if($emelmentName == 'Country'){
					
				}else{
					$value = $item->getElementsByTagName($elementName)->item(0)->nodeValue;		
					if ($value) { $tabAddress[$index][$elementName] = $value; }
				}
			}
			$countAddress++;
		}
	    return $tabAddress;
	}else{
		return false;		
	}
}

function pxmlCommunication($query){
global $xpath;
	global $firephp;
	$tabCommunication = array();
	$items = $xpath->query($query);
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type != '' ){ 	
				$tabCommunication[$type] = $item->nodeValue;
			}
		}
	    return $tabCommunication;
	}else{
		return false;		
	}
}

function pxmlCountry($query){
	global $xpath;
	global $firephp;
	$tabCountry = array();
	$items = $xpath->query($query);
	$countCountry = 0;
	if($items->length>0){
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type !='' ){ $tabName[$countCountry]['Type'] = $type; } 
			$dialcode = $item->getAttribute('DialCode');
			if($dialcode !='' ){ $tabName[$countCountry]['DialCode'] = $dialcode; } 
			$isocode = $item->getAttribute('ISOCode');
			if($isocode !='' ){ $tabName[$countCountry]['ISOCode'] = $isocode; } 
			$tabName[$countCountry]['Value'] = $item->nodeValue;
			$countCountry++;
		}
	    return $tabCountry;
	}else{
		return false;
	}
}

function pxmlCreditRating($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	$count = 0;
	if($items->length>0){
		foreach ($items as $item){
			//Get type of Credit Rating
			$type = $item->getAttribute('Type');
			$units = $item->getAttribute('Units');
			if($units !='' ){ $tab[$type]['Units'] = $units; }	
			switch($type){
				case "Credit_Score" :
					$tab[$type]['Value'] = $item->firstChild->nodeValue; 
					$freetextList = $item->getElementsByTagName('FreeText');
					foreach ($freetextList as $freetext){
						$tab[$type]['Text'][$freetext->getAttribute('Type')] = $freetext->nodeValue;
					}
					break;
				case "Previous_Credit_Score" :
					$tab[$type]['Value'] = $item->firstChild->nodeValue; 
					$freetextList = $item->getElementsByTagName('FreeText');
					foreach ($freetextList as $freetext){
						$tab[$type]['Text'][$freetext->getAttribute('Type')] = $freetext->nodeValue;
					}
					break;
				case "Maximum_Credit_Guide" :
					$amount = $item->getElementsByTagName('Amount')->item(0)->nodeValue;
					$tab[$type]['Currency']= $item->getElementsByTagName('Currency')->item(0)->nodeValue;
					$tab[$type]['Value'] = $amount;
					$freetextList = $item->getElementsByTagName('FreeText');
					foreach ($freetextList as $freetext){
						$tab[$type]['Text'][$freetext->getAttribute('Type')] = $freetext->nodeValue;
					}
					break;
			}	
			$count++;
		}
	    return $tab;
	}else{
		return false;
	}
}

function pxmlDirectorsDetail($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $item) {  
			//Traitement des attributs
			$type = $item->getAttribute('Type');
			if($type !='' ){ $tab[$count]['Type'] = $type; } 
			$title = $item->getAttribute('Title');
			if($title !='' ){ $tab[$count]['Title'] = $title; }	
			$nationality = $item->getAttribute('Nationality');
			if($nationality !='' ){ $tab[$count]['Nationality'] = $nationality; }
			//Traitement des éléments
			$name = pxmlName($query.'['.($count+1).']/Name');
			if ($name) $tab[$count]['Name'] = $name;
			$address = pxmlAddress($query.'['.($count+1).']/Address');
			if ($address) $tab[$count]['Address'] = $address;
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlEmployees($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	$count = 0;
	if($items->length>0){
		foreach ($items as $item) {  
			//Traitement des attributs
			$type = $item->getAttribute('Type');
			if($type !='' ){ $index = $type;  }else{ $index = $count; }
			$tab[$index] = $item->nodeValue;		
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlFinancialSummary($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	$elements = array('FreeText', 'Amount', 'Date');
	if($items->length>0){
		foreach ($items as $item){
			foreach($elements as $elementName){
				switch($elementName){
					case 'FreeText':
						$freetextList = $item->getElementsByTagName('FreeText');
						foreach ($freetextList as $freetext){
							$tab['Text'][$freetext->getAttribute('Type')] = $freetext->nodeValue;
						}
						break;
					case 'Amount':
						$amount = $item->getElementsByTagName('Amount')->item(0)->nodeValue;
						$tab['Amount']['Currency']= $item->getElementsByTagName('Currency')->item(0)->nodeValue;
						$tab['Amount']['Value'] = $amount;
						break;
					case 'Date':
						$date = pxmlDate($query.'/Date');
						if($date) $tab['Date'] = $date;
						break;
				}
			}
		}
		return $tab;
	}else{
		return false;
	}
}
function pxmlLegalForm($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type !='' ){ $index = $type;  }else{ $index = $count; }
			$tab[$index] = $item->nodeValue;
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}
function pxmlShareCapitalSummary($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		foreach ($items as $item){
			//FreeText
			$freetextList = $item->getElementsByTagName('FreeText');
			foreach ($freetextList as $freetext){
				$tab['Text'][$freetext->getAttribute('Type')] = $freetext->nodeValue;
			}
			//ShareCapital
			$sharecapitalList = $item->getElementsByTagName('Share_Capital');
			$countShareCapital = 0;
			foreach ($sharecapitalList as $sharecapital){
				//Amount
				$type = $sharecapital->getElementsByTagName('Amount')->item(0)->getAttribute('Type');
				$tab['ShareCapital'][$countShareCapital]['Amount'][$type] = $sharecapital->getElementsByTagName('Amount')->item(0)->nodeValue;				
				//Currency
				$tab['ShareCapital'][$countShareCapital]['Currency'] = $sharecapital->getElementsByTagName('Currency')->item(0)->nodeValue;
				$tab['ShareCapital'][$countShareCapital]['FreeText'] = $sharecapital->getElementsByTagName('FreeText')->item(0)->nodeValue;
				$countShareCapital++;				
			}
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlTaxonomy($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $item){
			$value = $item->nodeValue;
			if($value){ $tab[$count]['Value'] = $value;}
		
			$type = $item->getAttribute('Type');
			if($type !='' ){ $tab[$count]['Type'] = $type;}
			
			$code = $item->getAttribute('Code');
			if($code !='' ){ $tab[$count]['Code'] = $code;}
			
			$item = $item->getAttribute('Item');
			if($item !='' ){ $tab[$count]['Item'] = $item;}
			
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlFreeText($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		$oldType = '';
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			if($type != $oldType){ $count = 0; }
			$value = $item->nodeValue;		
			if($value){ $tab[$type][$count] = $value;}
			
			$oldType = $type;
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlAccountItem($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	
	//Pour chaque AccountItem
		//Lire chaque AccountPeriod
		//Pour chaque AccountPeriod
			//Classer les AccoutElement
	if($items->length>0){
		$count = 0;
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			$units = $item->getAttribute('Units');
			$tab[$type]['Units'] = $units; 
			$tab[$type]['AccountingPeriod'] = pxmlAccountingPeriod($query.'[@Type="'.$type.'"]/AccountingPeriod');	
			$tab[$type]['AccountElement'] = pxmlAccountElement($query.'[@Type="'.$type.'"]/AccountElement');
			$count++;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlAccountingPeriod($query){
	global $xpath;
	global $firephp;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $AccountingPeriod){
			$item = $AccountingPeriod->getAttribute('Item');
			//@todo : Intégrer avec la fonction de parsing de la date
			$date = $AccountingPeriod->getElementsByTagName('Date')->item(0)->nodeValue;
			$currency = $AccountingPeriod->getElementsByTagName('Currency')->item(0)->nodeValue;
			$tab[$item]['Date'] = $date;
			$tab[$item]['Currency'] = $currency;
		}
		return $tab;
	}else{
		return false;
	}
}

function pxmlAccountElement($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $item){
			$type = $item->getAttribute('Type');
			$tab[$type] = pxmlAmount($query.'[@Type="'.$type.'"]/Amount');
		}
		return $tab;
	}else{
		return false;
	}	
}

function pxmlAmount($query){
	global $xpath;
	$tab = array();
	$items = $xpath->query($query);
	if($items->length>0){
		$count = 0;
		foreach ($items as $item){
			$indexItem = $item->getAttribute('Item');
			if($indexItem){ $index = $indexItem; }
			$amount = $item->nodeValue;
			$tab[$index] = $amount;
			$count++;
		}
		return $tab;
	}else{
		return false;
	}	
}