From ba3456456d206d1993088c27cc71e5d8e4b91e00 Mon Sep 17 00:00:00 2001 From: Daniel Galla Date: Wed, 23 Oct 2024 12:34:27 +0200 Subject: [PATCH 1/2] Require magento module instead of php --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5a6618b..e2135e0 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "OSL-3.0" ], "require": { - "php": "^7.2|~8.0.0|~8.1.0" + "magento/module-contact": "^100.4.5" }, "type": "magento2-module", "authors": [ From fdc765ffff12e1c3aeb823a1c8c2dc23f0cdd732 Mon Sep 17 00:00:00 2001 From: Daniel Galla Date: Mon, 28 Oct 2024 12:56:38 +0100 Subject: [PATCH 2/2] Fix dynamic property declaration and some cleanup --- Block/ContactForm.php | 142 ++++++++++---------- Controller/Form/Post.php | 42 ++---- Helper/Email.php | 9 +- Ui/Component/Listing/Column/PostActions.php | 25 +--- 4 files changed, 92 insertions(+), 126 deletions(-) diff --git a/Block/ContactForm.php b/Block/ContactForm.php index 72d46ae..52ee898 100644 --- a/Block/ContactForm.php +++ b/Block/ContactForm.php @@ -6,91 +6,51 @@ */ namespace SY\Contact\Block; -class ContactForm extends \Magento\Contact\Block\ContactForm { - private $_fields; +use Magento\Contact\Block\ContactForm as BaseContactForm; +use Magento\Customer\Model\Session; +use Magento\Email\Model\Template\FilterFactory; +use Magento\Framework\Data\Form\FormKey; +use Magento\Framework\Serialize\Serializer\Json; +use Magento\Framework\View\Element\Template\Context; +use SY\Contact\Helper\Data; - /** @var \Magento\Customer\Model\Session */ - protected $_customerSession; +class ContactForm extends BaseContactForm +{ - private $helper; - - private $json; - - protected $formKey; + private $_fields; public function __construct( - \Magento\Framework\View\Element\Template\Context $context, - \Magento\Email\Model\Template\FilterFactory $templateFilter, - \Magento\Customer\Model\Session $_customerSession, - \SY\Contact\Helper\Data $helper, - \Magento\Framework\Serialize\Serializer\Json $json, - \Magento\Framework\Data\Form\FormKey $formKey, + Context $context, + protected readonly Session $_customerSession, + protected readonly FormKey $formKey, + private readonly FilterFactory $templateFilter, + private readonly Data $helper, + private readonly Json $json, array $data = [] - ) - { + ) { parent::__construct($context, $data); - $this->templateFilter = $templateFilter; - $this->_customerSession = $_customerSession; - $this->helper = $helper; - $this->json = $json; - $this->formKey = $formKey; } - public function getFields(){ - if(!$this->_fields){ - $fields = $this->helper->getContactConfig( - 'general/fields', - $this->_storeManager->getStore()->getId() - ); - $fields = $this->json->unserialize($fields); - if(count($fields)>0){ - foreach ($fields as $key => $field) { - $object = new \Magento\Framework\DataObject; - $field['default_value'] = $this->runDirectives($field['default_value']); - $object->addData($field); - $fields[$key] = $object; - } - } - $this->_fields = $fields; - } - return $this->_fields; - } - public function getFormKey(){ - return $this->formKey->getFormKey(); - } - - protected function collectVariables() { - $customer = $this->_customerSession->getCustomer(); - - $customerDefaultBilling = $customer->getDefaultBillingAddress(); - $customerDefaultShipping = $customer->getDefaultShippingAddress(); - - return compact('customer', 'customerDefaultBilling', 'customerDefaultShipping'); + public function getFormKey() + { + return $this->formKey->getFormKey(); } - /** - * Runs email template directives on the value, enabling us to use default values like - * {{customer.firstname}} - * {{customerDefaultBilling.city}} - * and such - * - * @param $value - */ - protected function runDirectives($value) { - $filter = $this->templateFilter->create([ - 'variables' => $this->collectVariables() + public function getJsFormConfig() + { + return json_encode([ + "validation" => new \stdClass(), + "customContactForm" => $this->getCustomContactFormData(), ]); - $result = $filter->filter($value); - return $result; } protected function getCustomContactFormData() { $fields = $this->getFields(); $result = []; - foreach($fields as $field) { + foreach ($fields as $field) { $result[$field->getKey()] = [ - 'show_if' => $field->getShowIf() + 'show_if' => $field->getShowIf(), ]; } @@ -98,10 +58,52 @@ protected function getCustomContactFormData() return $result; } - public function getJsFormConfig() + public function getFields() { - return json_encode([ - "validation" => new \stdClass(), "customContactForm" => $this->getCustomContactFormData() + if (!$this->_fields) { + $fields = $this->helper->getContactConfig( + 'general/fields', + $this->_storeManager->getStore()->getId() + ); + $fields = $this->json->unserialize($fields); + if (count($fields) > 0) { + foreach ($fields as $key => $field) { + $object = new \Magento\Framework\DataObject; + $field['default_value'] = $this->runDirectives($field['default_value']); + $object->addData($field); + $fields[$key] = $object; + } + } + $this->_fields = $fields; + } + + return $this->_fields; + } + + /** + * Runs email template directives on the value, enabling us to use default values like + * {{customer.firstname}} + * {{customerDefaultBilling.city}} + * and such + * + * @param $value + */ + protected function runDirectives($value) + { + $filter = $this->templateFilter->create([ + 'variables' => $this->collectVariables(), ]); + + return $filter->filter($value); + } + + protected function collectVariables() + { + $customer = $this->_customerSession->getCustomer(); + + $customerDefaultBilling = $customer->getDefaultBillingAddress(); + $customerDefaultShipping = $customer->getDefaultShippingAddress(); + + return compact('customer', 'customerDefaultBilling', 'customerDefaultShipping'); } } diff --git a/Controller/Form/Post.php b/Controller/Form/Post.php index d3b4b1e..172ace5 100644 --- a/Controller/Form/Post.php +++ b/Controller/Form/Post.php @@ -9,41 +9,27 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Data\Form\FormKey\Validator; +use Magento\Framework\Serialize\Serializer\Json; +use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface; +use SY\Contact\Helper\Data; +use SY\Contact\Helper\Email; +use SY\Contact\Model\Request; class Post extends Action { - private $request; - private $helper; - private $json; - private $validator; - private $_storemanagerinterface; - private $email; - - /** - * @var LoggerInterface - */ - private $logger; - public function __construct( - \Magento\Framework\App\Action\Context $context, - \SY\Contact\Model\Request $request, - \SY\Contact\Helper\Data $helper, - \Magento\Framework\Serialize\Serializer\Json $json, - \Magento\Framework\Data\Form\FormKey\Validator $validator, - \Magento\Store\Model\StoreManagerInterface $_storemanagerinterface, - \SY\Contact\Helper\Email $email, - LoggerInterface $logger + Context $context, + private readonly Request $request, + private readonly Data $helper, + private readonly Json $json, + private readonly Validator $validator, + private readonly StoreManagerInterface $storeManager, + private readonly Email $email, + private readonly LoggerInterface $logger ){ parent::__construct($context); - - $this->request = $request; - $this->helper = $helper; - $this->json = $json; - $this->validator = $validator; - $this->storeManager = $_storemanagerinterface; - $this->email = $email; - $this->logger = $logger; } public function execute() { diff --git a/Helper/Email.php b/Helper/Email.php index 949d781..035d105 100644 --- a/Helper/Email.php +++ b/Helper/Email.php @@ -85,16 +85,11 @@ public function receive(Request $request, $storeId = 0) } } - /** - * @param $array - * - * @return array - */ - public function toVars(array $array) + public function toVars(array $array): array { $allFieldsHtml = ''; $vars = []; - if (is_array($array) && count($array) > 0) { + if (count($array) > 0) { foreach ($array as $field) { $value = is_array($field['value']) ? implode(', ', $field['value']) : (string) $field['value']; $vars[$field['key']] = $value; diff --git a/Ui/Component/Listing/Column/PostActions.php b/Ui/Component/Listing/Column/PostActions.php index e72f43a..631277e 100644 --- a/Ui/Component/Listing/Column/PostActions.php +++ b/Ui/Component/Listing/Column/PostActions.php @@ -13,23 +13,16 @@ class PostActions extends Column { - const EDIT = 'sy_contact/requests/edit'; - const DELETE = 'sy_contact/requests/delete'; - protected $urlBuilder; - private $editUrl; - private $deleteUrl; + private const EDIT = 'sy_contact/requests/edit'; + public function __construct( + protected readonly UrlInterface $urlBuilder, ContextInterface $context, UiComponentFactory $uiComponentFactory, - UrlInterface $urlBuilder, + private readonly string $editUrl = self::EDIT, array $components = [], array $data = [], - $editUrl = self::EDIT, - $deleteUrl = self::DELETE ) { - $this->urlBuilder = $urlBuilder; - $this->editUrl = $editUrl; - $this->deleteUrl = $deleteUrl; parent::__construct($context, $uiComponentFactory, $components, $data); } public function prepareDataSource(array $dataSource){ @@ -41,16 +34,6 @@ public function prepareDataSource(array $dataSource){ 'href' => $this->urlBuilder->getUrl($this->editUrl, ['id' => $item['id']]), 'label' => __('Edit') ]; - /* - $item[$name]['delete'] = [ - 'href' => $this->urlBuilder->getUrl($this->deleteUrl, ['id' => $item['id']]), - 'label' => __('Delete'), - 'confirm' => [ - 'title' => __('Delete'), - 'message' => __('Are you sure you wan\'t to delete record?') - ] - ]; - */ } } }