123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\AccountNotFoundException;
- use app\Exceptions\ContactTrackerNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use app\Exceptions\SecurityFaultException;
- class ContactTracker implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?string $name;
- public private(set) ?string $email;
- public private(set) ?string $message;
- public private(set) ?string $ip;
- public private(set) ?int $date_sent;
- public function __construct(?int $id = null, ?string $name = null, ?string $email = null, ?string $message = null, ?string $ip = null, ?int $date_sent = null)
- {
- $this->id = $id;
- $this->name = $name;
- $this->email = $email;
- $this->message = $message;
- $this->ip = $ip;
- $this->date_sent = $date_sent;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void
- {
- throw new SecurityFaultException("Attempt to create table on contact tracker object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void
- {
- throw new SecurityFaultException("Attempt to drop table on contact tracker object.");
- }
-
- /**
- * Saves the object to the database.
- */
- public function Save(): int
- {
- Hajeebtok::$Database->Query("INSERT INTO contact_tracking (name, email, message, ip) VALUES (:name, :email, :message, :ip)", [
- "name" => $this->name,
- "email" => $this->email,
- "message" => $this->message,
- "ip" => $this->ip
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved contact id ($id).");
- return $id;
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete()
- {
- Hajeebtok::$Database->Query("DELETE FROM contact_tracking WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load(): void
- {
- if(empty($this->id)) { // search via ip
- $data = Hajeebtok::$Database->Row("SELECT * FROM contact_tracking WHERE ip = :ip", ["ip" => $this->ip]);
- if (empty($data)) throw new ContactTrackerNotFoundException($this->ip, 404);
- } else {
- $data = Hajeebtok::$Database->Row("SELECT * FROM contact_tracking WHERE id = :id", ["id" => $this->id]);
- if (empty($data)) throw new ContactTrackerNotFoundException($this->id, 404);
- }
- $this->id = $data["id"];
- $this->name = $data["name"];
- $this->email = $data["email"];
- $this->message = $data["message"];
- $this->ip = $data["ip"];
- $this->date_sent = strtotime($data["date_sent"]);
- }
-
- public function LoadMany(): array {
- if(!empty($this->email) && empty($this->name) && empty($this->ip)) {
- $query = "SELECT * FROM contact_tracking WHERE email = :email";
- $array = ["email" => $this->email];
- } else if (empty($this->email) && !empty($this->name) && empty($this->ip)) {
- $query = "SELECT * FROM contact_tracking WHERE name = :name";
- $array = ["name" => $this->name];
- } else if (empty($this->email) && empty($this->name) && !empty($this->ip)) {
- $query = "SELECT * FROM contact_tracking WHERE ip = :ip";
- $array = ["ip" => $this->ip];
- } else {
- throw new ContactTrackerNotFoundException(0, 404);
- }
- $data = Hajeebtok::$Database->Query($query, $array);
- if(empty($data)) throw new ContactTrackerNotFoundException(0, 404);
- return $data;
- }
- public function Exists(): bool {
- if(empty($this->ip)) throw new ContactTrackerNotFoundException(0, 400);
- $data = Hajeebtok::$Database->Query("SELECT * FROM contact_tracking WHERE ip = :ip", ["ip" => $this->ip]);
- return !empty($data);
- }
- }
|