[mod] Persistenz eingebaut
This commit is contained in:
parent
562f3cda0a
commit
6df7f11bfd
3 changed files with 101 additions and 9 deletions
|
@ -67,4 +67,13 @@ function render(string $template_name, array $arguments) : string
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
function navigate(string $target) : void
|
||||
{
|
||||
\header('Location: ' . $target);
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -21,6 +40,12 @@ $id = (($id_encoded === null) ? null : \rosavox\logic\docs_id_decode($id_encoded
|
|||
<?php
|
||||
switch ($mode)
|
||||
{
|
||||
case 'example':
|
||||
{
|
||||
\rosavox\logic\docs_add_examples();
|
||||
nav('list', []);
|
||||
break;
|
||||
}
|
||||
case 'list':
|
||||
{
|
||||
echo(
|
||||
|
@ -88,7 +113,22 @@ $id = (($id_encoded === null) ? null : \rosavox\logic\docs_id_decode($id_encoded
|
|||
}
|
||||
case 'save':
|
||||
{
|
||||
throw (new \Exception('not implemented yet'));
|
||||
$doc = [
|
||||
'title' => $_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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue