protected function _hasCustomUpdate(Mage_Catalog_Model_Category $category)
 {
     if ($category->getData('custom_layout_update') && in_array($category->getCustomDesignApply(), array(Mage_Catalog_Model_Design::CATEGORY_APPLY_CATEGORY_AND_PRODUCT_RECURSIVE, Mage_Catalog_Model_Design::CATEGORY_APPLY_CATEGORY_RECURSIVE))) {
         return true;
     }
     return false;
 }
 /**
  * Search category by the name from root category or specified one.
  * Create category when it doesn't exist if $createIfNotExists
  * parameter is set.
  * Search category by store-specific name if $store parameter is set.
  *
  * @param string $name
  * @param bool $createIfNotExists
  * @param Mage_Catalog_Model_Category $parent
  * @param null|Mage_Core_Model_Store $store
  *
  * @return Mage_Catalog_Model_Category|null
  */
 public function getCategory($name, $createIfNotExists = false, $parent = null, $store = null)
 {
     $store = $store instanceof Mage_Core_Model_Store ? $store : Mage::app()->getStore();
     $collection = $parent && ($parentId = $parent->getId()) ? $parent->setStoreId($store->getId())->getCollection()->addFieldToFilter('parent_id', $parentId) : Mage::getModel('catalog/category')->setStoreId($store->getId())->load($store->getRootCategoryId())->getCollection();
     $collection->addAttributeToFilter('name', $name);
     if ($collection->count()) {
         return $collection->getFirstItem();
     }
     if (!$createIfNotExists) {
         return;
     }
     if ($parent && $parent->getId()) {
         $rootCategory = $parent;
     } else {
         $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('parent_id', 1);
         if (count($collection) != 1) {
             return null;
         }
         $rootCategory = $collection->getFirstItem();
         if (!$rootCategory->getId()) {
             return null;
         }
     }
     $model = Mage::getModel('catalog/category');
     $model->setStoreId($rootCategory->getStoreId())->setData(array('name' => $name, 'is_active' => 1, 'include_in_menu' => 1))->setPath($rootCategory->getPath())->setAttributeSetId($model->getDefaultAttributeSetId());
     try {
         $model->save();
     } catch (Exception $e) {
         return null;
     }
     return $model;
 }
 /**
  * Recursively apply custom design settings to product if it's container
  * category custom_use_for_products option is setted to 1.
  * If not or product shows not in category - applyes product's internal settings
  *
  * @deprecated after 1.4.2.0-beta1, functionality moved to Mage_Catalog_Model_Design
  * @param Mage_Catalog_Model_Category|Mage_Catalog_Model_Product $object
  * @param Mage_Core_Model_Layout_Update $update
  */
 protected function _applyCustomDesignSettings($object, $update)
 {
     if ($object instanceof Mage_Catalog_Model_Category) {
         // lookup the proper category recursively
         if ($object->getCustomUseParentSettings()) {
             $parentCategory = $object->getParentCategory();
             if ($parentCategory && $parentCategory->getId() && $parentCategory->getLevel() > 1) {
                 $this->_applyCustomDesignSettings($parentCategory, $update);
             }
             return;
         }
         // don't apply to the product
         if (!$object->getCustomApplyToProducts()) {
             return;
         }
     }
     if ($this->_designProductSettingsApplied) {
         return;
     }
     $date = $object->getCustomDesignDate();
     if (array_key_exists('from', $date) && array_key_exists('to', $date) && Mage::app()->getLocale()->isStoreDateInInterval(null, $date['from'], $date['to'])) {
         if ($object->getPageLayout()) {
             $this->_designProductSettingsApplied['layout'] = $object->getPageLayout();
         }
         $this->_designProductSettingsApplied['update'] = $object->getCustomLayoutUpdate();
     }
 }
