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_Downloadable_Model_Resource_Link extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('downloadable/link', 'link_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function saveItemTitleAndPrice($linkObject)
53: {
54:
55: $writeAdapter = $this->_getWriteAdapter();
56: $linkTitleTable = $this->getTable('downloadable/link_title');
57: $linkPriceTable = $this->getTable('downloadable/link_price');
58:
59: $select = $writeAdapter->select()
60: ->from($this->getTable('downloadable/link_title'))
61: ->where('link_id=:link_id AND store_id=:store_id');
62: $bind = array(
63: ':link_id' => $linkObject->getId(),
64: ':store_id' => (int)$linkObject->getStoreId()
65: );
66:
67: if ($writeAdapter->fetchOne($select, $bind)) {
68: $where = array(
69: 'link_id = ?' => $linkObject->getId(),
70: 'store_id = ?' => (int)$linkObject->getStoreId()
71: );
72: if ($linkObject->getUseDefaultTitle()) {
73: $writeAdapter->delete(
74: $linkTitleTable, $where);
75: } else {
76: $insertData = array('title' => $linkObject->getTitle());
77: $writeAdapter->update(
78: $linkTitleTable,
79: $insertData,
80: $where);
81: }
82: } else {
83: if (!$linkObject->getUseDefaultTitle()) {
84: $writeAdapter->insert(
85: $linkTitleTable,
86: array(
87: 'link_id' => $linkObject->getId(),
88: 'store_id' => (int)$linkObject->getStoreId(),
89: 'title' => $linkObject->getTitle(),
90: ));
91: }
92: }
93:
94: $select = $writeAdapter->select()
95: ->from($linkPriceTable)
96: ->where('link_id=:link_id AND website_id=:website_id');
97: $bind = array(
98: ':link_id' => $linkObject->getId(),
99: ':website_id' => (int)$linkObject->getWebsiteId(),
100: );
101: if ($writeAdapter->fetchOne($select, $bind)) {
102: $where = array(
103: 'link_id = ?' => $linkObject->getId(),
104: 'website_id = ?' => $linkObject->getWebsiteId()
105: );
106: if ($linkObject->getUseDefaultPrice()) {
107: $writeAdapter->delete(
108: $linkPriceTable, $where);
109: } else {
110: $writeAdapter->update(
111: $linkPriceTable,
112: array('price' => $linkObject->getPrice()),
113: $where);
114: }
115: } else {
116: if (!$linkObject->getUseDefaultPrice()) {
117: $dataToInsert[] = array(
118: 'link_id' => $linkObject->getId(),
119: 'website_id' => (int)$linkObject->getWebsiteId(),
120: 'price' => (float)$linkObject->getPrice()
121: );
122: if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) {
123: $_isNew = true;
124: } else {
125: $_isNew = false;
126: }
127: if ($linkObject->getWebsiteId() == 0 && $_isNew && !Mage::helper('catalog')->isPriceGlobal()) {
128: $websiteIds = $linkObject->getProductWebsiteIds();
129: foreach ($websiteIds as $websiteId) {
130: $baseCurrency = Mage::app()->getBaseCurrencyCode();
131: $websiteCurrency = Mage::app()->getWebsite($websiteId)->getBaseCurrencyCode();
132: if ($websiteCurrency == $baseCurrency) {
133: continue;
134: }
135: $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($websiteCurrency);
136: if (!$rate) {
137: $rate = 1;
138: }
139: $newPrice = $linkObject->getPrice() * $rate;
140: $dataToInsert[] = array(
141: 'link_id' => $linkObject->getId(),
142: 'website_id' => (int)$websiteId,
143: 'price' => $newPrice
144: );
145: }
146: }
147: $writeAdapter->insertMultiple($linkPriceTable, $dataToInsert);
148: }
149: }
150: return $this;
151: }
152:
153: 154: 155: 156: 157: 158:
159: public function deleteItems($items)
160: {
161: $writeAdapter = $this->_getWriteAdapter();
162: $where = array();
163: if ($items instanceof Mage_Downloadable_Model_Link) {
164: $where = array('link_id = ?' => $items->getId());
165: } elseif (is_array($items)) {
166: $where = array('link_id in (?)' => $items);
167: } else {
168: $where = array('sample_id = ?' => $items);
169: }
170: if ($where) {
171: $writeAdapter->delete(
172: $this->getMainTable(), $where);
173: $writeAdapter->delete(
174: $this->getTable('downloadable/link_title'), $where);
175: $writeAdapter->delete(
176: $this->getTable('downloadable/link_price'), $where);
177: }
178: return $this;
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public function getSearchableData($productId, $storeId)
189: {
190: $adapter = $this->_getReadAdapter();
191: $ifNullDefaultTitle = $adapter->getIfNullSql('st.title', 's.title');
192: $select = $adapter->select()
193: ->from(array('m' => $this->getMainTable()), null)
194: ->join(
195: array('s' => $this->getTable('downloadable/link_title')),
196: 's.link_id=m.link_id AND s.store_id=0',
197: array())
198: ->joinLeft(
199: array('st' => $this->getTable('downloadable/link_title')),
200: 'st.link_id=m.link_id AND st.store_id=:store_id',
201: array('title' => $ifNullDefaultTitle))
202: ->where('m.product_id=:product_id');
203: $bind = array(
204: ':store_id' => (int)$storeId,
205: ':product_id' => $productId
206: );
207:
208: return $adapter->fetchCol($select, $bind);
209: }
210: }
211: