/** * @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 $postId) { /** @var \Mageplaza\Blog\Model\Post $post */ $post = $this->postFactory->create()->load($postId); try { $postData = $postItems[$postId]; //todo: handle dates $post->addData($postData); $post->save(); } catch (\Magento\Framework\Exception\LocalizedException $e) { $messages[] = $this->getErrorWithPostId($post, $e->getMessage()); $error = true; } catch (\RuntimeException $e) { $messages[] = $this->getErrorWithPostId($post, $e->getMessage()); $error = true; } catch (\Exception $e) { $messages[] = $this->getErrorWithPostId($post, __('Something went wrong while saving the Post.')); $error = true; } } return $resultJson->setData(['messages' => $messages, 'error' => $error]); }
/** * Init Post * * @return \Mageplaza\Blog\Model\Post */ protected function initPost() { $postId = (int) $this->getRequest()->getParam('post_id'); /** @var \Mageplaza\Blog\Model\Post $post */ $post = $this->postFactory->create(); if ($postId) { $post->load($postId); } $this->coreRegistry->register('mageplaza_blog_post', $post); return $post; }