コード例 #1
0
ファイル: Observer.php プロジェクト: sz-bill/Magento2.x
 /**
  * save product data
  * @param $observer
  * @return $this
  */
 public function saveProductData(EventObserver $observer)
 {
     $post = $this->context->getRequest()->getPost('authors', -1);
     if ($post != '-1') {
         $post = $this->jsHelper->decodeGridSerializedInput($post);
         $product = $this->coreRegistry->registry('product');
         $this->authorResource->saveAuthorProductRelation($product, $post);
     }
     return $this;
 }
コード例 #2
0
 /**
  * run the action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('post');
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         $post = $this->initPost();
         $post->setData($data);
         $image = $this->uploadModel->uploadFileAndGetName('image', $this->imageModel->getBaseDir(), $data);
         if ($image) {
             $post->setImage('mageplaza/blog/post/image' . $image);
         }
         $tags = $this->getRequest()->getPost('tags', -1);
         if ($tags != -1) {
             $post->setTagsData($this->jsHelper->decodeGridSerializedInput($tags));
         }
         $topics = $this->getRequest()->getPost('topics', -1);
         if ($topics != -1) {
             $post->setTopicsData($this->jsHelper->decodeGridSerializedInput($topics));
         }
         if (!isset($data['categories_ids'])) {
             $post->setCategoriesIds([]);
         }
         $this->_eventManager->dispatch('mageplaza_blog_post_prepare_save', ['post' => $post, 'request' => $this->getRequest()]);
         try {
             $post->save();
             $trafficModel = $this->trafficFactory->create()->load($post->getId(), 'post_id');
             if (!$trafficModel->getId()) {
                 $trafficData = ['post_id' => $post->getId(), 'numbers_view' => '0'];
                 $trafficModel->setData($trafficData);
                 $trafficModel->save();
             }
             $this->messageManager->addSuccess(__('The Post has been saved.'));
             $this->backendSession->setMageplazaBlogPostData(false);
             if ($this->getRequest()->getParam('back')) {
                 $resultRedirect->setPath('mageplaza_blog/*/edit', ['post_id' => $post->getId(), '_current' => true]);
                 return $resultRedirect;
             }
             $resultRedirect->setPath('mageplaza_blog/*/');
             return $resultRedirect;
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while saving the Post.'));
         }
         $this->_getSession()->setMageplazaBlogPostData($data);
         $resultRedirect->setPath('mageplaza_blog/*/edit', ['post_id' => $post->getId(), '_current' => true]);
         return $resultRedirect;
     }
     $resultRedirect->setPath('mageplaza_blog/*/');
     return $resultRedirect;
 }
コード例 #3
0
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $productId = $this->request->getParam('id');
     $links = $this->request->getPost('links');
     $links = is_array($links) ? $links : [];
     if (isset($links['question'])) {
         $links['question'] = $this->jsHelper->decodeGridSerializedInput($links['question']);
         $idsCollection = $this->getIdsCollection();
         $idsCollection->addFieldToFilter('product_id', $productId);
         $questionIds = array();
         foreach ($idsCollection as $question) {
             $questionIds[] = $question->getQuestionId();
         }
         //update question position
         foreach ($questionIds as $questionId) {
             if (isset($links['question'][$questionId])) {
                 $idsCollection = $this->getIdsCollection();
                 $idsCollection->addFieldToFilter('question_id', $questionId);
                 $idsCollection->addFieldToFilter('product_id', $productId);
                 $item = $idsCollection->getFirstItem();
                 if ($item->getPosition() != $links['question'][$questionId]['position'] && !empty($links['question'][$questionId]['position'])) {
                     $item->setPosition($links['question'][$questionId]['position']);
                     $item->save();
                 }
             }
         }
         // save new checked questions and position
         $insert = array_diff(array_keys($links['question']), $questionIds);
         if (!empty($insert)) {
             foreach ($insert as $i) {
                 $questionIdModel = $this->questionId->create();
                 $questionIdModel->setProductId((int) $productId);
                 $questionIdModel->setQuestionId($i);
                 $questionIdModel->setPosition($links['question'][$i]['position']);
                 $questionIdModel->save();
             }
         }
         //delete unchecked Questions
         $delete = array_diff($questionIds, array_keys($links['question']));
         if (!empty($delete)) {
             $idsCollection = $this->getIdsCollection();
             $idsCollection->addFieldToFilter('product_id', $productId);
             $idsCollection->addFieldToFilter('question_id', ['in' => $delete]);
             foreach ($idsCollection as $item) {
                 $item->delete();
             }
         }
     }
 }
