/**
  * Execute method.
  */
 public function execute()
 {
     if ($this->getRequest()->getParams()) {
         try {
             $ruleModel = $this->ruleFactory->create();
             $data = $this->getRequest()->getParams();
             $id = $this->getRequest()->getParam('id');
             if ($data['website_ids']) {
                 foreach ($data['website_ids'] as $websiteId) {
                     $result = $ruleModel->checkWebsiteBeforeSave($websiteId, $data['type'], $id);
                     if (!$result) {
                         $websiteName = $this->storeManager->getWebsite($websiteId)->getName();
                         $this->messageManager->addErrorMessage(__('Rule already exist for website ' . $websiteName . '. You can only have one rule per website.'));
                         $this->_redirect('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
                         return;
                     }
                 }
             }
             $ruleModel->getResource()->load($ruleModel, $id);
             if ($id != $ruleModel->getId()) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('Wrong rule specified.'));
             }
             foreach ($data as $key => $value) {
                 if ($key != 'form_key') {
                     if ($key == 'condition') {
                         if (is_array($value)) {
                             unset($value['__empty']);
                         }
                     }
                     $ruleModel->setData($key, $value);
                 }
             }
             $this->_getSession()->setPageData($ruleModel->getData());
             $ruleModel->getResource()->save($ruleModel);
             $this->messageManager->addSuccessMessage(__('The rule has been saved.'));
             $this->_getSession()->setPageData(false);
             if ($this->getRequest()->getParam('back')) {
                 $this->_redirect('*/*/edit', ['id' => $ruleModel->getId()]);
                 return;
             }
             $this->_redirect('*/*/');
             return;
         } catch (\Exception $e) {
             $this->messageManager->addErrorMessage(__('An error occurred while saving the rule data. Please review the log and try again.'));
             $this->_getSession()->setPageData($data);
             $this->_redirect('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
             return;
         }
     }
     $this->_redirect('*/*/');
 }
 /**
  * Execute method.
  */
 public function execute()
 {
     $id = $this->getRequest()->getParam('id');
     $this->_view->loadLayout();
     $this->_setActiveMenu('Magento_CatalogRule::exclusion_rules')->_addBreadcrumb($id ? __('Edit Rule') : __('New Rule'), $id ? __('Edit Rule') : __('New Rule'));
     $emailRules = $this->rules;
     if ($id) {
         $emailRules = $emailRules->load($id);
         if (!$emailRules->getId()) {
             $this->messageManager->addErrorMessage(__('This rule no longer exists.'));
             $this->_redirect('*/*');
         }
     }
     $this->_view->getPage()->getConfig()->getTitle()->prepend($emailRules->getId() ? $emailRules->getName() : __('New Rule'));
     // set entered data if was error when we do save
     $data = $this->_session->getPageData(true);
     if (!empty($data)) {
         $this->rules->addData($data);
     }
     $this->registry->register('current_ddg_rule', $emailRules);
     $this->_view->getLayout()->getBlock('dotdigitalgroup.email.rules.edit')->setData('action', $this->getUrl('*/*/save'));
     $this->_view->renderLayout();
 }
 /**
  * @param Rules $rule
  * @return array
  */
 private function getConditionsFromRule(Rules $rule)
 {
     $conditions = $rule->getData('condition') ? $rule->getData('condition') : [];
     if (is_string($conditions)) {
         $conditions = unserialize($conditions);
     }
     return $conditions;
 }