<?php


function CreatePDFContent($dash,$from="",$to="") {
	global $profile, $reports, $cm , $cloudapi;
	
	flush();	
	ob_start();

	if(!empty($cm) && method_exists($cm, 'PDFstylingOverwrite')){
		$cm->PDFstylingOverwrite();
	}

	$dashboard = json_decode(stripslashes($profile->dashboards[$profile->profilename.".dashboards.$dash"]["Value"]),true);
	
	?>
	<!DOCTYPE html><html>
		<head>
		<meta charset="UTF-8">
        <title>Logaholic</title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>

        <link rel="icon" href="favicon.ico?v=2" type="image/x-icon">
        <link rel="shortcut icon" href="favicon.ico?v=2" type="image/x-icon">
        <link href="components/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
		        <link rel="stylesheet" href="templates/logaholic_6/font-awesome-4.3.0/css/font-awesome.min.css">

		        <link href="templates/logaholic_6/css/jvectormap/jquery-jvectormap-1.2.2.css" rel="stylesheet" type="text/css" />
		        
		        <link href="templates/logaholic_6/css/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
		        
		        <link href="templates/logaholic_6/css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />

		        <link type="text/css" rel="stylesheet" href="components/jquery/jqplot/jquery.jqplot.min.css" />

		        <link href="templates/logaholic_6/css/ionslider/ion.rangeSlider.css" rel="stylesheet" type="text/css" />
		        <link href="templates/logaholic_6/css/ionslider/ion.rangeSlider.skinHTML5.css" rel="stylesheet" type="text/css" />

		        <link href="templates/logaholic_6/css/AdminLTE.css" rel="stylesheet" type="text/css" />

		        <link href="templates/logaholic_6/css/reports.css" rel="stylesheet" type="text/css"  />
				<link href="components/d3.v3/nv.d3.css" rel="stylesheet" type="text/css"  />
				<link href="components/jquery/jqplot/jquery.jqplot.min.css" rel="stylesheet" type="text/css"  />
				<link href="templates/logaholic_6/css/print-overview.css" rel="stylesheet" type="text/css"  />
				<link href="templates/logaholic_6/css/print.css" rel="stylesheet" type="text/css" media="print" />
				<link href="components/leaflet-0.6.4/leaflet.css" rel="stylesheet" type="text/css"  />
		        
		        <link href="images/infographics/infograhic.css" rel="stylesheet" type="text/css" />

		        <link href="templates/logaholic_6/css/ui.css" rel="stylesheet" type="text/css" />
		        <link href="templates/logaholic_6/css/ui.responsive.css" rel="stylesheet" type="text/css" />

		        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
		        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
		        <!--[if lt IE 9]>
		          <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
		          <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
		        <![endif]-->

		        <!-- JS resources -->        
		        <script src="components/jquery/js/jquery-1.11.3.min.js"></script>
		        
		        <script src="components/jquery/js/jquery-ui.min.js" type="text/javascript"></script>
		        <script src="components/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
		        
		        <script src="components/d3.v3/d3.min.js" type="text/javascript"></script>
		        <script src="components/d3.v3/nv.d3.js" type="text/javascript"></script>
		        <script src="components/leaflet-0.6.4/leaflet-src.js" type="text/javascript"></script>
		        
		        <!-- Sparkline -->
		        <script src="templates/logaholic_6/js/plugins/sparkline/jquery.sparkline.min.js" type="text/javascript"></script>
		        <!-- jvectormap -->

		        <script src="templates/logaholic_6/js/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js" type="text/javascript"></script>
		        <script src="templates/logaholic_6/js/plugins/jvectormap/jquery-jvectormap-world-mill-en.js" type="text/javascript"></script>
		        

		        <!-- daterangepicker -->
		        <script src="templates/logaholic_6/js/plugins/daterangepicker/daterangepicker.js" type="text/javascript"></script>
		        
		        <!-- DATA TABES SCRIPT -->        
		        <script src="templates/logaholic_6/js/plugins/datatables/jquery.dataTables10.js" type="text/javascript"></script>        		       

		        <script>(function($){var d=$(document),h=$("head"),drag=null,tables=[],count=0,ID="id",PX="px",SIGNATURE="JColResizer",FLEX="JCLRFlex",I=parseInt,M=Math,ie=navigator.userAgent.indexOf('Trident/4.0')>0,S;try{S=sessionStorage}catch(e){}h.append("<style type='text/css'>  .JColResizer{table-layout:fixed;} .JColResizer td, .JColResizer th{overflow:hidden;padding-left:0!important; padding-right:0!important;}  .JCLRgrips{ height:0; position:relative;} .JCLRgrip{margin-left:-5px; position:absolute; z-index:5; } .JCLRgrip .JColResizer{position:absolute;background-color:red;filter:alpha(opacity=1);opacity:0;width:10px;height:100%;cursor: e-resize;top:0} .JCLRLastGrip{position:absolute; width:1px; } .JCLRgripDrag{ border-left:1px dotted black; } .JCLRFlex{width:auto!important;}</style>");var init=function(tb,options){var t=$(tb);t.opt=options;if(t.opt.disable)return destroy(t);var id=t.id=t.attr(ID)||SIGNATURE+ count++;t.p=t.opt.postbackSafe;if(!t.is("table")||tables[id]&&!t.opt.partialRefresh)return;t.addClass(SIGNATURE).attr(ID,id).before('<div class="JCLRgrips"/>');t.g=[];t.c=[];t.w=t.width();t.gc=t.prev();t.f=t.opt.fixed;if(options.marginLeft)t.gc.css("marginLeft",options.marginLeft);if(options.marginRight)t.gc.css("marginRight",options.marginRight);t.cs=I(ie?tb.cellSpacing||tb.currentStyle.borderSpacing:t.css('border-spacing'))||2;t.b=I(ie?tb.border||tb.currentStyle.borderLeftWidth:t.css('border-left-width'))||1;tables[id]=t;createGrips(t)},destroy=function(t){var id=t.attr(ID),t=tables[id];if(!t||!t.is("table"))return;t.removeClass(SIGNATURE+" "+FLEX).gc.remove();delete tables[id]},createGrips=function(t){var th=t.find(">thead>tr>th,>thead>tr>td");if(!th.length)th=t.find(">tbody>tr:first>th,>tr:first>th,>tbody>tr:first>td, >tr:first>td");th=th.filter(":visible");t.cg=t.find("col");t.ln=th.length;if(t.p&&S&&S[t.id])memento(t,th);th.each(function(i){var c=$(this),g=$(t.gc.append('<div class="JCLRgrip"></div>')[0].lastChild);g.append(t.opt.gripInnerHtml).append('<div class="'+SIGNATURE+'"></div>');if(i==t.ln-1){g.addClass("JCLRLastGrip");if(t.f)g.html("")}g.bind('touchstart mousedown',onGripMouseDown);g.t=t;g.i=i;g.c=c;c.w=c.width();t.g.push(g);t.c.push(c);c.width(c.w).removeAttr("width");g.data(SIGNATURE,{i:i,t:t.attr(ID),last:i==t.ln-1})});t.cg.removeAttr("width");syncGrips(t);t.find('td, th').not(th).not('table th, table td').each(function(){$(this).removeAttr('width')});if(!t.f)t.removeAttr('width').addClass(FLEX)},memento=function(t,th){var w,m=0,i=0,aux=[],tw;if(th){t.cg.removeAttr("width");if(t.opt.flush){S[t.id]="";return}w=S[t.id].split(";");tw=w[t.ln+1];if(!t.f&&tw)t.width(tw);for(;i<t.ln;i++){aux.push(100*w[i]/w[t.ln]+"%");th.eq(i).css("width",aux[i])}for(i=0;i<t.ln;i++)t.cg.eq(i).css("width",aux[i])}else{S[t.id]="";for(;i<t.c.length;i++){w=t.c[i].width();S[t.id]+=w+";";m+=w}S[t.id]+=m;if(!t.f)S[t.id]+=";"+t.width()}},syncGrips=function(t){t.gc.width(t.w);for(var i=0;i<t.ln;i++){var c=t.c[i];t.g[i].css({left:c.offset().left-t.offset().left+c.outerWidth(false)+t.cs/2+PX,height:t.opt.headerOnly?t.c[0].outerHeight(false):t.outerHeight(false)})}},syncCols=function(t,i,isOver){var inc=drag.x-drag.l,c=t.c[i],c2=t.c[i+1],w=c.w+inc,w2=c2.w-inc;c.width(w+PX);t.cg.eq(i).width(w+PX);if(t.f){c2.width(w2+PX);t.cg.eq(i+1).width(w2+PX)}if(isOver){c.w=w;c2.w=t.f?w2:c2.w}},applyBounds=function(t){var w=$.map(t.c,function(c){return c.width()});t.width(t.width()).removeClass(FLEX);$.each(t.c,function(i,c){c.width(w[i]).w=w[i]});t.addClass(FLEX)},onGripDrag=function(e){if(!drag)return;var t=drag.t,oe=e.originalEvent.touches,ox=oe?oe[0].pageX:e.pageX,x=ox-drag.ox+drag.l,mw=t.opt.minWidth,i=drag.i,l=t.cs*1.5+mw+t.b,last=i==t.ln-1,min=i?t.g[i-1].position().left+t.cs+mw:l,max=t.f?i==t.ln-1?t.w-l:t.g[i+1].position().left-t.cs-mw:Infinity;x=M.max(min,M.min(max,x));drag.x=x;drag.css("left",x+PX);if(last){var c=t.c[drag.i];drag.w=c.w+x-drag.l}if(t.opt.liveDrag){if(last){c.width(drag.w);t.w=t.width()}else syncCols(t,i);syncGrips(t);var cb=t.opt.onDrag;if(cb){e.currentTarget=t[0];cb(e)}}return false},onGripDragOver=function(e){d.unbind('touchend.'+SIGNATURE+' mouseup.'+SIGNATURE).unbind('touchmove.'+SIGNATURE+' mousemove.'+SIGNATURE);$("head :last-child").remove();if(!drag)return;drag.removeClass(drag.t.opt.draggingClass);var t=drag.t,cb=t.opt.onResize,i=drag.i,last=i==t.ln-1,c=t.g[i].c;if(last){c.width(drag.w);c.w=drag.w}else syncCols(t,i,true);if(!t.f)applyBounds(t);syncGrips(t);if(cb){e.currentTarget=t[0];cb(e)}if(t.p&&S)memento(t);drag=null},onGripMouseDown=function(e){var o=$(this).data(SIGNATURE),t=tables[o.t],g=t.g[o.i],oe=e.originalEvent.touches;g.ox=oe?oe[0].pageX:e.pageX;g.l=g.position().left;d.bind('touchmove.'+SIGNATURE+' mousemove.'+SIGNATURE,onGripDrag).bind('touchend.'+SIGNATURE+' mouseup.'+SIGNATURE,onGripDragOver);h.append("<style type='text/css'>*{cursor:"+t.opt.dragCursor+"!important}</style>");g.addClass(t.opt.draggingClass);drag=g;if(t.c[o.i].l)for(var i=0,c;i<t.ln;i++){c=t.c[i];c.l=false;c.w=c.width()}return false},onResize=function(){for(t in tables){var t=tables[t],i,mw=0;t.removeClass(SIGNATURE);if(t.f&&t.w!=t.width()){t.w=t.width();for(i=0;i<t.ln;i++)mw+=t.c[i].w;for(i=0;i<t.ln;i++)t.c[i].css("width",M.round(1e3*t.c[i].w/mw)/10+"%").l=true}syncGrips(t.addClass(SIGNATURE))}};$(window).bind('resize.'+SIGNATURE,onResize);$.fn.extend({colResizable:function(options){var defaults={draggingClass:'JCLRgripDrag',gripInnerHtml:'',liveDrag:false,fixed:true,minWidth:15,headerOnly:false,hoverCursor:"e-resize",dragCursor:"e-resize",postbackSafe:false,flush:false,marginLeft:null,marginRight:null,disable:false,partialRefresh:false,onDrag:null,onResize:null},options=$.extend(defaults,options);return this.each(function(){init(this,options)})}})})(jQuery)</script>
		        <!-- InputMask -->
		        <script src="templates/logaholic_6/js/plugins/input-mask/jquery.inputmask.js" type="text/javascript"></script>
		        <script src="templates/logaholic_6/js/plugins/input-mask/jquery.inputmask.date.extensions.js" type="text/javascript"></script>
		        <script src="templates/logaholic_6/js/plugins/input-mask/jquery.inputmask.extensions.js" type="text/javascript"></script>
		        

		        <script src="components/jquery/jqplot/jquery.jqplot.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.canvasTextRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.canvasAxisTickRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.canvasAxisLabelRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.dateAxisRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.barRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.categoryAxisRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.pieRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.donutRenderer.min.js" type="text/javascript"></script>
			   	<script src="components/jquery/jqplot/plugins/jqplot.trendline.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.meterGaugeRenderer.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.bubbleRenderer.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.cursor.min.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.highlighter.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.smoothFunnelRenderer.js" type="text/javascript"></script>
			    <script src="components/jquery/jqplot/plugins/jqplot.pointLabels.min.js" type="text/javascript"></script>
			    <script src="app/functions.js" type="text/javascript"></script>			    
			    <script src="components/leaflet-0.6.4/world-countries.js" type="text/javascript"></script>
			    <script type="text/javascript">
			    	var ui = {};
			    	ui.data = {};			    
			        <?php
					$lang_labels = array(					    
					    "_MONTH" => _MONTH,
					    "_YEAR" => _YEAR,
					    "_CREATE_NEW_PROFILE" => _CREATE_NEW_PROFILE,
					    "_SHOW" => _SHOW,
					    "_PREVIOUS" => _PREVIOUS,
					    "_NEXT" => _NEXT,
					    "_HELP" => _HELP
					);
					
					?>
					ui.data.languages = $.parseJSON('<?php echo json_encode($lang_labels);?>');

				</script>
        </head>
	<body>
	<?php
	echo "<div id='printer' class='content'>";
	if(empty($cloudapi)){ # for the cloud we do not need a header in the pdf
		echo "<h1 class='h1-title'>"._WEB_ANALYTICS_REPORT.": {$profile->confdomain}</h1>";
	}
		//dump($dashboard);
		echo "<div class='page'>";
		foreach($dashboard["minis"] as $mini) {
			
				if (!isset($reports[$mini]["ClassName"]) ) {
					continue;
				}
				$r="";
				$r = new $reports[$mini]["ClassName"]();
				$r->IgnReIm = 1; # even if there is a problem with the date we may never ever show the reimport box in de email alerts
				$d = $r->GetReportData();
				?>
				<div class="col-lg-3 col-xs-6 metric-report">
				<div class="small-box <?php echo $d[4]; ?>">
                    <div class="inner">
                        <h3 class="number"><?php echo $d[1]; ?></h3>
                        <p class="title"><?php echo $d[0]; ?></p>
                    </div>
                    <div class="icon">
                        <i class="ion <?php echo $d[3]; ?>"></i>
                    </div>                    
                </div>
                </div>
                <?php
		}
		foreach($dashboard["reports"] as $grid) {		
			foreach($grid as $report) {
				if (!isset($report["label"]) || !isset($reports[$report["label"]]["ClassName"]) ) {
					continue;
				}
				$r="";
				$r = new $reports[$report["label"]]["ClassName"]();
				$r->IgnReIm = 1; # even if there is a problem with the date we may never ever show the reimport box in de email alerts
				
				parse_str($report["url"],$options);
				foreach($options as $option => $value) {
					$r->$option = $value;
					$r->options[$option] = $value;
				}
				
				if(!empty($r->profileselector)){
					$r->profile = new SiteProfile($r->profileselector);
				}
				
				if (!empty($from) && !empty($to)) {
					$r->from = $from;
					$r->to = $to;
				} else if(isset($r->minimumDate) && isset($r->maximumDate)) {
					$dr = DateToTimestamp($r->minimumDate, $r->maximumDate);
					$r->from = $dr['from'];
					$r->to = $dr['to'];
				}
				
				if(!isset($r->trafficsource)){
					$r->trafficsource = null;
					$r->applytrafficsource = false;
				}else{
					$r->applytrafficsource = true;
				}
				$r->displayReportLabel = false;
				$r->displayHeader = true;
				$r->displayReportButtons = false;
				$r->animate_graph = false;
				?>
				<div class="box report readheight">
                    <div class="box-header ui-sortable-handle">   
                        <h3 class="box-title"><?php echo $r->label; ?></h3>
                       
                    </div> 
                    <div class="report-options" contenteditable="false"></div>    
                    <div class="box-body">
                    <?php $r->DisplayReport(); ?></div>    
                    <div class="box-footer"></div>    
                    <div style="clear:both;"></div>
                </div>
                <?php
			}	
		}
		echo "</div>";

	echo "</div>";
	?>
	<script type="text/javascript">
	$(document).ready(function() {
		$("a").removeAttr("href");
		$("a").removeAttr("onclick");
		
		var totalHeight = 0;
		var allowedHeight = 950;
		
		$(".readheight").each(function() {
			
			$(this).css('page-break-inside','avoid');
			$(this).css('display','inline-block');
			$(this).css('width','100%');
			
		});		
	});
	</script>

	</body>
	</html>
	<?php
	$content = ob_get_clean();
	return $content;
}
?>