<?php

class LB {

  private $class_obj;
  private $prefix;
  private $eintragsdatum;
  private $monate;
  private $tage;
  private $stunden;
  private $minuten;
  private $dauer;
  private $aktjahr;
  private $jahre;
  private $a_kat;
  private $a_kat_arr;
  private $group;
  private $OrteTableEmpty;

  public function __construct ($class_needed,$file_ending,$userdata) { // Konstruktor
    include_once($class_needed.$file_ending);
    $this->class_obj = new $class_needed;
    $this->prefix = $userdata[0];
    $this->eintragsdatum = date('YmdHis');
    $this->monate = array("01","02","03","04","05","06","07","08","09","10","11","12");
    $this->tage = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
    $this->stunden = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
    $this->minuten = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30",
                           "31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59");
    $this->dauer = array("","0.25","0.50","0.75","1.00","1.25","1.50","1.75","2.00","2.25","2.50","2.75","3.00","3.25","3.50","3.75","4.00","4.25","4.50","4.75","5.00");
    $this->aktjahr = date('Y');
    $letztes = $this->aktjahr -1;
    $naechstes = $this->aktjahr +1;
    $this->jahre = array($letztes,$this->aktjahr,$naechstes);
    $this->lese_orte();
    // $this->debug($this->a_kat_arr);
    if(isset($_POST['del_entry'])) $this->del_entry();
  }

  public function history() {
    $inhalt = file_get_contents("History");
    if ($inhalt !== false) {
        echo nl2br($inhalt);
    }
  }

  public function addusergui() {
      echo "<form method=\"post\" action=\"index.php\">\n";
      echo "<h3>neue User anlegen</h3>\n";
      echo "";
      echo "";
      echo "<input placeholder=\"Benutzername\" type=\"text\" name=\"bname\" value=\"\">\n";
      echo "<input placeholder=\"Passwort\" type=\"password\" name=\"pass\" value=\"\">\n";
      echo "<input type=\"submit\" name=\"subuser\" value=\"Erstellen\">\n";
      echo "</form>\n";
  }

  public function wlogs($text) {
    $arr = array($_SESSION['id'], $text, $this->eintragsdatum);
    $this->debug($arr);
    $this->class_obj->insert("INSERT INTO logs (uid,text,datum) VALUES (?,?,?)", $arr);
  }

  public function gruppen() {
    $this->debug($this->a_kat_arr);
    asort($this->a_kat_arr);
    $this->debug($this->a_kat_arr);
    $name="gruppe";
    $class="";
    echo "<form method=\"post\" action=\"index.php?cont=show_group\">\n";
    $this->select($this->a_kat_arr, $name, $class, "", 2, 1);
    #echo "<input type=\"submit\" name=\"chg_orte\" value=\"zeigen\"><br />\n";
    echo "</form>\n";
    echo "<hr />\n";
  }

  public function show_group($post) {
    $this->gruppen();
    $this->debug($post);
    echo "<h2>".$this->a_kat_arr[$post['gruppe']]."</h2>\n";
    $this->group = $this->class_obj->select("SELECT text,datum FROM logbuch".$_SESSION['id']." WHERE ort = ".$post['gruppe']." ORDER BY datum DESC");
    foreach ($this->group as $key => $value) {
      echo $this->datum_mit_punkt($value['datum'])." - ". $value['text']."<br />\n";
    }
  }

  private function del_entry() {
    $this->class_obj->delete("DELETE FROM logbuch".$_SESSION['id']." WHERE id = ".$_POST['del_id']);
  }

  public function eingabe() {
    echo "<h3>Neuer Eintrag</h3>\n";
    echo "<form action=\"index.php?cont=schreiben\" method=\"post\">\n";
    if($this->OrteTableEmpty == 1 ) {
      echo "kein Ort vorhanden.";
    } else {
      $this->debug($this->a_kat_arr);
      asort($this->a_kat_arr);
      $this->select($this->a_kat_arr,"ort","","",0);
    }
    echo "<input type=\"text\" name=\"neuer_ort\" value=\"\" size=\"11\" maxlength=\"8\" placeholder=\"Ort hinzuf&uuml;gen\"><br />\n";
    echo "<br /><br />\n";
    echo "<input type=\"text\" name=\"text\" value=\"\" size=\"30\" placeholder=\"Was wird gemacht?\">\n<br /><br />";
    // echo "<br />\n";
    if($_SESSION['dauereinaus'] == "1") {
      echo "Dauer: ";
      $this->select3($this->dauer,"dauer","",$_SESSION['dauer']);
      echo "<br /><br />\n";
    }
    $this->select3($this->jahre,"jahr","",substr($this->eintragsdatum,0,4));
    echo " - ";
    $this->select3($this->monate,"monat","",substr($this->eintragsdatum,4,2));
    echo " - ";
    $this->select3($this->tage,"tag","",substr($this->eintragsdatum,6,2));
    echo "&nbsp;";
    $this->select3($this->stunden,"stunde","",substr($this->eintragsdatum,8,2));
    echo ": ";
    $this->select3($this->minuten,"minute","",substr($this->eintragsdatum,10,2));
    echo "<input type=\"hidden\" name=\"sekunde\" value=\"".substr($this->eintragsdatum,12,2)."\" >\n";
    echo "<br /><br />";
    echo "<input type=\"submit\" name=\"eintragen\" value=\"Eintragen\" size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;\n";
    echo "</form>\n";

  }

  public function schreiben ($post) {
    $this->debug($post);
    $dauer = isset($post['dauer']) ? $post['dauer'] : "" ;
      if(strlen($post['neuer_ort']) > 0 ) {
        $ORT = array($post['neuer_ort']);
        $post['neuer_ort'] = $this->class_obj->insert("INSERT INTO orte".$_SESSION['id']." (ort) VALUES (?)", $ORT);
        $ort = $this->class_obj->lastInsertId();
      } else {
        $ort =$post['ort'];
      }
      $arr = array($ort, $post['text'], $dauer ,$post['jahr'].$post['monat'].$post['tag'].$post['stunde'].$post['minute'].$post['sekunde']);
      $this->debug($arr);
      $this->class_obj->insert("INSERT INTO logbuch".$_SESSION['id']." (ort,text,dauer,datum) VALUES (?,?,?,?)", $arr);
      $this->liste();
  }

  public function konfig() {
    echo "<b><a href=\"index.php?cont=userkonfig\">Userconfig aufrufen</a></b><br /><hr />\n";
    $this->konfig_orte();
    $this->konfig_dauer();
  }

  private function session() {
    
  }

  private function konfig_dauer() {
    if(isset($_REQUEST['dauer'])){
      $this->class_obj->update("UPDATE User SET dauer=? WHERE id = ? LIMIT 1",array($_REQUEST['dauer'],$_SESSION['id']));
      $_SESSION['dauer'] =$_REQUEST['dauer'];
    }
    if(isset($_REQUEST['dauereinaus'])) {
      $this->class_obj->update("UPDATE User SET dauereinaus=? WHERE id = ? LIMIT 1",array($_REQUEST['dauereinaus'],$_SESSION['id']));
      $_SESSION['dauereinaus'] = $_REQUEST['dauereinaus'];
    }
    $value0 = "";
    $value1 = "";
    if($_SESSION['dauereinaus'] == 1) $value1 = " checked ";
    if($_SESSION['dauereinaus'] == 0) $value0 = " checked ";
    echo "<h3>Standard Dauer</h3>\n";
    echo "<form action=\"index.php?cont=konfig\" method=\"post\">\n";
    echo "Eingaben einer Dauer nutzen? ";
    echo "<br />\n";
    echo "Ja <input type=\"radio\" name=\"dauereinaus\" value=\"1\" $value1 >\n";
    echo "Nein <input type=\"radio\" name=\"dauereinaus\" value=\"0\" $value0 >\n";
    echo "<input type=\"submit\" name=\"subdauereinaus\" value=\"&auml;ndern\">\n";
    echo "</form>\n";
    echo "<br />\n";
    echo "<form method=\"post\" action=\"index.php?cont=konfig\">\n";
    echo "Standard-Dauer definieren: ";
    $this->select3($this->dauer,"dauer","",$_SESSION['dauer']);
    echo "<input type=\"submit\" name=\"chg_dauer\" value=\"&auml;ndern\">\n";
    echo "</form>\n";
    echo "<hr />\n";

  }

  private function konfig_orte() {
    if (isset($_REQUEST['chg_orte'])) {
      $this->debug($_REQUEST);
      $arr_keys=array_keys($_REQUEST);
      $ids=explode("_", $arr_keys[1]);
      $this->debug($ids[1]);
      $wert=$_REQUEST[$arr_keys[1]];
      $this->debug($wert);
      $this->class_obj->update("UPDATE orte".$_SESSION['id']." SET ort=? WHERE id = ? LIMIT 1",
        array($wert,$ids[1]));
        $this->lese_orte();
    }
    echo "<h3>Orte &auml;ndern</h3>\n";
    if($this->OrteTableEmpty == 1 ) {
      echo "kein Ort vorhanden.";
    } else {
      asort($this->a_kat_arr);
      foreach ($this->a_kat_arr as $key => $value) {
        $zeilen = $this->class_obj->select("SELECT ort FROM logbuch".$_SESSION['id']." WHERE ort = $key ");
        echo "<form method=\"post\" action=\"index.php?cont=konfig\">\n";
        echo "<input type=\"text\" name=\"o_$key\" size=\"20\" value=\"$value\">
        <input type=\"submit\" name=\"chg_orte\" value=\"&auml;ndern\">&nbsp; | &nbsp;".count($zeilen)." Eintr&auml;ge<br />\n";
        echo "</form>\n";
      }
    }
    echo "<hr />\n";

  }

  private function lese_orte() {
    $this->a_kat = $this->class_obj->select("SELECT id, ort FROM orte".$_SESSION['id']);
    foreach ($this->a_kat as $key => $value) {
      $this->a_kat_arr[$value['id']] = $value['ort'];
    }
    $this->OrteTableEmpty = 0;
    if(count($this->a_kat) == 0 ) $this->OrteTableEmpty=1;
  }

  public function liste () {
    if($_SESSION['readonly']) {
      $liste = $this->class_obj->select("SELECT id,ort,text,dauer,datum FROM logbuch".$_SESSION['id']." ORDER BY datum DESC LIMIT 30");
    } else {
      $liste = $this->class_obj->select("SELECT id,ort,text,dauer,datum FROM logbuch".$_SESSION['id']." ORDER BY datum DESC ");
    }
    // $this->debug($liste);
    $this->lese_orte();
    echo "<div class=\"container\">\n";
    $farbe=0;
    $z =1;
    $f=1;
    foreach ($liste as $key => $value) {
      $farbe = $f%2 ? "heller" : "dunkler";
      $f++;
      // echo "<div class=\"liste\"><a href=\"index.php?cont=aendern&id=".$value['id']."\"><img src=\"pics/chg_menu.png\" border=\"0\"></a></div>\n";
      echo "<div class=\"$farbe\">";
      if($_SESSION['readonly'] == "0") {
        echo "<a href=\"index.php?cont=aendern&id=".$value['id']."\">".$this->a_kat_arr[$value['ort']]."</a>";
      } else {
        echo $this->a_kat_arr[$value['ort']];
      }
      echo "</div>\n";
      echo "<div class=\"$farbe\">".$value['text']."</div>\n";
      if($_SESSION['dauereinaus'] == "1")  echo "<div class=\"$farbe\">".$value['dauer']."</div>\n";
      echo "<div class=\"$farbe\">".$this->datum_mit_punkt($value['datum'])."</div>\n";
    }
    echo "</div>\n";
  }

  public function aendern($get) {
    $this->debug($get);
    asort($this->a_kat_arr);
    $zeile = $this->class_obj->select("SELECT id,ort,text,dauer,datum FROM logbuch".$_SESSION['id']." WHERE id=".$get['id']);
    $this->debug($zeile);
    echo "<form action=\"index.php?cont=updaten\" method=\"post\">\n";
    $this->select($this->a_kat_arr,"ort","",$this->a_kat_arr[$zeile[0]['ort']]);
    // echo "<input type=\"text\" name=\"neuer_ort\" value=\"\" size=\"10\" placeholder=\"Ort hinzuf&uuml;gen\"><br />\n";
    echo "<br /><br />\n";
    echo "<input type=\"text\" name=\"text\" value=\"".$zeile[0]['text']."\" size=\"30\" placeholder=\"Was wird gemacht?\">\n<br /><br />";
    // echo "<input type=\"text\" name=\"datum\" value=\"".$zeile[0]['datum']."\" size=\"30\" placeholder=\"Datum?\">\n<br /><br />";
    if($_SESSION['dauereinaus'] == "1") {
      echo "Dauer: ";
      $this->select3($this->dauer,"dauer","",$zeile[0]['dauer']);
      echo "<br /><br />\n";
    }
    $this->select3($this->jahre,"jahr","",substr($zeile[0]['datum'],0,4));
    echo " - ";
    $this->select3($this->monate,"monat","",substr($zeile[0]['datum'],4,2));
    echo " - ";
    $this->select3($this->tage,"tag","",substr($zeile[0]['datum'],6,2));
    echo "&nbsp;";
    $this->select3($this->stunden,"stunde","",substr($zeile[0]['datum'],8,2));
    echo ": ";
    $this->select3($this->minuten,"minute","",substr($zeile[0]['datum'],10,2));
    echo "<input type=\"hidden\" name=\"id\" value=\"".$zeile[0]['id']."\" >\n";
    echo "<input type=\"hidden\" name=\"sekunde\" value=\"".substr($zeile[0]['datum'],12,2)."\" >\n";
    echo "<br /><br /><input type=\"submit\" name=\"eintragen\" value=\"&Auml;ndern\" size=\"3\">&nbsp;&nbsp;&nbsp;&nbsp;\n";
    echo "</form>\n";
    echo "<form action=\"index.php?cont=liste\" method=\"post\">\n";
    echo "<input type=\"hidden\" name=\"del_id\" value=\"".$zeile[0]['id']."\" >\n";
    echo "<input id=\"del_entry\" type=\"submit\" name=\"del_entry\" value=\"L&ouml;schen\" size=\"3\">\n";
    echo "</form>\n";

  }

  public function updaten($arr) {
    $this->debug($arr);
    $dauer = isset($arr['dauer']) ? $arr['dauer'] : "" ;
    $this->class_obj->update("UPDATE logbuch".$_SESSION['id']." SET ort=?, text=?,dauer=?,datum=? WHERE id = ? LIMIT 1",
      array($arr['ort'],$arr['text'],$dauer,$arr['jahr'].$arr['monat'].$arr['tag'].$arr['stunde'].$arr['minute'].$arr['sekunde'],$arr['id']));
      $this->liste();
  }

    // ---------------------------------------

  private function datum_mit_punkt ($d) {
  	$jahr 	= substr($d,0,4);
  	$monat 	= substr($d,4,2);
  	$tag 	= substr($d,6,2);
  	$HH 	= substr($d,8,2);
  	$MM 	= substr($d,10,2);
  	$SS 	= substr($d,12,2);
  	$a  =$jahr.".".$monat.".".$tag." ".$HH.":".$MM.":".$SS;
  	return $a;
  }

  private function select($typen, $name, $class, $selected="", $default=0, $submit=0) {
    if($submit == 1) {$autosubmit = "onchange=\"this.form.submit()\" ";} else {$autosubmit=0;}
    echo "<select class=\"$class\" size=\"1\" name=\"$name\" $autosubmit>\n";
    if (isset($default)AND ($default==1)) { echo "<option  value=\"0\">alle zeigen</option>\n";}
    if (isset($default)AND ($default==2)) { echo "<option  value=\"0\">W&auml;hlen</option>\n";}
    foreach ($typen as $typ => $key) {
      echo '<option ';
      if ($key == $selected) {
        echo " selected ";
      }
      echo "  value=\"".$typ."\">".$key."</option>\n";
    }
    echo "</select>\n";
  }

  private function select2($typen, $name, $class, $selected="", $default=0) {
    echo "<select class=\"$class\" size=\"1\" name=\"$name\">\n";
    if (isset($default)AND ($default==1)) { echo "<option  value=\"0\">alle zeigen</option>\n";}
    foreach ($typen as $key => $value) {
      echo '<option ';
      if ($value['id'] == $selected) {
        echo " selected ";
      }
      echo "  value=\"".$value['id']."\">".$value['ort']."</option>\n";
    }
    echo "</select>\n";
  }

  private function select3($typen, $name, $class, $selected="", $default=0) {
    echo "<select class=\"$class\" size=\"1\" name=\"$name\">\n";
    if (isset($default)AND ($default==1)) { echo "<option  value=\"0\">alle zeigen</option>\n";}
    foreach ($typen as $typ => $key) {
      echo '<option ';
      if ($key == $selected) {
        echo " selected ";
      }
      echo "  value=\"".$key."\">".$key."</option>\n";
    }
    echo "</select>\n";
  }

  private function debug ($ausgabe="", $array=true) {
  	if ($ausgabe !="") {
  		if (isset($_SESSION['debug']) and ($_SESSION['debug'] == true)) {
  			if (is_string($ausgabe)) echo "<font color=\"#FF0000\">".$ausgabe."</font><br>\n";
  			if ($array) {
  				echo "<pre>\n";
  				print_r($ausgabe);
  				echo "</pre>\n";
  			} else
  			if (is_array($ausgabe)) {
  				foreach ($ausgabe as $ausgabe) {
  					echo "<font color=\"#FF0000\">".$ausgabe."</font><br>\n";
  				}
  			}
  		}
  	}
  }

  private function js_autoload($formname, $feld) {
    echo "<script type=\"text/javascript\" language=\"JavaScript\">\n";
    echo "<!--\n";
    echo "function start() {\n";
    echo "document.forms[\"".$formname."\"].".$feld.".select();}\n";
    echo "window.onload=start;\n";
    echo "-->\n";
    echo "</script>\n";
  }

  public function get_orte() {
    return $this->a_kat_arr;
  }

  public function suche_ajax($suchbegriff) {
    $param = array("%".$suchbegriff."%");
    $treffer = $this->class_obj->select("SELECT id,ort,text,dauer,datum FROM logbuch".$_SESSION['id']." WHERE text LIKE ? ORDER BY datum DESC", $param);

    // Auch Einträge finden, deren Gruppe dem Suchbegriff entspricht
    $gefundene_ids = array();
    foreach ($this->a_kat_arr as $id => $ort) {
      if(stripos($ort, $suchbegriff) !== false) {
        $gefundene_ids[] = $id;
      }
    }
    $treffer_gruppe = array();
    if(!empty($gefundene_ids)) {
      $placeholders = implode(',', array_fill(0, count($gefundene_ids), '?'));
      $treffer_gruppe = $this->class_obj->select("SELECT id,ort,text,dauer,datum FROM logbuch".$_SESSION['id']." WHERE ort IN ($placeholders) ORDER BY datum DESC", $gefundene_ids);
    }

    // Zusammenführen ohne Duplikate
    $alle = array();
    $ids_gesehen = array();
    foreach($treffer as $t) { $alle[] = $t; $ids_gesehen[$t['id']] = true; }
    foreach($treffer_gruppe as $t) { if(!isset($ids_gesehen[$t['id']])) { $alle[] = $t; } }

    // Datum formatieren
    foreach($alle as &$e) {
      $e['datum_fmt'] = $this->datum_mit_punkt($e['datum']);
    }
    return $alle;
  }

// ende klasse
}
  ?>
