/**
  * @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];
 }
예제 #2
0
 /**
  * @param Category $category
  * @return void
  */
 public function processDelete(Category $category)
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
     $resourceModel = $category->getResource();
     /**
      * Update children count for all parent categories
      */
     $parentIds = $category->getParentIds();
     if ($parentIds) {
         $childDecrease = $category->getChildrenCount() + 1;
         // +1 is itself
         $data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
         $where = ['entity_id IN(?)' => $parentIds];
         $resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
     }
 }
예제 #3
0
 /**
  * Retrieve category attributes
  *
  * @param string $attributeCode
  * @param int|array $categoryIds
  * @param int $storeId
  * @return array
  */
 protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
 {
     $linkField = $this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField();
     $identifierFiled = $this->metadataPool->getMetadata(CategoryInterface::class)->getIdentifierField();
     $connection = $this->getConnection();
     if (!isset($this->_categoryAttributes[$attributeCode])) {
         $attribute = $this->_catalogCategory->getResource()->getAttribute($attributeCode);
         $this->_categoryAttributes[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'is_static' => $attribute->isStatic()];
         unset($attribute);
     }
     if (!is_array($categoryIds)) {
         $categoryIds = [$categoryIds];
     }
     $attributeTable = $this->_categoryAttributes[$attributeCode]['table'];
     $select = $connection->select();
     $bind = [];
     if ($this->_categoryAttributes[$attributeCode]['is_static']) {
         $select->from($this->getTable('catalog_category_entity'), ['value' => $attributeCode, 'entity_id' => 'entity_id'])->where('entity_id IN(?)', $categoryIds);
     } elseif ($this->_categoryAttributes[$attributeCode]['is_global'] || $storeId == 0) {
         $select->from(['t1' => $this->getTable('catalog_category_entity')], [$identifierFiled])->joinLeft(['e' => $attributeTable], "t1.{$linkField} = e.{$linkField}", ['value'])->where("t1.{$identifierFiled} IN(?)", $categoryIds)->where('e.attribute_id = :attribute_id')->where('e.store_id = ?', 0);
         $bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
     } else {
         $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
         $select->from(['t1' => $attributeTable], [$identifierFiled => 'e.' . $identifierFiled, 'value' => $valueExpr])->joinLeft(['t2' => $attributeTable], "t1.{$linkField} = t2.{$linkField} AND t1.attribute_id = t2.attribute_id AND t2.store_id = :store_id", [])->joinLeft(['e' => $this->getTable('catalog_category_entity')], "e.{$linkField} = t1.{$linkField}", [])->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where("e.entity_id IN(?)", $categoryIds)->group('e.entity_id');
         $bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
         $bind['store_id'] = $storeId;
     }
     $rowSet = $connection->fetchAll($select, $bind);
     $attributes = [];
     foreach ($rowSet as $row) {
         $attributes[$row[$identifierFiled]] = $row['value'];
     }
     unset($rowSet);
     foreach ($categoryIds as $categoryId) {
         if (!isset($attributes[$categoryId])) {
             $attributes[$categoryId] = null;
         }
     }
     return $attributes;
 }
