/** * @return \Sample\News\Model\Resource\Author\Collection */ public function getAuthorCollection() { if (is_null($this->authorCollection)) { $collection = $this->authorProduct->getSelectedAuthorsCollection($this->getProduct()); $collection->addStoreFilter($this->_storeManager->getStore()->getId()); $collection->addFieldToFilter('is_active', Author::STATUS_ENABLED); $collection->getSelect()->order('position'); $this->authorCollection = $collection; } return $this->authorCollection; }
/** * @param Product $product * @param $authors * @return $this */ public function saveAuthorProductRelation(Product $product, $authors) { $product->setIsChangedAuthorList(false); $id = $product->getId(); if ($authors === null) { return $this; } $oldAuthorObjects = $this->authorProduct->getSelectedAuthors($product); if (!is_array($oldAuthorObjects)) { $oldAuthorObjects = []; } $oldAuthors = []; foreach ($oldAuthorObjects as $author) { /** @var \Sample\News\Model\Author $author */ $oldAuthors[$author->getId()] = ['position' => $author->getPosition()]; } $insert = array_diff_key($authors, $oldAuthors); $delete = array_diff_key($oldAuthors, $authors); $update = array_intersect_key($authors, $oldAuthors); $toUpdate = []; foreach ($update as $productId => $values) { if (isset($oldAuthors[$productId]) && $oldAuthors[$productId]['position'] != $values['position']) { $toUpdate[$productId] = []; $toUpdate[$productId]['position'] = $values['position']; } } $update = $toUpdate; $adapter = $this->getConnection(); if (!empty($delete)) { $condition = ['author_id IN(?)' => array_keys($delete), 'product_id=?' => $id]; $adapter->delete($this->authorProductTable, $condition); } if (!empty($insert)) { $data = []; foreach ($insert as $authorId => $position) { $data[] = ['product_id' => (int) $id, 'author_id' => (int) $authorId, 'position' => (int) $position['position']]; } $adapter->insertMultiple($this->authorProductTable, $data); } if (!empty($update)) { foreach ($update as $authorId => $position) { $where = ['product_id = ?' => (int) $id, 'author_id = ?' => (int) $authorId]; $bind = ['position' => (int) $position['position']]; $adapter->update($this->authorProductTable, $bind, $where); } } if (!empty($insert) || !empty($delete)) { $authorIds = array_unique(array_merge(array_keys($insert), array_keys($delete))); $this->eventManager->dispatch('sample_news_product_change_authors', ['product' => $product, 'author_ids' => $authorIds]); } if (!empty($insert) || !empty($update) || !empty($delete)) { $product->setIsChangedAuthorList(true); $authorIds = array_keys($insert + $delete + $update); $product->setAffectedAuthorIds($authorIds); } return $this; }
/** * get selected authors * @return array */ public function getSelectedAuthors() { $authors = []; $selected = $this->authorProduct->getSelectedAuthors($this->getProduct()); if (!is_array($selected)) { $selected = []; } foreach ($selected as $author) { /** @var \Sample\News\Model\Author $author */ $authors[$author->getId()] = ['position' => $author->getPosition()]; } return $authors; }