<?php
	require_once( "../local.config.inc.php" );
	FNTH_class( "CSV" );

	define( "COL_MONTH" , "Hónap" );
	define( "COL_NUM" , "Munkaajánlatok száma" );

	$filters = array();
	$filters["poster_acknowledged"] = "poster_acknowledged";
	$filters["admin_acknowledged"] = "admin_acknowledged";
	$filters["job_version"] = "job_version = 3";

	$trunc = (isset( $getparams["trunc"] )) ? $getparams["trunc"] : "month";

	$query = sprintf( "select date_trunc( '%s' , ctstamp ) as ym,count(*) as num from jobs where %s group by date_trunc( '%s' , ctstamp ) order by date_trunc( '%s' , ctstamp )",
		$trunc,
		implode( " and " , $filters ),
		$trunc,
		$trunc );

	$results = FNTH_db_get_resultset( $query );
	$csv = new FNTHCSV();
	$csv_type = (isset( $_SERVER["HTTP_USER_AGENT"] ) && stripos( $_SERVER["HTTP_USER_AGENT"] , "linux" )!==false) ? "gnumeric" : "excel";
	$csv->setType( $csv_type );

	$csv->addColumn( COL_MONTH );
	$csv->addColumn( COL_NUM );

	header( "Content-Type: application/vnd.ms-excel" );
	foreach ($results as $row) {
		$item = array();
		switch ($trunc) {
		case "month":
			$item[COL_MONTH]=substr( $row["ym"] , 0 , 7 );
			break;
		default:
			$item[COL_MONTH]=substr( $row["ym"] , 0 , 19 );
			break;
		}
		$item[COL_NUM]=$row["num"];
		$csv->addRow( $item );
	}

	echo $csv->exportToString();
?>
