Your IP : 216.73.216.220


Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Observer/
Upload File :
Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Observer/CustomerChildrenRegisterValidation.php

<?php
namespace FlipsDigital\HappyMind\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\ObjectManager;
use \FlipsDigital\EducationInstitution\Model\EducationInstitution;
use Magento\Framework\Session\SessionManager;


class CustomerChildrenRegisterValidation implements ObserverInterface
{
    protected $_messageManager;
    protected $_responseFactory;
    protected $_urlModel;
    protected $_customerSession;
    protected $_actionFlag;
    protected $redirect;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManagerInterface,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlFactory $urlFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \FlipsDigital\EducationInstitution\Model\EducationInstitutionFactory $educationInstitutionFactory,
        \Magento\Framework\App\ActionFlag $actionFlag
    )
    {
        $this->_objectManager = $objectManagerInterface;
        $this->_customerSession = $customerSession;
        $this->_messageManager = $messageManager;
        $this->_responseFactory = $responseFactory;
        $this->_urlModel = $urlFactory->create();
        $this->_actionFlag = $actionFlag;
		$this->_eiFactory = $educationInstitutionFactory;
        $this->redirect = $redirect;
    }
    public function execute(Observer $observer)
    {
        $objectManager = $this->_objectManager;
        $event = $observer->getEvent();
        $controllerAction = $event->getControllerAction();
        $customerFormData = $controllerAction->getRequest()->getPostValue();
        $arrErrorMsg = [];
        if (
            isset($customerFormData['children']) &&
            count($customerFormData['children'])
        ){
            $childrenFormData = $customerFormData['children'];
            if (isset($childrenFormData['school_code'])){
                $objEI = $this->_eiFactory->create();
                $collectionEI = $objEI->getCollection();
                $collectionEI->addAttributeToFilter('group_id', $childrenFormData['school_code']);
                if (!$collectionEI->count()){
                    $arrErrorMsg[] = __('School Code not find 找不到相關的學校代碼');
                }
            }
        }

        if (count($arrErrorMsg)){
            foreach ($arrErrorMsg as $key => $value) {
                $this->_messageManager->addError($value);
            }
            $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
            $this->_customerSession->setCustomerFormData($controllerAction->getRequest()->getPostValue());

            $CoreSession = $objectManager->create(SessionManager::class);
            $CoreSession->setData('childrenData', $childrenFormData);
            $redirectionUrl = $this->_urlModel->getUrl('*/*/create');
            $controllerAction->getResponse()->setRedirect($this->redirect->error($redirectionUrl));

        }
        return $this;
    }
}