jsHelper = $jsHelper; $this->layoutFactory = $layoutFactory; $this->resultJsonFactory = $resultJsonFactory; $this->_helperData = $helperData; parent::__construct($context, $registry, $topicFactory); } /** * @return $this|ResponseInterface|Redirect|ResultInterface */ public function execute() { if ($this->getRequest()->getPost('return_session_messages_only')) { $topic = $this->initTopic(); $topicPostData = $this->getRequest()->getPostValue(); $topicPostData['store_ids'] = 0; $topicPostData['enabled'] = 1; $this->_helperData->handleSeoValueBeforeSave($topicPostData); $topic->addData($topicPostData); try { $topic->save(); $this->messageManager->addSuccessMessage(__('You saved the topic.')); } catch (AlreadyExistsException $e) { $this->messageManager->addErrorMessage($e->getMessage()); $this->_objectManager->get(LoggerInterface::class)->critical($e); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); $this->_objectManager->get(LoggerInterface::class)->critical($e); } catch (Exception $e) { $this->messageManager->addErrorMessage(__('Something went wrong while saving the topic.')); $this->_objectManager->get(LoggerInterface::class)->critical($e); } $hasError = (bool) $this->messageManager->getMessages()->getCountByType( MessageInterface::TYPE_ERROR ); $topic->load($topic->getId()); $topic->addData([ 'level' => 1, 'entity_id' => $topic->getId(), 'is_active' => $topic->getEnabled(), 'parent' => 0 ]); // to obtain truncated category name /** @var $block Messages */ $block = $this->layoutFactory->create()->getMessagesBlock(); $block->setMessages($this->messageManager->getMessages(true)); /** @var Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData( [ 'messages' => $block->getGroupedHtml(), 'error' => $hasError, 'category' => $topic->toArray(), ] ); } $resultRedirect = $this->resultRedirectFactory->create(); if ($data = $this->getRequest()->getPost('topic')) { /** @var \Mageplaza\Blog\Model\Topic $topic */ $topic = $this->initTopic(); $this->_helperData->handleSeoValueBeforeSave($data); $topic->setData($data); if ($posts = $this->getRequest()->getPost('posts', false)) { $topic->setPostsData($this->jsHelper->decodeGridSerializedInput($posts)); } try { $topic->save(); $this->messageManager->addSuccessMessage(__('The Topic has been saved.')); $this->_getSession()->setData('mageplaza_blog_topic_data', false); if ($this->getRequest()->getParam('back')) { $resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $topic->getId(), '_current' => true]); } else { $resultRedirect->setPath('mageplaza_blog/*/'); } return $resultRedirect; } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (RuntimeException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (Exception $e) { $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Topic.')); } $this->_getSession()->setData('mageplaza_blog_topic_data', $data); $resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $topic->getId(), '_current' => true]); return $resultRedirect; } $resultRedirect->setPath('mageplaza_blog/*/'); return $resultRedirect; } }