Skip to content

Commit

Permalink
Merge pull request #5 from agorbulin/1.0.0-dev
Browse files Browse the repository at this point in the history
1.0.0 dev
  • Loading branch information
agorbulin authored Jan 20, 2019
2 parents 2c2b674 + 38bc1c9 commit df7e8c7
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Release 1.0.0 (2019-01-20)
10 changes: 1 addition & 9 deletions Controller/Adminhtml/Contact/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ public function execute()
$this->contactRepository->save($model);
$this->messageManager->addSuccessMessage(__('You saved the contact.'));
$this->dataPersistor->clear('goral_contactus');
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath(
'*/*/edit',
['entity_id' => $model->getId(), '_current' => true]
);
}

return $resultRedirect->setPath('*/*/');
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
Expand All @@ -93,7 +85,7 @@ public function execute()

return $resultRedirect->setPath(
'*/*/edit',
['entity_id' => $this->getRequest()->getParam('entity_id')]
['entity_id' => $id]
);
}

Expand Down
62 changes: 38 additions & 24 deletions Controller/Adminhtml/Contact/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,44 @@
namespace Goral\ContactUs\Controller\Adminhtml\Contact;

use Goral\ContactUs\Api\ContactRepositoryInterface as ContactRepository;
use Goral\ContactUs\Api\Data\ContactInterfaceFactory as ContactFactory;
use Goral\ContactUs\Api\Data\ContactInterface;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Backend\App\Action\Context;
use Goral\ContactUs\Model\ContactValidator;
use Goral\ContactUs\Model\Mail;
use Goral\ContactUs\Ui\Component\Listing\Column\Status;

/**
* Contact Save
*/
class Send extends AbstractAction
{
/**
* @var DataPersistorInterface
*/
private $dataPersistor;

/**
* @var ContactRepository
*/
private $contactRepository;

/**
* @var ContactFactory
*/
private $contactFactory;
private $contactValidator;

private $mail;

/**
* Save constructor.
* Send constructor.
*
* @param Context $context
* @param DataPersistorInterface $dataPersistor
* @param ContactRepository $contactRepository
* @param ContactFactory $contactFactory
* @param Context $context
* @param ContactRepository $contactRepository
* @param ContactValidator $contactValidator
* @param Mail $mail
*/
public function __construct(
Context $context,
DataPersistorInterface $dataPersistor,
ContactRepository $contactRepository,
ContactFactory $contactFactory
ContactValidator $contactValidator,
Mail $mail
) {
$this->dataPersistor = $dataPersistor;
$this->contactRepository = $contactRepository;
$this->contactFactory = $contactFactory;
$this->contactValidator = $contactValidator;
$this->mail = $mail;
parent::__construct($context);
}

Expand All @@ -59,12 +55,30 @@ public function execute()
$resultRedirect = $this->resultRedirectFactory->create();
$id = $this->getRequest()->getParam('entity_id');

if ($id) {
/** @todo Send email */
$this->messageManager->addSuccessMessage(__('You sent answer for contact ' . $id));
if (!$id) {
return $resultRedirect->setPath('*/*/');
}

try {
/** @var ContactInterface $contact */
$contact = $this->contactRepository->getById($id);
if ($this->contactValidator->isValid($contact)) {
$this->mail->send($contact);
if ($contact->getStatus() != Status::STATUS_PROCESSED) {
$contact->setStatus(Status::STATUS_PROCESSED);
$this->contactRepository->save($contact);
}
$this->messageManager->addSuccessMessage(__('You sent the answer email.'));
} else {
foreach ($this->contactValidator->getMessages() as $message) {
throw new \Exception($message);
}
}
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(__('Email was not sent. ' . $e->getMessage()));
}

return $resultRedirect->setPath('*/*/');
return $resultRedirect->setPath('*/*/edit', ['entity_id' => $id]);
}

}
38 changes: 38 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Goral\ContactUs\Helper;

use Magento\Store\Model\ScopeInterface;

/**
* Class Helper Data
*
* @package Goral\ContactUs\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_ENABLED = 'contactus/general/enabled';
const XML_PATH_TEMPLATE = 'contactus/general/template';

/**
* @param string $scope
* @param null $scopeCode
*
* @return mixed
*/
public function isEnabled($scope = ScopeInterface::SCOPE_STORE, $scopeCode = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_ENABLED, $scope, $scopeCode);
}

/**
* @param string $scope
* @param null $scopeCode
*
* @return mixed
*/
public function getTemplate($scope = ScopeInterface::SCOPE_STORE, $scopeCode = null)
{
return $this->scopeConfig->getValue(self::XML_PATH_TEMPLATE, $scope, $scopeCode);
}
}
16 changes: 16 additions & 0 deletions Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,20 @@ public function setExtensionAttributes(ContactExtensionInterface $extensionAttri
return $this->_setExtensionAttributes($extensionAttributes);
}

