feat: replace mzur/kirby-uniform with custom Kontaktformular implementation
Replaced the `mzur/kirby-uniform` dependency with a custom form handling solution. Updated `kontakt.php`, its backend controller, and corresponding email template for streamlined submission handling. Adjusted styles in `input.css` and integrated a honeypot field for spam prevention.
This commit is contained in:
@@ -1,33 +1,71 @@
|
|||||||
<?php
|
<?php
|
||||||
|
return function($kirby, $pages, $page) {
|
||||||
|
|
||||||
use Uniform\Form;
|
$alert = null;
|
||||||
|
|
||||||
return function ($kirby)
|
if ($kirby->request()->is('POST') && get('submit')) {
|
||||||
{
|
|
||||||
$form = new Form([
|
|
||||||
'email' => [
|
|
||||||
'rules' => ['required', 'email'],
|
|
||||||
'message' => 'Bitte geben Sie eine gültige E-Mail Adresse ein.',
|
|
||||||
],
|
|
||||||
'name' => [
|
|
||||||
'rules' => ['required'],
|
|
||||||
'message' => 'Bitte geben Sie Ihren Namen ein.',
|
|
||||||
],
|
|
||||||
'message' => [
|
|
||||||
'rules' => ['required'],
|
|
||||||
'message' => 'Bitte geben Sie eine Nachricht ein.',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($kirby->request()->is('POST')) {
|
// check the honeypot
|
||||||
$form->emailAction([
|
if (empty(get('website')) === false) {
|
||||||
'to' => [
|
go($page->url());
|
||||||
'info@feigel.it'
|
}
|
||||||
],
|
|
||||||
'from' => 'webmaster@schachfreunde-badsteben.de',
|
$data = [
|
||||||
'subject' => 'Kontaktanfrage über die Homepage der Schachfreunde Bad Steben',
|
'name' => get('name'),
|
||||||
])->done();
|
'email' => get('email'),
|
||||||
|
'text' => get('text')
|
||||||
|
];
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
'name' => ['required', 'minLength' => 3],
|
||||||
|
'email' => ['required', 'email'],
|
||||||
|
'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
|
||||||
|
];
|
||||||
|
|
||||||
|
$messages = [
|
||||||
|
'name' => 'Bitte geben Sie einen Namen ein.',
|
||||||
|
'email' => 'Bitte geben Sie eine gültige E-Mail-Adresse ein.',
|
||||||
|
'text' => 'Bitte geben Sie einen Text ein, der zwischen 3 und 3000 Zeichen lang ist.'
|
||||||
|
];
|
||||||
|
|
||||||
|
// some of the data is invalid
|
||||||
|
if ($invalid = invalid($data, $rules, $messages)) {
|
||||||
|
$alert = $invalid;
|
||||||
|
|
||||||
|
// the data is fine, let's send the email
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
$kirby->email([
|
||||||
|
'template' => 'email',
|
||||||
|
'from' => 'webmaster@schachfreunde-badsteben.de',
|
||||||
|
'replyTo' => $data['email'],
|
||||||
|
'to' => 'info@feigel.it',
|
||||||
|
'subject' => esc($data['name']) . ' hat eine Nachricht über das Kontaktformular gesendet',
|
||||||
|
'data' => [
|
||||||
|
'text' => esc($data['text']),
|
||||||
|
'sender' => esc($data['name'])
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (Exception $error) {
|
||||||
|
if (option('debug')):
|
||||||
|
$alert['error'] = 'Beim Versenden ist ein Fehler aufgetreten: <strong>' . $error->getMessage() . '</strong>';
|
||||||
|
else:
|
||||||
|
$alert['error'] = 'Beim Versenden ist ein Fehler aufgetreten!';
|
||||||
|
endif;
|
||||||
|
}
|
||||||
|
|
||||||
|
// no exception occurred, let's send a success message
|
||||||
|
if (empty($alert) === true) {
|
||||||
|
$success = 'Ihre Nachricht wurde gesendet! Wir werden uns bald bei Ihnen melden.';
|
||||||
|
$data = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return compact('form');
|
return [
|
||||||
|
'alert' => $alert,
|
||||||
|
'data' => $data ?? false,
|
||||||
|
'success' => $success ?? false
|
||||||
|
];
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Hallo Schachfreunde Bad Steben,
|
||||||
|
|
||||||
|
<?= $text ?>
|
||||||
|
|
||||||
|
<?= $sender ?>
|
||||||
+40
-32
@@ -58,45 +58,53 @@
|
|||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
<form action="<?php echo $page->url() ?>" method="POST" class="px-6 pt-20 pb-24 sm:pb-32 lg:px-8 lg:py-12">
|
<form action="<?= $page->url() ?>" method="POST" class="px-6 pt-20 pb-24 sm:pb-32 lg:px-8 lg:py-12">
|
||||||
<div class="mx-auto max-w-xl lg:mr-0 lg:max-w-lg">
|
<div class="honeypot">
|
||||||
<div class="grid grid-cols-1 gap-x-8 gap-y-6 sm:grid-cols-2">
|
<label for="website">Website <abbr title="required">*</abbr></label>
|
||||||
<div class="sm:col-span-2">
|
<input type="url" id="website" name="website" tabindex="-1">
|
||||||
<label for="name" class="block text-sm/6 font-semibold text-gray-900">Name</label>
|
</div>
|
||||||
<div class="mt-2.5">
|
<div class="mx-auto max-w-xl lg:mr-0 lg:max-w-lg">
|
||||||
<input type="text" id="name" name="name" class="<?php if ($form->error('name')): ?>error <?php endif; ?>block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600" value="<?php echo $form->old('name') ?>" />
|
<div class="grid grid-cols-1 gap-x-8 gap-y-6 sm:grid-cols-2">
|
||||||
|
<div class="sm:col-span-2">
|
||||||
|
<label for="name" class="block text-sm/6 font-semibold text-gray-900">Name</label>
|
||||||
|
<div class="mt-2.5">
|
||||||
|
<input type="text" id="name" name="name" class="block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600" value="<?= esc($data['name'] ?? '', 'attr') ?>" required>
|
||||||
|
<?= isset($alert['name']) ? '<span class="alert error">' . esc($alert['name']) . '</span>' : '' ?>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="sm:col-span-2">
|
||||||
|
<label for="email" class="block text-sm/6 font-semibold text-gray-900">E-Mail-Adresse</label>
|
||||||
|
<div class="mt-2.5">
|
||||||
|
<input type="email" name="email" id="email" autocomplete="email" class="block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600" value="<?= esc($data['email'] ?? '', 'attr') ?>" required>
|
||||||
|
<?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="sm:col-span-2">
|
||||||
|
<label for="message" class="block text-sm/6 font-semibold text-gray-900">Nachricht</label>
|
||||||
|
<div class="mt-2.5">
|
||||||
|
<textarea name="message" id="message" rows="4" class="block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600" required><?= esc($data['text'] ?? '') ?></textarea>
|
||||||
|
<?= isset($alert['text']) ? '<span class="alert error">' . esc($alert['text']) . '</span>' : '' ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mt-8 flex justify-end">
|
||||||
|
<button type="submit" class="rounded-md bg-sf_blau-500 px-3.5 py-2.5 text-center text-sm font-semibold text-white shadow-xs hover:bg-sf_gelb-500 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Absenden</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="sm:col-span-2">
|
|
||||||
<label for="email" class="block text-sm/6 font-semibold text-gray-900">E-Mail-Adresse</label>
|
|
||||||
<div class="mt-2.5">
|
|
||||||
<input type="email" name="email" id="email" autocomplete="email" class="<?php if ($form->error('email')): ?>error <?php endif; ?>block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600" value="<?php echo $form->old('email') ?>"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="sm:col-span-2">
|
|
||||||
<label for="message" class="block text-sm/6 font-semibold text-gray-900">Nachricht</label>
|
|
||||||
<div class="mt-2.5">
|
|
||||||
<textarea name="message" id="message" rows="4" class="<?php if ($form->error('message')): ?>error <?php endif; ?>block w-full rounded-md bg-white px-3.5 py-2 text-base text-gray-900 outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"><?php echo $form->old('message') ?></textarea>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-8 flex justify-end">
|
</form>
|
||||||
<button type="submit" class="rounded-md bg-sf_blau-500 px-3.5 py-2.5 text-center text-sm font-semibold text-white shadow-xs hover:bg-sf_gelb-500 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Absenden</button>
|
<?php if ($success): ?>
|
||||||
|
<div class="alert success">
|
||||||
|
<p><?= $success ?></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<?php echo csrf_field() ?>
|
|
||||||
<?php echo honeypot_field() ?>
|
|
||||||
<?php if ($form->success()): ?>
|
|
||||||
Thank you for your message. We will get back to you soon!
|
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<div class="text-red-500 pt-12">
|
<?php if (isset($alert['error'])): ?>
|
||||||
<?php snippet('uniform/errors', ['form' => $form]) ?>
|
<div><?= $alert['error'] ?></div>
|
||||||
</div>
|
<?php endif ?>
|
||||||
|
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</form>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+1
-1
@@ -70,7 +70,7 @@
|
|||||||
@apply text-sf_blau-500 font-bold hover:text-sf_gelb-600;
|
@apply text-sf_blau-500 font-bold hover:text-sf_gelb-600;
|
||||||
}
|
}
|
||||||
|
|
||||||
.uniform__potty {
|
.honeypot {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: -9999px;
|
left: -9999px;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user