Esempio n. 1
0
 /**
  * Get filter value for reset current filter state
  *
  * @return mixed
  */
 public function getResetValue()
 {
     if ($this->_appliedCategory) {
         /**
          * Revert path ids
          */
         $pathIds = array_reverse($this->_appliedCategory->getPathIds());
         $curCategoryId = $this->getLayer()->getCurrentCategory()->getId();
         if (isset($pathIds[1]) && $pathIds[1] != $curCategoryId) {
             return $pathIds[1];
         }
     }
     return null;
 }
 /**
  * @param Mage_Catalog_Model_Category $category
  * @return Mage_Catalog_Model_Category
  */
 public function getFirstLevelCategory($category)
 {
     if (!isset($this->_firstLevelCategories[$category->getId()])) {
         $rootId = Mage::app()->getStore()->getRootCategoryId();
         $id = $rootId;
         $rootFound = false;
         foreach ($category->getPathIds() as $pathId) {
             if ($rootFound) {
                 $id = $pathId;
                 break;
             } else {
                 if ($pathId == $rootId) {
                     $rootFound = true;
                 }
             }
         }
         if ($id == $category->getId()) {
             $this->_firstLevelCategories[$category->getId()] = $category;
         } elseif ($id == $rootId) {
             $this->_firstLevelCategories[$category->getId()] = $this->getRootCategory();
         } else {
             $this->_firstLevelCategories[$category->getId()] = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($id);
         }
     }
     return $this->_firstLevelCategories[$category->getId()];
 }