/**
* @param array|string $key
* @param null|string|int $value
*
* @return $this|\Magento\Framework\Model\AbstractExtensibleModel
*/
public function setData($key, $value = null)
{
parent::setData($key, $value);
if (is_array($key) && !empty($key['store_id']) && is_array($key['store_id'])) {
$this->setStoreId(reset($key['store_id']));
}

return $this;
}

}
47 changes: 47 additions & 0 deletions Model/ContactValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Goral\ContactUs\Model;

use Magento\Framework\Validator\AbstractValidator;
use Goral\ContactUs\Api\Data\ContactInterface;

/**
* Contact Validator
*/
class ContactValidator extends AbstractValidator
{

/**
* Validate contact data
*
* @param ContactInterface $contact
*
* @return bool
* @throws \Zend_Validate_Exception
*/
public function isValid($contact)
{
$errors = [];

if (!\Zend_Validate::is($contact->getName(), 'NotEmpty')) {
$errors['name'] = __('Name can\'t be empty.');
}

if (!\Zend_Validate::is($contact->getEmail(), 'NotEmpty')) {
$errors['email'] = __('Email can\'t be empty.');
}

if (!\Zend_Validate::is($contact->getComment(), 'NotEmpty')) {
$errors['comment'] = __('Comment can\'t be empty.');
}

if (!\Zend_Validate::is($contact->getAnswer(), 'NotEmpty')) {
$errors['answer'] = __('Answer can\'t be empty.');
}

$this->_addMessages($errors);

return empty($errors);
}

}
99 changes: 99 additions & 0 deletions Model/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Goral\ContactUs\Model;

use Goral\ContactUs\Api\Data\ContactInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Area;
use Goral\ContactUs\Helper\Data;
use Magento\Contact\Model\ConfigInterface;
use Magento\Framework\DataObject;

/**
* Class Mail
*
* @package Goral\ContactUs\Model
*/
class Mail
{
/**
* @var ConfigInterface
*/
private $contactsConfig;

/**
* @var TransportBuilder
*/
private $transportBuilder;

/**
* @var StateInterface
*/
private $inlineTranslation;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var Data
*/
private $helper;

/**
* Mail constructor.
*
* @param ConfigInterface $contactsConfig
* @param TransportBuilder $transportBuilder
* @param StateInterface $inlineTranslation
* @param StoreManagerInterface $storeManager
* @param Data $helper
*/
public function __construct(
ConfigInterface $contactsConfig,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
StoreManagerInterface $storeManager,
Data $helper
) {
$this->contactsConfig = $contactsConfig;
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->storeManager = $storeManager;
$this->helper = $helper;
}

/**
* Send email to contact
*
* @param ContactInterface $contact
*
* @throws \Magento\Framework\Exception\MailException
*/
public function send($contact)
{
$this->inlineTranslation->suspend();
try {
$transport = $this->transportBuilder
->setTemplateIdentifier($this->helper->getTemplate())
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $contact->getStoreId()
]
)
->setTemplateVars(['data' => new DataObject($contact->getData())])
->setFrom($this->contactsConfig->emailSender())
->addTo($contact->getEmail())
->setReplyTo($this->contactsConfig->emailRecipient(), $this->contactsConfig->emailSender())
->getTransport();

$transport->sendMessage();
} finally {
$this->inlineTranslation->resume();
}
}
}
12 changes: 12 additions & 0 deletions Plugin/MagentoContactPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Log\LoggerInterface;
use Magento\Contact\Controller\Index\Post;
use Magento\Framework\Controller\Result\Redirect;
use Goral\ContactUs\Helper\Data;

/**
* Class MagentoContactPost
Expand All @@ -32,6 +33,11 @@ class MagentoContactPost
*/
private $storeManager;

/**
* @var Data
*/
private $helper;

/**
* @var LoggerInterface
*/
Expand All @@ -43,18 +49,21 @@ class MagentoContactPost
* @param ContactRepositoryInterface $contactRepository
* @param ContactInterfaceFactory $contactFactory
* @param StoreManagerInterface $storeManager
* @param Data $helper
* @param LoggerInterface $logger
*/
public function __construct(
ContactRepositoryInterface $contactRepository,
ContactInterfaceFactory $contactFactory,
StoreManagerInterface $storeManager,
Data $helper,
LoggerInterface $logger

) {
$this->contactRepository = $contactRepository;
$this->contactFactory = $contactFactory;
$this->storeManager = $storeManager;
$this->helper = $helper;
$this->logger = $logger;
}

Expand All @@ -66,6 +75,9 @@ public function __construct(
*/
public function afterExecute(Post $subject, $result)
{
if (!$this->helper->isEnabled()) {
return $result;
}
$post = $subject->getRequest()->getParams();
try {
$this->createContact($post);
Expand Down
Loading

0 comments on commit df7e8c7

Please sign in to comment.