summaryrefslogtreecommitdiff
path: root/src/application/actions/subscribetopic/post.php
blob: 78dad04726f0a57870490042b6e56a67f4a93e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

/** @var ?User $currentUser */
/** @var \mystic\forum\Database $db */

use mystic\forum\orm\Subscription;
use mystic\forum\orm\Topic;
use mystic\forum\utils\RequestUtils;

if (!$currentUser) {
    http_response_code(403);
    msg_error(__("You need to be logged in to delete topics!"));
    exit;
}

$formId = "subscribetopic";
$topicId = RequestUtils::getRequiredField("topic", $formId);

$topic = new Topic();
$topic->id = $topicId;

if (!$db->fetch($topic)) {
    http_response_code(404);
    msg_error(__("No topic exists with this id"));
    exit;
}

$subscription = new Subscription;
$subscription->userId = $currentUser->id;
$subscription->topicId = $topic->id;

if ($db->fetchWhere($subscription, ["user_id", "topic_id"])) {
    $db->delete($subscription);
} else {
    $subscription->id = $db->generateId();
    $db->insert($subscription);
}

header("Location: ?_action=viewtopic&topic=" . urlencode($topic->id));