I have been following the cookbook on updating from the front end. It works great, but I want to have one form that creates a new page and one form that updates the current page
I have this so far, both forms work fine if I remove the other.
<?php
return function ($kirby, $page) {
// convert to invoice
if ($kirby->request()->is('POST') && get('convert')) {
// check the honeypot and exit if is has been filled in
if(empty(get('website')) === false) {
go($page->url());
exit;
}
// authenticate as almighty
$kirby->impersonate('kirby');
try {
// create new invoice
$newPage = $page->slug().'-inv';
$invoice = page($page->parent())->createChild([
'slug' => $newPage,
'template' => 'invoice',
'content' => [
'title' =>$page->title(),
'total' => $page->total(),
]
]);
// go to new invoice
if ($invoice) {
// store referer and name in session
$kirby->session()->set([
'referer' => $page->uri()
]);
go($invoice->uri());
}
} catch (Exception $e) {
$alert = ['Update failed: ' . $e->getMessage()];
}
}
return [
'alert' => $alert ?? null,
];
// mark as sent
if ($kirby->request()->is('POST') && get('sent')) {
// check the honeypot and exit if is has been filled in
if(empty(get('name')) === false) {
go($page->url());
exit;
}
// authenticate as almighty
$kirby->impersonate('kirby');
try {
// update page to sent
$page->changeStatus('listed');
} catch (Exception $e) {
$alert = ['Update failed: ' . $e->getMessage()];
}
}
return [
'alert' => $alert ?? null,
];
};