summaryrefslogtreecommitdiff
path: root/src/web/helpers
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-18 14:14:53 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-18 14:48:54 -0400
commit1f9024763d9224c4cd9a181bac27e6b9f12ad672 (patch)
tree00f827470dad9aa2692483acbdef9502c1a464d3 /src/web/helpers
parentfix rss (diff)
downloadwebsite-1f9024763d9224c4cd9a181bac27e6b9f12ad672.tar.gz
website-1f9024763d9224c4cd9a181bac27e6b9f12ad672.tar.bz2
website-1f9024763d9224c4cd9a181bac27e6b9f12ad672.zip
refactor
Diffstat (limited to 'src/web/helpers')
-rw-r--r--src/web/helpers/aria.php3
-rw-r--r--src/web/helpers/database.php81
-rw-r--r--src/web/helpers/image.php17
-rw-r--r--src/web/helpers/lang.php4
4 files changed, 66 insertions, 39 deletions
diff --git a/src/web/helpers/aria.php b/src/web/helpers/aria.php
index 9782299..0e06b97 100644
--- a/src/web/helpers/aria.php
+++ b/src/web/helpers/aria.php
@@ -1,6 +1,7 @@
<?php /* Copyright (c) 2024 Freya Murphy */
-function aria_section(string $id, ?string $title = NULL): string {
+function aria_section(string $id, ?string $title = NULL): string
+{
$out = '';
if ($title) {
$idh = $id . '_heading';
diff --git a/src/web/helpers/database.php b/src/web/helpers/database.php
index 132ed81..82c711d 100644
--- a/src/web/helpers/database.php
+++ b/src/web/helpers/database.php
@@ -1,6 +1,7 @@
<?php /* Copyright (c) 2024 Freya Murphy */
-function __nullify(mixed $val): mixed {
+function __nullify(mixed $val): mixed
+{
if (!$val) {
return NULL;
} else {
@@ -18,7 +19,8 @@ class DatabaseQuery {
private array $param;
- function __construct(\PDO $conn) {
+ function __construct(\PDO $conn)
+ {
$this->conn = $conn;
$this->query = '';
@@ -31,7 +33,8 @@ class DatabaseQuery {
/// ARBITRARY QUERY
///
- public function query(string $query): DatabaseQuery {
+ public function query(string $query): DatabaseQuery
+ {
$this->query .= $query;
return $this;
}
@@ -40,12 +43,14 @@ class DatabaseQuery {
/// SELECT
///
- public function select(string $select): DatabaseQuery {
+ public function select(string $select): DatabaseQuery
+ {
$this->query .= "SELECT $select\n";
return $this;
}
- public function from(string $from): DatabaseQuery {
+ public function from(string $from): DatabaseQuery
+ {
$this->query .= "FROM $from\n";
return $this;
}
@@ -54,7 +59,8 @@ class DatabaseQuery {
/// INSERT
///
- public function insert_into(string $insert, string ...$columns): DatabaseQuery {
+ public function insert_into(string $insert, string ...$columns): DatabaseQuery
+ {
$this->query .= "INSERT INTO $insert\n (";
foreach ($columns as $idx => $column) {
if ($idx !== 0) {
@@ -66,7 +72,8 @@ class DatabaseQuery {
return $this;
}
- public function values(mixed ...$values): DatabaseQuery {
+ public function values(mixed ...$values): DatabaseQuery
+ {
$this->query .= "VALUES (";
foreach ($values as $idx => $value) {
if ($idx !== 0) {
@@ -83,7 +90,8 @@ class DatabaseQuery {
/// WHERE
///
- public function where(string $cond): DatabaseQuery {
+ public function where(string $cond): DatabaseQuery
+ {
if (!$this->where) {
$this->where = TRUE;
$this->query .= "WHERE ";
@@ -97,7 +105,8 @@ class DatabaseQuery {
/**
* @param array<mixed> $array
*/
- public function where_in(string $column, array $array): DatabaseQuery {
+ public function where_in(string $column, array $array): DatabaseQuery
+ {
if (!$this->where) {
$this->where = TRUE;
$this->query .= "WHERE ";
@@ -116,7 +125,8 @@ class DatabaseQuery {
/**
* @param array<mixed> $array
*/
- private function in(array $array): DatabaseQuery {
+ private function in(array $array): DatabaseQuery
+ {
$in = 'IN (';
foreach ($array as $idx => $item) {
if ($idx != 0) {
@@ -133,31 +143,36 @@ class DatabaseQuery {
/// OPERATORS
///
- public function like(mixed $item): DatabaseQuery {
+ public function like(mixed $item): DatabaseQuery
+ {
$this->query .= "LIKE ?\n";
array_push($this->param, $item);
return $this;
}
- public function eq(mixed $item): DatabaseQuery {
+ public function eq(mixed $item): DatabaseQuery
+ {
$this->query .= "= ?\n";
array_push($this->param, $item);
return $this;
}
- public function ne(mixed $item): DatabaseQuery {
+ public function ne(mixed $item): DatabaseQuery
+ {
$this->query .= "<> ?\n";
array_push($this->param, $item);
return $this;
}
- public function lt(mixed $item): DatabaseQuery {
+ public function lt(mixed $item): DatabaseQuery
+ {
$this->query .= "< ?\n";
array_push($this->param, $item);
return $this;
}
- public function le(mixed $item): DatabaseQuery {
+ public function le(mixed $item): DatabaseQuery
+ {
$this->query .= "<= ?\n";
array_push($this->param, $item);
return $this;
@@ -167,7 +182,8 @@ class DatabaseQuery {
/// JOINS
///
- public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery {
+ public function join(string $table, string $on, string $type = 'LEFT'): DatabaseQuery
+ {
$this->query .= "$type JOIN $table ON $on\n";
return $this;
}
@@ -176,19 +192,22 @@ class DatabaseQuery {
/// LIMIT, OFFSET, ORDER
///
- public function limit(int $limit): DatabaseQuery {
+ public function limit(int $limit): DatabaseQuery
+ {
$this->query .= "LIMIT ?\n";
array_push($this->param, $limit);
return $this;
}
- public function offset(int $offset): DatabaseQuery {
+ public function offset(int $offset): DatabaseQuery
+ {
$this->query .= "OFFSET ?\n";
array_push($this->param, $offset);
return $this;
}
- public function order_by(string $column, string $order = 'ASC'): DatabaseQuery {
+ public function order_by(string $column, string $order = 'ASC'): DatabaseQuery
+ {
$this->query .= "ORDER BY " . $column . ' ' . $order . ' ';
return $this;
}
@@ -197,7 +216,8 @@ class DatabaseQuery {
/// COLLECT
///
- public function rows(mixed ...$params): ?array {
+ public function rows(mixed ...$params): ?array
+ {
$args = $this->param;
foreach ($params as $param) {
array_push($args, $param);
@@ -213,7 +233,8 @@ class DatabaseQuery {
return __nullify($stmt->fetchAll(PDO::FETCH_ASSOC));
}
- public function row(mixed ...$params): ?array {
+ public function row(mixed ...$params): ?array
+ {
$args = $this->param;
foreach ($params as $param) {
array_push($args, $param);
@@ -223,7 +244,8 @@ class DatabaseQuery {
return __nullify($stmt->fetch(PDO::FETCH_ASSOC));
}
- public function execute(mixed ...$params): bool {
+ public function execute(mixed ...$params): bool
+ {
$args = $this->param;
foreach ($params as $param) {
array_push($args, $param);
@@ -250,11 +272,13 @@ class DatabaseHelper {
private ?\PDO $conn;
- function __construct() {
+ function __construct()
+ {
$this->conn = NULL;
}
- private function connect(): \PDO {
+ private function connect(): \PDO
+ {
if ($this->conn === NULL) {
$user = getenv("POSTGRES_USER");
$pass = getenv("POSTGRES_PASSWORD");
@@ -275,19 +299,22 @@ class DatabaseHelper {
return $this->conn;
}
- public function select(string $select): DatabaseQuery {
+ public function select(string $select): DatabaseQuery
+ {
$conn = $this->connect();
$query = new DatabaseQuery($conn);
return $query->select($select);
}
- public function insert_into(string $insert, string ...$columns): DatabaseQuery {
+ public function insert_into(string $insert, string ...$columns): DatabaseQuery
+ {
$conn = $this->connect();
$query = new DatabaseQuery($conn);
return $query->insert_into($insert, ...$columns);
}
- public function query(string $query_str): DatabaseQuery {
+ public function query(string $query_str): DatabaseQuery
+ {
$conn = $this->connect();
$query = new DatabaseQuery($conn);
return $query->query($query_str);
diff --git a/src/web/helpers/image.php b/src/web/helpers/image.php
index fd395b5..ec867b8 100644
--- a/src/web/helpers/image.php
+++ b/src/web/helpers/image.php
@@ -1,6 +1,7 @@
<?php /* Copyright (c) 2024 Freya Murphy */
-function __get_mime($type) {
+function __get_mime($type)
+{
switch ($type) {
case 'mp4':
return 'video/mp4';
@@ -22,15 +23,14 @@ function __get_mime($type) {
function __make_source(
$name,
$format,
- $media
-) {
+ $media)
+{
if ($media) {
$media = "media=\"$media\"";
} else {
$media = '';
}
- $main = $GLOBALS['main_model'];
- $path = $main->get_url('public/' . $name . '.' . $format, TRUE);
+ $path = Core::get_url('public/' . $name . '.' . $format, TRUE);
$mime = __get_mime($format);
return sprintf('<source type="%s" srcset="%s" %s>',
$mime, $path, $media);
@@ -45,8 +45,8 @@ function image(
$height = NULL,
$width = NULL,
- $size = NULL,
-) :string {
+ $size = NULL) :string
+{
if ($animated === TRUE) {
$animated = array('gif');
@@ -68,8 +68,7 @@ function image(
}
$format = end($formats);
- $main = $GLOBALS['main_model'];
- $path = $main->get_url('public/' . $name . '.' . $format, TRUE);
+ $path = Core::get_url('public/' . $name . '.' . $format, TRUE);
$out .= "<img src=\"$path\"";
if ($alt) {
$alt = lang($alt);
diff --git a/src/web/helpers/lang.php b/src/web/helpers/lang.php
index b11cc7c..ba7616e 100644
--- a/src/web/helpers/lang.php
+++ b/src/web/helpers/lang.php
@@ -1,5 +1,5 @@
<?php /* Copyright (c) 2024 Freya Murphy */
-$lang = array();
+$__lang = array();
/**
* @param ?array<string,mixed> $sub
@@ -8,7 +8,7 @@ function lang(
string $key,
?string $default = NULL,
?array $sub = NULL) {
- $lang = $GLOBALS['lang'];
+ $lang = $GLOBALS['__lang'];
if(array_key_exists($key, $lang)) {
if ($sub) {
return sprintf($lang[$key], ...$sub);