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

Formbuilder multiple emails #2961

Open
wants to merge 3 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 3 additions & 40 deletions src/Backend/Modules/FormBuilder/Actions/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,7 @@ private function loadForm(): void
{
$this->form = new BackendForm('add');
$this->form->addText('name')->makeRequired();
$this->form->addDropdown(
'method',
[
'database' => BL::getLabel('MethodDatabase'),
'database_email' => BL::getLabel('MethodDatabaseEmail'),
'email' => BL::getLabel('MethodEmail'),
],
'database_email'
)->makeRequired();
$this->form->addText('email');
$this->form->addText('email_subject');
$this->form->addCheckbox('database');
$this->form->addText('identifier', BackendFormBuilderModel::createIdentifier());
$this->form->addEditor('success_message')->makeRequired();

Expand All @@ -63,35 +53,13 @@ private function validateForm(): void

// shorten the fields
$txtName = $this->form->getField('name');
$txtEmail = $this->form->getField('email');
$txtEmailSubject = $this->form->getField('email_subject');
$ddmMethod = $this->form->getField('method');
$chkDatabase = $this->form->getField('database');
$txtSuccessMessage = $this->form->getField('success_message');
$txtIdentifier = $this->form->getField('identifier');

$emailAddresses = (array) explode(',', $txtEmail->getValue());

// validate fields
$txtName->isFilled(BL::getError('NameIsRequired'));
$txtSuccessMessage->isFilled(BL::getError('SuccessMessageIsRequired'));
if ($ddmMethod->isFilled(BL::getError('NameIsRequired')) && $ddmMethod->getValue() == 'database_email') {
$error = false;

// check the addresses
foreach ($emailAddresses as $address) {
$address = trim($address);

if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
$error = true;
break;
}
}

// add error
if ($error) {
$txtEmail->addError(BL::getError('EmailIsInvalid'));
}
}

