| <?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; |
| } |
| } |
| ?> |