Hi there! Hope someone can help us out!
Im trying to figure out how to combine the two docs for creating pages and adding files to those pages frontend. I cant get it to work and have been add it for couple of days but we need this quick as our project is growing pretty quick and this would speed up the process.
Here is the link of the project: https://www.stay-sane-stay-safe.com
We want the people to be able to submit posters with the files on their own.
And that we can select those files for thumbnail and download-link.
Im able to create the (poster)pages in draft but not with the files that should go with them.
Controller (posters):
<?php
return function ($kirby, $page) {
// if the form has been submitted…
if ($kirby->request()->is('POST') && get('register')) {
// check the honeypot and exit if is has been filled in
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'name' => get('name'),
];
$rules = [
'name' => ['required'],
];
$messages = [
'name' => 'Please enter your (link: #name text: name)',
];
// some of the data is invalid
if ($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
} else {
// authenticate as almighty
$kirby->impersonate('kirby');
// everything is ok, let's try to create a new registration
try {
// we store registrations as subpages of the current page
$registration = $page->createChild([
'slug' => $data['name']. date('D-M-Y-H-M'),
'template' => 'poster',
'content' => $data,
]);
if ($registration) {
// store referer and name in session
$kirby->session()->set([
'referer' => $page->uri(),
'regName' => esc($data['name'])
]);
go('success');
}
} catch (Exception $e) {
$alert = ['Your registration failed: ' . $e->getMessage()];
}
}
}
// return data to template
return [
'alert' => $alert ?? null,
'data' => $data ?? false,
];
};
Snippet (registration-form):
<form class="registration-form" action="<?= $page->url() ?>" enctype="multipart/form-data" method="POST">
<div class="form-element">
<label for="name">Name <abbr title="required">*</abbr></label>
<input type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>" required/>
</div>
<input class="registration-button" type="submit" name="register" value="Register" />
</form>
Plugin:
<?php
Kirby::plugin('eventkit/posters', [
'hooks' => [
'kirbytags:after' => function ($text, $data, $options) {
$session = kirby()->session();
if ($location = $session->get('referer')) {
if ($page = page(urldecode($location))) {
$title = $page->title();
}
}
return Str::template($text, [
'poster' => $title ?? '',
'name' => $session->get('regName') ?? ''
]);
}
],
]);