Validate form submissions and action configuration, execute finishers with submission context, and return JSON success or error responses. Add configurable confirmation emails to submitters and report failed submissions in the form UI. Harden database and admin email finishers, and add submission handler and confirmation email checks.
42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Plugi\Extensions\Form\Finishers;
|
|
use Plugi\Extensions;
|
|
|
|
class SendEmailToAdmin extends DefaultFinisher
|
|
{
|
|
public function execute(): bool
|
|
{
|
|
$settings = Extensions::getSettings('form');
|
|
$to = $settings['email']['value'] ?? null;
|
|
if (!is_string($to) || filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
|
|
return false;
|
|
}
|
|
|
|
$subject = 'Form submission: '.$this->getFormName();
|
|
$message = $this->getNiceFields();
|
|
return mail(
|
|
$to,
|
|
$subject,
|
|
$message,
|
|
$additional_headers = [],
|
|
$additional_params = ""
|
|
);
|
|
}
|
|
private function getNiceFields(): string {
|
|
$text = '';
|
|
foreach ($this->getFormFields() as $field) {
|
|
$name = $field['name'] ?? '';
|
|
$label = $field['label'] ?? $name;
|
|
$value = $this->getEntryFields()[$name] ?? '';
|
|
if (is_array($value)) {
|
|
$value = implode(', ', array_map('strval', $value));
|
|
} elseif (!is_scalar($value)) {
|
|
$value = '';
|
|
}
|
|
$text .= $label . ': ' . $value . "\n";
|
|
}
|
|
return $text;
|
|
}
|
|
}
|