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:
35: class Mage_Catalog_Model_Resource_Product_Link extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_attributesTable;
43:
44: 45: 46:
47: protected function _construct()
48: {
49: $this->_init('catalog/product_link', 'link_id');
50: $this->_attributesTable = $this->getTable('catalog/product_link_attribute');
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: public function saveProductLinks($product, $data, $typeId)
62: {
63: if (!is_array($data)) {
64: $data = array();
65: }
66:
67: $attributes = $this->getAttributesByType($typeId);
68: $adapter = $this->_getWriteAdapter();
69:
70: $bind = array(
71: ':product_id' => (int)$product->getId(),
72: ':link_type_id' => (int)$typeId
73: );
74: $select = $adapter->select()
75: ->from($this->getMainTable(), array('linked_product_id', 'link_id'))
76: ->where('product_id = :product_id')
77: ->where('link_type_id = :link_type_id');
78:
79: $links = $adapter->fetchPairs($select, $bind);
80:
81: $deleteIds = array();
82: foreach($links as $linkedProductId => $linkId) {
83: if (!isset($data[$linkedProductId])) {
84: $deleteIds[] = (int)$linkId;
85: }
86: }
87: if (!empty($deleteIds)) {
88: $adapter->delete($this->getMainTable(), array(
89: 'link_id IN (?)' => $deleteIds,
90: ));
91: }
92:
93: foreach ($data as $linkedProductId => $linkInfo) {
94: $linkId = null;
95: if (isset($links[$linkedProductId])) {
96: $linkId = $links[$linkedProductId];
97: unset($links[$linkedProductId]);
98: } else {
99: $bind = array(
100: 'product_id' => $product->getId(),
101: 'linked_product_id' => $linkedProductId,
102: 'link_type_id' => $typeId
103: );
104: $adapter->insert($this->getMainTable(), $bind);
105: $linkId = $adapter->lastInsertId($this->getMainTable());
106: }
107:
108: foreach ($attributes as $attributeInfo) {
109: $attributeTable = $this->getAttributeTypeTable($attributeInfo['type']);
110: if ($attributeTable) {
111: if (isset($linkInfo[$attributeInfo['code']])) {
112: $value = $this->_prepareAttributeValue($attributeInfo['type'],
113: $linkInfo[$attributeInfo['code']]);
114: $bind = array(
115: 'product_link_attribute_id' => $attributeInfo['id'],
116: 'link_id' => $linkId,
117: 'value' => $value
118: );
119: $adapter->insertOnDuplicate($attributeTable, $bind, array('value'));
120: } else {
121: $adapter->delete($attributeTable, array(
122: 'link_id = ?' => $linkId,
123: 'product_link_attribute_id = ?' => $attributeInfo['id']
124: ));
125: }
126: }
127: }
128: }
129:
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138: 139:
140: protected function _prepareAttributeValue($type, $value)
141: {
142: if ($type == 'int') {
143: $value = (int)$value;
144: } elseif ($type == 'decimal') {
145: $value = (float)sprintf('%F', $value);
146: }
147: return $value;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function getAttributesByType($typeId)
157: {
158: $adapter = $this->_getReadAdapter();
159: $select = $adapter->select()
160: ->from($this->_attributesTable, array(
161: 'id' => 'product_link_attribute_id',
162: 'code' => 'product_link_attribute_code',
163: 'type' => 'data_type'
164: ))
165: ->where('link_type_id = ?', $typeId);
166: return $adapter->fetchAll($select);
167: }
168:
169: 170: 171: 172: 173: 174:
175: public function getAttributeTypeTable($type)
176: {
177: return $this->getTable('catalog/product_link_attribute_' . $type);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
190: public function getChildrenIds($parentId, $typeId)
191: {
192: $adapter = $this->_getReadAdapter();
193: $childrenIds = array();
194: $bind = array(
195: ':product_id' => (int)$parentId,
196: ':link_type_id' => (int)$typeId
197: );
198: $select = $adapter->select()
199: ->from(array('l' => $this->getMainTable()), array('linked_product_id'))
200: ->where('product_id = :product_id')
201: ->where('link_type_id = :link_type_id');
202: if ($typeId == Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED) {
203: $select->join(
204: array('e' => $this->getTable('catalog/product')),
205: 'e.entity_id = l.linked_product_id AND e.required_options = 0',
206: array()
207: );
208: }
209:
210: $childrenIds[$typeId] = array();
211: $result = $adapter->fetchAll($select, $bind);
212: foreach ($result as $row) {
213: $childrenIds[$typeId][$row['linked_product_id']] = $row['linked_product_id'];
214: }
215:
216: return $childrenIds;
217: }
218:
219: 220: 221: 222: 223: 224: 225:
226: public function getParentIdsByChild($childId, $typeId)
227: {
228: $parentIds = array();
229: $adapter = $this->_getReadAdapter();
230: $select = $adapter->select()
231: ->from($this->getMainTable(), array('product_id', 'linked_product_id'))
232: ->where('linked_product_id IN(?)', $childId)
233: ->where('link_type_id = ?', $typeId);
234:
235: $result = $adapter->fetchAll($select);
236: foreach ($result as $row) {
237: $parentIds[] = $row['product_id'];
238: }
239:
240: return $parentIds;
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250:
251: public function saveGroupedLinks($product, $data, $typeId)
252: {
253: $adapter = $this->_getWriteAdapter();
254:
255: $bind = array(
256: 'product_id' => (int)$product->getId(),
257: 'link_type_id' => (int)$typeId
258: );
259: $select = $adapter->select()
260: ->from($this->getMainTable(), array('linked_product_id'))
261: ->where('product_id = :product_id')
262: ->where('link_type_id = :link_type_id');
263: $old = $adapter->fetchCol($select, $bind);
264: $new = array_keys($data);
265:
266: if (array_diff($old, $new) || array_diff($new, $old)) {
267: $product->setIsRelationsChanged(true);
268: }
269:
270:
271: $this->saveProductLinks($product, $data, $typeId);
272:
273:
274: Mage::getResourceSingleton('catalog/product_relation')
275: ->processRelations($product->getId(), $new);
276:
277: return $this;
278: }
279: }
280: