Added wrapper for label and input, Added options for redirecting and email customization, Added basic resources.

This commit is contained in:
Lewin Probst 2020-02-03 16:08:45 +01:00
parent 0222265abc
commit 29d903d070
11 changed files with 126 additions and 22 deletions

View file

@ -9,8 +9,14 @@ return new class extends Lewp\Module
const KEY_CSRF_TOKEN = 'csrf_token'; const KEY_CSRF_TOKEN = 'csrf_token';
const KEY_CAPTCHA_PHRASE = 'captcha_phrase'; const KEY_CAPTCHA_PHRASE = 'captcha_phrase';
const KEY_POST_EMAIL = 'email';
const KEY_POST_NAME = 'name';
const KEY_POST_MESSAGE = 'message';
const OPTIONS_REDIRECT_URI = 'location_on_success';
const OPTIONS_MAIL_FROM = 'mail_from';
const OPTIONS_MAIL_TO = 'mail_to';
private function createForm() private function createForm(bool $group_input_and_labels = true)
{ {
$form = $this->createAndSetupElement( $form = $this->createAndSetupElement(
'form', 'form',
@ -20,6 +26,7 @@ return new class extends Lewp\Module
'action' => '', 'action' => '',
] ]
); );
// LABEL NAME // LABEL NAME
$label_name = $this->createAndSetupElement( $label_name = $this->createAndSetupElement(
'label', 'label',
@ -31,7 +38,6 @@ return new class extends Lewp\Module
'for' => 'name', 'for' => 'name',
] ]
); );
$form->appendChild($label_name);
// NAME INPUT // NAME INPUT
$name = $this->createAndSetupElement( $name = $this->createAndSetupElement(
'input', 'input',
@ -40,10 +46,11 @@ return new class extends Lewp\Module
'id' => 'name', 'id' => 'name',
'name' => 'name', 'name' => 'name',
'type' => 'text', 'type' => 'text',
'value' => $_POST['name'] ?? '', 'value' => $_POST[self::KEY_POST_NAME] ?? '',
'required' => 'required',
'placeholder' => ' ',
] ]
); );
$form->appendChild($name);
// LABEL Email // LABEL Email
$label_email = $this->createAndSetupElement( $label_email = $this->createAndSetupElement(
'label', 'label',
@ -55,7 +62,6 @@ return new class extends Lewp\Module
'for' => 'email' 'for' => 'email'
] ]
); );
$form->appendChild($label_email);
// EMAIL INPUT // EMAIL INPUT
$email = $this->createAndSetupElement( $email = $this->createAndSetupElement(
'input', 'input',
@ -64,10 +70,11 @@ return new class extends Lewp\Module
'id' => 'email', 'id' => 'email',
'name' => 'email', 'name' => 'email',
'type' => 'email', 'type' => 'email',
'value' => $_POST['email'] ?? '', 'value' => $_POST[self::KEY_POST_EMAIL] ?? '',
'required' => 'required',
'placeholder' => ' ',
] ]
); );
$form->appendChild($email);
// LABEL Message // LABEL Message
$label_message = $this->createAndSetupElement( $label_message = $this->createAndSetupElement(
'label', 'label',
@ -79,27 +86,28 @@ return new class extends Lewp\Module
'for' => 'message' 'for' => 'message'
] ]
); );
$form->appendChild($label_message);
// MESSAGE INPUT // MESSAGE INPUT
$message = $this->createAndSetupElement( $message = $this->createAndSetupElement(
'textarea', 'textarea',
$_POST['message'] ?? '', $_POST[self::KEY_POST_MESSAGE] ?? '',
[ [
'id' => 'message', 'id' => 'message',
'name' => 'message', 'name' => 'message',
'required' => 'required',
'placeholder' => ' ',
] ]
); );
$form->appendChild($message);
// LABEL Captcha // LABEL Captcha
$label_captcha = $this->createAndSetupElement( $label_captcha = $this->createAndSetupElement(
'label', 'label',
'', $this->loadTextFile(Resolve::arrayToId([
$this->getLanguage(),
'captcha'
])),
[ [
'for' => self::KEY_CAPTCHA_PHRASE 'for' => self::KEY_CAPTCHA_PHRASE
] ]
); );
$label_captcha->appendChild($this->generateCaptcha());
$form->appendChild($label_captcha);
// CAPTCHA INPUT // CAPTCHA INPUT
$captcha = $this->createAndSetupElement( $captcha = $this->createAndSetupElement(
'input', 'input',
@ -108,9 +116,10 @@ return new class extends Lewp\Module
'id' => 'captchainput', 'id' => 'captchainput',
'name' => self::KEY_CAPTCHA_PHRASE, 'name' => self::KEY_CAPTCHA_PHRASE,
'type' => 'text', 'type' => 'text',
'required' => 'required',
'placeholder' => ' ',
] ]
); );
$form->appendChild($captcha);
// SUBMIT BUTTON // SUBMIT BUTTON
$submit = $this->createAndSetupElement( $submit = $this->createAndSetupElement(
'button', 'button',
@ -123,6 +132,48 @@ return new class extends Lewp\Module
'type' => 'submit', 'type' => 'submit',
] ]
); );
// APPEND THEM TO DOCUMENT
if ($group_input_and_labels) {
$div = $this->createAndSetupElement(
'div', '', ['class' => 'fieldwrapper']
);
$div->appendChild($name);
$div->appendChild($label_name);
$form->appendChild($div);
$div = $this->createAndSetupElement(
'div', '', ['class' => 'fieldwrapper']
);
$div->appendChild($email);
$div->appendChild($label_email);
$form->appendChild($div);
$div = $this->createAndSetupElement(
'div', '', ['class' => 'fieldwrapper']
);
$div->appendChild($message);
$div->appendChild($label_message);
$form->appendChild($div);
} else {
$form->appendChild($label_name);
$form->appendChild($name);
$form->appendChild($label_email);
$form->appendChild($email);
$form->appendChild($label_message);
$form->appendChild($message);
}
$form->appendChild($this->generateCaptcha());
if ($group_input_and_labels) {
$div = $this->createAndSetupElement(
'div', '', ['class' => 'fieldwrapper']
);
$div->appendChild($captcha);
$div->appendChild($label_captcha);
$form->appendChild($div);
} else {
$form->appendChild($label_captcha);
$form->appendChild($captcha);
}
$form->appendChild($submit); $form->appendChild($submit);
// ADD CSRF CHECK // ADD CSRF CHECK
$form->appendChild($this->generateCsrfInput()); $form->appendChild($this->generateCsrfInput());
@ -202,8 +253,26 @@ return new class extends Lewp\Module
) ? true : false; ) ? true : false;
} }
private function sendMail($payload, $options)
{
mail(
$options[self::OPTIONS_MAIL_TO],
'['.$this->getTLD().'] '.$payload[self::KEY_POST_NAME],
$payload[self::KEY_POST_MESSAGE],
'From: '.$options[self::OPTIONS_MAIL_FROM]
."\r\n"
.'Reply-To: '.$payload[self::KEY_POST_EMAIL]
);
}
public function run(array $options = []) : bool public function run(array $options = []) : bool
{ {
// set default options
$options += [
self::OPTIONS_REDIRECT_URI => "/"
];
if (!$this->validateCsrfInput()) { if (!$this->validateCsrfInput()) {
$this->getSession()->getFlashBag()->add( $this->getSession()->getFlashBag()->add(
'error', 'error',
@ -226,8 +295,39 @@ return new class extends Lewp\Module
])) ]))
); );
} }
$form = $this->createForm();
$this->appendChild($form); // send form again if captcha or csrf not valid, or form not submitted yet
if (
!$this->validateCaptcha()
|| !$this->validateCsrfInput()
|| !$this->formSubmitted()
) {
$form = $this->createForm();
$this->appendChild($form);
return true;
}
// send email
if (
!is_null($options[self::OPTIONS_MAIL_TO])
|| !is_null($options[self::OPTIONS_MAIL_FROM])
) {
$this->sendMail($_POST, $options);
}
// notify user about success
$this->getSession()->getFlashBag()->add(
'success',
$this->loadTextFile(Resolve::arrayToId([
$this->getLanguage(),
'flashmessages',
'success',
'thanks-for-message'
]))
);
header('Location: '.$options[self::OPTIONS_REDIRECT_URI]);
exit; // if not present, session gets not saved
return true; return true;
} }

View file

@ -0,0 +1 @@
Captcha

View file

@ -1 +1 @@
Email: Email

View file

@ -0,0 +1 @@
Vielen Dank für Ihre Nachricht! Wir werden uns schnellstmöglich bei Ihnen melden!

View file

@ -1 +1 @@
Deine Nachricht: Deine Nachricht

View file

@ -1 +1 @@
Name: Name

View file

@ -0,0 +1 @@
Captcha

View file

@ -1 +1 @@
Email: Email

View file

@ -0,0 +1 @@
Thanks for your message! We will respond as soon as possible!

View file

@ -1 +1 @@
Your message: Your message

View file

@ -1 +1 @@
Name: Name