コード例 #4
0
ファイル: Helper.php プロジェクト: pavelnovitsky/magento2
 /**
  * Initialize product before saving
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  */
 public function initialize(\Magento\Catalog\Model\Product $product)
 {
     $productData = $this->request->getPost('product');
     if ($productData) {
         $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : array();
         $productData['stock_data'] = $this->stockFilter->filter($stockData);
     }
     foreach (array('category_ids', 'website_ids') as $field) {
         if (!isset($productData[$field])) {
             $productData[$field] = array();
         }
     }
     $wasLockedMedia = false;
     if ($product->isLockedAttribute('media')) {
         $product->unlockAttribute('media');
         $wasLockedMedia = true;
     }
     $product->addData($productData);
     if ($wasLockedMedia) {
         $product->lockAttribute('media');
     }
     if ($this->storeManager->hasSingleStore()) {
         $product->setWebsiteIds(array($this->storeManager->getStore(true)->getWebsite()->getId()));
     }
     /**
      * Create Permanent Redirect for old URL key
      */
     if ($product->getId() && isset($productData['url_key_create_redirect'])) {
         $product->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']);
     }
     /**
      * Check "Use Default Value" checkboxes values
      */
     $useDefaults = $this->request->getPost('use_default');
     if ($useDefaults) {
         foreach ($useDefaults as $attributeCode) {
             $product->setData($attributeCode, false);
         }
     }
     $links = $this->request->getPost('links');
     $links = is_array($links) ? $links : array();
     $linkTypes = array('related', 'upsell', 'crosssell');
     foreach ($linkTypes as $type) {
         if (isset($links[$type])) {
             $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
         }
     }
     $product = $this->productLinks->initializeLinks($product, $links);
     /**
      * Initialize product options
      */
     if (isset($productData['options']) && !$product->getOptionsReadonly()) {
         // mark custom options that should to fall back to default value
         $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
         $product->setProductOptions($options);
     }
     $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
     return $product;
 }
コード例 #5
0
ファイル: Save.php プロジェクト: sz-bill/Magento2.x
 /**
  * run the action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('author');
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         $data = $this->filterData($data);
         $author = $this->initAuthor();
         $author->setData($data);
         $avatar = $this->uploadModel->uploadFileAndGetName('avatar', $this->imageModel->getBaseDir(), $data);
         $author->setAvatar($avatar);
         $resume = $this->uploadModel->uploadFileAndGetName('resume', $this->fileModel->getBaseDir(), $data);
         $author->setResume($resume);
         $products = $this->getRequest()->getPost('products', -1);
         if ($products != -1) {
             $author->setProductsData($this->jsHelper->decodeGridSerializedInput($products));
         }
         $this->_eventManager->dispatch('sample_news_author_prepare_save', ['author' => $author, 'request' => $this->getRequest()]);
         try {
             $author->save();
             $this->messageManager->addSuccess(__('The author has been saved.'));
             $this->_getSession()->setSampleNewsAuthorData(false);
             if ($this->getRequest()->getParam('back')) {
                 $resultRedirect->setPath('sample_news/*/edit', ['author_id' => $author->getId(), '_current' => true]);
                 return $resultRedirect;
             }
             $resultRedirect->setPath('sample_news/*/');
             return $resultRedirect;
         } catch (LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while saving the author.'));
         }
         $this->_getSession()->setSampleNewsAuthorData($data);
         $resultRedirect->setPath('sample_news/*/edit', ['author_id' => $author->getId(), '_current' => true]);
         return $resultRedirect;
     }
     $resultRedirect->setPath('sample_news/*/');
     return $resultRedirect;
 }
コード例 #6
0
ファイル: Save.php プロジェクト: pleminh/Gemtoo
 /**
  * @return mixed
  * @throws \Magento\Framework\Model\Exception
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('article');
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         $data = $this->filterData($data);
         $article = $this->initArticle();
         $article->setData($data);
         $image = $this->uploadModel->uploadFileAndGetName('image', $this->imageModel->getBaseDir(), $data);
         $article->setImage($image);
         $file = $this->uploadModel->uploadFileAndGetName('file', $this->fileModel->getBaseDir(), $data);
         $article->setFile($file);
         $products = $this->getRequest()->getPost('products', -1);
         if ($products != -1) {
             $article->setProductsData($this->jsHelper->decodeGridSerializedInput($products));
         }
         $this->_eventManager->dispatch('gemtoo_blog_article_prepare_save', ['article' => $article, 'request' => $this->getRequest()]);
         try {
             $article->save();
             $this->messageManager->addSuccess(__('The article has been saved.'));
             $this->_getSession()->setGemtooBlogArticleData(false);
             if ($this->getRequest()->getParam('back')) {
                 $resultRedirect->setPath('gemtoo_blog/*/edit', ['article_id' => $article->getId(), '_current' => true]);
                 return $resultRedirect;
             }
             $resultRedirect->setPath('gemtoo_blog/*/');
             return $resultRedirect;
         } catch (LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while saving the article.'));
         }
         $this->_getSession()->setGemtooBlogArticleData($data);
         $resultRedirect->setPath('gemtoo_blog/*/edit', ['article_id' => $article->getId(), '_current' => true]);
         return $resultRedirect;
     }
     $resultRedirect->setPath('gemtoo_blog/*/');
     return $resultRedirect;
 }
