1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_Abstract
35: {
36: protected $_productsCount = null;
37:
38: const DEFAULT_PRODUCTS_COUNT = 5;
39:
40: 41: 42:
43: protected function _construct()
44: {
45: parent::_construct();
46:
47: $this->addColumnCountLayoutDepend('empty', 6)
48: ->addColumnCountLayoutDepend('one_column', 5)
49: ->addColumnCountLayoutDepend('two_columns_left', 4)
50: ->addColumnCountLayoutDepend('two_columns_right', 4)
51: ->addColumnCountLayoutDepend('three_columns', 3);
52:
53: $this->addData(array(
54: 'cache_lifetime' => 86400,
55: 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
56: ));
57: }
58:
59: 60: 61: 62: 63:
64: public function getCacheKeyInfo()
65: {
66: return array(
67: 'CATALOG_PRODUCT_NEW',
68: Mage::app()->getStore()->getId(),
69: Mage::getDesign()->getPackageName(),
70: Mage::getDesign()->getTheme('template'),
71: Mage::getSingleton('customer/session')->getCustomerGroupId(),
72: 'template' => $this->getTemplate(),
73: $this->getProductsCount()
74: );
75: }
76:
77: 78: 79: 80: 81:
82: protected function _beforeToHtml()
83: {
84: $todayStartOfDayDate = Mage::app()->getLocale()->date()
85: ->setTime('00:00:00')
86: ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
87:
88: $todayEndOfDayDate = Mage::app()->getLocale()->date()
89: ->setTime('23:59:59')
90: ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
91:
92: $collection = Mage::getResourceModel('catalog/product_collection');
93: $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
94:
95:
96: $collection = $this->_addProductAttributesAndPrices($collection)
97: ->addStoreFilter()
98: ->addAttributeToFilter('news_from_date', array('or'=> array(
99: 0 => array('date' => true, 'to' => $todayEndOfDayDate),
100: 1 => array('is' => new Zend_Db_Expr('null')))
101: ), 'left')
102: ->addAttributeToFilter('news_to_date', array('or'=> array(
103: 0 => array('date' => true, 'from' => $todayStartOfDayDate),
104: 1 => array('is' => new Zend_Db_Expr('null')))
105: ), 'left')
106: ->addAttributeToFilter(
107: array(
108: array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
109: array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
110: )
111: )
112: ->addAttributeToSort('news_from_date', 'desc')
113: ->setPageSize($this->getProductsCount())
114: ->setCurPage(1)
115: ;
116:
117: $this->setProductCollection($collection);
118:
119: return parent::_beforeToHtml();
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function setProductsCount($count)
129: {
130: $this->_productsCount = $count;
131: return $this;
132: }
133:
134: 135: 136: 137: 138:
139: public function getProductsCount()
140: {
141: if (null === $this->_productsCount) {
142: $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
143: }
144: return $this->_productsCount;
145: }
146: }
147: