Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner forms submit logic #1399

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/Forms/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

if ($active) {
$this->helper('admin')->favicon = 'forms:icon.svg';
}
}

/**
* listen to app search to filter forms
Expand Down Expand Up @@ -78,6 +78,7 @@
'forms.save.before.{$name}',
'forms.submit.after',
'forms.submit.before',
'forms.submit.email',
] as &$evt) { $triggers[] = $evt; }
});

Expand Down
45 changes: 25 additions & 20 deletions modules/Forms/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,27 @@

$frm = $this->form($form);

// Invalid form name
if (!$frm) {
return false;
}

// custom form validation
// Load custom form validator
if ($this->app->path("#config:forms/{$form}.php") && false===include($this->app->path("#config:forms/{$form}.php"))) {
return false;
}

// Filter submitted data
$this->app->trigger('forms.submit.before', [$form, &$data, $frm, &$options]);

// Send email
if (isset($frm['email_forward']) && $frm['email_forward']) {

$emails = array_map('trim', explode(',', $frm['email_forward']));
$filtered_emails = [];

foreach ($emails as $to){

// Validate each email address individually, push if valid
// Validate each email address individually, push if valid
foreach ($emails as $to) {
if ($this->app->helper('utils')->isEmail($to)){
$filtered_emails[] = $to;
}
Expand All @@ -283,42 +285,45 @@

$frm['email_forward'] = implode(',', $filtered_emails);

// There is an email template available
// Load custom email template
if ($template = $this->app->path("#config:forms/emails/{$form}.php")) {
$body = $this->app->view($template, ['data' => $data, 'frm' => $frm]);
}

$body = $this->app->renderer->file($template, ['data' => $data, 'frm' => $frm], false);

// Prepare template manually
} else {

$body = [];

foreach ($data as $key => $value) {
$body[] = "<b>{$key}:</b>\n<br>";
$body[] = (is_string($value) ? $value:json_encode($value))."\n<br>";
}
// Filter email content
$this->app->trigger('forms.submit.email', [$form, &$data, $frm, &$body, &$options]);

$body = implode("\n<br>", $body);
// Fallback to default email template
if (empty($body)) {
$body = $this->app->view("forms:views/api/email.php", ['data' => $data, 'frm' => $frm]);
}

$formname = isset($frm['label']) && trim($frm['label']) ? $frm['label'] : $form;
$to = $frm['email_forward'];
$subject = $options['subject'] ?? $this->app->helper('i18n')->getstr("New form data for: %s", [$formname]);

// success = true
try {
$response = $this->app->mailer->mail($frm['email_forward'], $options['subject'] ?? "New form data for: {$formname}", $body, $options);
$response = $this->app->mailer->mail($to, $subject, $body, $options);
} catch (\Exception $e) {
$response = $e->getMessage();
}
}
}

// Push entry to database
if (isset($frm['save_entry']) && $frm['save_entry']) {
$entry = ['data' => $data];
$this->save($form, $entry);
}

$this->app->trigger('forms.submit.after', [$form, &$data, $frm]);
// Generate response array
$response = (isset($response) && $response !== true) ? ['error' => $response, 'data' => $data] : $data;

// Filter submission response
$this->app->trigger('forms.submit.after', [$form, &$data, $frm, &$response]);

return (isset($response) && $response !== true) ? ['error' => $response, 'data' => $data] : $data;
return $response;
}
]);

Expand Down
8 changes: 8 additions & 0 deletions modules/Forms/views/api/email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
@foreach($data as $key => $value)
<p><strong>{{ $key }}:</strong> {{ (is_string($value) ? $value : json_encode($value)) }}</p>
@endforeach
</body>
</html>