<?php
class baseDeDatosDS{
    public function connectDBBDD(){
        $servername = "localhost";
        $username = "sec_user";
        $password = "Resilencia19@";
        $dbname =   "ppd";
        $conn = new mysqli($servername, $username, $password, $dbname);
        $conn->set_charset("utf8");
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        return $conn;
        return 2;
    }
    public function readtableCountTotal(){
        $conn = $this->connectDBBDD();
        $res = mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general;");
        $info = mysqli_fetch_array($res)[0];
        $conn->close();
        return $info;
    }
    public function readtableCount01($val){
        $conn = $this->connectDBBDD();
        $res = mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general WHERE expl LIKE '%".$val."%' AND (date >= '".date('Y-m-d', strtotime('-1 year'))." 00:00:00' AND date <= '".date('Y-m-d', strtotime('now'))." 23:59:59');");
        $info = mysqli_fetch_array($res)[0];
        $conn->close();
        return $info;
    }
    public function readtableCount02($va1,$va2,$va3){
        $conn = $this->connectDBBDD();
        $res = mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general WHERE expl NOT LIKE '%".$va1."%' AND expl NOT LIKE '%".$va2."%' AND expl NOT LIKE '%".$va3."%' AND (date >= '".date('Y-m-d', strtotime('-1 year'))." 00:00:00' AND date <= '".date('Y-m-d', strtotime('now'))." 23:59:59');");
        $info = mysqli_fetch_array($res)[0];
        $conn->close();
        return $info;
    }
    public function readtablePorcent01($val){
        $conn = $this->connectDBBDD();
        $info = ((mysqli_fetch_array(mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general WHERE expl LIKE '%".$val."%' AND (date >= '".date('Y-m-d', strtotime('-1 year'))." 00:00:00' AND date <= '".date('Y-m-d', strtotime('now'))." 23:59:59');"))[0])*100)/(mysqli_fetch_array(mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general; WHERE (date >= '".date('Y-m-d', strtotime('-1 year'))." 00:00:00' AND date <= '".date('Y-m-d', strtotime('now'))." 23:59:59')"))[0]);
        //$info = $val.",".(mysqli_fetch_array(mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general WHERE expl LIKE '%".$val."%';"))[0]).",".(mysqli_fetch_array(mysqli_query($conn,"SELECT COUNT(id) FROM tbl_registro_general;"))[0])." = ".$info;
        $conn->close();
        return $info;
    }
    public function readtableDB11(){
        $conn = $this->connectDBBDD();
        $a = 1;
        $str = "";
        $res = mysqli_query($conn,"SELECT id_documento,titulo,vistas,descargas FROM tbl_documentos WHERE activo = 1 ORDER BY descargas DESC LIMIT 0,40;");
        while($cRow = mysqli_fetch_array($res)){
            $info[$a][1] = $cRow[0];
            $info[$a][2] = $cRow[1];
            $info[$a][3] = $cRow[2];
            $info[$a][4] = $cRow[3];
            $a++;
        }
        $conn->close();
        for($b = 1;$b < $a;$b++) $str = $str."<tr><td>".$b.'</td><td><a href="/resiliencia/index.php/panel/documentos/update/id/'.$info[$b][1].'.html"><i class="fa fa-file-text-o" style="font-size: px; line-height:middle;"></i> '.substr($info[$b][2],0,70)."...</a></td><td>".$info[$b][3]."</td><td>".$info[$b][4]."</td></tr>\n";
        return $str;
    }
    public function readtableDB12(){
        $conn = $this->connectDBBDD();
        $a = 1;
        $str = "";
        $res = mysqli_query($conn,"SELECT id_encuesta_base,encuesta,vistas FROM tbl_bases_encuestas WHERE activo = 1 ORDER BY vistas DESC LIMIT 0,50;");
        while($cRow = mysqli_fetch_array($res)){
            $info[$a][1] = $cRow[0];
            $info[$a][2] = $cRow[1];
            $info[$a][3] = $cRow[2];
            $a++;
        }
        $conn->close();
        for($b = 1;$b < $a;$b++) $str = $str."<tr><td>".$b.'</td><td><a href="/resiliencia/index.php/panel/basesEncuestas/update/id/'.$info[$b][1].'.html"><i class="fa fa-file-text-o" style="font-size: px; line-height:middle;"></i> '.substr($info[$b][2],0,85)."...</a></td><td>".$info[$b][3]."</td></tr>\n";
        return $str;
    }
}
?>
<?php $baseDeDatosDS = new baseDeDatosDS(); ?>
<?php if(SYS_ADMIN_PLANTILLA){ ?><?php }else{ ?>
    <h3 class="page-title" style="margin: 0 0 20px;">Estad&iacute;sticas <small> Algunos valores mostrados pueden ser una aproximaci&oacute;n.</small></h3>
    <div class="page-bar"><ul class="page-breadcrumb"><li><i class="fa fa-dashboard"></i><a href="<?php echo $this->createUrl('dashboard/'); ?>">Inicio</a><i class="fa fa-angle-right"></i></li></ul></div>
<?php } ?>
<div class="row">
    <div class="col-lg-12">
        <!--<div id="container" style="height: 400px; min-width: 600px"></div>-->
        <div id="container" style="height: 580px; min-width: 600px"></div>
        <!-- -- ><center><img class="img-responsive" src="<?php echo Yii::app()->request->baseUrl?>/images/logoPPD.png" width="70% !important" style="margin-top:80px;" alt=""></center>
		<!-- -->
    </div>
    <!-- /.col-lg-12 -->
