/**
  * @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 $tagId) {
         /** @var \Mageplaza\Blog\Model\Tag $tag */
         $tag = $this->tagFactory->create()->load($tagId);
         try {
             $tagData = $postItems[$tagId];
             //todo: handle dates
             $tag->addData($tagData);
             $tag->save();
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
             $error = true;
         } catch (\RuntimeException $e) {
             $messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
             $error = true;
         } catch (\Exception $e) {
             $messages[] = $this->getErrorWithTagId($tag, __('Something went wrong while saving the Tag.'));
             $error = true;
         }
     }
     return $resultJson->setData(['messages' => $messages, 'error' => $error]);
 }
Пример #2
0
 /**
  * Init Tag
  *
  * @return \Mageplaza\Blog\Model\Tag
  */
 protected function initTag()
 {
     $tagId = (int) $this->getRequest()->getParam('tag_id');
     /** @var \Mageplaza\Blog\Model\Tag $tag */
     $tag = $this->tagFactory->create();
     if ($tagId) {
         $tag->load($tagId);
     }
     $this->coreRegistry->register('mageplaza_blog_tag', $tag);
     return $tag;
 }