Example #4
0
 protected function _getProductCollection()
 {
     if (is_null($this->_productCollection)) {
         $categoryID = $this->getCategoryId();
         if ($categoryID) {
             $category = new Mage_Catalog_Model_Category();
             $category->load($categoryID);
             // this is category id
             $collection = $category->getProductCollection();
         } else {
             $collection = Mage::getResourceModel('catalog/product_collection');
         }
         $todayDate = date('m/d/y');
         $tomorrow = mktime(0, 0, 0, date('m'), date('d') + 1, date('y'));
         $tomorrowDate = date('m/d/y', $tomorrow);
         Mage::getModel('catalog/layer')->prepareProductCollection($collection);
         //$collection->getSelect()->order('rand()');
         $collection->addAttributeToSort('created_at', 'desc');
         $collection->addStoreFilter();
         $collection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))->addAttributeToFilter('special_to_date', array('or' => array(0 => array('date' => true, 'from' => $tomorrowDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left');
         $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
         $collection->setPage(1, $numProducts)->load();
         $this->_productCollection = $collection;
     }
     return $this->_productCollection;
 }
 protected function _reindexCategoryUrlKey(Mage_Catalog_Model_Category $category, Mage_Core_Model_Store $store)
 {
     $requestPath = trim($category->getParentUrl(), '/');
     if (Mage::getStoreConfig('catalog/seo/category_use_parent_category')) {
         $requestPath = (!empty($requestPath) ? $requestPath . '/' : '') . $category->getUrlKey();
     } else {
         $requestPath = $category->getUrlKey();
     }
     $requestPath = $this->_cutRequestPath($requestPath);
     $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store);
     if (empty($urlKeyValue) && empty($urlKeyValue['value_id'])) {
         $category = $this->_setUrlKeyForDefaultStore($category, $store);
         $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store);
     }
     $valueId = $urlKeyValue['value_id'];
     $rewriteRow = $this->_getRewrite($requestPath, $store->getId());
     if (!$rewriteRow || $rewriteRow['value_id'] != $valueId) {
         $rewriteForValueId = $this->_getRewriteForValueId($store->getId(), $valueId);
         $suffix = trim(str_replace($requestPath, '', $category->getRequestPath()), '-');
         $requestPathIncrement = (int) $this->_getRewriteRequestIncrement($requestPath, $store);
         if (!$rewriteForValueId || !preg_match('#^(\\d)+$#', $suffix) || $suffix > $requestPathIncrement) {
             if ($rewriteRow && $rewriteRow['value_id'] != $valueId) {
                 $requestPath .= '-' . ++$requestPathIncrement;
             }
             $category = $this->_saveRewrite($category, $store, $requestPath, $valueId);
         }
     }
     $this->_indexedCategoryIds[$store->getId()][$category->getId()] = 1;
     return $category;
 }
Example #6
0
 /**
  * Checks if a category matches criteria: active && url_key not null && included in menu if it has to
  */
 protected static function isCategoryAcceptable(Mage_Catalog_Model_Category $category = null, $mustBeIncludedInNavigation = true)
 {
     if (!$category->getIsActive() || is_null($category->getUrlKey()) || $mustBeIncludedInNavigation && !$category->getIncludeInMenu()) {
         return false;
     }
     return true;
 }
Example #7
0
 protected function setUp()
 {
     $this->_category = Mage::getModel('Mage_Catalog_Model_Category');
     $this->_category->load(5);
     $this->_model = Mage::getModel('Mage_Catalog_Model_Layer_Filter_Category');
     $this->_model->setData(array('layer' => Mage::getModel('Mage_Catalog_Model_Layer', array('data' => array('current_category' => $this->_category)))));
 }
