
<?php

if(isset($_GET["exportacionXLS"])) {
	//header('Content-type: application/vnd.ms-excel;charset=iso-8859-15');
	//header('Content-Disposition: attachment; filename=nombre_archivo.xls');
	
		global $wpdb;
			$wpdb->show_errors(); 
							
		// Grab any post values you sent with your submit function
		
		$var_fecha_ini=$_GET['fechaIni'];
		$var_fecha_fin=$_GET['fechaFin'];
		
		 $varDominio=$_GET['dominio'];
		 $varUsuario=$_GET['usuario'];
		 $var_concepto=$_GET['concepto'];
		 
		 $email='%'.$varDominio.'%';
	
		
		$DownloadReportFrom = $var_fecha_ini;
		$DownloadReportTo = $var_fecha_fin;
		
		$query='select display_name usuario,user_email correo, empresa.dominio empresa,fecha_lectura,t.name concepto,post_title titulo,guid enlace
			from (SELECT user_id,object_id,date fecha_lectura  FROM wp_user_relations where object_type =\'browse\' and date between %s and %s ) grupo ';
		$query=$query.'STRAIGHT_JOIN 
			(select substring(mail1,1,position(\'.\' in mail1)-1) dominio,user_email,mail1,id,display_name 
			from 
				( select substring(user_email,position(\'@\'in user_email)+1,(length(user_email)-position(\'@\' in user_email))) mail1,user_email,id,display_name 
					from wp_users ';
		if ($varUsuario){
			$query=$query.' where id=%d ';
		} else {
			if ($varDominio){
				$query=$query.' where user_email  like %s ';
			}
		}
		
		$query=$query.') 
				tabla)
			 empresa on empresa.id=grupo.user_id ';
   		 $query=$query.'STRAIGHT_JOIN wp_posts p on p.ID=grupo.object_id 
   		 				 STRAIGHT_JOIN wp_term_relationships tr on tr.object_id=p.ID 
   		 				 STRAIGHT_JOIN wp_term_taxonomy tt on tt.term_taxonomy_id=tr.term_taxonomy_id and tt.taxonomy=\'category\' 
						 STRAIGHT_JOIN wp_terms t on t.term_id=tt.term_id ';
   		if ($var_concepto){
			$query=$query.' where t.term_id=%d ';
		} 
		
		// Build your query			

		if ($varUsuario){
				$MyQuery = $wpdb->get_results($wpdb->prepare($query,$DownloadReportFrom,$DownloadReportTo,$varUsuario,$var_concepto));
			
		} else {
			if ($varDominio){
				$MyQuery = $wpdb->get_results($wpdb->prepare($query,$DownloadReportFrom,$DownloadReportTo,$email,$var_concepto));
			}else {
				$MyQuery = $wpdb->get_results($wpdb->prepare($query,$DownloadReportFrom,$DownloadReportTo,$var_concepto));
			}
		}
		
								
		
		// Process report request
		if (! $MyQuery) {
	
		} else {
		// Prepare our csv download
		
		// Set header row values
		$csv_fields=array();
		$csv_fields[] = 'Usuario';
		$csv_fields[] = 'Correo';
		$csv_fields[] = 'Empresa';
		$csv_fields[] = 'Fecha Lectura';
		$csv_fields[] = 'Concepto';
		$csv_fields[] = 'Titulo';
		$csv_fields[] = 'Enlace';
		$output_filename = 'Estadisticas_' . $DownloadReportFrom .'-'. $DownloadReportTo  . '.csv';
		$output_handle = @fopen( 'php://output', 'w' );
		 
		header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
		header( 'Content-Description: File Transfer' );
		header( 'Content-type: text/csv;charset=utf-8');
		header( 'Content-Disposition: attachment; filename=' . $output_filename );
		header( 'Expires: 0' );
		header( 'Pragma: public' );	
	
		// Insert header row
		fputcsv( $output_handle, $csv_fields );
		
		// Parse results to csv format
		if (! $MyQuery) {
				$csv_fields1=array();
				$csv_fields1[] = 'No hay resultados que esportar';
				echo 'No hay resultados que esportar';
			fputcsv( $output_handle, $leadArray );
		}else {
			foreach ($MyQuery as $Result) {
			
				$leadArray = (array) $Result; // Cast the Object to an array
				$leadArray = array_map("utf8_decode", $leadArray);
				// Add row to file
				fputcsv( $output_handle, $leadArray );
			}
		}
		 
		// Close output file stream
		fclose( $output_handle ); 
		
		
		die();
		

}
}

?>

