<?php
class ADMIN {
  public function __construct ($class_needed,$file_ending) { // Konstruktor
    // Die Klasse wird eingebunden
    include_once($class_needed.$file_ending);
    // Es wird eine Instanz der Klasse erzeugt, und "class_obj" zugewiesen
    $this->class_obj = new $class_needed;
  }

  public function config($tabelle, $spalten, $farben) {
    $this->debug($tabelle);
    $this->debug($spalten);
    // $this->debug($farben);
    $this->farben = $farben;
    $this->tabellen = $tabelle;
    $this->spalten = $spalten;
    // Bereiche auslesen
    $this->bereiche = $this->class_obj->select("SELECT ".$spalten[5]." FROM $tabelle[0] ORDER BY id "); // Bereiche
    $this->config = $this->class_obj->select("SELECT ".$this->spalten[4]." FROM ".$this->tabellen[4]." ;");
    // $this->debug($this->bereiche);
    $this->debug($this->config);
    return $this->bereiche;
  }

  public function admin_sql($array, $spalten, $typ) {
    $this->debug($array);
    $del = array_pop($array);
    foreach ($array as $key => $value) {
      #echo $key."-".$value."<br/>";
      $arr[] = $value;
    }
    $sp = explode(",",$spalten['config']);
    // updatestring erstellen
    $string ="";
    foreach ($sp as $k => $w) {
      $string .=$w."=?, ";
    }
    $string = substr($string, 0, -2);
    // $this->debug($arr);
    // $this->debug($string);
    // $this->debug($spalten['config']);

    switch ($typ) {
      case "config":
        #rsort($array);
        $this->debug($array);
        $this->class_obj->update("UPDATE a_kat SET $string WHERE id = 1 ", $arr);
        #$this->config($this->tabellen,$this->spalten,$this->farben);
      break;
      case "mail_del":
        $this->class_obj->delete("DELETE FROM ".$this->tabellen[3]." WHERE id = ". $arr[0]);
      break;

      case "mail_chg":
        $this->class_obj->update("UPDATE ".$this->tabellen[3]." SET adresse=?  WHERE id = ? LIMIT 1", array($arr[1],$arr[0]));
      break;

      case "mail_neu":
        $this->class_obj->insert("INSERT INTO ".$this->tabellen[3]." (adresse) VALUES (?)", $arr);
      break;

      case "bereich_del":
        $this->class_obj->delete("DELETE FROM ".$this->tabellen[0]." WHERE id = ". $arr[0]);
      break;

      case "bereich_chg":
        $this->class_obj->update("UPDATE ".$this->tabellen[0]." SET bezeichnung=?  WHERE id = ? LIMIT 1",
                  array($arr[1],$arr[0]));
      break;

      case "bereich_neu":
        $this->class_obj->insert("INSERT INTO ".$this->tabellen[0]." (bezeichnung) VALUES (?)", $arr);
      break;

      case "tage_del":
        $this->class_obj->delete("DELETE FROM ".$this->tabellen[1]." WHERE id = ". $arr[0]);
      break;

      case "tage_chg":
        $this->class_obj->update("UPDATE ".$this->tabellen[1]." SET bereich=?, bezeichnung=?,tage=? WHERE id = ? LIMIT 1",
                  array($arr[1],$arr[2],$arr[3],$arr[0]));
      break;

      case "tage_neu":
        $this->class_obj->insert("INSERT INTO ".$this->tabellen[1]." (bereich, bezeichnung, tage) VALUES (?,?,?)", $arr);
      break;
    }
  }

  public function admin_mail_form() {
    $selected = $this->class_obj->select("SELECT ".$this->spalten[3]." FROM ".$this->tabellen[3]." ORDER BY id ");
    $rowCount = $this->class_obj->rowCount();
    if(isset($meldung)) echo $meldung."<br/>";
    echo "<table border=\"0\">\n";
    echo "<tr>\n";
    echo "<th>ID</th>\n";
    #echo "<th>Name</th>\n";
    echo "<th>E-Mailadresse</th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "</tr>\n";
    echo "";
    echo "";
    echo "<tr>\n";
    echo "<form action=\"index.php?cont=admin_mail\" method=\"post\"></td>\n";
      echo "<td>&nbsp;</td>\n";
      #echo "<td><input type=\"text\" name=\"name\" value=\"\"></td>\n";
      echo "<td><input type=\"text\" name=\"adresse\" value=\"\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_mail_neu\" value=\"neu\"></td>\n";
      echo "<td>\n";
      echo "</td>\n";
      echo "</form>\n";
      echo "</tr>\n";

    foreach ($selected as $key => $value) {
    echo "<form action=\"index.php?cont=admin_mail\" method=\"post\">\n";
    echo "<tr>\n";
    echo "<td>".$value['id']."</td>\n";
      echo "<input type=\"hidden\" name=\"id\" value=\"".$value['id']."\">\n";
      #echo "<td><input type=\"text\" name=\"name\" value=\"".$value['name']."\"></td>\n";
      echo "<td><input type=\"text\" name=\"adresse\" value=\"".$value['adresse']."\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_mail_chg\" value=\"&auml;ndern\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_mail_del\" value=\"L&ouml;schen\"></td>\n";
    echo "</form>\n";
      echo "</tr>\n";
    }
    echo "</table>\n";
    print "Es wurden $rowCount Eintr&auml;ge gefunden.<br/>";
    $this->debug($selected,true);

  }

