Skip to content

Commit

Permalink
Merge pull request #17 from iMi-digital/php-82
Browse files Browse the repository at this point in the history
Add support for php 8.2
  • Loading branch information
DanieliMi authored Oct 28, 2024
2 parents ab330e7 + fdc765f commit 5b77fa3
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 127 deletions.
142 changes: 72 additions & 70 deletions Block/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,104 @@
*/
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(),
];

}

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');
}
}
42 changes: 14 additions & 28 deletions Controller/Form/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 2 additions & 7 deletions Helper/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 4 additions & 21 deletions Ui/Component/Listing/Column/PostActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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?')
]
];
*/
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit 5b77fa3

Please sign in to comment.