<?php

class LB {

  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->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);
  }

  public function eingabe() {
    $this->debug($this->a_kat_arr);
    asort($this->a_kat_arr);
    echo "<h3>Neuer Eintrag</h3>\n";
    echo "<form action=\"index.php?cont=schreiben\" method=\"post\">\n";
    $this->select($this->a_kat_arr,"ort","","",0);
    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=\"\" size=\"30\" placeholder=\"Was wird gemacht?\">\n<br /><br />";
    $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);
      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'] ,$post['jahr'].$post['monat'].$post['tag'].$post['stunde'].$post['minute'].$post['sekunde']);
      $this->debug($arr);
      $this->class_obj->insert("INSERT INTO logbuch (ort,text,datum) VALUES (?,?,?)", $arr);
      $this->liste();

  }

  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'];
    }

  }

  public function liste () {
    $liste = $this->class_obj->select("SELECT id,ort,text,datum FROM logbuch".$_SESSION['id']." ORDER BY datum DESC ");
    // $this->debug($liste);
    $this->lese_orte();
    echo "<div class=\"container\">\n";
    foreach ($liste as $key => $value) {
      // 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=\"liste\"><a href=\"index.php?cont=aendern&id=".$value['id']."\">".$this->a_kat_arr[$value['ort']]."</a></div>\n";
      echo "<div class=\"liste\">".$value['text']."</div>\n";
      echo "<div class=\"liste\">".$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,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 />";
    $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";

  }

  public function updaten($arr) {
    $this->debug($arr);
    $this->class_obj->update("UPDATE logbuch".$_SESSION['id']." SET ort=?, text=?,datum=? WHERE id = ? LIMIT 1",
      array($arr['ort'],$arr['text'],$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) {
    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=\"".$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";
  }

// ende klasse
}
  ?>
