rosavox/source/index.html.php
2025-05-22 06:46:44 +00:00

187 lines
4.1 KiB
PHP

<?php
require_once('helpers/string.php');
require_once('helpers/misc.php');
require_once('services/doc.php');
\rosavox\services\doc\init();
$mode = ($_GET['mode'] ?? 'list');
$id_encoded = (! empty($_GET['id']) ? $_GET['id'] : null);
$id = (($id_encoded === null) ? null : \intval($id_encoded));
/**
*/
function make_link(string $mode, array $args) : string
{
$a = \array_merge($args, ['mode' => $mode]);
return (
'?'
.
\implode(
'&',
\array_map(
fn ($key) => \rosavox\helpers\string_\coin(
'{{key}}={{value}}',
[
'key' => $key,
'value' => $a[$key],
]
),
\array_keys($a)
)
)
);
}
/**
*/
function nav(string $mode, array $args) : void
{
\rosavox\helpers\misc\navigate(make_link($mode, $args));
}
/**
*/
function render_list() : string
{
return \rosavox\helpers\misc\render(
'docs-list',
[
'label_make' => \rosavox\helpers\misc\translate('action.make'),
'link_make' => make_link('make', []),
'entries' => \implode(
"\n",
\array_map(
fn ($entry) => \rosavox\helpers\misc\render(
'docs-list-entry',
[
'label_read' => \rosavox\helpers\misc\translate('action.read'),
'label_hear' => \rosavox\helpers\misc\translate('action.hear'),
'value_link_open' => make_link('edit', ['id' => \sprintf('%u', $entry['id'])]),
'value_link_read' => \rosavox\services\doc\readable_path($entry['id']),
'value_link_hear' => \rosavox\services\doc\audio_path($entry['id']),
'value_name' => \rosavox\helpers\string_\coin(
'{{name}}.oga',
[
'name' => \rosavox\services\doc\name($entry['id']),
]
),
'value_text' => $entry['value']['title'],
]
),
\rosavox\services\doc\list_()
)
),
]
);
}
/**
*/
function render_edit(?int $id) : string
{
$doc = (
($id === null)
?
\rosavox\services\doc\empty_()
:
\rosavox\services\doc\read($id)
);
return \rosavox\helpers\misc\render(
'docs-edit',
[
'label_action_back' => \rosavox\helpers\misc\translate('action.back'),
'label_action_save' => \rosavox\helpers\misc\translate('action.save'),
'label_action_delete' => \rosavox\helpers\misc\translate('action.delete'),
'label_doc_title' => \rosavox\helpers\misc\translate('domain.doc.title'),
'label_doc_authors' => \rosavox\helpers\misc\translate('domain.doc.authors'),
'label_doc_content' => \rosavox\helpers\misc\translate('domain.doc.content'),
'label_doc_reasoning' => \rosavox\helpers\misc\translate('domain.doc.reasoning'),
'value_action_back' => make_link('list', []),
'value_action_save' => make_link('save', (($id === null) ? [] : ['id' => \strval($id)])),
'value_action_delete' => make_link('delete', (($id === null) ? [] : ['id' => \strval($id)])),
'value_doc_title' => $doc['title'],
'value_doc_authors' => \implode(', ', $doc['authors']),
'value_doc_content' => $doc['content'],
'value_doc_reasoning' => ($doc['reasoning'] ?? ''),
]
);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="style.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<h1>rosavox</h1>
<?php
switch ($mode)
{
case 'delete':
{
if ($id === null)
{
// do nothing
}
else
{
\rosavox\services\doc\delete($id);
}
nav('list', []);
break;
}
case 'save':
{
$doc = [
'title' => $_POST['title'],
'authors' => \explode(',', $_POST['authors']),
'content' => $_POST['content'],
'reasoning' => (empty($_POST['reasoning']) ? null : $_GET['reasoning']),
];
if ($id === null)
{
$id = \rosavox\services\doc\create($doc);
}
else
{
\rosavox\services\doc\update($id, $doc);
}
// nav('edit', ['id' => \rosavox\services\doc\id_encode($id)]);
nav('list', []);
break;
}
case 'list':
{
echo(render_list());
break;
}
case 'make':
{
echo(render_edit(null));
break;
}
case 'edit':
{
echo(render_edit($id));
break;
}
default:
{
throw (new \Exception(\sprintf('invalid mode: %s', $mode)));
break;
}
}
?>
</body>
</html>