Example #8
0
 /**
  * Retrieve loaded category collection.
  * Variables collected from CMS markup: category_id, product_count, is_random
  */
 protected function _getProductCollection()
 {
     if (is_null($this->_productCollection)) {
         $categoryID = $this->getCategoryId();
         if ($categoryID) {
             $category = new Mage_Catalog_Model_Category();
             $category->load($categoryID);
             $collection = $category->getProductCollection();
             //Sort order parameters
             $sortBy = $this->getSortBy();
             //param: sort_by
             if ($sortBy === NULL) {
                 $sortBy = 'position';
             }
             $sortDirection = $this->getSortDirection();
             //param: sort_direction
             if ($sortDirection === NULL) {
                 $sortDirection = 'ASC';
             }
             $collection->addAttributeToSort($sortBy, $sortDirection);
         } else {
             $collection = Mage::getResourceModel('catalog/product_collection');
         }
         Mage::getModel('catalog/layer')->prepareProductCollection($collection);
         if ($this->getIsRandom()) {
             $collection->getSelect()->order('rand()');
         }
         $collection->addStoreFilter();
         $productCount = $this->getProductCount() ? $this->getProductCount() : 8;
         $collection->setPage(1, $productCount)->load();
         $this->_productCollection = $collection;
     }
     return $this->_productCollection;
 }
Example #9
0
 protected function setUp()
 {
     $category = new Mage_Catalog_Model_Category();
     $category->load(4);
     $this->_model = new Mage_Catalog_Model_Layer_Filter_Price();
     $this->_model->setData(array('layer' => new Mage_Catalog_Model_Layer(array('current_category' => $category))));
 }
 /**
  * @magentoDataFixture Mage/Core/_files/store.php
  * @magentoDbIsolation enabled
  * @dataProvider saveActionDataProvider
  * @param array $inputData
  * @param array $defaultAttributes
  * @param array $attributesSaved
  */
 public function testSaveAction($inputData, $defaultAttributes, $attributesSaved = array())
 {
     $store = new Mage_Core_Model_Store();
     $store->load('fixturestore', 'code');
     $storeId = $store->getId();
     $this->getRequest()->setPost($inputData);
     $this->getRequest()->setParam('store', $storeId);
     $this->getRequest()->setParam('id', 2);
     $this->dispatch('backend/admin/catalog_category/save');
     $messages = Mage::getSingleton('Mage_Backend_Model_Session')->getMessages(false)->getItemsByType(Mage_Core_Model_Message::SUCCESS);
     $this->assertNotEmpty($messages, "Could not save category");
     $this->assertEquals('The category has been saved.', current($messages)->getCode());
     $category = new Mage_Catalog_Model_Category();
     $category->setStoreId($storeId);
     $category->load(2);
     $errors = array();
     foreach ($attributesSaved as $attribute => $value) {
         $actualValue = $category->getData($attribute);
         if ($value !== $actualValue) {
             $errors[] = "value for '{$attribute}' attribute must be '{$value}', but '{$actualValue}' is found instead";
         }
     }
     foreach ($defaultAttributes as $attribute => $exists) {
         if ($exists !== $category->getExistsStoreValueFlag($attribute)) {
             if ($exists) {
                 $errors[] = "custom value for '{$attribute}' attribute is not found";
             } else {
                 $errors[] = "custom value for '{$attribute}' attribute is found, but default one must be used";
             }
         }
     }
     $this->assertEmpty($errors, "\n" . join("\n", $errors));
 }
Example #11
0
 /**
  * Retrieve category url
  *
  * @param   Mage_Catalog_Model_Category $category
  * @return  string
  */
 public function getCategoryUrl($category)
 {
     if ($category instanceof Mage_Catalog_Model_Category) {
         return $category->getUrl();
     }
     return Mage::getModel('catalog/category')->setData($category->getData())->getUrl();
 }
 /**
  * Reindex a single virtual category.
  *
  * @param Mage_Catalog_Model_Category $category The category.
  *
  * @return void
  */
 public function reindex($category)
 {
     /** Reindex all data from virtual categories products positions index */
     $engine = Mage::helper('catalogsearch')->getEngine();
     $mapping = $engine->getCurrentIndex()->getMapping('product');
     $dataprovider = $mapping->getDataProvider('virtual_categories_products_position');
     $dataprovider->updateAllData($category->getStoreId(), $category->getVirtualProductIds());
 }
