Skip to content

Commit

Permalink
#14 Still send to the customer service if there is no mail field
Browse files Browse the repository at this point in the history
  • Loading branch information
amenk committed Sep 21, 2023
1 parent b22fb1d commit a140c2f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 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);
}
}
}
22 changes: 18 additions & 4 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 @@ -98,14 +107,14 @@ public function toVars(array $array)
}

/**
* @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 @@ -115,8 +124,13 @@ 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($storeId));

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();
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ Based on (https://github.com/SlavaYurthev/Contact-M2/)

`composer require imi/contact-m2`

## Mail Template
## 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

0 comments on commit a140c2f

Please sign in to comment.