<?php
require_once MVROOT."/aggregation_service/models/Content.class.php";
//require_once MVROOT."/aggregation_service/FeedExport.class.php";

class ContentImporter {
	public static $import_list=array();
	private static $has_init=false;
	
	public static function AddContent($c) {
		self::$import_list[]=$c;
	}
	
	public static function import() {
		self::$import_list=array();
		require_once "feeds/TNCFeed.class.php";

		$feed = new TNCFeed();
		$feed->request(0);
		
		
		if(!self::$has_init) {
			function cmp($a, $b) { return $a->created<$b->created; }
			usort(self::$import_list,"cmp");
			self::$has_init=true;
		}
		
		self::persist(self::$import_list);
	}
	
	public static function persist($data) {
		file_put_contents(MVROOT."/beatstore.json",json_encode($data));
	}
	
	public static function getData() {
		$fname=MVROOT."/beatstore.json";
		/*print_r(filemtime($fname));
		print_r("] [");
		print_r(time()-(24*60*60));
		exit(0);*/
		if(!file_exists($fname))
			self::import();
		elseif(filemtime($fname)<time()-(24*60*60))
			self::import();
		else
			self::$import_list=json_decode(file_get_contents($fname));
		
		return self::$import_list;
	}
	
}

?>