Example #13
0
 /**
  * Save the collection array as object property
  * Added name and url_key as part of the array.
  */
 private function _initProductCollectionArray()
 {
     if (!$this->_collectionArray && !$this->_doNothing) {
         $productCollection = $this->_category->getProductCollection();
         $productCollection->addAttributeToSelect(array('entity_id', 'name', 'url_key'))->addAttributeToFilter('status', 1)->addAttributeToFilter('visibility', 4)->addCategoryFilter($this->_category)->setOrder('entity_id', 'ASC');
         $this->_collectionArray = $productCollection->exportToArray();
     }
 }
 /**
  * Take a Mage_Catalog_Model_Category object and build a category tree from
  * the child leaf to the root of the category (root > inner child > inner most child)
  * @param Mage_Catalog_Model_Category $category the inner most child
  * @return string
  */
 protected function _buildCategoryTree(Mage_Catalog_Model_Category $category)
 {
     $collecton = $this->_getCategoriesByIds(explode('/', $category->getPath()));
     $categories = array();
     foreach ($collecton as $cat) {
         $categories[] = $cat->getName();
     }
     return implode(' > ', array_filter($categories));
 }
Example #15
0
 /**
  * Check if product is inside of the category
  * 
  * @param  Mage_Catalog_Model_Product   $product  
  * @param  Mage_Catalog_Model_Category  $category 
  * @return boolean
  */
 private function _isProductInCategory($product, $category)
 {
     $categoryIds = $product->getCategoryIds();
     $categoryId = $category->getId();
     if (in_array($categoryId, $categoryIds)) {
         return true;
     }
     return false;
 }
Example #16
0
 /**
  * Get static block identifier
  *
  * @param Mage_Catalog_Model_Category $category
  *
  * @return string
  */
 protected function getCmsIdentifier(Mage_Catalog_Model_Category $category)
 {
     $cmsBlockId = intval($category->getLandingPage());
     if (is_integer($cmsBlockId) && $cmsBlockId > 0) {
         $cmsBlock = Mage::getModel('cms/block')->load($cmsBlockId);
         return $cmsBlock->getIdentifier();
     }
     return NULL;
 }
Example #17
0
 /**
  * Check if click able enabled for category.
  *
  * @param Varien_Data_Tree_Node|Mage_Catalog_Model_Category $category
  * @return bool
  */
 protected function _isCategoryClickAble($category)
 {
     $isCategoryClickAbleStatus = true;
     $isCategoryClickAble = $category->getData(Monsoon_Test_Helper_Data::IS_CLICK_ABLE_LINK_CODE);
     if ($isCategoryClickAble !== null && (bool) $isCategoryClickAble === false) {
         $isCategoryClickAbleStatus = false;
     }
     return $isCategoryClickAbleStatus;
 }
Example #18
0
 public function testGetUrlPath()
 {
     $product = new Mage_Catalog_Model_Product();
     $product->setUrlPath('product.html');
     $category = new Mage_Catalog_Model_Category();
     $category->setUrlPath('category.html');
     $this->assertEquals('product.html', $this->_model->getUrlPath($product));
     $this->assertEquals('category/product.html', $this->_model->getUrlPath($product, $category));
 }
 /**
  * Get url for category data
  *
  * @param Mage_Catalog_Model_Category $category
  * @return string
  */
 public function getCategoryUrl($category)
 {
     if ($category instanceof Mage_Catalog_Model_Category) {
         $url = $category->getUrl();
     } else {
         $url = $this->_getCategoryInstance()->setData($category->getData())->getUrl();
     }
     return $url;
 }
Example #20
0
 protected function setUp()
 {
     $category = new Mage_Catalog_Model_Category();
     $category->load(4);
     $attribute = new Mage_Catalog_Model_Entity_Attribute();
     $attribute->loadByCode('catalog_product', 'weight');
     $this->_model = new Mage_Catalog_Model_Layer_Filter_Decimal();
     $this->_model->setData(array('layer' => new Mage_Catalog_Model_Layer(array('current_category' => $category)), 'attribute_model' => $attribute));
 }