예제 #4
0
파일: Url.php 프로젝트: nja78/magento2
 /**
  * Retrieve category attributes
  *
  * @param string $attributeCode
  * @param int|array $categoryIds
  * @param int $storeId
  * @return array
  */
 protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
 {
     $adapter = $this->_getWriteAdapter();
     if (!isset($this->_categoryAttributes[$attributeCode])) {
         $attribute = $this->_catalogCategory->getResource()->getAttribute($attributeCode);
         $this->_categoryAttributes[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'is_static' => $attribute->isStatic()];
         unset($attribute);
     }
     if (!is_array($categoryIds)) {
         $categoryIds = [$categoryIds];
     }
     $attributeTable = $this->_categoryAttributes[$attributeCode]['table'];
     $select = $adapter->select();
     $bind = [];
     if ($this->_categoryAttributes[$attributeCode]['is_static']) {
         $select->from($this->getTable('catalog_category_entity'), ['value' => $attributeCode, 'entity_id' => 'entity_id'])->where('entity_id IN(?)', $categoryIds);
     } elseif ($this->_categoryAttributes[$attributeCode]['is_global'] || $storeId == 0) {
         $select->from($attributeTable, ['entity_id', 'value'])->where('attribute_id = :attribute_id')->where('store_id = ?', 0)->where('entity_id IN(?)', $categoryIds);
         $bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
     } else {
         $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
         $select->from(['t1' => $attributeTable], ['entity_id', 'value' => $valueExpr])->joinLeft(['t2' => $attributeTable], 't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id = :store_id', [])->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where('t1.entity_id IN(?)', $categoryIds);
         $bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
         $bind['store_id'] = $storeId;
     }
     $rowSet = $adapter->fetchAll($select, $bind);
     $attributes = [];
     foreach ($rowSet as $row) {
         $attributes[$row['entity_id']] = $row['value'];
     }
     unset($rowSet);
     foreach ($categoryIds as $categoryId) {
         if (!isset($attributes[$categoryId])) {
             $attributes[$categoryId] = null;
         }
     }
     return $attributes;
 }
 public function getObject(Category $category)
 {
     /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
     $productCollection = $category->getProductCollection();
     $productCollection = $productCollection->addMinimalPrice();
     $category->setProductCount($productCollection->getSize());
     $transport = new DataObject();
     $this->eventManager->dispatch('algolia_category_index_before', ['category' => $category, 'custom_data' => $transport]);
     $customData = $transport->getData();
     $storeId = $category->getStoreId();
     $category->getUrlInstance()->setStore($storeId);
     $path = '';
     foreach ($category->getPathIds() as $categoryId) {
         if ($path != '') {
             $path .= ' / ';
         }
         $path .= $this->getCategoryName($categoryId, $storeId);
     }
     $image_url = null;
     try {
         $image_url = $category->getImageUrl();
     } catch (\Exception $e) {
         /* no image, no default: not fatal */
     }
     $data = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['category'], 'popularity' => 1, 'product_count' => $category->getProductCount()];
     if (!empty($image_url)) {
         $data['image_url'] = $image_url;
     }
     foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) {
         $value = $category->getData($attribute['attribute']);
         $attribute_resource = $category->getResource()->getAttribute($attribute['attribute']);
         if ($attribute_resource) {
             $value = $attribute_resource->getFrontend()->getValue($category);
         }
         if (isset($data[$attribute['attribute']])) {
             $value = $data[$attribute['attribute']];
         }
         if ($value) {
             $data[$attribute['attribute']] = $value;
         }
     }
     $data = array_merge($data, $customData);
     foreach ($data as &$data0) {
         $data0 = $this->try_cast($data0);
     }
     return $data;
 }
예제 #6
0
 /**
  * Validate category process
  *
  * @param  Category $category
  * @return void
  * @throws \Magento\Framework\Model\Exception
  */
 protected function validateCategory(Category $category)
 {
     $useConfigFields = [];
     foreach ($this->useConfigFields as $field) {
         if (!$category->getData($field)) {
             $useConfigFields[] = $field;
         }
     }
     $category->setData('use_post_data_config', $useConfigFields);
     $validate = $category->validate();
     if ($validate !== true) {
         foreach ($validate as $code => $error) {
             if ($error === true) {
                 $attribute = $category->getResource()->getAttribute($code)->getFrontend()->getLabel();
                 throw new \Magento\Framework\Model\Exception(__('Attribute "%1" is required.', $attribute));
             } else {
                 throw new \Magento\Framework\Model\Exception($error);
             }
         }
     }
     $category->unsetData('use_post_data_config');
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function getResource()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getResource');
     if (!$pluginInfo) {
         return parent::getResource();
     } else {
         return $this->___callPlugins('getResource', func_get_args(), $pluginInfo);
     }
 }