/** * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->jsonFactory->create(); $error = false; $messages = []; $postItems = $this->getRequest()->getParam('items', []); if (!($this->getRequest()->getParam('isAjax') && count($postItems))) { return $resultJson->setData(['messages' => [__('Please correct the data sent.')], 'error' => true]); } foreach (array_keys($postItems) as $topicId) { /** @var \Mageplaza\Blog\Model\Topic $topic */ $topic = $this->topicFactory->create()->load($topicId); try { $topicData = $postItems[$topicId]; //todo: handle dates $topic->addData($topicData); $topic->save(); } catch (\Magento\Framework\Exception\LocalizedException $e) { $messages[] = $this->getErrorWithTopicId($topic, $e->getMessage()); $error = true; } catch (\RuntimeException $e) { $messages[] = $this->getErrorWithTopicId($topic, $e->getMessage()); $error = true; } catch (\Exception $e) { $messages[] = $this->getErrorWithTopicId($topic, __('Something went wrong while saving the Topic.')); $error = true; } } return $resultJson->setData(['messages' => $messages, 'error' => $error]); }
/** * Init Topic * * @return \Mageplaza\Blog\Model\Topic */ protected function initTopic() { $topicId = (int) $this->getRequest()->getParam('topic_id'); /** @var \Mageplaza\Blog\Model\Topic $topic */ $topic = $this->topicFactory->create(); if ($topicId) { $topic->load($topicId); } $this->coreRegistry->register('mageplaza_blog_topic', $topic); return $topic; }