コード例 #7
0
 /**
  * run the action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $data = $this->getRequest()->getPost('tag');
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         $tag = $this->initTag();
         $tag->setData($data);
         $posts = $this->getRequest()->getPost('posts', -1);
         if ($posts != -1) {
             $tag->setPostsData($this->jsHelper->decodeGridSerializedInput($posts));
         }
         $this->_eventManager->dispatch('mageplaza_blog_tag_prepare_save', ['tag' => $tag, 'request' => $this->getRequest()]);
         try {
             $tag->save();
             $this->messageManager->addSuccess(__('The Tag has been saved.'));
             $this->backendSession->setMageplazaBlogTagData(false);
             if ($this->getRequest()->getParam('back')) {
                 $resultRedirect->setPath('mageplaza_blog/*/edit', ['tag_id' => $tag->getId(), '_current' => true]);
                 return $resultRedirect;
             }
             $resultRedirect->setPath('mageplaza_blog/*/');
             return $resultRedirect;
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while saving the Tag.'));
         }
         $this->_getSession()->setMageplazaBlogTagData($data);
         $resultRedirect->setPath('mageplaza_blog/*/edit', ['tag_id' => $tag->getId(), '_current' => true]);
         return $resultRedirect;
     }
     $resultRedirect->setPath('mageplaza_blog/*/');
     return $resultRedirect;
 }
コード例 #8
0
ファイル: Helper.php プロジェクト: koliaGI/magento2
 /**
  * Initialize product before saving
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function initialize(\Magento\Catalog\Model\Product $product)
 {
     $productData = $this->request->getPost('product');
     unset($productData['custom_attributes']);
     unset($productData['extension_attributes']);
     if ($productData) {
         $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
         $productData['stock_data'] = $this->stockFilter->filter($stockData);
     }
     foreach (['category_ids', 'website_ids'] as $field) {
         if (!isset($productData[$field])) {
             $productData[$field] = [];
         }
     }
     $wasLockedMedia = false;
     if ($product->isLockedAttribute('media')) {
         $product->unlockAttribute('media');
         $wasLockedMedia = true;
     }
     $dateFieldFilters = [];
     $attributes = $product->getAttributes();
     foreach ($attributes as $attrKey => $attribute) {
         if ($attribute->getBackend()->getType() == 'datetime') {
             if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
                 $dateFieldFilters[$attrKey] = $this->dateFilter;
             }
         }
     }
     $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
     $productData = $inputFilter->getUnescaped();
     $product->addData($productData);
     if ($wasLockedMedia) {
         $product->lockAttribute('media');
     }
     if ($this->storeManager->hasSingleStore()) {
         $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
     }
     /**
      * Check "Use Default Value" checkboxes values
      */
     $useDefaults = $this->request->getPost('use_default');
     if ($useDefaults) {
         foreach ($useDefaults as $attributeCode) {
             $product->setData($attributeCode, false);
         }
     }
     $links = $this->request->getPost('links');
     $links = is_array($links) ? $links : [];
     $linkTypes = ['related', 'upsell', 'crosssell'];
     foreach ($linkTypes as $type) {
         if (isset($links[$type])) {
             $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
         }
     }
     $product = $this->productLinks->initializeLinks($product, $links);
     $productLinks = $product->getProductLinks();
     $linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()];
     foreach ($linkTypes as $linkType => $readonly) {
         if (isset($links[$linkType]) && !$readonly) {
             foreach ($links[$linkType] as $linkId => $linkData) {
                 $linkProduct = $this->productRepository->getById($linkId);
                 $link = $this->productLinkFactory->create();
                 $link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0);
                 $productLinks[] = $link;
             }
         }
     }
     $product->setProductLinks($productLinks);
     /**
      * Initialize product options
      */
     if (isset($productData['options']) && !$product->getOptionsReadonly()) {
         // mark custom options that should to fall back to default value
         $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
         $customOptions = [];
         foreach ($options as $customOptionData) {
             if (!(bool) $customOptionData['is_delete']) {
                 $customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
                 $customOption->setProductSku($product->getSku());
                 $customOption->setOptionId(null);
                 $customOptions[] = $customOption;
             }
         }
         $product->setOptions($customOptions);
     }
     $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
     return $product;
 }