%s comments',
$action, $id, ucfirst($match[1])
);
}
function createdBy(int $ownProfile, string $user, string $objects): string {
return $ownProfile ? "your $objects" : "$objects created by $user";
}
$userMan = new Gazelle\Manager\User;
if (!isset($_GET['id'])) {
$User = $Viewer;
} else {
$User = $userMan->findById((int)($_GET['id'] ?? 0));
if (is_null($User)) {
error(404);
}
if (!$User->propertyVisible($Viewer, 'torrentcomments')) {
error(403);
}
}
$UserID = $User->id();
$Username = $User->username();
if ($Viewer->id() == $UserID) {
$ownProfile = true;
$linkId = '';
} else {
$ownProfile = false;
$linkId = "&id=$UserID";
}
$Action = $_REQUEST['action'] ?? 'torrents';
$Type = $_REQUEST['type'] ?? 'default';
$BaseLink = "comments.php?action=$Action$linkId";
// SQL components
$condArgs = [];
$condition = [];
$Join = [];
$joinArgs = [];
$ActionLinks = [];
$TypeLinks = [];
switch ($Action) {
case 'artist':
$Title = '%s › Artist comments';
$table = 'artists_group AS ag';
$idField = 'ag.ArtistID';
$nameField = 'ag.Name';
$condition[] = "C.AuthorID = ?";
$condArgs[] = $UserID;
$ActionLinks = [
linkBuild($linkId, 'collages'),
linkBuild($linkId, 'requests'),
linkBuild($linkId, 'torrents'),
];
break;
case 'collages':
switch ($Type) {
case 'created':
$Title = "%s › Comments on their collages";
$condition[] = "cl.UserID = ?";
$condition[] = "C.AuthorID != ?";
$condArgs[] = $UserID;
$condArgs[] = $UserID;
$TypeLinks = [
[$BaseLink, "$Username › Collage comments"],
["$BaseLink&type=contributed", "$Username › Contributed collage comments"],
];
break;
case 'contributed':
$Title = '%s › Contributed collage comments';
$condition[] = "C.AuthorID != ? AND cl.ID IN (
SELECT DISTINCT CollageID FROM collages_torrents ct WHERE ct.UserID = ?
UNION ALL
SELECT DISTINCT CollageID FROM collages_artists ca WHERE ca.UserID = ?)";
$condArgs = array_merge($condArgs, [$UserID, $UserID, $UserID]);
$TypeLinks = [
[$BaseLink, "$Username › Collage comments"],
["$BaseLink&type=created", "$Username › Comments on their collages"],
];
break;
default:
$Title = "%s › Collage comments";
$condition[] = "C.AuthorID = ?";
$condArgs[] = $UserID;
$TypeLinks = [
["$BaseLink&type=contributed", "$Username › Contributed collage comments"],
["$BaseLink&type=created", "$Username › Comments on their collages"],
];
break;
}
$table = 'collages AS cl';
$idField = 'cl.ID';
$nameField = 'cl.Name';
$condition[] = "cl.Deleted = '0'";
$ActionLinks = [
linkBuild($linkId, 'artist'),
linkBuild($linkId, 'requests'),
linkBuild($linkId, 'torrents'),
];
break;
case 'requests':
switch($Type) {
case 'created':
$Title = "%s › Comments on their requests";
$condition[] = "r.UserID = ?";
$condition[] = "C.AuthorID != ?";
$condArgs[] = $UserID;
$condArgs[] = $UserID;
$TypeLinks = [
[$BaseLink, "$Username › Request comments"],
["$BaseLink&type=contributed", "$Username › Voted-on request comments"],
];
break;
case 'voted':
$Title = "%s › Comments on voted-on requests";
$Join[] = 'INNER JOIN requests_votes rv ON (rv.RequestID = r.ID)';
$condition[] = "rv.UserID = ?";
$condition[] = "C.AuthorID != ?";
$condArgs[] = $UserID;
$condArgs[] = $UserID;
$TypeLinks = [
[$BaseLink, "$Username › Request comments"],
["$BaseLink&type=created", "$Username › Created request comments"],
];
break;
default:
$Title = "%s › Request comments";
$condition[] = "C.AuthorID = ?";
$condArgs[] = $UserID;
$TypeLinks = [
["$BaseLink&type=created", "$Username › Created request comments"],
["$BaseLink&type=contributed", "$Username › Voted-on request comments"],
];
break;
}
$table = 'requests AS r';
$idField = 'r.ID';
$nameField = 'r.Title';
$ActionLinks = [
linkBuild($linkId, 'artist'),
linkBuild($linkId, 'collages'),
linkBuild($linkId, 'torrents'),
];
break;
case 'torrents':
switch($Type) {
case 'uploaded':
$Title = "%s › Comments on their uploads";
$Join[] = 'INNER JOIN torrents t ON (t.GroupID = tg.ID)';
$condition[] = 'C.AddedTime > t.Time';
$condition[] = "C.AuthorID != ?";
$condition[] = "t.UserID = ?";
$condArgs[] = $UserID;
$condArgs[] = $UserID;
$TypeLinks[] = [$BaseLink, "$Username › Torrent comments"];
break;
default:
$Title = "%s › Torrent comments";
$condition[] = "C.AuthorID = ?";
$condArgs[] = $UserID;
$TypeLinks[] = ["$BaseLink&type=uploaded", "$Username › Comments on their uploads"];
break;
}
$table = 'torrents_group AS tg';
$idField = 'tg.ID';
$nameField = 'tg.Name';
$ActionLinks = [
linkBuild($linkId, 'artist'),
linkBuild($linkId, 'collages'),
linkBuild($linkId, 'requests'),
];
break;
}
$Join[] = "INNER JOIN comments C ON (C.Page = ? AND C.PageID = $idField)";
$joinArgs[] = $Action;
$Join = implode("\n", $Join);
$cond = $condition ? 'WHERE ' . implode(" AND ", $condition) : '';
// Posts per page limit stuff
$paginator = new Gazelle\Util\Paginator($Viewer->postsPerPage(), (int)($_GET['page'] ?? 1));
$paginator->setTotal(
$DB->scalar("
SELECT count(DISTINCT(C.ID)) FROM $table $Join $cond", ...array_merge($joinArgs, $condArgs)
)
);
array_push($condArgs, $paginator->limit(), $paginator->offset());
$Comments = $DB->prepared_query("
SELECT C.AuthorID,
C.Page,
C.PageID,
$nameField,
C.ID,
C.Body,
C.AddedTime,
C.EditedTime,
C.EditedUserID
FROM $table
$Join
$cond
GROUP BY C.ID
ORDER BY C.ID DESC
LIMIT ? OFFSET ?
", ...array_merge($joinArgs, $condArgs)
);
if ($Action == 'requests') {
$RequestIDs = array_flip(array_flip($DB->collect('PageID')));
$Artists = [];
foreach ($RequestIDs as $RequestID) {
$Artists[$RequestID] = Requests::get_artists($RequestID);
}
} elseif ($Action == 'torrents') {
$GroupIDs = array_flip(array_flip($DB->collect('PageID')));
$Artists = Artists::get_artists($GroupIDs);
}
$Links = implode(' ', $ActionLinks)
. ($TypeLinks
? ('
' . implode(' ', array_map(
function ($x) {
return sprintf('%s', $x[0], $x[1]);
}, $TypeLinks
)))
: ''
);
View::show_header(sprintf($Title, $Username), ['js' => 'bbcode,comments']);
?>