init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\ProductInquiry\Controller\Adminhtml\Inquiry;
|
||||
|
||||
use Esun\ProductInquiry\Model\InquiryFactory;
|
||||
use Esun\ProductInquiry\Model\ResourceModel\Inquiry as InquiryResource;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
|
||||
class Delete extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_ProductInquiry::delete';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly InquiryFactory $inquiryFactory,
|
||||
private readonly InquiryResource $inquiryResource
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$inquiry = $this->inquiryFactory->create();
|
||||
$this->inquiryResource->load($inquiry, (int)$this->getRequest()->getParam('id'));
|
||||
if ($inquiry->getId()) {
|
||||
try {
|
||||
$this->inquiryResource->delete($inquiry);
|
||||
$this->messageManager->addSuccessMessage(__('The inquiry has been deleted.'));
|
||||
} catch (\Throwable $exception) {
|
||||
$this->messageManager->addExceptionMessage($exception, __('Unable to delete the inquiry.'));
|
||||
}
|
||||
}
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\ProductInquiry\Controller\Adminhtml\Inquiry;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class Index extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_ProductInquiry::inquiries';
|
||||
|
||||
public function __construct(Context $context, private readonly PageFactory $pageFactory)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Page
|
||||
{
|
||||
$page = $this->pageFactory->create();
|
||||
$page->setActiveMenu('Esun_ProductInquiry::inquiries');
|
||||
$page->getConfig()->getTitle()->prepend(__('Product Inquiries'));
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\ProductInquiry\Controller\Adminhtml\Inquiry;
|
||||
|
||||
use Esun\ProductInquiry\Model\Inquiry;
|
||||
use Esun\ProductInquiry\Model\InquiryFactory;
|
||||
use Esun\ProductInquiry\Model\ResourceModel\Inquiry as InquiryResource;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
|
||||
class SaveStatus extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_ProductInquiry::manage';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly InquiryFactory $inquiryFactory,
|
||||
private readonly InquiryResource $inquiryResource
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$id = (int)$this->getRequest()->getParam('id');
|
||||
$status = (string)$this->getRequest()->getParam('status');
|
||||
$inquiry = $this->inquiryFactory->create();
|
||||
$this->inquiryResource->load($inquiry, $id);
|
||||
if (!$inquiry->getId() || !array_key_exists($status, Inquiry::getStatuses())) {
|
||||
$this->messageManager->addErrorMessage(__('Unable to update the inquiry status.'));
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/index');
|
||||
}
|
||||
try {
|
||||
$inquiry->setData('status', $status);
|
||||
$this->inquiryResource->save($inquiry);
|
||||
$this->messageManager->addSuccessMessage(__('The inquiry status has been updated.'));
|
||||
} catch (\Throwable $exception) {
|
||||
$this->messageManager->addExceptionMessage($exception, __('Unable to update the inquiry status.'));
|
||||
}
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/view', ['id' => $id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\ProductInquiry\Controller\Adminhtml\Inquiry;
|
||||
|
||||
use Esun\ProductInquiry\Model\InquiryFactory;
|
||||
use Esun\ProductInquiry\Model\ResourceModel\Inquiry as InquiryResource;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class View extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_ProductInquiry::inquiries';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly PageFactory $pageFactory,
|
||||
private readonly InquiryFactory $inquiryFactory,
|
||||
private readonly InquiryResource $inquiryResource,
|
||||
private readonly Registry $registry
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$inquiry = $this->inquiryFactory->create();
|
||||
$this->inquiryResource->load($inquiry, (int)$this->getRequest()->getParam('id'));
|
||||
if (!$inquiry->getId()) {
|
||||
$this->messageManager->addErrorMessage(__('The inquiry no longer exists.'));
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/index');
|
||||
}
|
||||
$this->registry->register('current_product_inquiry', $inquiry);
|
||||
$page = $this->pageFactory->create();
|
||||
$page->setActiveMenu('Esun_ProductInquiry::inquiries');
|
||||
$page->getConfig()->getTitle()->prepend(__('Product Inquiry #%1', $inquiry->getId()));
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\ProductInquiry\Controller\Index;
|
||||
|
||||
use Esun\ProductInquiry\Model\SubmissionService;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Post implements HttpPostActionInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RequestInterface $request,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly FormKeyValidator $formKeyValidator,
|
||||
private readonly SubmissionService $submissionService,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
if (!$this->formKeyValidator->validate($this->request)) {
|
||||
return $this->error($result, __('Your session has expired. Refresh the page and try again.'), 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$submission = $this->submissionService->submit($this->request);
|
||||
} catch (LocalizedException $exception) {
|
||||
return $this->error($result, $exception->getMessage(), 422);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->logger->critical($exception);
|
||||
return $this->error($result, __('Unable to submit your inquiry. Please try again later.'), 500);
|
||||
}
|
||||
|
||||
return $result->setData([
|
||||
'success' => true,
|
||||
'duplicate' => (bool)$submission['duplicate'],
|
||||
'message' => (string)($submission['duplicate']
|
||||
? __('This inquiry has already been submitted. We will contact you as soon as possible.')
|
||||
: __('Thank you. Your inquiry has been submitted.')),
|
||||
]);
|
||||
}
|
||||
|
||||
private function error(Json $result, \Stringable|string $message, int $statusCode): Json
|
||||
{
|
||||
return $result->setHttpResponseCode($statusCode)->setData([
|
||||
'success' => false,
|
||||
'message' => (string)$message,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user