  public function admin_bereich_form() {
    $selected = $this->class_obj->select("SELECT ".$this->spalten[5]." FROM ".$this->tabellen[0]." ORDER BY id ");
    $rowCount = $this->class_obj->rowCount();
    if(isset($meldung)) echo $meldung."<br/>";
    echo "<table border=\"0\">\n";
    echo "<tr>\n";
    echo "<th>ID</th>\n";
    echo "<th>Bezeichnung</th>\n";
    #echo "<th>Tage</th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "</tr>\n";
    echo "";
    echo "";
    echo "<tr>\n";
    echo "<form action=\"index.php?cont=admin_bereich\" method=\"post\"></td>\n";
      echo "<td>&nbsp;</td>\n";
      echo "<td><input type=\"text\" name=\"bezeichnung\" value=\"\"></td>\n";
      #echo "<td><input type=\"text\" name=\"tage\" value=\"\" size=\"1\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_bereich_neu\" value=\"neu\"></td>\n";
      echo "<td>\n";
      echo "</td>\n";
      echo "</form>\n";
      echo "</tr>\n";

    foreach ($selected as $key => $value) {
    echo "<form action=\"index.php?cont=admin_bereich\" method=\"post\">\n";
    echo "<tr>\n";
    echo "<td>".$value['id']."</td>\n";
      echo "<input type=\"hidden\" name=\"id\" value=\"".$value['id']."\">\n";
      echo "<td><input type=\"text\" name=\"bezeichnung\" value=\"".$value['bezeichnung']."\"></td>\n";
      #echo "<td><input type=\"text\" name=\"tage\" value=\"".$value['tage']."\" size=\"1\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_bereich_chg\" value=\"&auml;ndern\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_bereich_del\" value=\"L&ouml;schen\"></td>\n";
    echo "</form>\n";
      echo "</tr>\n";
    }
    echo "</table>\n";
    print "Es wurden $rowCount Eintr&auml;ge gefunden.<br/>";

  }

  public function admin_tage_form($bereich) {
    $selected = $this->class_obj->select("SELECT ".$this->spalten[0]." FROM ".$this->tabellen[1]." WHERE bereich = $bereich ORDER BY id ");
    $rowCount = $this->class_obj->rowCount();
    if(isset($meldung)) echo $meldung."<br/>";
    echo "<table border=\"0\">\n";
    echo "<tr>\n";
    echo "<th>ID</th>\n";
    echo "<th>Bezeichnung</th>\n";
    echo "<th>Tage</th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "</tr>\n";
    echo "";
    echo "";
    echo "<tr>\n";
    echo "<form action=\"index.php?cont=admin_tage&b=$bereich\" method=\"post\"></td>\n";
      #echo "<td>&nbsp;</td>\n";
      echo "<td><input type=\"hidden\" name=\"bereich\" value=\"".$bereich."\"></td>\n";
      echo "<td><input type=\"text\" name=\"bezeichnung\" value=\"\"></td>\n";
      echo "<td><input type=\"text\" name=\"tage\" value=\"\" size=\"2\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_tage_neu\" value=\"neu\"></td>\n";
      echo "<td>\n";
      echo "</td>\n";
      echo "</form>\n";
      echo "</tr>\n";

    foreach ($selected as $key => $value) {
    echo "<form action=\"index.php?cont=admin_tage&b=$bereich\" method=\"post\">\n";
    echo "<tr>\n";
    echo "<td>".$value['id']."</td>\n";
      echo "<input type=\"hidden\" name=\"id\" value=\"".$value['id']."\">\n";
      echo "<input type=\"hidden\" name=\"bereich\" value=\"".$bereich."\">";
      echo "<td><input type=\"text\" name=\"bezeichnung\" value=\"".$value['bezeichnung']."\"></td>\n";
      echo "<td><input type=\"text\" name=\"tage\" value=\"".$value['tage']."\" size=\"2\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_tage_chg\" value=\"&auml;ndern\"></td>\n";
      echo "<td><input type=\"submit\" name=\"admin_tage_del\" value=\"L&ouml;schen\"></td>\n";
    echo "</form>\n";
      echo "</tr>\n";
    }
    echo "</table>\n";
    print "Es wurden $rowCount Eintr&auml;ge gefunden.<br/>";

  }

