diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-04-26 10:06:03 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-04-26 10:06:03 +0200 |
commit | f9b93c4bbb23b452551bd46ce86cc6e3602fd594 (patch) | |
tree | 3fd8f14afb8248fcd9a3092a3f8ee1755dc936c3 | |
parent | 975c0b735be0808883116616d934ee257a24fb08 (diff) | |
download | faktura-f9b93c4bbb23b452551bd46ce86cc6e3602fd594.tar.gz faktura-f9b93c4bbb23b452551bd46ce86cc6e3602fd594.tar.bz2 faktura-f9b93c4bbb23b452551bd46ce86cc6e3602fd594.zip |
Begin web app frontend in PHP.main
While I'd like a command line client for retreiving information and
scripting stuff based on the db contents, a web app is convenient for
the less common stuff like adding and editing data.
The web app could also have been done in rust, however, I feel php is so
convenient for simple web stuff that I think it makes more sense this
way.
-rw-r--r-- | .ddev/config.yaml | 2 | ||||
-rw-r--r-- | .editorconfig | 3 | ||||
-rw-r--r-- | web/client/index.php | 43 | ||||
-rw-r--r-- | web/clients/index.php | 45 | ||||
-rw-r--r-- | web/faktura.php | 11 | ||||
-rw-r--r-- | web/include/api.php | 31 | ||||
-rw-r--r-- | web/include/misc.php | 36 | ||||
-rw-r--r-- | web/index.php | 2 |
8 files changed, 172 insertions, 1 deletions
diff --git a/.ddev/config.yaml b/.ddev/config.yaml index 20b7d2f..84dd754 100644 --- a/.ddev/config.yaml +++ b/.ddev/config.yaml @@ -5,7 +5,7 @@ name: faktura type: php -docroot: "" +docroot: "web" php_version: "8.1" webserver_type: nginx-fpm xdebug_enabled: false diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7198dbf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +[*.php] +indent_style=tab +indent_size=4 diff --git a/web/client/index.php b/web/client/index.php new file mode 100644 index 0000000..c89bde1 --- /dev/null +++ b/web/client/index.php @@ -0,0 +1,43 @@ +<?php +/** + * View to display information about a single client. + * + * @package volse.faktura.web + */ + +namespace Faktura; + +require_once __DIR__ . '/../faktura.php'; + +$result = API\call('GET', 'clients?id=eq.' . intval($_GET['id'])); +$client = json_decode($result)[0]; +?> +<!DOCTYPE html> +<head> + <title>faktura: client</title> +</head> +<body> +<h1><?php echo esc_html($client->name); ?></h1> +<table> + <tr class="field"> + <th class="row-label">Contact:</th> + <td class="value"><?php echo esc_html($client->contact); ?></td> + </tr> + <tr class="field"> + <th class="row-label">Email:</th> + <td class="value"><?php echo esc_html($client->email); ?></td> + </tr> + <tr class="field"> + <th class="row-label">phone:</td> + <td class="value"><?php echo esc_html($client->phone); ?></td> + </tr> + <tr class="field"> + <th class="row-label">Address:</th> + <td class="value"><?php echo esc_html($client->address); ?></td> + </tr> + <tr class="field"> + <th class="row-label">VAT:</th> + <td class="value"><?php echo $client->vat ? 'Yes' : 'No'; ?></td> + </tr> +</body> + diff --git a/web/clients/index.php b/web/clients/index.php new file mode 100644 index 0000000..4725b17 --- /dev/null +++ b/web/clients/index.php @@ -0,0 +1,45 @@ +<?php +/** + * Clients index view + * + * @package volse.faktura.web + */ + +namespace Faktura; + +require_once __DIR__ . '/../faktura.php'; + +$result = API\call('GET', 'clients'); +$clients = json_decode($result); +?> +<!DOCTYPE html> +<head> + <title>faktura: clients</title> +</head> +<body> + <h1>Clients</h1> + <table> + <tr> + <th>#</th> + <th>Name</th> + <th>Contact</th> + <th>Email</th> + <th>Phone</th> + <th>VAT</th> + </tr> + <?php foreach ($clients as $client) { ?> + <tr> + <td><?php echo esc_html($client->id); ?></td> + <td> + <a href="/client/?id=<?php echo esc_attr($client->id); ?>"> + <?php echo esc_html($client->name); ?> + </a> + </td> + <td><?php echo esc_html($client->contact); ?></td> + <td><?php echo esc_html($client->email); ?></td> + <td><?php echo esc_html($client->phone); ?></td> + <td><?php echo esc_html($client->vat ? 'X' : ''); ?></td> + </tr> + <?php } ?> + </table> +</body> diff --git a/web/faktura.php b/web/faktura.php new file mode 100644 index 0000000..455f5f9 --- /dev/null +++ b/web/faktura.php @@ -0,0 +1,11 @@ +<?php +/** + * Main bootstrap module for the faktura web project + * + * @packkage volse.faktura.web + */ + +namespace Faktura; + +require_once __DIR__ . '/include/api.php'; +require_once __DIR__ . '/include/misc.php'; diff --git a/web/include/api.php b/web/include/api.php new file mode 100644 index 0000000..c13b15c --- /dev/null +++ b/web/include/api.php @@ -0,0 +1,31 @@ +<?php +/** + * Functions for intercting with the remote API. + * + * @package volse.faktura.web + */ + +namespace Faktura\API; + +define('API_URL', 'http://postgrest:3000'); + +function call(string $method, string $resource, mixed $data = null): string { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, API_URL . '/' . $resource); + curl_setopt($ch, CURLOPT_HEADER, 'Accept: application/json'); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + + if ($result === false) { + echo curl_error($ch); + die(); + } + + curl_close($ch); + + return $result; +} + diff --git a/web/include/misc.php b/web/include/misc.php new file mode 100644 index 0000000..15b5cea --- /dev/null +++ b/web/include/misc.php @@ -0,0 +1,36 @@ +<?php +/** + * Misc utility functions + * + * @package volse.faktura.web + */ + +/** + * Escape an input string so that it's safe fr use in a html context. + * + * @param string $input The unescaped input string. + * + * @return A string that's safe for use in html tags. + */ +function esc_html(?string $input): string { + if (empty($input)) { + return ''; + } + + return htmlspecialchars($input, ENT_NOQUOTES | ENT_HTML5); +} + +/** + * Escape input so that it's safe for use in html attrubutes. + * + * @param string $input The unescaped input string. + * + * @return A string that's safe for use in html attibutes. + */ +function esc_attr(?string $input): string { + if (empty($input)) { + return ''; + } + + return htmlspecialchars($input, ENT_QUOTES | ENT_HTML5); +} diff --git a/web/index.php b/web/index.php new file mode 100644 index 0000000..aefe49e --- /dev/null +++ b/web/index.php @@ -0,0 +1,2 @@ +<?php +include 'faktura.php'; |