File size: 1,857 Bytes
628740b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
class Subscription {
    private $conn;
    private $table_name = "subscriptions";

    public $id;
    public $email;
    public $notification_opt_in;
    public $subscribed_at;
    public $unsubscribed_at;
    public $is_active;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function subscribe() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET email=:email, notification_opt_in=:notification_opt_in, is_active=1";
        
        $stmt = $this->conn->prepare($query);
        
        $this->email = htmlspecialchars(strip_tags($this->email));
        
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":notification_opt_in", $this->notification_opt_in);
        
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    public function unsubscribe($email) {
        $query = "UPDATE " . $this->table_name . " 
                  SET is_active=0, unsubscribed_at=NOW() 
                  WHERE email=:email";
        
        $stmt = $this->conn->prepare($query);
        
        $email = htmlspecialchars(strip_tags($email));
        $stmt->bindParam(":email", $email);
        
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    public function checkSubscription($email) {
        $query = "SELECT is_active FROM " . $this->table_name . " 
                  WHERE email = :email";
        
        $stmt = $this->conn->prepare($query);
        
        $email = htmlspecialchars(strip_tags($email));
        $stmt->bindParam(":email", $email);
        
        $stmt->execute();
        
        if ($stmt->rowCount() > 0) {
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            return $row['is_active'];
        }
        return false;
    }
}
?>