1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Catalog Product Website Resource Model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Resource_Product_Website extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Initialize connection and define resource table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('catalog/product_website', 'product_id');
44: }
45:
46: /**
47: * Get catalog product resource model
48: *
49: * @return Mage_Catalog_Model_Resource_Product
50: */
51: protected function _getProductResource()
52: {
53: return Mage::getResourceSingleton('catalog/product');
54: }
55:
56: /**
57: * Removes products from websites
58: *
59: * @param array $websiteIds
60: * @param array $productIds
61: * @return Mage_Catalog_Model_Resource_Product_Website
62: * @throws Exception
63: */
64: public function removeProducts($websiteIds, $productIds)
65: {
66: if (!is_array($websiteIds) || !is_array($productIds)
67: || count($websiteIds) == 0 || count($productIds) == 0)
68: {
69: return $this;
70: }
71:
72: $adapter = $this->_getWriteAdapter();
73: $whereCond = array(
74: $adapter->quoteInto('website_id IN(?)', $websiteIds),
75: $adapter->quoteInto('product_id IN(?)', $productIds)
76: );
77: $whereCond = join(' AND ', $whereCond);
78:
79: $adapter->beginTransaction();
80: try {
81: $adapter->delete($this->getMainTable(), $whereCond);
82: $adapter->commit();
83: } catch (Exception $e) {
84: $adapter->rollBack();
85: throw $e;
86: }
87:
88: return $this;
89: }
90:
91: /**
92: * Add products to websites
93: *
94: * @param array $websiteIds
95: * @param array $productIds
96: * @return Mage_Catalog_Model_Resource_Product_Website
97: * @throws Exception
98: */
99: public function addProducts($websiteIds, $productIds)
100: {
101: if (!is_array($websiteIds) || !is_array($productIds)
102: || count($websiteIds) == 0 || count($productIds) == 0)
103: {
104: return $this;
105: }
106:
107: $this->_getWriteAdapter()->beginTransaction();
108:
109: // Before adding of products we should remove it old rows with same ids
110: $this->removeProducts($websiteIds, $productIds);
111: try {
112: foreach ($websiteIds as $websiteId) {
113: foreach ($productIds as $productId) {
114: if (!$productId) {
115: continue;
116: }
117: $this->_getWriteAdapter()->insert($this->getMainTable(), array(
118: 'product_id' => (int) $productId,
119: 'website_id' => (int) $websiteId
120: ));
121: }
122:
123: // Refresh product enabled index
124: $storeIds = Mage::app()->getWebsite($websiteId)->getStoreIds();
125: foreach ($storeIds as $storeId) {
126: $store = Mage::app()->getStore($storeId);
127: $this->_getProductResource()->refreshEnabledIndex($store, $productIds);
128: }
129: }
130:
131: $this->_getWriteAdapter()->commit();
132: } catch (Exception $e) {
133: $this->_getWriteAdapter()->rollBack();
134: throw $e;
135: }
136: return $this;
137: }
138:
139: /**
140: * Retrieve product(s) website ids.
141: *
142: * @param array $productIds
143: * @return array
144: */
145: public function getWebsites($productIds)
146: {
147: $select = $this->_getReadAdapter()->select()
148: ->from($this->getMainTable(), array('product_id', 'website_id'))
149: ->where('product_id IN (?)', $productIds);
150: $rowset = $this->_getReadAdapter()->fetchAll($select);
151:
152: $result = array();
153: foreach ($rowset as $row) {
154: $result[$row['product_id']][] = $row['website_id'];
155: }
156:
157: return $result;
158: }
159: }
160: