<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

include_once './common.php';

function get_archive_metadata($root,$path){

	$myArrayRT 	 = array ();
	$my_title 	 = "";
	$my_abstract = "";

	include("$root$path/config.php");

	if(is_array($config['title'])==true){
		$my_title 	 = $config['title']['fr'];
		$my_abstract = $config['abstract']['fr'];
	}else {
		$my_title 	 = $config['title'];
		$my_abstract = $config['abstract'];
	}
	
		//print ($path." => ".$my_title."\n");

	$myArrayRT['title'] 	= htmlentities(utf8_decode($my_title));
	$myArrayRT['abstract']	= htmlentities(utf8_decode($my_abstract));
	$myArrayRT['path']		= $path;
	$myArrayRT['metadata']  = array ();
	
	if(is_array($config['metadata'])==false){
		if(!isset($config['duration'])){$config['duration']=0;}
			$myArrayRT['metadata'][0]=array (
					'url'=>$config['metadata'],
					'duration'=>$config['duration']);
	}else{
		// test si c'est un tableaux de traduction 
		if(!isset($config['metadata']['fr']) ){

			// sinon 
			foreach ($config['metadata'] as $key => $value) {
				if(!isset($value['duration'])){$value['duration']=0;}
				if(!isset($value['metadata'])){$value['metadata']=null;}
				if(!isset($value['display'])){$value['display']=null;}
	
	
				$tmetaa	= array (
						'url'=>$value['url'],
						'duration'=>$value['duration'],
						'display'=>$value['display']
						);
	
				array_push($myArrayRT['metadata'],$tmetaa);
			}

		}else{
				$tmetaa	= array (
						'url'=>$config['metadata']['fr'],
						'duration'=>$config['duration'],
						'display'=>null
						);
	
				array_push($myArrayRT['metadata'],$tmetaa);

		}
	}

	return $myArrayRT;
}
function display_archives_list_metadata($archives_list, $url_root, $basepath) {
    
	$myJson = array ();

	for($i=0;$i<count($archives_list);$i++) {
		
		$archive_ref = $archives_list[$i];
		$archive_name = $archive_ref;
		if(is_array($archive_ref)) {
			$archive_name = $archive_ref[0];
		}

		array_push($myJson,get_archive_metadata($basepath, $archive_name));

	}
	echo (json_encode($myJson));
}

display_archives_list_metadata($archives_list, URL_ROOT, dirname(__FILE__)."/");

?>