Esempio n. 3
0
 public function testGetPathIds()
 {
     $this->assertEquals(array(''), $this->_model->getPathIds());
     $this->_model->setPathIds(array(1));
     $this->assertEquals(array(1), $this->_model->getPathIds());
     $this->_model->unsetData();
     $this->_model->setPath('1/2/3');
     $this->assertEquals(array(1, 2, 3), $this->_model->getPathIds());
 }
 private function getLayoutHandles()
 {
     $handlePrefix = 'CATEGORY';
     //if we are taking a look at a category page we don't add handles
     if (in_array('catalog_product_view', $this->layout->getUpdate()->getHandles())) {
         $handlePrefix = 'PRODUCT_CATEGORY';
     }
     //We add the category paths here, ex: CATEGORY_123 and CATEGORY_12_child
     $path = array_reverse(array_slice($this->category->getPathIds(), 1, -1));
     foreach ($path as $id) {
         $elem = array_pop($path);
         $count = count($path);
         $handle = $handlePrefix . '_' . $elem;
         for ($i = 0; $i <= $count; $i++) {
             $handle .= '_child';
         }
         $this->addHandle($handle);
         $this->addHandle(substr($handle, 0, strrpos($handle, 'child')) . $this->category->getUrlKey());
     }
 }
 public function getObject(Mage_Catalog_Model_Category $category)
 {
     /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
     $productCollection = $category->getProductCollection();
     $productCollection = $productCollection->addMinimalPrice();
     $category->setProductCount($productCollection->getSize());
     $transport = new Varien_Object();
     Mage::dispatchEvent('algolia_category_index_before', array('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 = array('objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => Mage::getBaseUrl() . $category->getRequestPath(), '_tags' => array('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_ressource = $category->getResource()->getAttribute($attribute['attribute']);
         if ($attribute_ressource) {
             $value = $attribute_ressource->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;
 }
Esempio n. 6
0
 /**
  * Move category to another parent node
  *
  * @param Mage_Catalog_Model_Category $category
  * @param Mage_Catalog_Model_Category $newParent
  * @param null|int $afterCategoryId
  * @return Mage_Catalog_Model_Resource_Category
  */
 public function changeParent(Mage_Catalog_Model_Category $category, Mage_Catalog_Model_Category $newParent, $afterCategoryId = null)
 {
     $childrenCount = $this->getChildrenCount($category->getId()) + 1;
     $table = $this->getEntityTable();
     $adapter = $this->_getWriteAdapter();
     $levelFiled = $adapter->quoteIdentifier('level');
     $pathField = $adapter->quoteIdentifier('path');
     /**
      * Decrease children count for all old category parent categories
      */
     $adapter->update($table, array('children_count' => new Zend_Db_Expr('children_count - ' . $childrenCount)), array('entity_id IN(?)' => $category->getParentIds()));
     /**
      * Increase children count for new category parents
      */
     $adapter->update($table, array('children_count' => new Zend_Db_Expr('children_count + ' . $childrenCount)), array('entity_id IN(?)' => $newParent->getPathIds()));
     $position = $this->_processPositions($category, $newParent, $afterCategoryId);
     $newPath = sprintf('%s/%s', $newParent->getPath(), $category->getId());
     $newLevel = $newParent->getLevel() + 1;
     $levelDisposition = $newLevel - $category->getLevel();
     /**
      * Update children nodes path
      */
     $adapter->update($table, array('path' => new Zend_Db_Expr('REPLACE(' . $pathField . ',' . $adapter->quote($category->getPath() . '/') . ', ' . $adapter->quote($newPath . '/') . ')'), 'level' => new Zend_Db_Expr($levelFiled . ' + ' . $levelDisposition)), array($pathField . ' LIKE ?' => $category->getPath() . '/%'));
     /**
      * Update moved category data
      */
     $data = array('path' => $newPath, 'level' => $newLevel, 'position' => $position, 'parent_id' => $newParent->getId());
     $adapter->update($table, $data, array('entity_id = ?' => $category->getId()));
     // Update category object to new data
     $category->addData($data);
     return $this;
 }
Esempio n. 7
0
 /**
  * Return parent category of current category with own custom design settings
  *
  * @param Mage_Catalog_Model_Category $category
  * @return Mage_Catalog_Model_Category
  */
 public function getParentDesignCategory($category)
 {
     $adapter = $this->_getReadAdapter();
     $levelField = $adapter->quoteIdentifier('level');
     $pathIds = array_reverse($category->getPathIds());
     $select = $adapter->select()->from(array('main_table' => $this->getMainStoreTable($category->getStoreId())), '*')->where('entity_id IN (?)', $pathIds)->where('custom_use_parent_settings = ?', 0)->where($levelField . ' != ?', 0)->order('level ' . Varien_Db_Select::SQL_DESC);
     $result = $adapter->fetchRow($select);
     return Mage::getModel('catalog/category')->setData($result);
 }
 /**
  * Get store identifiers where category is presented
  *
  * @deprecated after 1.3.2.2 moved to model
  * @param   Mage_Catalog_Model_Category $category
  * @return  array
  */
 public function getStoreIds($category)
 {
     if (!$category->getId()) {
         return array();
     }
     $nodes = array();
     foreach ($category->getPathIds() as $id) {
         $nodes[] = $id;
     }
     $stores = array();
     $storeCollection = Mage::getModel('core/store')->getCollection()->loadByCategoryIds($nodes);
     foreach ($storeCollection as $store) {
         $stores[$store->getId()] = $store->getId();
     }
     $entityStoreId = $category->getStoreId();
     if (!in_array($entityStoreId, $stores)) {
         array_unshift($stores, $entityStoreId);
     }
     if (!in_array(0, $stores)) {
         array_unshift($stores, 0);
     }
     return $stores;
 }
 /**
  * Return all parent categories for the given category
  * 
  * @param Mage_Catalog_Model_Category $category
  * @return Mage_Catalog_Model_Category[]
  */
 public function getParentCategoriesForCategory(Mage_Catalog_Model_Category $category)
 {
     if (!isset($this->_parents[$category->getId()])) {
         $parent = null;
         $parents = array();
         foreach ($category->getPathIds() as $parentId) {
             if ($parent = $this->getAllParentCategories()->getItemById($parentId)) {
                 // Set flag for css class
                 if (empty($parents)) {
                     $parent->setIsBase(true);
                 }
                 $parents[] = $parent;
             }
         }
         if ($parent) {
             $parent->setIsHead(true);
         }
         $this->_parents[$category->getId()] = $parents;
     }
     // Set flag for css class and separator
     return $this->_parents[$category->getId()];
 }
 /**
  * @param Mage_Catalog_Model_Category $category
  * @return string
  */
 protected function _getCategoryPath($category)
 {
     $categoryPathIds = $category->getPathIds();
     array_shift($categoryPathIds);
     $categoryNames = array();
     foreach ($categoryPathIds as $categoryId) {
         $categoryNames[] = Mage::getResourceSingleton('catalog/category')->getAttributeRawValue($categoryId, 'name', $category->getStoreId());
     }
     return implode(' > ', $categoryNames);
 }
 /**
  * Prepare category JSON
  *
  * @param Mage_Catalog_Model_Category $category
  * @return array
  */
 public function getCategoryJSON(Mage_Catalog_Model_Category $category)
 {
     $transport = new Varien_Object();
     Mage::dispatchEvent('algolia_category_index_before', array('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);
     }
     $imageUrl = NULL;
     try {
         $imageUrl = $category->getImageUrl();
     } catch (Exception $e) {
         /* no image, no default: not fatal */
     }
     $data = array('objectID' => $this->getCategoryObjectId($category), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), '_tags' => array('category'), 'popularity' => 1);
     if ($this->isIndexProductCount()) {
         $data['product_count'] = $data['popularity'] = $category->getProductCount();
     }
     if (!empty($imageUrl)) {
         $data['image_url'] = $imageUrl;
     }
     foreach ($this->getCategoryAdditionalAttributes($storeId) as $attributeCode) {
         $value = $category->hasData($this->_dataPrefix . $attributeCode) ? $category->getData($this->_dataPrefix . $attributeCode) : $category->getData($attributeCode);
         $value = Mage::getResourceSingleton('algoliasearch/fulltext')->getAttributeValue($attributeCode, $value, $storeId, Mage_Catalog_Model_Category::ENTITY);
         if ($value) {
             $data[$attributeCode] = $value;
         }
     }
     $data = array_merge($data, $customData);
     return $data;
 }
Esempio n. 12
0
 /**
  * @param Mage_Catalog_Model_Category $category
  * @param Mage_Catalog_Model_Resource_Category_Collection $categoryCollection
  * @return array
  */
 protected function _prepareData($category, $categoryCollection = null)
 {
     return array("id" => $category->getId(), "path" => $category->getData("path"), self::PATH_NAMES => $this->_getNames($category->getPathIds(), $categoryCollection));
 }