diff --git a/source/helpers.php b/source/helpers.php index e8086e4..46b4081 100644 --- a/source/helpers.php +++ b/source/helpers.php @@ -67,4 +67,13 @@ function render(string $template_name, array $arguments) : string ); } + +/** + */ +function navigate(string $target) : void +{ + \header('Location: ' . $target); +} + + ?> diff --git a/source/index.html.php b/source/index.html.php index f96639a..0140df9 100644 --- a/source/index.html.php +++ b/source/index.html.php @@ -3,13 +3,32 @@ require_once(__DIR__ . '/helpers.php'); require_once(__DIR__ . '/logic.php'); -/** - * @todo deactivate - */ -\rosavox\logic\docs_add_examples(); +\rosavox\logic\docs_init(); + $mode = ($_GET['mode'] ?? 'list'); -$id_encoded = ($_GET['id'] ?? null); +$id_encoded = (! empty($_GET['id']) ? $_GET['id'] : null); $id = (($id_encoded === null) ? null : \rosavox\logic\docs_id_decode($id_encoded)); + + +function nav(string $mode, array $args) : void +{ + $a = \array_merge($args, ['mode' => $mode]); + $target = \implode( + '&', + \array_map( + fn ($key) => \rosavox\helpers\string_coin( + '{{key}}={{value}}', + [ + 'key' => $key, + 'value' => $a[$key], + ] + ), + \array_keys($a) + ) + ); + \rosavox\helpers\navigate('?' . $target); +} + ?> @@ -21,6 +40,12 @@ $id = (($id_encoded === null) ? null : \rosavox\logic\docs_id_decode($id_encoded $_GET['title'], + 'authors' => \explode(',', $_GET['authors']), + 'content' => $_GET['content'], + 'reasoning' => (empty($_GET['reasoning']) ? null : $_GET['reasoning']), + ]; + if ($id === null) + { + $id = \rosavox\logic\docs_create($doc); + } + else + { + \rosavox\logic\docs_update($id, $doc); + } + // nav('edit', ['id' => \rosavox\logic\docs_id_encode($id)]); + nav('list', []); break; } default: diff --git a/source/logic.php b/source/logic.php index 4c9b8de..2ae512f 100644 --- a/source/logic.php +++ b/source/logic.php @@ -18,6 +18,38 @@ class docs_state } +/** + */ +function docs_push() : void +{ + $path = 'docs.json'; + $data = [ + 'current_id' => docs_state::$current_id, + 'pool' => docs_state::$pool, + ]; + $content = \json_encode($data, \JSON_PRETTY_PRINT); + \file_put_contents($path, $content); +} + + +/** + */ +function docs_pull() : void +{ + $path = 'docs.json'; + $content = (\file_exists($path) ? \file_get_contents($path) : null); + $data = ( + ($content === null) + ? + ['current_id' => 0, 'pool' => []] + : + \json_decode($content, true) + ); + docs_state::$current_id = $data['current_id']; + docs_state::$pool = $data['pool']; +} + + /** */ function docs_id_encode(int $id) : string @@ -55,7 +87,7 @@ function docs_read(int $id) : array $id_encoded = docs_id_encode($id); if (! \array_key_exists($id_encoded, docs_state::$pool)) { - throw (new Error('not found')); + throw (new \Exception('not found')); } else { @@ -72,6 +104,7 @@ function docs_create(array $doc) : int $id = docs_state::$current_id; $id_encoded = docs_id_encode($id); docs_state::$pool[$id_encoded] = $doc; + docs_push(); return $id; } @@ -83,11 +116,12 @@ function docs_update(int $id, array $doc) : void $id_encoded = docs_id_encode($id); if (! \array_key_exists($id_encoded, docs_state::$pool)) { - throw (new Error('not found')); + throw (new \Exception('not found')); } else { docs_state::$pool[$id_encoded] = $doc; + docs_push(); } } @@ -99,16 +133,25 @@ function docs_delete(int $id) : void $id_encoded = docs_id_encode($id); if (! \array_key_exists($id_encoded, docs_state::$pool)) { - throw (new Error('not found')); + throw (new \Exception('not found')); } else { docs_state::$pool[$id_encoded] = null; unset(docs_state::$pool[$id_encoded]); + docs_push(); } } +/** + */ +function docs_init() : void +{ + docs_pull(); +} + + /** */ function docs_add_examples() : void