<?php #pgs/board/main.php
// Liste aus Kategorien mit Threads

$tpl["main"] = tplExtrSection(tplLoadFile("pgs/board/thread.html"), "###BOARDTHR###");
$tpl["thr"] = "";

if (isset($input["thread"]) && ($userID = lgnCheckLogin($mysqli))) {
  $tpl["thr"] = $tpl["main"];
  
  $replyto = isset($input["msg"]);
  $qry = "SELECT c.ID, c.name, c.description, m.ID mID, m.rootID FROM board_messages m "
        ."LEFT JOIN board_categories c ON c.ID=m.categoryID "
        ."LEFT JOIN rolerights r ON r.rightID=c.rightID "
        ."LEFT JOIN users u ON u.roleID=r.roleID "
        ."WHERE (c.rightID IS NULL OR u.ID = ?) AND m.ID = ? ";
  if ($stmt = $mysqli->prepare($qry)) {
    $stmt->bind_param("ii", $userID, $input["thread"]);
    $stmt->execute();
    $catres = $stmt->get_result();
    if ($catres->num_rows==0) {
      addError("Access", "Kategorie nicht vorhanden oder Zugriff nicht erlaubt3");
    } else {
      $cat = $catres->fetch_assoc();
      $replace = array(
        "###BOARDTHR.CAT.ID###" => $cat["ID"],
        "###BOARDTHR.CAT.NAME###" => $cat["name"],
        "###BOARDTHR.CAT.DESC###" => $cat["description"]
      );
      $tpl["thr"] = tplReplMarkerArray($tpl["thr"], $replace);
      $tpl["msg"] = tplExtrSection(tplLoadFile("pgs/board/msg.html"), "###BOARDMSG###");
      
      $rootID = $cat["rootID"] ?? $cat["mID"];
      $tpl["thr"] = tplReplMarker($tpl["thr"], "###BOARDTHR.MSGS###", boardLoadThread($mysqli, $userID, $rootID, $tpl["msg"]));
    }
  } else {
    addError("Mysql", $mysqli->error);
  }
}
$tpl["main"] = $tpl["thr"];

?>
