00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Rss_Block_Catalog_Special extends Mage_Rss_Block_Abstract
00035 {
00036 protected function _construct()
00037 {
00038
00039
00040
00041 $this->setCacheKey('rss_catalog_special_'.$this->getStoreId().'_'.$this->_getCustomerGroupId());
00042 $this->setCacheLifetime(600);
00043 }
00044
00045 protected function _toHtml()
00046 {
00047
00048 $storeId = $this->_getStoreId();
00049 $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
00050
00051
00052 $custGroup = $this->_getCustomerGroupId();
00053
00054 $product = Mage::getModel('catalog/product');
00055 $todayDate = $product->getResource()->formatDate(time());
00056
00057 $rulePriceWhere = "({{table}}.rule_date is null) or ({{table}}.rule_date='$todayDate' and {{table}}.website_id='$websiteId' and {{table}}.customer_group_id='$custGroup')";
00058
00059 $specials = $product->setStoreId($storeId)->getResourceCollection()
00060 ->addAttributeToFilter('special_price', array('gt'=>0), 'left')
00061 ->addAttributeToFilter('special_from_date', array('date'=>true, 'to'=> $todayDate), 'left')
00062 ->addAttributeToFilter(array(
00063 array('attribute'=>'special_to_date', 'date'=>true, 'from'=>$todayDate),
00064 array('attribute'=>'special_to_date', 'is' => new Zend_Db_Expr('null'))
00065 ), '', 'left')
00066 ->addAttributeToSort('special_from_date', 'desc')
00067 ->addAttributeToSelect(array('name', 'short_description', 'description', 'price', 'thumbnail', 'special_to_date'), 'inner')
00068 ->joinTable('catalogrule/rule_product_price', 'product_id=entity_id', array('rule_price'=>'rule_price', 'rule_start_date'=>'latest_start_date'), $rulePriceWhere, 'left')
00069 ;
00070
00071
00072 $rulePriceCollection = Mage::getResourceModel('catalogrule/rule_product_price_collection')
00073 ->addFieldToFilter('website_id', $websiteId)
00074 ->addFieldToFilter('customer_group_id', $custGroup)
00075 ->addFieldToFilter('rule_date', $todayDate)
00076 ;
00077
00078 $productIds = $rulePriceCollection->getProductIds();
00079
00080 if (!empty($productIds)) {
00081 $specials->getSelect()->orWhere('e.entity_id in ('.implode(',',$productIds).')');
00082 }
00083
00084
00085
00086
00087 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($specials);
00088 Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($specials);
00089
00090
00091
00092
00093 $newurl = Mage::getUrl('rss/catalog/new');
00094 $title = Mage::helper('rss')->__('%s - Special Discounts', Mage::app()->getStore()->getName());
00095 $lang = Mage::getStoreConfig('general/locale/code');
00096
00097 $rssObj = Mage::getModel('rss/rss');
00098 $data = array('title' => $title,
00099 'description' => $title,
00100 'link' => $newurl,
00101 'charset' => 'UTF-8',
00102 'language' => $lang
00103 );
00104 $rssObj->_addHeader($data);
00105
00106 $results = array();
00107
00108
00109
00110
00111 Mage::getSingleton('core/resource_iterator')
00112 ->walk($specials->getSelect(), array(array($this, 'addSpecialXmlCallback')), array('rssObj'=> $rssObj, 'results'=> &$results));
00113
00114 if(sizeof($results)>0){
00115 usort($results, array(&$this, 'sortByStartDate'));
00116 foreach($results as $result){
00117 $product->setData($result);
00118
00119
00120 $special_price = ($result['use_special'] ? $result['special_price'] : $result['rule_price']);
00121 $description = '<table><tr>'.
00122 '<td><a href="'.$product->getProductUrl().'"><img src="'. $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75) .'" border="0" align="left" height="75" width="75"></a></td>'.
00123 '<td style="text-decoration:none;">'.$product->getDescription().
00124 '<p> Price:'.Mage::helper('core')->currency($product->getPrice()).
00125 ' Special Price:'. Mage::helper('core')->currency($special_price).
00126 ($result['use_special'] && $result['special_to_date'] ? '<br/> Special Expires on: '.$this->formatDate($result['special_to_date'], Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM) : '').
00127 '</p>'.
00128 '</td>'.
00129 '</tr></table>';
00130 $data = array(
00131 'title' => $product->getName(),
00132 'link' => $product->getProductUrl(),
00133 'description' => $description,
00134
00135 );
00136 $rssObj->_addEntry($data);
00137 }
00138 }
00139 return $rssObj->createRssXml();
00140 }
00141
00142 public function addSpecialXmlCallback($args)
00143 {
00144
00145
00146 $row = $args['row'];
00147 $special_price = $row['special_price'];
00148 $rule_price = $row['rule_price'];
00149 if (!$rule_price || ($rule_price && $special_price && $special_price<=$rule_price)) {
00150 $row['start_date'] = $row['special_from_date'];
00151 $row['use_special'] = true;
00152 } else {
00153 $row['start_date'] = $row['rule_start_date'];
00154 $row['use_special'] = false;
00155 }
00156 $args['results'][] = $row;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 public function sortByStartDate($a, $b)
00168 {
00169 return $a['start_date']>$b['start_date'] ? -1 : ($a['start_date']<$b['start_date'] ? 1 : 0);
00170 }
00171 }