<?php
/*
# Fuer debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
#echo __LINE__."<br>";
*/

include_once './classes/TestProjektSmarty.class.php';
$smarty = new SmartyAdmin();
require_once("./config.inc.php");
$template = "standard";
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
require_once "./language/german.inc.php";
require_once("func_rollenrechte.php");
require_once("func_agent.php");


// Rechteüberprüfung
#$db = dbconnect();
if ($user_admin == ""){ require("index.php"); exit;} //Wenn man nicht angemeldet ist, darf man nicht auf die Seite
if(!rore($user_admin,'a_stat','RE')){require("lib/rechte.php");exit;}
// Rechteüberprüfung ende




if(!isset($_GET["sort"])){
 $sort = "datumsort";
}else{
 $sort = $_GET["sort"];
}
$smarty->assign('sort', "$sort");

if(!isset($_GET["option"])){
  $option = "ASC";
}else{
  $option = $_GET["option"];
}
if($option == 'ASC'){
  $smarty->assign('option', 'DESC');
  $option = "DESC";
}else{
  $smarty->assign('option', 'ASC');
  $option = "ASC";
}



// DATABASE variables and query
#$db = dbconnect();

// Rechteüberprüfung historie
if(rore($user_admin,'r_historie','RE')){
  $rechte = 1;
}else{
  $rechte = 0;
}
// Rechteüberprüfung ende


  $query = "SELECT u.uid, u.vorname, u.nachname, u.user, COUNT(l.`uid`) as `logins`, DATE_Format(max(datum), '%d.%m.%Y %H:%i:%s') as datum, DATE_Format(max(datum), '%Y%m%d%H%i%s') as datumsort
             FROM `prog_admin` as u 
             LEFT JOIN `prog_userlog_admin` as l
               ON u.`uid` = l.`uid`
            WHERE u.inaktiv !='J'
            GROUP BY u.`user`
            ORDER BY $sort $option ";

  $result = $db->query( $query)
  	 or die ("Cannot execute query");

  while ($row = $result->fetch_array()){
  $nachname2 = str_replace(' ', '|', $row['nachname']);
  $vorname2 = str_replace(' ', '|', $row['vorname']);
  
  if($rechte == 1){

    $result_rechte_anz = $db->query("SELECT count(*) Anz
                                        FROM historie
                                       WHERE uid='$row[uid]'");
    $row_rechte_anz = $result_rechte_anz->fetch_array();
    if($row_rechte_anz['Anz'] > 0){
      $rechte_anz = 1;
    }else{
      $rechte_anz = 0;
    }

  }
  
  if($rechte == 1 and $rechte_anz == 1){
   $link = 1;
  }else{
   $link = 0;
  }
  
  $row['vorname2'] = $vorname2;
  $row['nachname2'] = $nachname2;
  $row['link'] = $link;
  $value[] = $row;
  }
  
  $smarty->assign('table_data', $value);
  
  
  
  $query2 = "SELECT u.uid, u.vorname, u.nachname, u.user, COUNT(l.`uid`) as `logins`, DATE_Format(max(datum), '%d.%m.%Y %H:%i:%s') as datum, DATE_Format(max(datum), '%Y%m%d%H%i%s') as datumsort
               FROM `users` as u 
               LEFT JOIN `prog_userlog_admin` as l
                 ON u.`uid` = l.`uid`
              WHERE u.inaktiv ='J'
              GROUP BY u.`user`
              ORDER BY $sort $option ";
  $result2 = $db->query( $query2)
  	 or die ("Cannot execute query2");

  while ($row2 = $result2->fetch_array()){
  $nachname2 = str_replace(' ', '|', $row2['nachname']);
  $vorname2 = str_replace(' ', '|', $row2['vorname']);

  if($rechte == 1){

    $result_rechte_anz = $db->query("SELECT count(*) Anz
                                        FROM historie
                                       WHERE uid='$row[uid]'");
    $row_rechte_anz = $result_rechte_anz->fetch_array();
    
    if($row_rechte_anz['Anz'] > 0){
      $rechte_anz = 1;
    }else{
      $rechte_anz = 0;
    }

  }
  
  if($rechte == 1 and $rechte_anz == 1){
   $link = 1;
  }else{
   $link = 0;
  }

  $row2['vorname2'] = $vorname2;
  $row2['nachname2'] = $nachname2;
  $row2['link'] = $link;
  $value2[] = $row2;
  }
  $smarty->assign('table_data2', $value2);


$result = $db->query("SELECT COUNT(*) as Anzahl FROM prog_userlog_admin");
$prog_userlog_admin = $result->fetch_array();

$smarty->assign('Anzahl', "$prog_userlog_admin[Anzahl]");

$result2 = $db->query("SELECT datum as datsort, DATE_Format(datum, '%d.%m.%Y') as datum FROM prog_userlog_admin ORDER BY datsort ASC limit 1");
$firstuser = $result2->fetch_array();

$smarty->assign('DatumFirst', "$firstuser[datum]");


$smarty->assign('useronline_stat_month', "<img src='./graph.php'>");
$smarty->assign('useronline_stat_user', "<img src='./graph_user.php'>");
                         
$smarty->display("$template/$templatename");
?>