/** * Update product categories * * @param \XLite\Model\Product $model Product model * @param array $categoryIds List of IDs of new categories * * @return void */ protected function updateProductCategories($model, $categoryIds) { $categoriesToRemoveCache = array(); // List of old category IDs $oldCategoryIds = array(); // Get old category IDs list $oldCategoryProducts = $model->getCategoryProducts()->toArray(); if (!empty($oldCategoryProducts)) { foreach ($oldCategoryProducts as $cp) { $oldCategoryIds[] = $cp->getCategory()->getCategoryId(); if (!in_array($cp->getCategory()->getCategoryId(), $categoryIds)) { $categoriesToRemoveCache[] = $cp->getCategory()->getCategoryId(); } } } $categoriesToRemoveCache = array_merge($categoriesToRemoveCache, array_diff($categoryIds, $oldCategoryIds)); if ($categoriesToRemoveCache) { \XLite\Core\Database::getRepo('XLite\\Model\\Category')->removeProductFilterCache($categoriesToRemoveCache); } parent::updateProductCategories($model, $categoryIds); }
/** * Import 'categories' value * * @param \XLite\Model\Product $model Product * @param mixed $value Value * @param array $column Column info * * @return void */ protected function importCategoriesColumn(\XLite\Model\Product $model, $value, array $column) { $position = array(); foreach ($model->getCategoryProducts() as $link) { $position[$link->getCategory()->getCategoryId()] = $link->getOrderby(); } \XLite\Core\Database::getRepo('\\XLite\\Model\\CategoryProducts')->deleteInBatch($model->getCategoryProducts()->toArray()); $model->getCategoryProducts()->clear(); foreach (array_unique($value) as $path) { $category = $this->addCategoryByPath($path); $link = \XLite\Core\Database::getRepo('\\XLite\\Model\\CategoryProducts')->findOneBy(array('category' => $category, 'product' => $model)); if (!$link) { $link = new \XLite\Model\CategoryProducts(); $link->setProduct($model); $link->setCategory($category); if (isset($position[$category->getCategoryId()])) { $link->setOrderby($position[$category->getCategoryId()]); } $model->addCategoryProducts($link); \XLite\Core\Database::getEM()->persist($link); } } }
/** * {@inheritDoc} */ public function getCategoryProducts() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getCategoryProducts', array()); return parent::getCategoryProducts(); }
/** * Import categories * * @param \XLite\Model\Product $product Product * @param string $data Data * * @return void */ protected function importCategories(\XLite\Model\Product $product, $data) { $oldLinks = array(); foreach ($product->getCategoryProducts() as $link) { $oldLinks[] = $link->getId(); } if ($data) { $root = \XLite\Core\Database::getRepo('XLite\\Model\\Category')->find(\XLite\Core\Database::getRepo('XLite\\Model\\Category')->getRootCategoryId()); foreach (explode(';', $data) as $path) { $path = trim($path); // Detect category $parent = $root; $category = null; foreach (explode('/', $path) as $name) { $name = trim($name); $category = null; foreach ($parent->getChildren() as $cat) { if ($cat->getName() == $name) { $category = $cat; break; } } if (!$category) { $category = \XLite\Core\Database::getRepo('\\XLite\\Model\\Category')->insert(array('parent_id' => $parent->getCategoryId(), 'name' => $name)); $parent->addChildren($category); } $parent = $category; } if ($category) { // Add link to category $link = null; foreach ($product->getCategoryProducts() as $cp) { if ($cp->getCategory()->getCategoryId() == $category->getCategoryId()) { $link = $cp; $key = array_search($link->getId(), $oldLinks); unset($oldLinks[$key]); break; } } if (!$link) { $link = new \XLite\Model\CategoryProducts(); $link->setProduct($product); $link->setCategory($category); $product->addCategoryProducts($link); \XLite\Core\Database::getEM()->persist($link); } } } } foreach ($product->getCategoryProducts() as $link) { if (in_array($link->getId(), $oldLinks)) { $product->getCategoryProducts()->removeElement($link); \XLite\Core\Database::getEM()->remove($link); } } }
/** * Update product categories * * @param \XLite\Model\Product $model Product model * @param array $categoryIds List of IDs of new categories * * @return void */ protected function updateProductCategories($model, $categoryIds) { // List of old category IDs $oldCategoryIds = array(); // Get old category IDs list $oldCategoryProducts = $model->getCategoryProducts()->toArray(); if (!empty($oldCategoryProducts)) { $categoriesToDelete = array(); foreach ($oldCategoryProducts as $cp) { $oldCategoryIds[] = $cp->getCategory()->getCategoryId(); if (!in_array($cp->getCategory()->getCategoryId(), $categoryIds)) { // Add old category to the remove queue $categoriesToDelete[] = $cp; } } if ($categoriesToDelete) { // Remove links between product and old categories \XLite\Core\Database::getRepo('XLite\\Model\\CategoryProducts')->deleteInBatch($categoriesToDelete); } } // Get list of category IDs which must be added to product $categoriesToAdd = array_diff($categoryIds, $oldCategoryIds); // Get list of categories (entities) from category IDs $categories = \XLite\Core\Database::getRepo('XLite\\Model\\Category')->findByIds($categoryIds); // Get list of category products $categoryProducts = $this->getCategoryProducts($model, $categories, $categoriesToAdd); if ($categoryProducts) { // Update category products list \XLite\Core\Database::getRepo('XLite\\Model\\Product')->update($model, array('categoryProducts' => $categoryProducts)); } }