/** * @return \Gemtoo\Blog\Model\ResourceModel\Article\Collection */ public function getArticleCollection() { if (is_null($this->articleCollection)) { $collection = $this->articleProduct->getSelectedArticlesCollection($this->getProduct()); $collection->addStoreFilter($this->_storeManager->getStore()->getId()); $collection->addFieldToFilter('is_active', Article::STATUS_ENABLED); $collection->getSelect()->order('position'); $this->articleCollection = $collection; } return $this->articleCollection; }
/** * @param Product $product * @param $articles * @return $this */ public function saveArticleProductRelation(Product $product, $articles) { $product->setIsChangedArticleList(false); $id = $product->getId(); if ($articles === null) { return $this; } $oldArticleObjects = $this->articleProduct->getSelectedArticles($product); if (!is_array($oldArticleObjects)) { $oldArticleObjects = []; } $oldArticles = []; foreach ($oldArticleObjects as $article) { /** @var \Gemtoo\Blog\Model\Article $article */ $oldArticles[$article->getId()] = ['position' => $article->getPosition()]; } $insert = array_diff_key($articles, $oldArticles); $delete = array_diff_key($oldArticles, $articles); $update = array_intersect_key($articles, $oldArticles); $toUpdate = []; foreach ($update as $productId => $values) { if (isset($oldArticles[$productId]) && $oldArticles[$productId]['position'] != $values['position']) { $toUpdate[$productId] = []; $toUpdate[$productId]['position'] = $values['position']; } } $update = $toUpdate; $adapter = $this->getConnection(); if (!empty($delete)) { $condition = ['article_id IN(?)' => array_keys($delete), 'product_id=?' => $id]; $adapter->delete($this->articleProductTable, $condition); } if (!empty($insert)) { $data = []; foreach ($insert as $articleId => $position) { $data[] = ['product_id' => (int) $id, 'article_id' => (int) $articleId, 'position' => (int) $position['position']]; } $adapter->insertMultiple($this->articleProductTable, $data); } if (!empty($update)) { foreach ($update as $articleId => $position) { $where = ['product_id = ?' => (int) $id, 'article_id = ?' => (int) $articleId]; $bind = ['position' => (int) $position['position']]; $adapter->update($this->articleProductTable, $bind, $where); } } if (!empty($insert) || !empty($delete)) { $articleIds = array_unique(array_merge(array_keys($insert), array_keys($delete))); $this->eventManager->dispatch('gemtoo_blog_product_change_articles', ['product' => $product, 'article_ids' => $articleIds]); } if (!empty($insert) || !empty($update) || !empty($delete)) { $product->setIsChangedArticleList(true); $articleIds = array_keys($insert + $delete + $update); $product->setAffectedArticleIds($articleIds); } return $this; }
/** * get selected articles * @return array */ public function getSelectedArticles() { $articles = []; $selected = $this->articleProduct->getSelectedArticles($this->getProduct()); if (!is_array($selected)) { $selected = []; } foreach ($selected as $article) { /** @var \Gemtoo\Blog\Model\Article $article */ $articles[$article->getId()] = ['position' => $article->getPosition()]; } return $articles; }