File: //opt/proundcontra.mosmuc.de/pages/default.php
<?php
class PageDefault extends Page
{
public function __construct($row)
{
global $sDB, $sRequest, $sStatistics, $sUser, $sTemplate;
parent::__construct($row);
$this->page = $sRequest->getInt("page");
$this->numPages = -1;
$this->tags = [];
$this->sort = SORT_TITLE;
if (in_array($sRequest->getInt("sort"), [SORT_TITLE, SORT_TOP, OVERVIEW])) {
$this->sort = $sRequest->getInt("sort");
}
$tags = $sRequest->getStringPlain("tags");
if ($tags !== "") {
$tags2 = explode(" ", $tags);
foreach ($tags2 as $k => $v) {
$this->tags[] = $v;
}
}
// Handle voting
if ($sRequest->getInt("vote_select")) {
$vote = $sRequest->getInt("vote");
$questionId = $sRequest->getInt("questionId");
$argumentId = $sRequest->getInt("argumentId");
$sStatistics->vote(new Question($questionId), $argumentId, $vote);
// Redirect
header("Location: ".$this->basePath().($this->getPage() != 0 ? $this->getPage()."/" : ""));
exit;
}
$this->questions = [];
$res = $sDB->exec($this->buildQuery());
while ($rowQ = mysqli_fetch_object($res)) {
$q = new Question($rowQ->questionId, $rowQ);
$this->questions[] = $q;
}
}
public function title()
{
global $sTemplate;
$tagString = $this->getTagsString();
if ($tagString !== "") {
$tagString .= " - ";
}
switch ($this->sort) {
case SORT_TITLE:
return $sTemplate->getString("HTML_META_TITLE_TITLE", ["[TAGS]"], [$tagString]);
case SORT_TOP:
return $sTemplate->getString("HTML_META_TITLE_TOP", ["[TAGS]"], [$tagString]);
}
return $sTemplate->getString("HTML_META_TITLE");
}
public function getQuestions()
{
return $this->questions;
}
public function numPages()
{
global $sDB;
if ($this->numPages == -1) {
$res = $sDB->exec($this->buildQuery(true));
$row = mysqli_fetch_object($res);
$this->numPages = ceil($row->cnt / QUESTIONS_PER_PAGE);
}
return $this->numPages;
}
private function buildQuery($cnt = false)
{
global $sDB;
$qry = "SELECT ";
if ($cnt) {
$qry .= "count(*) as `cnt`";
} else {
$qry .= "*";
}
$qry .= " FROM `questions` WHERE `type` = '".QUESTION_TYPE_LISTED."' ";
if (count($this->tags)) {
$res = false;
$idString = "";
if (defined('FULLTEXT_TAGS') && FULLTEXT_TAGS) {
// Fulltext search approach
$tagString = "";
foreach ($this->tags as $k => $v) {
$tagString .= "+".mysqli_real_escape_string($sDB->getLink(), $v)."* ";
}
$res = $sDB->exec("
SELECT count(*) as `cnt`, `questionId`
FROM `tags`
WHERE MATCH(`tag`) AGAINST ('".$tagString."' IN BOOLEAN MODE)
GROUP BY `questionId`;
");
} else {
// Simple tag matching
$tagString = "";
foreach ($this->tags as $k => $v) {
$tagString .= ", '".mysqli_real_escape_string($sDB->getLink(), $v)."'";
}
$tagString = substr($tagString, 2);
$res = $sDB->exec("
SELECT count(*) as `cnt`, `questionId`
FROM `tags`
WHERE `tag` IN (".$tagString.")
GROUP BY `questionId`;
");
}
// Build an IN() string for questionId
while ($row = mysqli_fetch_object($res)) {
// If the row's count is >= # of tags, it matches all searched tags
if ($row->cnt >= count($this->tags)) {
$idString .= ", ".$row->questionId;
}
}
if (strlen($idString)) {
$idString = substr($idString, 2);
} else {
$idString = "0";
}
$qry .= " AND `questionId` IN (".$idString.") ";
}
// Sorting logic
switch ($this->sort) {
case SORT_TITLE:
$qry .= " ORDER BY `title`";
break;
case SORT_TOP:
$qry .= " ORDER BY `scoreTop` DESC, `questionId`";
break;
}
// If counting total results, skip limit
if (!$cnt) {
$qry .= " LIMIT ".(i($this->page) * QUESTIONS_PER_PAGE).",".QUESTIONS_PER_PAGE.";";
}
return $qry;
}
public function getSort()
{
return $this->sort;
}
public function getTags()
{
return $this->tags;
}
public function getTagsString()
{
$tags = "";
foreach ($this->tags as $k => $v) {
$tags .= " ".htmlspecialchars($v);
}
if (strlen($tags)) {
$tags = substr($tags, 1);
}
return $tags;
}
public function basePath()
{
global $sTemplate;
$path = $sTemplate->getRoot()."tags/title/";
if ($this->sort == SORT_TOP) {
$path = $sTemplate->getRoot()."tags/top/";
}
if ($this->tags) {
foreach ($this->tags as $k => $v) {
$path .= ($k != 0 ? "-" : "").$v;
}
$path .= "/";
}
return $path;
}
public function basePathNoFilter()
{
global $sTemplate;
if ($this->sort == SORT_TITLE) {
return $sTemplate->getRoot()."tags/title/";
} else if ($this->sort == SORT_TOP) {
return $sTemplate->getRoot()."tags/top/";
}
return $sTemplate->getRoot();
}
public function getPage()
{
return $this->page;
}
private $tags;
private $type;
private $questions;
private $numPages;
private $page;
private $sort;
}
?>