Example #21
0
 /**
  * Returns list of subcategories recursively.
  *
  * @param Mage_Catalog_Model_Category $category
  * @return mixed
  */
 protected function getSubcategories(Mage_Catalog_Model_Category $category)
 {
     if (!isset($this->subcategories[$category->getId()])) {
         $list = array();
         $categories = $category->getChildrenCategories();
         $this->getAllChildCategories($categories, $list);
         $this->subcategories[$category->getId()] = $list;
     }
     return $this->subcategories[$category->getId()];
 }
Example #22
0
 /**
  * @test
  */
 public function testCatalogCategorySaveAfter()
 {
     $this->_fpc->save('category1', 'category1_cache_id', array(sha1('category_1')));
     $this->_fpc->save('category2', 'category2_cache_id', array(sha1('category_2')));
     $category = new Mage_Catalog_Model_Category();
     $category->setId(1);
     Mage::dispatchEvent('catalog_category_save_after', array('category' => $category));
     $this->assertFalse($this->_fpc->load('category1_cache_id'));
     $this->assertEquals('category2', $this->_fpc->load('category2_cache_id'));
 }
Example #23
0
 /**
  * Should return list of tags to clean
  *
  * @param Mage_Catalog_Model_Category $object
  * @return string[]|string
  */
 protected function _collectTags($object)
 {
     $tags = array(self::TAG_PREFIX . $object->getId());
     if ($this->_isForUpdate) {
         foreach ($object->getParentIds() as $categoryId) {
             $tags[] = self::TAG_PREFIX . $categoryId;
         }
     }
     return $tags;
 }
 /**
  * Purge Category
  *
  * @param Mage_Catalog_Model_Category $category
  * @return Phoenix_VarnishCache_Model_Control_Catalog_Category
  */
 public function purge(Mage_Catalog_Model_Category $category)
 {
     if ($this->_canPurge()) {
         $this->_purgeById($category->getId());
         if ($categoryName = $category->getName()) {
             $this->_getSession()->addSuccess(Mage::helper('varnishcache')->__('Varnish cache for "%s" has been purged.', $categoryName));
         }
     }
     return $this;
 }
Example #25
0
 /**
  * Check unique url_key value in catalog_category_entity_url_key table.
  *
  * @param Mage_Catalog_Model_Category $object
  * @return Mage_Catalog_Model_Category_Attribute_Backend_Urlkey
  * @throws Mage_Core_Exception
  */
 protected function _validateEntityUrl($object)
 {
     $connection = $object->getResource()->getReadConnection();
     $select = $connection->select()->from($this->getAttribute()->getBackendTable(), array('count' => new Zend_Db_Expr('COUNT(\'value_id\')')))->where($connection->quoteInto('entity_id <> ?', $object->getId()))->where($connection->quoteInto('value = ?', $object->getUrlKey()));
     $result = $connection->fetchOne($select);
     if ((int) $result) {
         throw new Mage_Core_Exception(Mage::helper('catalog')->__("Category with the '%s' url_key attribute already exists.", $object->getUrlKey()));
     }
     return $this;
 }
 /**
  * get the selected cmspages for a category
  *
  * @access public
  * @param Mage_Catalog_Model_Category $category
  * @return array()
  * @author Ultimate Module Creator
  */
 public function getSelectedCmspages(Mage_Catalog_Model_Category $category)
 {
     if (!$category->hasSelectedCmspages()) {
         $cmspages = array();
         foreach ($this->getSelectedCmspagesCollection($category) as $cmspage) {
             $cmspages[] = $cmspage;
         }
         $category->setSelectedCmspages($cmspages);
     }
     return $category->getData('selected_cmspages');
 }
 /**
  * Get products count in category
  *
  * @param Mage_Catalog_Model_Category $category
  * @return integer
  */
 public function getProductCountExcludeOutStock($category)
 {
     $read = $this->_getReadAdapter();
     $store_data = Mage::getModel('core/store')->load($category->getStoreId());
     //load store object
     $website_id = $store_data->getWebsiteId();
     //get website id from the store
     $select = $read->select()->from(array('main_table' => $this->getTable('catalog/category_product')), "COUNT(main_table.product_id)")->joinLeft(array('stock' => $this->getTable('cataloginventory/stock_status')), 'main_table.product_id=stock.product_id AND ' . $read->quoteInto('stock.website_id=? ', $website_id), array())->where("main_table.category_id = ?", $category->getId())->where("round(stock.qty) > 0 ")->where("stock.stock_status = ? ", 1)->group("main_table.category_id");
     //echo $select->__toString(); exit;
     return (int) $read->fetchOne($select);
 }
