246 lines
4.3 KiB
PHP
246 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace rosavox\services\doc;
|
|
|
|
require_once('helpers/string.php');
|
|
require_once('helpers/storage.php');
|
|
require_once('helpers/misc.php');
|
|
|
|
|
|
/**
|
|
*/
|
|
class state
|
|
{
|
|
public static ?\rosavox\helpers\storage\interface_ $storage = null;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function name(int $id) : string
|
|
{
|
|
return \sprintf('doc-%04u', $id);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function audio_path(int $id) : string
|
|
{
|
|
return \sprintf('docs/%s.oga', name($id));
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function readable_path(int $id) : string
|
|
{
|
|
return \sprintf('docs/%s.md', name($id));
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function generate_readable(int $id, $doc) : void
|
|
{
|
|
$markdown = \rosavox\helpers\string_\coin(
|
|
"# {{value_title}}
|
|
|
|
## {{label_authors}}
|
|
|
|
{{value_authors}}
|
|
|
|
|
|
## {{label_content}}
|
|
|
|
{{value_content}}
|
|
|
|
{{macro_reasoning}}",
|
|
[
|
|
'label_title' => \rosavox\helpers\misc\translate('domain.doc.title'),
|
|
'value_title' => $doc['title'],
|
|
'label_authors' => \rosavox\helpers\misc\translate('domain.doc.authors'),
|
|
'value_authors' => \implode(
|
|
"\n",
|
|
\array_map(
|
|
fn ($author) => \rosavox\helpers\string_\coin(
|
|
'- {{author}}',
|
|
[
|
|
'author' => $author,
|
|
]
|
|
),
|
|
$doc['authors']
|
|
)
|
|
),
|
|
'label_content' => \rosavox\helpers\misc\translate('domain.doc.content'),
|
|
'value_content' => $doc['content'],
|
|
'macro_reasoning' => (
|
|
($doc['reasoning'] === null)
|
|
?
|
|
''
|
|
:
|
|
\rosavox\helpers\string_\coin(
|
|
"
|
|
## {{label_reasoning}}
|
|
|
|
{{value_reasoning}}",
|
|
[
|
|
'label_reasoning' => \rosavox\helpers\misc\translate('domain.doc.reasoning'),
|
|
'value_reasoning' => $doc['reasoning'],
|
|
]
|
|
)
|
|
)
|
|
]
|
|
);
|
|
\file_put_contents(
|
|
readable_path($id),
|
|
$markdown
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function generate_audio(int $id, $doc) : void
|
|
{
|
|
$pause = " .\n";
|
|
$text = \rosavox\helpers\string_\coin(
|
|
"{{value_title}}{{pause}}{{label_authors}}: {{value_authors}}{{pause}}{{label_content}}: {{value_content}}{{macro_reasoning}}",
|
|
[
|
|
'pause' => $pause,
|
|
'label_title' => \rosavox\helpers\misc\translate('domain.doc.title'),
|
|
'value_title' => $doc['title'],
|
|
'label_authors' => \rosavox\helpers\misc\translate('domain.doc.authors'),
|
|
'value_authors' => \implode($pause, $doc['authors']),
|
|
'label_content' => \rosavox\helpers\misc\translate('domain.doc.content'),
|
|
'value_content' => $doc['content'],
|
|
'macro_reasoning' => (
|
|
($doc['reasoning'] === null)
|
|
?
|
|
''
|
|
:
|
|
\rosavox\helpers\string_\coin(
|
|
"{{pause}}{{label_reasoning}}: {{value_reasoning}}",
|
|
[
|
|
'pause' => $pause,
|
|
'label_reasoning' => \rosavox\helpers\misc\translate('domain.doc.reasoning'),
|
|
'value_reasoning' => $doc['reasoning'],
|
|
]
|
|
)
|
|
)
|
|
]
|
|
);
|
|
\rosavox\helpers\misc\generate_audio(
|
|
$text,
|
|
audio_path($id)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function empty_() : array
|
|
{
|
|
return [
|
|
'title' => '',
|
|
'authors' => [],
|
|
'content' => '',
|
|
'reasoning' => null,
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function list_() : array
|
|
{
|
|
return state::$storage->list_();
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function read(int $id) : array
|
|
{
|
|
return state::$storage->read($id);
|
|
}
|
|
|
|
|
|
/**
|
|
* @todo async generating
|
|
*/
|
|
function create(array $doc) : int
|
|
{
|
|
$id = state::$storage->create($doc);
|
|
generate_readable($id, $doc);
|
|
generate_audio($id, $doc);
|
|
return $id;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function update(int $id, array $doc) : void
|
|
{
|
|
state::$storage->update($id, $doc);
|
|
generate_readable($id, $doc);
|
|
generate_audio($id, $doc);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function delete(int $id) : void
|
|
{
|
|
state::$storage->delete($id);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function add_examples() : void
|
|
{
|
|
create(
|
|
[
|
|
'title' => 'Freibier bei Parteitagen',
|
|
'authors' => [
|
|
'Björn Biernot',
|
|
'Doreen Dauerdurst',
|
|
],
|
|
'content' => 'Der Landesverband möge beschließen, dass zu Beginn eines jeden Parteitags für jeden Deligierten mindestens zwei Flaschen Bier auf den zugehörigen Platz zu stellen sind.',
|
|
'reasoning' => 'Wir haben Durst!',
|
|
]
|
|
);
|
|
create(
|
|
[
|
|
'title' => 'Götterdämmerung',
|
|
'authors' => [
|
|
'Fenriswolf',
|
|
],
|
|
'content' => 'Der Allvater hat mich betrogen. Ich werde ihn und seine elende Asenbrut verschlingen.',
|
|
'reasoning' => null,
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function init() : void
|
|
{
|
|
state::$storage = new \rosavox\helpers\storage\class_jsonfile(
|
|
'docs.json',
|
|
fn ($id) => \sprintf('%u', $id),
|
|
fn ($id_encoded) => \intval($id_encoded)
|
|
);
|
|
if (empty(state::$storage->list_()))
|
|
{
|
|
add_examples();
|
|
}
|
|
else
|
|
{
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
?>
|