</div>
<br>
<div class="row">
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 "><div id="container2" style="min-width: 310px; height: 380px; max-width: 600px; margin: 0 auto"></div></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 "><div id="container3" style="min-width: 310px; height: 380px; max-width: 600px; margin: 0 auto"></div></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 "><div id="container4" style="min-width: 310px; height: 380px; max-width: 600px; margin: 0 auto"></div></div>
</div>
<br>
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-6 ">
        <div class="portlet-body">
            <div class="table-responsive">
                <!--<table id="example-table" class="table table-striped table-bordered table-hover table-green">-->
                <table id="sample_1" class="table table-striped table-bordered table-hover table-green">
                    <thead><tr><th></th><th><center>40 Documentos M&aacute;s Visitados</center></th><th>Vistas</th><th>Descargas</th></tr></thead>
                    <tbody><?php echo $baseDeDatosDS->readtableDB11(); ?></tbody>
                </table>
            </div>
            <!-- /.table-responsive -->
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-6 ">
        <div class="portlet-body">
            <div class="table-responsive">
                <table id="sample_2" class="table table-striped table-bordered table-hover table-green">
                    <thead><tr><th></th><th><center>40 Archivos M&aacute;s Visitados</center></th><th>Vistas</th></tr></thead>
                    <tbody><?php echo $baseDeDatosDS->readtableDB12(); ?></tbody>
                </table>
            </div>
            <!-- /.table-responsive -->
        </div>
    </div>
</div>
<!----><?php if(SYS_ADMIN_PLANTILLA){ ?>
    <script src="/resiliencia/js/jquery-3.1.1.min.js"></script>
    <script src="/resiliencia/js/highstock.js"></script>
    <script src="/resiliencia/js/highcharts.js"></script>
    <script src="/resiliencia/js/exporting.js"></script>
    <script src="/resiliencia/js/export-data.js"></script>
<?php } ?>
<!--<script type="text/javascript" src="/resiliencia/themes/site/plugins/jquery/jquery.min.js"></script>-->
<script src="/resiliencia/themes/admin/js/plugins/datatables/table-datatables-scroller.min.js"" type="text/javascript"></script><!---->
<script>
var seriesOptions = [],
    seriesCounter = 0,
    //names = ['MSFT', 'AAPL', 'GOOG'];
    names = ['TOTAL','DOCUMENTOS','COLABORADORES','CATEGORIAS','DESCARGA','ARCHIVO-DESCARGA','GRAFICAS','*USUARIOS*','*BOTS*','PANEL','PRINCIPAL','SITIO'];
