/** * Build response for ajax request * * @param \Magento\Catalog\Model\Category $category * @param \Magento\Backend\Model\View\Result\Page $resultPage * * @return \Magento\Framework\Controller\Result\Json * * @deprecated */ protected function ajaxRequestResponse($category, $resultPage) { // prepare breadcrumbs of selected category, if any $breadcrumbsPath = $category->getPath(); if (empty($breadcrumbsPath)) { // but if no category, and it is deleted - prepare breadcrumbs from path, saved in session $breadcrumbsPath = $this->_objectManager->get('Magento\\Backend\\Model\\Auth\\Session')->getDeletedPath(true); if (!empty($breadcrumbsPath)) { $breadcrumbsPath = explode('/', $breadcrumbsPath); // no need to get parent breadcrumbs if deleting category level 1 if (count($breadcrumbsPath) <= 1) { $breadcrumbsPath = ''; } else { array_pop($breadcrumbsPath); $breadcrumbsPath = implode('/', $breadcrumbsPath); } } } $eventResponse = new \Magento\Framework\DataObject(['content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml() . $resultPage->getLayout()->getBlock('category.tree')->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'), 'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(), 'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()]); $this->_eventManager->dispatch('category_prepare_ajax_response', ['response' => $eventResponse, 'controller' => $this]); /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->_objectManager->get('Magento\\Framework\\Controller\\Result\\Json'); $resultJson->setHeader('Content-type', 'application/json', true); $resultJson->setData($eventResponse->getData()); return $resultJson; }
/** * Creates a new child category for $parentCategory * * @param \Magento\Catalog\Model\Category $parentCategory * @return \Magento\Catalog\Model\Category */ protected function createCategory($parentCategory) { /** @var \Magento\Catalog\Model\Category $category */ $category = $this->objectManager->create('Magento\\Catalog\\Model\\Category'); $category->setStoreId(self::DEFAULT_STORE_ID)->setParentId($parentCategory->getId())->setName(self::NAMES_PREFIX . $this->titlesGenerator->generateCategoryTitle())->setAttributeSetId($category->getDefaultAttributeSetId())->setLevel($parentCategory->getLevel() + 1)->setPath($parentCategory->getPath())->setIsActive(1)->save(); return $category; }
/** * @param Category $category * @return string */ protected function buildPath(Category $category) { $data = []; $path = $category->getPath(); foreach (explode('/', $path) as $categoryId) { $category = $this->_categoryRepository->get($categoryId); if ($category && $category->getLevel() > 1) { $data[] = $category->getName(); } } return count($data) ? '/' . implode('/', $data) : ''; }
/** * @param \Magento\Catalog\Model\Category $category * @param boolean $recursive * @return int[] */ public function getChildrenIds(Category $category, $recursive = false) { $cacheKey = $category->getId() . '_' . (int) $recursive; if (!isset($this->childrenIds[$cacheKey])) { $connection = $category->getResource()->getReadConnection(); $select = $connection->select()->from($category->getResource()->getEntityTable(), 'entity_id')->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); $bind = ['c_path' => $category->getPath() . '/%']; if (!$recursive) { $select->where($connection->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } $this->childrenIds[$cacheKey] = $connection->fetchCol($select, $bind); } return $this->childrenIds[$cacheKey]; }
/** * Return children ids of category * * @param \Magento\Catalog\Model\Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = (int) $this->_getIsActiveAttributeId(); $backendTable = $this->getTable(array($this->getEntityTablePrefix(), 'int')); $adapter = $this->_getReadAdapter(); $checkSql = $adapter->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $bind = array('attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1, 'c_path' => $category->getPath() . '/%'); $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), 'entity_id')->joinLeft(array('d' => $backendTable), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = m.entity_id', array())->joinLeft(array('c' => $backendTable), 'c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = m.entity_id', array())->where($checkSql . ' = :scope')->where($adapter->quoteIdentifier('path') . ' LIKE :c_path'); if (!$recursive) { $select->where($adapter->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } return $adapter->fetchCol($select, $bind); }
/** * Return children ids of category * * @param \Magento\Catalog\Model\Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $linkField = $this->getLinkField(); $attributeId = $this->getIsActiveAttributeId(); $backendTable = $this->getTable([$this->getEntityTablePrefix(), 'int']); $connection = $this->getConnection(); $checkSql = $connection->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $linkField = $this->getLinkField(); $bind = ['attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1, 'c_path' => $category->getPath() . '/%']; $select = $this->getConnection()->select()->from(['m' => $this->getEntityTable()], 'entity_id')->joinLeft(['d' => $backendTable], "d.attribute_id = :attribute_id AND d.store_id = 0 AND d.{$linkField} = m.{$linkField}", [])->joinLeft(['c' => $backendTable], "c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.{$linkField} = m.{$linkField}", [])->where($checkSql . ' = :scope')->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); if (!$recursive) { $select->where($connection->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } return $connection->fetchCol($select, $bind); }
/** * Return children ids of category * * @param \Magento\Catalog\Model\Category $category * @param bool $recursive * @param bool $isActive * @return array */ public function getChildren($category, $recursive = true, $isActive = true) { $select = $this->_getReadAdapter()->select()->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')->where('path LIKE ?', "{$category->getPath()}/%"); if (!$recursive) { $select->where('level <= ?', $category->getLevel() + 1); } if ($isActive) { $select->where('is_active = ?', '1'); } $_categories = $this->_getReadAdapter()->fetchAll($select); $categoriesIds = []; foreach ($_categories as $_category) { $categoriesIds[] = $_category['entity_id']; } return $categoriesIds; }
/** * Return a mini-breadcrumb for a category * * @param \Magento\Catalog\Model\Category $category The category * * @return array */ private function getCategoryBreadcrumb(\Magento\Catalog\Model\Category $category) { $path = $category->getPath(); $rawPath = explode('/', $path); // First occurence is root category (1), second is root category of store. $rawPath = array_slice($rawPath, 2); // Last occurence is the category displayed. array_pop($rawPath); $breadcrumb = []; foreach ($rawPath as $categoryId) { $breadcrumb[] = html_entity_decode($this->getCategoryNameById($categoryId, $category->getStoreId())); } return $breadcrumb; }
/** * @param Category $parentCategory * @param array $children */ public function addCategoryTreeChildren(Category $parentCategory, array $children) { foreach ($children as $index => $categoryData) { // Prepare Data $category = $this->categoryFactory->create(); /** @var \Magento\Catalog\Model\Category\ $category */ // Set Data $category->addData($this->categorySkeleton)->setName($categoryData['name'])->setPath($parentCategory->getPath())->setIsAnchor(isset($categoryData['children']))->setPosition($index + 1)->save(); // Recursive if (isset($categoryData['children']) && is_array($categoryData['children'])) { $this->addCategoryTreeChildren($category, $categoryData['children']); } // Store Names if (isset($categoryData['store_name']) && is_array($categoryData['store_name'])) { foreach ($categoryData['store_name'] as $code => $name) { $store = $this->getStore($code); if (!$store->getId()) { $this->logger->warning(__('There is no Store with code: %1', $code)); continue; } $category->setStoreId($store->getId())->setName($name)->save(); } } } }
/** * {@inheritdoc} */ public function getPath() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getPath'); if (!$pluginInfo) { return parent::getPath(); } else { return $this->___callPlugins('getPath', func_get_args(), $pluginInfo); } }
/** * Convert category to array * * @param \Magento\Catalog\Model\Category $category * @param \Magento\Catalog\Model\Category $currentCategory * @return array */ private function getCategoryAsArray($category, $currentCategory) { return ['name' => $category->getName(), 'id' => 'category-node-' . $category->getId(), 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => in_array((string) $category->getId(), explode('/', $currentCategory->getPath()), true), 'is_active' => $category->getId() == $currentCategory->getId()]; }