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 extends Mage_Rss_Block_Catalog_Abstract
35: {
36: 37: 38: 39: 40:
41: protected static $_currentDate = null;
42:
43: protected function _construct()
44: {
45: 46: 47:
48: $this->setCacheKey('rss_catalog_special_'.$this->_getStoreId().'_'.$this->_getCustomerGroupId());
49: $this->setCacheLifetime(600);
50: }
51:
52: protected function _toHtml()
53: {
54:
55: $storeId = $this->_getStoreId();
56: $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
57:
58:
59: $customerGroupId = $this->_getCustomerGroupId();
60:
61: $product = Mage::getModel('catalog/product');
62:
63: $fields = array(
64: 'final_price',
65: 'price'
66: );
67: $specials = $product->setStoreId($storeId)->getResourceCollection()
68: ->addPriceDataFieldFilter('%s < %s', $fields)
69: ->addPriceData($customerGroupId, $websiteId)
70: ->addAttributeToSelect(
71: array(
72: 'name', 'short_description', 'description', 'price', 'thumbnail',
73: 'special_price', 'special_to_date',
74: 'msrp_enabled', 'msrp_display_actual_price_type', 'msrp'
75: ),
76: 'left'
77: )
78: ->addAttributeToSort('name', 'asc')
79: ;
80:
81: $newurl = Mage::getUrl('rss/catalog/special/store_id/' . $storeId);
82: $title = Mage::helper('rss')->__('%s - Special Products', Mage::app()->getStore()->getFrontendName());
83: $lang = Mage::getStoreConfig('general/locale/code');
84:
85: $rssObj = Mage::getModel('rss/rss');
86: $data = array('title' => $title,
87: 'description' => $title,
88: 'link' => $newurl,
89: 'charset' => 'UTF-8',
90: 'language' => $lang
91: );
92: $rssObj->_addHeader($data);
93:
94: $results = array();
95: 96: 97: 98:
99: Mage::getSingleton('core/resource_iterator')->walk(
100: $specials->getSelect(),
101: array(array($this, 'addSpecialXmlCallback')),
102: array('rssObj'=> $rssObj, 'results'=> &$results)
103: );
104:
105: if (sizeof($results)>0) {
106: foreach($results as $result){
107:
108: $product->setData($result);
109: $html = sprintf('<table><tr>
110: <td><a href="%s"><img src="%s" alt="" border="0" align="left" height="75" width="75" /></a></td>
111: <td style="text-decoration:none;">%s',
112: $product->getProductUrl(),
113: $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75),
114: $this->helper('catalog/output')->productAttribute(
115: $product,
116: $product->getDescription(),
117: 'description'
118: )
119: );
120:
121:
122: if ($product->getAllowedPriceInRss()) {
123: if (Mage::helper('catalog')->canApplyMsrp($product)) {
124: $html .= '<br/><a href="' . $product->getProductUrl() . '">'
125: . $this->__('Click for price') . '</a>';
126: } else {
127: $special = '';
128: if ($result['use_special']) {
129: $special = '<br />' . Mage::helper('catalog')->__('Special Expires On: %s', $this->formatDate($result['special_to_date'], Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM));
130: }
131: $html .= sprintf('<p>%s %s%s</p>',
132: Mage::helper('catalog')->__('Price: %s', Mage::helper('core')->currency($result['price'])),
133: Mage::helper('catalog')->__('Special Price: %s', Mage::helper('core')->currency($result['final_price'])),
134: $special
135: );
136: }
137: }
138:
139: $html .= '</td></tr></table>';
140:
141: $rssObj->_addEntry(array(
142: 'title' => $product->getName(),
143: 'link' => $product->getProductUrl(),
144: 'description' => $html
145: ));
146: }
147: }
148: return $rssObj->createRssXml();
149: }
150:
151: 152: 153: 154: 155:
156: public function addSpecialXmlCallback($args)
157: {
158: if (!isset(self::$_currentDate)) {
159: self::$_currentDate = new Zend_Date();
160: }
161:
162:
163: $product = new Varien_Object(array('allowed_in_rss' => true, 'allowed_price_in_rss' => true));
164: $args['product'] = $product;
165: Mage::dispatchEvent('rss_catalog_special_xml_callback', $args);
166: if (!$product->getAllowedInRss()) {
167: return;
168: }
169:
170:
171: $row = $args['row'];
172: $row['use_special'] = false;
173: $row['allowed_price_in_rss'] = $product->getAllowedPriceInRss();
174: if (isset($row['special_to_date']) && $row['final_price'] <= $row['special_price']
175: && $row['allowed_price_in_rss']
176: ) {
177: $compareDate = self::$_currentDate->compareDate($row['special_to_date'], Varien_Date::DATE_INTERNAL_FORMAT);
178: if (-1 === $compareDate || 0 === $compareDate) {
179: $row['use_special'] = true;
180: }
181: }
182:
183: $args['results'][] = $row;
184: }
185:
186:
187: 188: 189: 190: 191: 192: 193:
194: public function sortByStartDate($a, $b)
195: {
196: return $a['start_date']>$b['start_date'] ? -1 : ($a['start_date']<$b['start_date'] ? 1 : 0);
197: }
198: }
199: