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
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Website extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('catalog/product_website', 'product_id');
00044 }
00045
00046
00047
00048
00049
00050
00051 protected function _getProductResource()
00052 {
00053 return Mage::getResourceSingleton('catalog/product');
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063 public function removeProducts($websiteIds, $productIds)
00064 {
00065 if (!is_array($websiteIds) || !is_array($productIds)
00066 || count($websiteIds) == 0 || count($productIds) == 0)
00067 {
00068 return $this;
00069 }
00070
00071 $whereCond = array(
00072 $this->_getWriteAdapter()->quoteInto('website_id IN(?)', $websiteIds),
00073 $this->_getWriteAdapter()->quoteInto('product_id IN(?)', $productIds)
00074 );
00075 $whereCond = join(' AND ', $whereCond);
00076
00077 $this->_getWriteAdapter()->beginTransaction();
00078 try {
00079 $this->_getWriteAdapter()->delete($this->getMainTable(), $whereCond);
00080 $this->_getWriteAdapter()->commit();
00081 }
00082 catch (Exception $e) {
00083 $this->_getWriteAdapter()->rollBack();
00084 throw $e;
00085 }
00086
00087 return $this;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 public function addProducts($websiteIds, $productIds)
00098 {
00099 if (!is_array($websiteIds) || !is_array($productIds)
00100 || count($websiteIds) == 0 || count($productIds) == 0)
00101 {
00102 return $this;
00103 }
00104
00105 $this->_getWriteAdapter()->beginTransaction();
00106
00107
00108 $this->removeProducts($websiteIds, $productIds);
00109
00110 foreach ($websiteIds as $websiteId) {
00111 foreach ($productIds as $productId) {
00112 if (!$productId) {
00113 continue;
00114 }
00115 $this->_getWriteAdapter()->insert($this->getMainTable(), array(
00116 'product_id' => $productId,
00117 'website_id' => $websiteId
00118 ));
00119 }
00120
00121
00122 $storeIds = Mage::app()->getWebsite($websiteId)->getStoreIds();
00123 foreach ($storeIds as $storeId) {
00124 $store = Mage::app()->getStore($storeId);
00125 $this->_getProductResource()->refreshEnabledIndex($store, $productIds);
00126 }
00127 }
00128
00129 $this->_getWriteAdapter()->commit();
00130
00131 return $this;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 public function getWebsites($productIds)
00141 {
00142 $select = $this->_getReadAdapter()->select()
00143 ->from($this->getMainTable(), array('product_id', 'website_id'))
00144 ->where('product_id IN (?)', $productIds);
00145 $rowset = $this->_getReadAdapter()->fetchAll($select);
00146
00147 $result = array();
00148 foreach ($rowset as $row) {
00149 $result[$row['product_id']][] = $row['website_id'];
00150 }
00151
00152 return $result;
00153 }
00154 }