// identifier
if ($txtIdentifier->isFilled()) {
Expand All @@ -110,12 +78,7 @@ private function validateForm(): void
$values['language'] = BL::getWorkingLanguage();
$values['user_id'] = BackendAuthentication::getUser()->getUserId();
$values['name'] = $txtName->getValue();
$values['method'] = $ddmMethod->getValue();
$values['email'] = ($ddmMethod->getValue() === 'database_email' || $ddmMethod->getValue() === 'email')
? serialize($emailAddresses) : null;
$values['email_subject'] = empty($txtEmailSubject->getValue()) ? null : $txtEmailSubject->getValue();
$values['email_template'] = count($this->templates) > 1
? $this->form->getField('template')->getValue() : $this->templates[0];
$values['database'] = (int) $chkDatabase->isChecked();
$values['success_message'] = $txtSuccessMessage->getValue(true);
$values['identifier'] = ($txtIdentifier->isFilled() ?
$txtIdentifier->getValue() :
Expand Down
185 changes: 185 additions & 0 deletions src/Backend/Modules/FormBuilder/Actions/AddEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace Backend\Modules\FormBuilder\Actions;

use Backend\Core\Engine\Base\ActionAdd as BackendBaseActionAdd;
use Backend\Core\Engine\Authentication as BackendAuthentication;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Engine\Form as BackendForm;
use Backend\Core\Language\Language as BL;
use Backend\Modules\FormBuilder\Engine\Model as BackendFormBuilderModel;

/**
* This is the add-action, it will display a form to create a new item.
*/
class AddEmail extends BackendBaseActionAdd
{
/**
* The form id
*
* @var int
*/
private $id;

/**
* The form data
*
* @var array
*/
private $record = [];

/**
* The available templates
*
* @var array
*/
private $templates = [];

public function execute(): void
{
$this->id = $this->getRequest()->query->getInt('id');

// does the item exist
if ($this->id !== 0 && BackendFormBuilderModel::exists($this->id)) {
parent::execute();
$this->getData();
$this->templates = BackendFormBuilderModel::getTemplates();
$this->loadForm();
$this->validateForm();
$this->parse();
$this->display();
} else {
// no item found, throw an exceptions, because somebody is fucking with our url
$this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing');
}
}

private function getData(): void
{
$this->record = BackendFormBuilderModel::get($this->id);
}

private function loadForm(): void
{
// set hidden values
$rbtRecipientOptions = [
['label' => BL::lbl('Email'), 'value' => 'email'],
['label' => BL::lbl('ValueOfAField'), 'value' => 'field'],
];

// set field values
$ddnFormFieldsOptions = BackendFormBuilderModel::getRecipientFieldsForDropdown($this->id);

$this->form = new BackendForm('add');
$mailerFrom = $this->get('fork.settings')->get('Core', 'mailer_from');
$this->form->addText('from_name', (isset($mailerFrom['name'])) ? $mailerFrom['name'] : '');
$this->form
->addText('from_email', (isset($mailerFrom['email'])) ? $mailerFrom['email'] : '')
->setAttribute('type', 'email')
;
$this->form->addRadiobutton('recipient', $rbtRecipientOptions, 'email');
$this->form->addText('email_addresses');
$this->form->addDropdown('email_fields', $ddnFormFieldsOptions)->setDefaultElement('');
$this->form->addCheckbox('email_data', 0);
$this->form->addText('email_subject');
$this->form->addEditor('email_body');

// if we have multiple templates, add a dropdown to select them
if (count($this->templates) > 1) {
$this->form->addDropdown('template', array_combine($this->templates, $this->templates));
}
}

private function validateForm(): void
{
if ($this->form->isSubmitted()) {
$this->form->cleanupFields();

// shorten the fields
$chkEmailData = $this->form->getField('email_data');
$txtEmailSubject = $this->form->getField('email_subject');
$rbtRecipient = $this->form->getField('recipient');
$txtEmailToAddresses = $this->form->getField('email_addresses');
$ddnEmailToField = $this->form->getField('email_fields');
$txtFromName = $this->form->getField('from_name');
$txtFromEmail = $this->form->getField('from_email');
$txtEmailBody = $this->form->getField('email_body');

$emailAddresses = (array) explode(',', $txtEmailToAddresses->getValue());

// validate fields
$txtEmailSubject->isFilled(BL::err('FieldIsRequired'));
$rbtRecipient->isFilled(BL::err('FieldIsRequired'));
$txtFromName->isFilled(BL::err('FieldIsRequired'));
$txtFromEmail->isFilled(BL::err('FieldIsRequired'));
$txtEmailBody->isFilled(BL::err('FieldIsRequired'));

if ($rbtRecipient->getValue() == 'email') {
$txtEmailToAddresses->isFilled(BL::err('FieldIsRequireed'));

$error = false;

// check the addresses
foreach ($emailAddresses as $address) {
$address = trim($address);

if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
$error = true;
break;
}
}

// add error
if ($error) {
$txtEmailToAddresses->addError(BL::getError('EmailIsInvalid'));
}
}
if ($rbtRecipient->getValue() == 'field') {
$ddnEmailToField->isFilled(BL::err('FieldIsRequireed'));
}

// save
if ($this->form->isCorrect()) {
// build array
$values = [];
$values['language'] = BL::getWorkingLanguage();
$values['form_id'] = $this->id;
$values['email_data'] = (int) $chkEmailData->isChecked();
$values['email_subject'] = $txtEmailSubject->getValue();
$values['email_recipient'] = $rbtRecipient->getValue();
$values['email_to_field'] = $ddnEmailToField->getValue();
$values['email_to_addresses'] = serialize($emailAddresses);
$values['email_from'] = serialize(
[
'name' => $txtFromName->getValue(),
'email' => $txtFromEmail->getValue(),
]
);
$values['email_body'] = $txtEmailBody->getValue();
$values['email_template'] = count($this->templates) > 1
? $this->form->getField('template')->getValue() : $this->templates[0];
$values['created_on'] = BackendModel::getUTCDate();
$values['edited_on'] = BackendModel::getUTCDate();

// insert the item
BackendFormBuilderModel::insertEmail($values);

// redirect
$this->redirect(
BackendModel::createUrlForAction('Emails') . '&id=' . $this->id .
'&report=added-email'
);
}
}
}

protected function parse(): void
{
parent::parse();

$this->template->assign('item', $this->record);

// add form name to the breadcrumb
$this->header->appendDetailToBreadcrumbs($this->record['name']);
}
}
4 changes: 0 additions & 4 deletions src/Backend/Modules/FormBuilder/Actions/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public function execute(): void
private function getData(): void
{
$this->record = BackendFormBuilderModel::get($this->id);

if ($this->record['method'] === 'email') {
$this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing');
}
}

private function loadDataGrid(): void
Expand Down
57 changes: 57 additions & 0 deletions src/Backend/Modules/FormBuilder/Actions/DeleteEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Backend\Modules\FormBuilder\Actions;

use Backend\Core\Engine\Base\ActionDelete as BackendBaseActionDelete;
use Backend\Core\Engine\Model as BackendModel;
use Backend\Form\Type\DeleteType;
use Backend\Modules\FormBuilder\Engine\Model as BackendFormBuilderModel;

/**
* This action will delete a category
*/
class DeleteEmail extends BackendBaseActionDelete
{
public function execute(): void
{
$deleteForm = $this->createForm(
DeleteType::class,
null,
['module' => $this->getModule(), 'action' => 'DeleteEmail']
);
$deleteForm->handleRequest($this->getRequest());
if (!$deleteForm->isSubmitted() || !$deleteForm->isValid()) {
$this->redirect(BackendModel::createUrlForAction(
'Emails',
null,
null,
['error' => 'something-went-wrong']
));

return;
}
$deleteFormData = $deleteForm->getData();

$this->id = (int) $deleteFormData['id'];

// does the item exist
if ($this->id === 0 || !BackendFormBuilderModel::existsEmail($this->id)) {
$this->redirect(BackendModel::createUrlForAction('Emails', null, null, ['error' => 'non-existing']));

return;
}

$this->record = (array) BackendFormBuilderModel::getEmail($this->id);

parent::execute();

BackendFormBuilderModel::deleteEmail($this->id);

$this->redirect(BackendModel::createUrlForAction(
'Emails',
null,
null,
['report' => 'deleted-email', 'id' => $this->record['form_id']]
));
}
}