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

$tpl["main"] = tplExtrSection(tplLoadFile("pgs/board/categories.html"), "###BOARDCATS###");
$tpl["cat"] = "";

if ($userID = lgnCheckLogin($mysqli)) {  
  $tpl["cat"] = tplExtrSection($tpl["main"], "###BOARDCATS.CAT###");
  $tpl["thread"] = tplExtrSection($tpl["cat"], "###BOARDCATS.CAT.THREAD###");
  $tpl["nothreads"] = tplExtrSection($tpl["cat"], "###BOARDCATS.CAT.NOTHREADS###");
  $cats = "";
  
  $qry = "SELECT c.ID, c.name, c.description FROM board_categories c "
        ."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 = ? "
        ."ORDER BY c.name ASC";
  if ($stmt = $mysqli->prepare($qry)) {
    $stmt->bind_param("i", $userID);
    $stmt->execute();
    $catres = $stmt->get_result();
    while ($catrow = $catres->fetch_assoc()) {
      $replace = array(
        "###BOARDCATS.CAT.ID###" => $catrow["ID"],
        "###BOARDCATS.CAT.NAME###" => $catrow["name"],
        "###BOARDCATS.CAT.DESC###" => $catrow["description"]
      );
      $cat = tplReplMarkerArray($tpl["cat"], $replace);
      $threads = "";
      
      $qry = "SELECT m.ID, m.dttm, m.title, m.text, g.prenames, g.surnames, "
            ."(SELECT COUNT(*) FROM board_messages ms WHERE ms.rootID=m.ID) replies "
            ."FROM board_messages m LEFT JOIN guests g ON g.ID = m.guestID "
            ."WHERE m.parentID IS NULL AND m.categoryID = ".$catrow["ID"]." "
            ."ORDER BY m.dttm DESC";
      if ($thrres = $mysqli->query($qry)) {
        if ($thrres->num_rows==0) {
          $threads = $tpl["nothreads"];
        } else {
          while ($thrrow = $thrres->fetch_assoc()) {
            $thread = tplReplMarker($tpl["thread"], "###BOARDCATS.CAT.THREAD.MSG###", tplExtrSection(tplLoadFile("pgs/board/msg.html"), "###BOARDMSG###"));
            $thread = tplReplSection($thread, "###BOARDMSG.FOOTER###", "");
            $thread = tplReplSection($thread, "###BOARDMSG.INDENT.IN###", "");
            $thread = tplReplSection($thread, "###BOARDMSG.INDENT.OUT###", "");
            preg_match_all("/([\s-]?)([A-Z])/", $thrrow["surnames"], $matches);
            $replace = array(
              "###BOARDMSG.ID###" => $thrrow["ID"],
              "###BOARDMSG.TITLE###" => cntCipherTextSym($thrrow["title"], SYM_CIPHER_KEY),
              "###BOARDMSG.AUTHOR###" => $thrrow["prenames"]." ".implode(".", $matches[0]).".",
              "###BOARDMSG.DATETIME###" => $thrrow["dttm"],
              "###BOARDMSG.LEVEL###" => 0
            );
            $decText = cntCipherTextSym($thrrow["text"], SYM_CIPHER_KEY);
            if (strlen($decText)>33) {
              $replace["###BOARDMSG.TEXT###"] = substr($decText, 0, 30)."...<br /><i>".$thrrow["replies"]." Antwort".($thrrow["replies"]!="1" ? "en" : "")."</i>";
            } else {
              $replace["###BOARDMSG.TEXT###"] = $decText."<br /><i>".$thrrow["replies"]." Antwort".($thrrow["replies"]!="1" ? "en" : "")."</i>";
            }
            $threads.= tplReplMarkerArray($thread, $replace);
          }
        }
      } else {
        addError("Mysql", $mysqli->error);
      }
      $cat = tplReplSection($cat, "###BOARDCATS.CAT.NOTHREADS###", "");
      $cats.= tplReplSection($cat, "###BOARDCATS.CAT.THREAD###", $threads);
    }
    $stmt->close();
    $tpl["cat"] = $cats;
  }
}
$tpl["main"] = tplReplSection($tpl["main"], "###BOARDCATS.CAT###", $tpl["cat"]);

?>
