From 34b1b391d4b03659a96f868857c230002b351514 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Mon, 9 Sep 2024 17:57:00 +0200 Subject: More updates --- src/application/mystic/forum/Database.php | 26 +++++- src/application/mystic/forum/Messaging.php | 101 +++++++++++++++++++++ .../mystic/forum/attributes/References.php | 5 +- .../exceptions/DatabaseConnectionException.php | 6 ++ src/application/mystic/forum/orm/Post.php | 2 +- .../mystic/forum/utils/RequestUtils.php | 19 ++++ 6 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 src/application/mystic/forum/Messaging.php create mode 100644 src/application/mystic/forum/exceptions/DatabaseConnectionException.php create mode 100644 src/application/mystic/forum/utils/RequestUtils.php (limited to 'src/application/mystic/forum') diff --git a/src/application/mystic/forum/Database.php b/src/application/mystic/forum/Database.php index f574386..9b9cf55 100644 --- a/src/application/mystic/forum/Database.php +++ b/src/application/mystic/forum/Database.php @@ -10,6 +10,7 @@ use mystic\forum\attributes\NotNull; use mystic\forum\attributes\PrimaryKey; use mystic\forum\attributes\References; use mystic\forum\attributes\Table; +use mystic\forum\exceptions\DatabaseConnectionException; use mystic\forum\orm\Entity; use mystic\forum\utils\ArrayUtils; use mystic\forum\utils\StringUtils; @@ -26,7 +27,13 @@ class Database { private const REFERENCES = 0b0000_0100; public function __construct(string $connectionString) { - $this->connection = \pg_connect($connectionString); + try { + $conn = \pg_connect($connectionString); + if ($conn !== false) + $this->connection = $conn; + } catch (\ErrorException $ex) { + throw new DatabaseConnectionException($ex->getMessage(), $ex->getCode(), $ex); + } } public static function getConnectionString(string $host, string $user, string $password, string $dbname, int $port = 5432): string { @@ -281,6 +288,23 @@ class Database { return true; } + public function delete(Entity &$entity): bool { + $entityClassName = get_class($entity); + $tableName = self::getTableName($entityClassName); + $reflClass = new ReflectionClass($entityClassName); + $cols = self::getColumns($reflClass); + $primaryCol = self::getPrimaryKeyColumn($cols); + if ($primaryCol === null) + throw new \RuntimeException("Deleting an entity requires a primary key column to be specified"); + $query = "DELETE FROM $tableName WHERE $primaryCol = \$1;"; + $result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]); + if ($result === false) + throw new \RuntimeException("Deletion failed: " . \pg_last_error($this->connection)); + $num_affected_rows = \pg_affected_rows($result); + \pg_free_result($result); + return $num_affected_rows >= 1; + } + public function update(Entity &$entity): bool { $tableName = self::getTableName(get_class($entity)); $reflClass = new ReflectionClass($entity); diff --git a/src/application/mystic/forum/Messaging.php b/src/application/mystic/forum/Messaging.php new file mode 100644 index 0000000..2f5c80d --- /dev/null +++ b/src/application/mystic/forum/Messaging.php @@ -0,0 +1,101 @@ +\n"; + echo "
\n"; + + echo "
\n"; + echo "".htmlentities($headerText, self::ENT_FLAGS)."\n"; + echo "
\n"; + + echo "
\n"; + + echo "
\n"; + + foreach ($items as $item) { + if (is_scalar($item)) { + echo "

" . htmlentities(strval($item), self::ENT_FLAGS) . "

\n"; + } elseif (is_array($item)) { + if (count(array_keys($item)) === 2 && isset($item["___!type"]) && isset($item["___!content"])) { + // special item + switch ($item["___!type"]) { + case "HTML": + echo $item["___!content"]; + break; + default: + echo "invalid"; + break; + } + } elseif (array_is_list($item)) { + echo "
    \n"; + foreach ($item as $i) + echo "
  • " . htmlentities($i, self::ENT_FLAGS) . "
  • \n"; + echo "
\n"; + } else { + echo "\n"; + foreach ($item as $k => $i) { + echo "\n"; + echo "\n"; + echo ""; + echo "\n"; + } + echo "
" . htmlentities($k, self::ENT_FLAGS) . ""; + if (is_scalar($i)) { + echo htmlentities($i, self::ENT_FLAGS); + } else { + echo gettype($i); + } + echo "
\n"; + } + } else { + echo gettype($item); + } + } + + echo "
\n"; + + echo "
\n"; + echo "\n"; + } + + public static function bold(string $contents): array { + return self::html("

" . htmlentities($contents, self::ENT_FLAGS) . "

\n"); + } + + public static function italic(string $contents): array { + return self::html("

" . htmlentities($contents, self::ENT_FLAGS) . "

\n"); + } + + public static function bold_italic(string $contents): array { + return self::html("

" . htmlentities($contents, self::ENT_FLAGS) . "

\n"); + } + + public static function html(string $contents): array { + return [ + "___!type" => "HTML", + "___!content" => $contents, + ]; + } + + public static function info(string|array $contents): void { + if (is_string($contents)) + $contents = [$contents]; + self::message($contents, "INFORMATION", "SKYBLUE", "BLACK"); + } + + public static function error(string|array $contents): void { + if (is_string($contents)) + $contents = [$contents]; + self::message($contents, "ERROR", "RED", "WHITE"); + } +} diff --git a/src/application/mystic/forum/attributes/References.php b/src/application/mystic/forum/attributes/References.php index ac431ff..9e33927 100644 --- a/src/application/mystic/forum/attributes/References.php +++ b/src/application/mystic/forum/attributes/References.php @@ -7,9 +7,12 @@ class References { public function __construct( public readonly string $foreignTableName, public readonly ?string $foreignColumnName = null, + public readonly bool $cascadeOnDelete = false, ) {} public function __toString(): string { - return $this->foreignTableName . ($this->foreignColumnName !== null ? " ({$this->foreignColumnName})" : ""); + return $this->foreignTableName + . ($this->foreignColumnName !== null ? " ({$this->foreignColumnName})" : "") + . ($this->cascadeOnDelete ? " ON DELETE CASCADE" : ""); } } diff --git a/src/application/mystic/forum/exceptions/DatabaseConnectionException.php b/src/application/mystic/forum/exceptions/DatabaseConnectionException.php new file mode 100644 index 0000000..f79d800 --- /dev/null +++ b/src/application/mystic/forum/exceptions/DatabaseConnectionException.php @@ -0,0 +1,6 @@ +