  public function admin_config_form($tabelle,$spalten) {
    echo "<h2>Konfiguration</h2>";
    $daten = $this->class_obj->select("SELECT ".$spalten." FROM $tabelle "); // Bereiche

    #$del = array_shift($this->config[0]);
    $this->debug($daten);
    $this->debug($tabelle);
    $this->debug($spalten);
    if(isset($meldung)) echo $meldung."<br/>";
    #$keys = get_key($config);
    echo "<form method=\"POST\" action=\"index.php?cont=config\" name=\"config\">\n";
    // echo "<input type=\"hidden\" name=\"id\" value=\"".$del."\"> \n";
    echo "<table>\n";
    foreach ($daten as $key => $value) {
      echo "<tr>\n";
      echo "<td>";
      echo ucfirst($key);
      echo "</td>\n";
      echo "<td>";
      echo "<input type=\"text\" name=\"".$key."\" value=\"".$value['name']."\">";
      echo "</td>\n";
      echo "</tr>\n";
    }
    echo "<tr>\n";
    echo "<td>&nbsp;</td>\n";
    echo "<td><input type=\"submit\" name=\"submit_config\" value=\"&auml;ndern\"></td>\n</tr>\n";
    echo "</table>\n";
    echo "</form>\n";

  }


  public function update($version,$update=0) {
    echo "<h2>Updates</h2>\n";
    $file = $this->read_dir("update/");
    rsort($file);
    $file = $file;
    #$this->debug($file);
    if ($version != "") {
      include("update/".$file[$version]);
      foreach ($sql as  $value) {
        #if(
          $arr = $this->class_obj->query($value);
          #) {
          echo "<b>".$value."</b><br />";
          echo "<p>Update durchgef&uuml;hrt.</p>";
          $this->debug($arr);
        #}
      }
    } else {
      foreach ($file as $key => $value) {
        echo "<p class=\"updatelink\"><a  class=\"updatelink\"href=\"index.php?cont=admin_update&version=".$key."\">".substr($value, 0, -4)."</a>\n";
      }
    }
  }

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

  private function rechne_datum($datum,$was="+",$wert) {
    $d = date('Y-m-d',strtotime($datum." ".$was.$wert." days"));
    return $d;
  }

  private function get_user($aktiv) {
    if($aktiv == false) {
      $aktiv = 0;
      $bereichsstring = "";
    } else {
      $bereichsstring = "  ";
      $aktiv = 1;
    }
    $daten = $this->class_obj->select("SELECT ".$this->spalten[1]." FROM ".$this->tabellen[2]." WHERE aktiv = '".$aktiv ."' and bereich = '".$this->bereich."' ORDER BY id ");
    return $daten;
  }

  private function get_head($bereich) {
    $selected = $this->class_obj->select("SELECT ".$this->spalten[0]." FROM ".$this->tabellen[1]." WHERE bereich = ".$bereich." ORDER BY id ");
    $this->anzahl = $this->class_obj->rowCount();
    $this->debug($selected);
    #echo "anzahl:".$rowCount;
    $head1 = explode(",",$this->spalten[2]);
    #$this->debug($head1);
    foreach($selected as $key => $value) {
      $head2[] = $value['bezeichnung'];
    }
    $head = array_merge($head1,$head2);
    return $head;
  }

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

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

  private function read_dir($dir) {
    $handle=opendir($dir);
    while ($file=readdir($handle)){ ## Dir auslesen
      if($file!=".."  ){ ## wenn keine Ordner
        if (!is_dir($file)) {
          $testfile=substr($file,0,1);#echo "erster buchstabe ".$testfile." - $file<br>";
          if ($testfile != ".") { ## wenn die datei keinen punkt vor dem nadeinamen hat
            $gekuerzt = substr($file,-1);
            if ($gekuerzt !="~") {
              #echo "normal:$file - gekuerzt:$gekuerzt<br>";
              $functions[] = $file; #echo "$file<br>";
            }
          }
        }
      }
    }
    closedir($handle);
    return $functions;
  }

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

  public function kor_eintrag() {
    echo "koreintrag";
    # echo "<pre>";
    # Werte auslesen für Tabelle 1
    $arr = array();
    foreach ($this->class_obj->select('SELECT id from ausgaben Order by id ') as $row) {
      echo $row['id'];
        $id[] = $row['id'];
        $this->class_obj->update("UPDATE ausgaben SET eintrag=? WHERE id = ? LIMIT 1", array($row['id'],$row['id']));
      }
    }


// ----------------------------------------------------------------------------
// Klassen ende
}
?>