Example #28
0
 /**
  * Save  category - post relations
  *
  * @access public
  * @param Mage_Catalog_Model_Category $category
  * @param array $data
  * @return Tech_Blog_Model_Resource_Post_Category
  * @author Ultimate Module Creator
  */
 public function saveCategoryRelation($category, $data)
 {
     if (!is_array($data)) {
         $data = array();
     }
     $deleteCondition = $this->_getWriteAdapter()->quoteInto('category_id=?', $category->getId());
     $this->_getWriteAdapter()->delete($this->getMainTable(), $deleteCondition);
     foreach ($data as $postId => $info) {
         $this->_getWriteAdapter()->insert($this->getMainTable(), array('post_id' => $postId, 'category_id' => $category->getId(), 'position' => @$info['position']));
     }
     return $this;
 }
Example #29
0
 /**
  * @test
  */
 public function testCatalogCategorySaveAfter()
 {
     $category1Data = new \Lesti_Fpc_Model_Fpc_CacheItem('category1', time(), 'text/html');
     $this->_fpc->save($category1Data, 'category1_cache_id', array(sha1('category_1')));
     $category2Data = new \Lesti_Fpc_Model_Fpc_CacheItem('category2', time(), 'text/html');
     $this->_fpc->save($category2Data, 'category2_cache_id', array(sha1('category_2')));
     $category = new Mage_Catalog_Model_Category();
     $category->setId(1);
     Mage::dispatchEvent('catalog_category_save_after', array('category' => $category));
     $this->assertFalse($this->_fpc->load('category1_cache_id'));
     $this->assertEquals($category2Data, $this->_fpc->load('category2_cache_id'));
 }
Example #30
0
 /**
  * Load url rewrite based on specified category
  *
  * @param Mage_Core_Model_Abstract $object
  * @param Mage_Catalog_Model_Category $category
  * @return Enterprise_Catalog_Model_Resource_Category
  */
 public function loadByCategory(Mage_Core_Model_Abstract $object, Mage_Catalog_Model_Category $category)
 {
     $idField = $this->_getReadAdapter()->getIfNullSql('url_rewrite_cat.id', 'default_urc.id');
     $requestPath = $this->_getReadAdapter()->getIfNullSql('url_rewrite.request_path', 'default_ur.request_path');
     $select = $this->_getReadAdapter()->select()->from(array('main_table' => $this->getTable('catalog/category')), array($this->getIdFieldName() => $idField))->where('main_table.entity_id = ?', (int) $category->getId())->joinLeft(array('url_rewrite_cat' => $this->getTable('enterprise_catalog/category')), 'url_rewrite_cat.category_id = main_table.entity_id AND url_rewrite_cat.store_id = ' . (int) $category->getStoreId(), array(''))->joinLeft(array('url_rewrite' => $this->getTable('enterprise_urlrewrite/url_rewrite')), 'url_rewrite.url_rewrite_id = url_rewrite_cat.url_rewrite_id', array(''))->joinLeft(array('default_urc' => $this->getTable('enterprise_catalog/category')), 'default_urc.category_id = main_table.entity_id AND default_urc.store_id = 0', array(''))->joinLeft(array('default_ur' => $this->getTable('enterprise_urlrewrite/url_rewrite')), 'default_ur.url_rewrite_id = default_urc.url_rewrite_id', array('request_path' => $requestPath));
     $result = $this->_getReadAdapter()->fetchRow($select);
     if ($result) {
         $object->setData($result);
     }
     $this->unserializeFields($object);
     $this->_afterLoad($object);
     return $this;
 }