![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/AdminHistory/Observer/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\AdminHistory\Observer; use Cnc\AdminHistory\Model\CustomerFactory; use Cnc\AdminHistory\Model\Customer; use Cnc\AdminHistory\Model\ResourceModel\Customer as CustomerResource; use Cnc\Customer\Model\Config; use Magento\Backend\Model\Auth\Session; use Magento\Customer\Model\GroupRegistry; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Stdlib\DateTime\DateTime; /** * Class SaveCustomerHistory * observer to log changes done in customer edit page */ class SaveCustomerHistory implements ObserverInterface { /** @var CustomerFactory */ protected $customerHistoryFactory; /** @var CustomerResource */ protected $customerHistoryResource; /** @var Session */ protected $authSession; /** @var DateTime */ protected $date; /** * @var GroupRegistry */ private $groupRegistry; /** * SaveCustomerHistory constructor. * @param CustomerFactory $customerHistoryFactory * @param CustomerResource $customerHistoryResource * @param Session $authSession * @param DateTime $date * @param GroupRegistry $groupRegistry */ public function __construct( CustomerFactory $customerHistoryFactory, CustomerResource $customerHistoryResource, Session $authSession, DateTime $date, GroupRegistry $groupRegistry ) { $this->customerHistoryFactory = $customerHistoryFactory; $this->customerHistoryResource = $customerHistoryResource; $this->authSession = $authSession; $this->date = $date; $this->groupRegistry = $groupRegistry; } /** * @param Observer $observer * @throws AlreadyExistsException * @todo add logger on payment authorisation change in customer edit */ public function execute(Observer $observer): void { $changedData = []; /** @var \Magento\Customer\Model\Data\Customer $customerData */ $customerData = $observer->getCustomerDataObject(); /** @var \Magento\Customer\Model\Data\Customer $originCustomerData */ $originCustomerData = $observer->getOrigCustomerDataObject(); $isGroupChanged = false; $oldCustomerGroup = ''; if ($customerData) { $customerId = $customerData->getId(); $newCustomerGroup = $customerData->getGroupId(); if ($originCustomerData) { $oldCustomerGroup = $originCustomerData->getGroupId(); if ($oldCustomerGroup != $newCustomerGroup) { $isGroupChanged = true; } } else { $isGroupChanged = true; } if ($isGroupChanged) { $oldCustomerGroupModel = $oldCustomerGroup ? $this->groupRegistry->retrieve($oldCustomerGroup) : null; $newCustomerGroupModel = $this->groupRegistry->retrieve($newCustomerGroup); $changedData []= [ 'attribute_code' => 'customer_group', 'old_value' => $oldCustomerGroupModel && $oldCustomerGroupModel->getId() ? $oldCustomerGroupModel->getCode() . ' (id: ' . $oldCustomerGroup . ')' : $oldCustomerGroup, 'new_value' => $newCustomerGroupModel && $newCustomerGroupModel->getId() ? $newCustomerGroupModel->getCode() . ' (id: ' . $newCustomerGroup . ')' : $newCustomerGroup ]; } } if ($originCustomerData != null) { $oldEnableOffPay = $originCustomerData->getCustomAttribute( Config::CUSTOMER_ATTRIBUTE_ENABLE_OFF_PAY_CODE ); $newEnableOffPay = $customerData->getCustomAttribute( Config::CUSTOMER_ATTRIBUTE_ENABLE_OFF_PAY_CODE ); $oldEnablePaymentOnReceipt = $originCustomerData->getCustomAttribute( Config::CUSTOMER_ATTRIBUTE_ENABLE_PAYMENT_ON_RECEIPT_INVOICE ); $newEnablePaymentOnReceipt = $customerData->getCustomAttribute( Config::CUSTOMER_ATTRIBUTE_ENABLE_PAYMENT_ON_RECEIPT_INVOICE ); $oldEnableOffPayValue = ($oldEnableOffPay ? $oldEnableOffPay->getValue() : ''); $newEnableOffPayValue = ($newEnableOffPay ? $newEnableOffPay->getValue() : ''); if ($oldEnableOffPayValue != $newEnableOffPayValue) { $changedData [] = [ 'attribute_code' => Config::CUSTOMER_ATTRIBUTE_ENABLE_OFF_PAY_CODE, 'old_value' => $oldEnableOffPayValue, 'new_value' => $newEnableOffPayValue ]; } $oldEnablePaymentOnReceiptValue = $oldEnablePaymentOnReceipt ? $oldEnablePaymentOnReceipt->getValue() : ''; $newEnablePaymentOnReceiptValue = $newEnablePaymentOnReceipt ? $newEnablePaymentOnReceipt->getValue() : ''; if ($oldEnablePaymentOnReceiptValue != $newEnablePaymentOnReceiptValue) { $changedData [] = [ 'attribute_code' => Config::CUSTOMER_ATTRIBUTE_ENABLE_PAYMENT_ON_RECEIPT_INVOICE, 'old_value' => $oldEnablePaymentOnReceiptValue, 'new_value' => $newEnablePaymentOnReceiptValue ]; } } foreach ($changedData as $data) { try { $userName = $this->authSession->getUser() ? $this->authSession->getUser()->getName() : ''; $updatedAt = $this->date->gmtDate(); /** @var Customer $customerHistory */ $customerHistory = $this->customerHistoryFactory->create(); $customerHistory->setAttributeCode($data['attribute_code']); $customerHistory->setOldValue($data['old_value']); $customerHistory->setNewValue($data['new_value']); $customerHistory->setCustomerId($customerId); $customerHistory->setUpdatedAt($updatedAt); $customerHistory->setUser($userName); $this->customerHistoryResource->save($customerHistory); } catch (AlreadyExistsException $e) { throw new AlreadyExistsException(__('Could not save customer history log: %1', $e->getMessage())); } } } }