Added wrapper for label and input, Added options for redirecting and email customization, Added basic resources.
This commit is contained in:
parent
0222265abc
commit
29d903d070
11 changed files with 126 additions and 22 deletions
132
module.php
132
module.php
|
|
@ -9,8 +9,14 @@ return new class extends Lewp\Module
|
|||
|
||||
const KEY_CSRF_TOKEN = 'csrf_token';
|
||||
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',
|
||||
|
|
@ -20,6 +26,7 @@ return new class extends Lewp\Module
|
|||
'action' => '',
|
||||
]
|
||||
);
|
||||
|
||||
// LABEL NAME
|
||||
$label_name = $this->createAndSetupElement(
|
||||
'label',
|
||||
|
|
@ -31,7 +38,6 @@ return new class extends Lewp\Module
|
|||
'for' => 'name',
|
||||
]
|
||||
);
|
||||
$form->appendChild($label_name);
|
||||
// NAME INPUT
|
||||
$name = $this->createAndSetupElement(
|
||||
'input',
|
||||
|
|
@ -40,10 +46,11 @@ return new class extends Lewp\Module
|
|||
'id' => 'name',
|
||||
'name' => 'name',
|
||||
'type' => 'text',
|
||||
'value' => $_POST['name'] ?? '',
|
||||
'value' => $_POST[self::KEY_POST_NAME] ?? '',
|
||||
'required' => 'required',
|
||||
'placeholder' => ' ',
|
||||
]
|
||||
);
|
||||
$form->appendChild($name);
|
||||
// LABEL Email
|
||||
$label_email = $this->createAndSetupElement(
|
||||
'label',
|
||||
|
|
@ -55,7 +62,6 @@ return new class extends Lewp\Module
|
|||
'for' => 'email'
|
||||
]
|
||||
);
|
||||
$form->appendChild($label_email);
|
||||
// EMAIL INPUT
|
||||
$email = $this->createAndSetupElement(
|
||||
'input',
|
||||
|
|
@ -64,10 +70,11 @@ return new class extends Lewp\Module
|
|||
'id' => 'email',
|
||||
'name' => 'email',
|
||||
'type' => 'email',
|
||||
'value' => $_POST['email'] ?? '',
|
||||
'value' => $_POST[self::KEY_POST_EMAIL] ?? '',
|
||||
'required' => 'required',
|
||||
'placeholder' => ' ',
|
||||
]
|
||||
);
|
||||
$form->appendChild($email);
|
||||
// LABEL Message
|
||||
$label_message = $this->createAndSetupElement(
|
||||
'label',
|
||||
|
|
@ -79,27 +86,28 @@ return new class extends Lewp\Module
|
|||
'for' => 'message'
|
||||
]
|
||||
);
|
||||
$form->appendChild($label_message);
|
||||
// MESSAGE INPUT
|
||||
$message = $this->createAndSetupElement(
|
||||
'textarea',
|
||||
$_POST['message'] ?? '',
|
||||
$_POST[self::KEY_POST_MESSAGE] ?? '',
|
||||
[
|
||||
'id' => 'message',
|
||||
'name' => 'message',
|
||||
'required' => 'required',
|
||||
'placeholder' => ' ',
|
||||
]
|
||||
);
|
||||
$form->appendChild($message);
|
||||
// LABEL Captcha
|
||||
$label_captcha = $this->createAndSetupElement(
|
||||
'label',
|
||||
'',
|
||||
$this->loadTextFile(Resolve::arrayToId([
|
||||
$this->getLanguage(),
|
||||
'captcha'
|
||||
])),
|
||||
[
|
||||
'for' => self::KEY_CAPTCHA_PHRASE
|
||||
]
|
||||
);
|
||||
$label_captcha->appendChild($this->generateCaptcha());
|
||||
$form->appendChild($label_captcha);
|
||||
// CAPTCHA INPUT
|
||||
$captcha = $this->createAndSetupElement(
|
||||
'input',
|
||||
|
|
@ -108,9 +116,10 @@ return new class extends Lewp\Module
|
|||
'id' => 'captchainput',
|
||||
'name' => self::KEY_CAPTCHA_PHRASE,
|
||||
'type' => 'text',
|
||||
'required' => 'required',
|
||||
'placeholder' => ' ',
|
||||
]
|
||||
);
|
||||
$form->appendChild($captcha);
|
||||
// SUBMIT BUTTON
|
||||
$submit = $this->createAndSetupElement(
|
||||
'button',
|
||||
|
|
@ -123,6 +132,48 @@ return new class extends Lewp\Module
|
|||
'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);
|
||||
// ADD CSRF CHECK
|
||||
$form->appendChild($this->generateCsrfInput());
|
||||
|
|
@ -202,8 +253,26 @@ return new class extends Lewp\Module
|
|||
) ? 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
|
||||
{
|
||||
|
||||
// set default options
|
||||
$options += [
|
||||
self::OPTIONS_REDIRECT_URI => "/"
|
||||
];
|
||||
|
||||
if (!$this->validateCsrfInput()) {
|
||||
$this->getSession()->getFlashBag()->add(
|
||||
'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;
|
||||
}
|
||||
|
|
|
|||
1
resources/text/de/captcha.txt
Normal file
1
resources/text/de/captcha.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Captcha
|
||||
|
|
@ -1 +1 @@
|
|||
Email:
|
||||
Email
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Vielen Dank für Ihre Nachricht! Wir werden uns schnellstmöglich bei Ihnen melden!
|
||||
|
|
@ -1 +1 @@
|
|||
Deine Nachricht:
|
||||
Deine Nachricht
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Name:
|
||||
Name
|
||||
|
|
|
|||
1
resources/text/en/captcha.txt
Normal file
1
resources/text/en/captcha.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
Captcha
|
||||
|
|
@ -1 +1 @@
|
|||
Email:
|
||||
Email
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Thanks for your message! We will respond as soon as possible!
|
||||
|
|
@ -1 +1 @@
|
|||
Your message:
|
||||
Your message
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Name:
|
||||
Name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue