Skip to content

Commit

Permalink
Merge pull request #16 from iMi-digital/15-wrong-recipient
Browse files Browse the repository at this point in the history
Fixes & all_fields_html
  • Loading branch information
amenk authored Sep 25, 2023
2 parents e89a883 + a140c2f commit ab330e7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 66 deletions.
16 changes: 8 additions & 8 deletions Block/Adminhtml/System/Config/Form/Field/Fields.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Contact
*
*
* @author Slava Yurthev
*/
namespace SY\Contact\Block\Adminhtml\System\Config\Form\Field;
Expand All @@ -19,24 +19,24 @@ private function getTypeRenderer(){
return $this->_typeRenderer
->addOption('text', 'Single Line Text')
->addOption('textarea', 'Multi Line Text')
->addOption('email', 'E-Mail')
->addOption('email', 'E-Mail***')
->addOption('checkbox', 'Checkbox')
->addOption('checkbox_list', 'Checkbox List')
->addOption('select', 'Drop Down');
}
protected function _prepareToRender(){
$this->addColumn('key', [
'label' => __('Key'),
'label' => __('Key'),
'style'=>'min-width:100px',
'class' => 'input-text required'
]);
$this->addColumn('label', [
'label' => __('Label'),
'label' => __('Label'),
'style'=>'min-width:100px',
'class' => 'input-text required'
]);
$this->addColumn('field_class', [
'label' => __('Field Class'),
'label' => __('Field Class'),
'style'=>'min-width:100px'
]);
$this->addColumn('default_value', [
Expand All @@ -52,7 +52,7 @@ protected function _prepareToRender(){
'style'=>'min-width:100px'
]);
$this->addColumn('field_type', [
'label' => __('Type'),
'label' => __('Type'),
'style'=>'min-width:100px',
'renderer' => $this->getTypeRenderer()
]);
Expand All @@ -65,7 +65,7 @@ protected function _prepareArrayRow(\Magento\Framework\DataObject $row){
$type = $row->getData('field_type');
$key = 'option_' . $this->getTypeRenderer()->calcOptionHash($type);
$options[$key] = 'selected="selected"';

$row->setData('option_extra_attrs', $options);
}
}
}
48 changes: 33 additions & 15 deletions Helper/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ public function receive(Request $request, $storeId = 0)
$this->request = $request;
$info = $this->request->getData('info');
$info = $this->_json->unserialize($info);

if (is_array($info) && count($info) > 0) {

$didSentEmail = false;
foreach ($info as $field) {
if (@$field['type'] == self::EMAIL_TYPE) {
$this->send($field['value'], $this->toVars($info), $storeId);
$didSentEmail = true;
}
}

if (!$didSentEmail) {
$this->send(null, $this->toVars($info), $storeId);

}
}
}

Expand All @@ -83,25 +92,29 @@ public function receive(Request $request, $storeId = 0)
*/
public function toVars(array $array)
{
$allFieldsHtml = '';
$vars = [];
if (is_array($array) && count($array) > 0) {
foreach ($array as $field) {
$vars[$field['key']] = $field['value'];
$value = is_array($field['value']) ? implode(', ', $field['value']) : (string) $field['value'];
$vars[$field['key']] = $value;
$allFieldsHtml .= sprintf('<strong>%s:</strong> %s<br>', htmlspecialchars($field['label']), htmlspecialchars($value)) . PHP_EOL;
}
}

$vars['_all_fields_html'] = $allFieldsHtml;
return $vars;
}

/**
* @param string $to
* @param ?string $to if this is null, send only to customer service (but use it as $to)
* @param array $vars
* @param int $storeId
*
* @throws LocalizedException
* @throws MailException
*/
public function send(string $to, array $vars, int $storeId = 0)
public function send(?string $to, array $vars, int $storeId = 0)
{
$this->inlineTranslate->suspend();
$this->transportBuilder->setTemplateIdentifier(
Expand All @@ -111,9 +124,14 @@ public function send(string $to, array $vars, int $storeId = 0)
'area' => Area::AREA_FRONTEND,
'store' => $storeId,
]);
$this->transportBuilder->addTo($to);
$this->transportBuilder->addBcc($this->getRecipientAddress());
$this->transportBuilder->setFromByScope($this->getFrom());

if ($to === null) {
$this->transportBuilder->addTo($this->getRecipientAddress($storeId));
} else {
$this->transportBuilder->addTo($to);
$this->transportBuilder->addBcc($this->getRecipientAddress($storeId));
}
$this->transportBuilder->setFromByScope($this->getFrom($storeId));
$this->transportBuilder->setTemplateVars($vars);
$this->transportBuilder->getTransport()->sendMessage();
$this->inlineTranslate->resume();
Expand All @@ -122,35 +140,35 @@ public function send(string $to, array $vars, int $storeId = 0)
/**
* @return mixed
*/
private function getRecipientAddress()
private function getRecipientAddress(int $storeId = 0)
{
return $this->getContactConfig('general/send_to') ?? $this->getConfig('trans_email/ident_sales/email');
return $this->getContactConfig('general/send_to', $storeId) ?? $this->getConfig('trans_email/ident_sales/email', $storeId);
}

/**
* @return array
*/
private function getFrom()
private function getFrom(int $storeId = 0)
{
return [
'name' => $this->getFromName(),
'email' => $this->getFromAddress(),
'name' => $this->getFromName($storeId),
'email' => $this->getFromAddress($storeId),
];
}

/**
* @return mixed
*/
private function getFromAddress()
private function getFromAddress(int $storeId = 0)
{
return $this->getContactConfig('general/send_from') ?? $this->getConfig('trans_email/ident_sales/email');
return $this->getContactConfig('general/send_from', $storeId) ?? $this->getConfig('trans_email/ident_sales/email', $storeId);
}

/**
* @return mixed
*/
private function getFromName()
private function getFromName(int $storeId = 0)
{
return $this->getContactConfig('general/send_from_name') ?? $this->getConfig('trans_email/ident_sales/name');
return $this->getContactConfig('general/send_from_name', $storeId) ?? $this->getConfig('trans_email/ident_sales/name', $storeId);
}
}
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@ Based on (https://github.com/SlavaYurthev/Contact-M2/)

`composer require imi/contact-m2`

## Usage

### Configuration

The plugin is configured at `Stores -> Configuration -> Custom Contact -> Contact Us`

### Recipient

Email type fields will receive the email. If you want to ask for another email, use the text type.


### Mail Template

You can use the following snipped to include all the non-empty fields:

```
{{var _all_fields_html|raw}}
```
3 changes: 2 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment><![CDATA[
* You can use directives in for the Default Value such as {{customer.firstname}} {{customerDefaultBilling.city}}<br>
** Enter a field name to show the current field only, if another one is filled.
** Enter a field name to show the current field only, if another one is filled.<br>
*** Email type fields will receive the email. If you want to ask for another email, use the text type.
]]></comment>
</field>
<field id="email_template" translate="label comment" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/email/sy_contact_general_email_template.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<!--@subject Contact Form @-->
<strong>Customer Message:</strong> {{var message}} <br/>
{{var _all_fields_html|raw}}
84 changes: 43 additions & 41 deletions view/frontend/templates/form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,67 @@
?>
<?php $fields = $block->getFields(); ?>
<?php if (count($fields) > 0): ?>
<form class="form contact" action="<?php echo $block->getUrl('customcontact/form/post'); ?>" id="contact-form"
method="post" data-mage-init='<?php echo $block->getJsFormConfig() ?>'>
<input type="hidden" name="form_key" value="<?php echo $block->getFormKey(); ?>">
<form class="form contact" action="<?= $block->getUrl('customcontact/form/post'); ?>" id="contact-form"
method="post" data-mage-init='<?= $block->getJsFormConfig() ?>'>
<input type="hidden" name="form_key" value="<?= $block->getFormKey(); ?>">
<fieldset class="fieldset">
<legend class="legend"><span><?php echo __('Write Us'); ?></span></legend>
<legend class="legend"><span><?= __('Write Us'); ?></span></legend>
<div class="field note no-label">
<?php echo __('Jot us a note and we’ll get back to you as quickly as possible.'); ?>
<?= __('Jot us a note and we’ll get back to you as quickly as possible.'); ?>
</div>
<?php foreach ($fields as $field): ?>
<div class="field <?php echo $field->getData('key'); ?> <?php echo $field->getData('field_class'); ?>">
<div class="field <?= $field->getData('key'); ?> <?= $field->getData('field_class'); ?>">
<label class="label"
for="<?php echo $field->getData('key'); ?>"><span><?php echo $field->getData('label'); ?></span></label>
for="<?= $field->getData('key'); ?>"><span><?= $field->getData('label'); ?></span></label>
<div class="control">
<?php if (in_array($field->getData('field_type'), ['text', 'email'])): ?>
<input name="<?php echo $field->getData('key'); ?>"
id="<?php echo $field->getData('key'); ?>"
title="<?php echo $field->getData('label'); ?>"
value="<?php echo $block->escapeHtml($field->getData('default_value')); ?>"
class="<?php echo $field->getData('field_class'); ?>"
type="<?php echo $field->getData('field_type'); ?>">
<input name="<?= $field->getData('key'); ?>"
id="<?= $field->getData('key'); ?>"
title="<?= $field->getData('label'); ?>"
value="<?= $block->escapeHtml($field->getData('default_value')); ?>"
class="<?= $field->getData('field_class'); ?>"
type="<?= $field->getData('field_type'); ?>">
<?php elseif ($field->getData('field_type') == 'textarea'): ?>
<textarea name="<?php echo $field->getData('key'); ?>"
id="<?php echo $field->getData('key'); ?>"
title="<?php echo $field->getData('label'); ?>"
class="<?php echo $field->getData('field_class'); ?>"
style="resize:none"><?php echo $block->escapeHtml($field->getData('default_value')); ?></textarea>
<textarea name="<?= $field->getData('key'); ?>"
id="<?= $field->getData('key'); ?>"
title="<?= $field->getData('label'); ?>"
class="<?= $field->getData('field_class'); ?>"
style="resize:none"><?= $block->escapeHtml($field->getData('default_value')); ?></textarea>
<?php elseif ($field->getData('field_type') == 'checkbox'): ?>
<input type="checkbox" name="<?php echo $field->getData('key'); ?>"
id="<?php echo $field->getData('key'); ?>"
title="<?php echo $field->getData('label'); ?>"
value="<?php echo $block->escapeHtml($field->getData('default_value')); ?>"
class="<?php echo $field->getData('field_class'); ?>"
type="<?php echo $field->getData('field_type'); ?>">
<label for="<?php echo $field->getData('key'); ?>">
<?php echo $block->escapeHtml($field->getData('default_value')); ?>
<input type="checkbox" name="<?= $field->getData('key'); ?>"
id="<?= $field->getData('key'); ?>"
title="<?= $field->getData('label'); ?>"
value="<?= $block->escapeHtml($field->getData('default_value')); ?>"
class="<?= $field->getData('field_class'); ?>"
type="<?= $field->getData('field_type'); ?>">
<label for="<?= $field->getData('key'); ?>">
<?= $block->escapeHtml($field->getData('default_value')); ?>
</label>
<?php elseif ($field->getData('field_type') == 'select'): ?>
<select name="<?php echo $field->getData('key'); ?>" id="<?php echo $field->getData('key'); ?>">
<select name="<?= $field->getData('key'); ?>" id="<?= $field->getData('key'); ?>">
<?php foreach(explode('|', $field->getData('options')) as $value): ?>
<option value="<?php echo $block->escapeHtml($value); ?>"
<option value="<?= $block->escapeHtml($value); ?>"
<?php if ($field->getData('default_value') == $value) echo " selected" ?>
>
<?php echo $block->escapeHtml($value); ?>
<?= $block->escapeHtml($value); ?>
</option>
<?php endforeach ?>
</select>
<?php elseif ($field->getData('field_type') == 'checkbox_list'): ?>
<?php foreach(explode('|', $field->getData('options')) as $value): ?>
<?php foreach (explode('|', $field->getData('options')) as $index => $value): ?>
<p>
<input type="checkbox" name="<?php echo $field->getData('key'); ?>[]"
id="<?php echo $field->getData('key'); ?>"
title="<?php echo $field->getData('label'); ?>"
value="<?php echo $block->escapeHtml($value); ?>"
class="<?php echo $field->getData('field_class'); ?>"
type="<?php echo $field->getData('field_type'); ?>"
<?php if ($field->getData('default_value') == $value) echo " selected" ?>
<input type="checkbox" name="<?= $field->getData('key'); ?>[]"
id="<?= $field->getData('key') . $index; ?>"
title="<?= $field->getData('label'); ?>"
value="<?= $block->escapeHtml($value); ?>"
class="<?= $field->getData('field_class'); ?>"
type="<?= $field->getData('field_type'); ?>"
<?php if ($field->getData('default_value') == $value) {
echo " selected";
} ?>
>
<label for="<?php echo $field->getData('key'); ?>">
<?php echo $block->escapeHtml($value); ?>
<label for="<?= $field->getData('key') . $index; ?>">
<?= $block->escapeHtml($value); ?>
</label>
</p>
<?php endforeach ?>
Expand All @@ -77,8 +79,8 @@
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="">
<button type="submit" title="<?php echo __('Submit'); ?>" class="action submit primary">
<span><?php echo __('Submit'); ?></span>
<button type="submit" title="<?= __('Submit'); ?>" class="action submit primary">
<span><?= __('Submit'); ?></span>
</button>
</div>
</div>
Expand Down

0 comments on commit ab330e7

Please sign in to comment.