function createChart() {
    Highcharts.stockChart('container', {
        rangeSelector: {
            selected: 14
        },
        plotOptions: {
            series: {
                showInNavigator: true
            }
        },
        tooltip: {
            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
            valueDecimals: 0
        },
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        series: seriesOptions
    });
}
$.each(names, function (i, name) {
    $.getJSON('/resiliencia/visitas01.php?hea89wheaiw8f4ao8374y5684w=fh48fgid8os74e47is7y3grt73s'+i+'',function (data) {
        seriesOptions[i] = {
            name: name,
            data: data
        };
        seriesCounter += 1;
        if (seriesCounter === names.length) {
            createChart();
        }
    });
});
</script>
<?php /*echo $baseDeDatosDS->readtableCount01("Bot");*/ ?>
<?php /*echo $baseDeDatosDS->readtablePorcent01("Bot");*/ ?>
<?php
$nt = $baseDeDatosDS->readtableCountTotal();
$n01 = ($baseDeDatosDS->readtableCount01("Chrome")*100)/$nt; // Chrome
$n02 = ($baseDeDatosDS->readtableCount01("MSIE")*100)/$nt;	 // IE
$n03 = ($baseDeDatosDS->readtableCount01("Firefox")*100)/$nt; // FireFox
$n04 = ($baseDeDatosDS->readtableCount01("Edge")*100)/$nt;	 // Edge
$n05 = ($baseDeDatosDS->readtableCount01("Version")*100)/$nt; // Safari
$n06 = ($baseDeDatosDS->readtableCount01("Opera")*100)/$nt;	 // Opera
$n07 = ($baseDeDatosDS->readtableCount01("Bot")*100)/$nt;	 // Bot
$n08 = 0;//$nt-$n01-$n02-$n03-$n04-$n05-$n06-$n07;				 // Otro
?>
<script>
    Highcharts.chart('container2', {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Navegadores'
        },
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: 'Porcentaje',
            colorByPoint: true,
            data: [{
                name: 'Chrome',
                y: <?php echo $n01; ?>,
                sliced: true,
                selected: true
            }, {
                name: 'Internet Explorer',
                y: <?php echo $n02; ?>,
            }, {
                name: 'Firefox',
                y: <?php echo $n03; ?>,
            }, {
                name: 'Edge',
                y: <?php echo $n04; ?>,
            }, {
                name: 'Safari',
                y: <?php echo $n05; ?>,
            }, {
                name: 'Opera',
                y: <?php echo $n06; ?>,
            }, {
                name: 'Bot',
                y: <?php echo $n07; ?>,
            }, {
                name: 'Otro',
                y: <?php echo $n08; ?>,
            }]
        }]
    });
</script>
<?php
$dt = $nt-$n07;
$d01 = ($baseDeDatosDS->readtableCount01("Windows")*100)/$dt; // Windows
$d02 = ($baseDeDatosDS->readtableCount01("Mac")*100)/$dt; // Mac
$d03 = ($baseDeDatosDS->readtableCount01("Linux")*100)/$dt; // Linux
?>
<script>
    Highcharts.chart('container3', {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Sistemas Operativos'
        },
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: 'Porcentaje',
            colorByPoint: true,
            data: [{
                name: 'Windows',
                y: <?php echo $d01; ?>,
                sliced: true,
                selected: true
            }, {
                name: 'Mac',
                y: <?php echo $d02; ?>,
            }, {
                name: 'Linux',
                y: <?php echo $d03; ?>,
            }]
        }]
    });
</script>
<?php
$st = $nt-$n07;
$s01 = ($baseDeDatosDS->readtableCount01("Bot","Tab","Mobile")*100)/$st; // PC
$s02 = ($baseDeDatosDS->readtableCount01("Tab")*100)/$st; // Tab
$s03 = ($baseDeDatosDS->readtableCount01("Mobile")*100)/$st; // Telefono
?>
<script>
    Highcharts.chart('container4', {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Dispositivos'
        },
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: 'Porcentaje',
            colorByPoint: true,
            data: [{
                name: 'PC',
                y: <?php echo $s01; ?>,
                sliced: true,
                selected: true
            }, {
                name: 'Tablet',
                y: <?php echo $s02; ?>,
            }, {
                name: 'Mobil',
                y: <?php echo $s03; ?>,
            }]
        }]
    });
</script>