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: class Mage_Downloadable_Model_Product_Type extends Mage_Catalog_Model_Product_Type_Virtual
35: {
36: const TYPE_DOWNLOADABLE = 'downloadable';
37:
38: 39: 40: 41: 42: 43:
44: public function getLinks($product = null)
45: {
46: $product = $this->getProduct($product);
47:
48: if (is_null($product->getDownloadableLinks())) {
49: $_linkCollection = Mage::getModel('downloadable/link')->getCollection()
50: ->addProductToFilter($product->getId())
51: ->addTitleToResult($product->getStoreId())
52: ->addPriceToResult($product->getStore()->getWebsiteId());
53: $linksCollectionById = array();
54: foreach ($_linkCollection as $link) {
55:
56:
57: $link->setProduct($product);
58: $linksCollectionById[$link->getId()] = $link;
59: }
60: $product->setDownloadableLinks($linksCollectionById);
61: }
62: return $product->getDownloadableLinks();
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function hasLinks($product = null)
72: {
73: if ($this->getProduct($product)->hasData('links_exist')) {
74: return $this->getProduct($product)->getData('links_exist');
75: }
76: return count($this->getLinks($product)) > 0;
77: }
78:
79: 80: 81: 82: 83: 84:
85: public function hasOptions($product = null)
86: {
87:
88: return $this->getProduct($product)->getLinksPurchasedSeparately()
89: || parent::hasOptions($product);
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function hasRequiredOptions($product = null)
99: {
100: if (parent::hasRequiredOptions($product) || $this->getProduct($product)->getLinksPurchasedSeparately()) {
101: return true;
102: }
103: return false;
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function getLinkSelectionRequired($product = null)
113: {
114: return $this->getProduct($product)->getLinksPurchasedSeparately();
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function getSamples($product = null)
124: {
125: $product = $this->getProduct($product);
126:
127: if (is_null($product->getDownloadableSamples())) {
128: $_sampleCollection = Mage::getModel('downloadable/sample')->getCollection()
129: ->addProductToFilter($product->getId())
130: ->addTitleToResult($product->getStoreId());
131: $product->setDownloadableSamples($_sampleCollection);
132: }
133:
134: return $product->getDownloadableSamples();
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function hasSamples($product = null)
144: {
145: return count($this->getSamples($product)) > 0;
146: }
147:
148: 149: 150: 151: 152: 153:
154: public function save($product = null)
155: {
156: parent::save($product);
157:
158: $product = $this->getProduct($product);
159:
160:
161: if ($data = $product->getDownloadableData()) {
162: if (isset($data['sample'])) {
163: $_deleteItems = array();
164: foreach ($data['sample'] as $sampleItem) {
165: if ($sampleItem['is_delete'] == '1') {
166: if ($sampleItem['sample_id']) {
167: $_deleteItems[] = $sampleItem['sample_id'];
168: }
169: } else {
170: unset($sampleItem['is_delete']);
171: if (!$sampleItem['sample_id']) {
172: unset($sampleItem['sample_id']);
173: }
174: $sampleModel = Mage::getModel('downloadable/sample');
175: $files = array();
176: if (isset($sampleItem['file'])) {
177: $files = Mage::helper('core')->jsonDecode($sampleItem['file']);
178: unset($sampleItem['file']);
179: }
180:
181: $sampleModel->setData($sampleItem)
182: ->setSampleType($sampleItem['type'])
183: ->setProductId($product->getId())
184: ->setStoreId($product->getStoreId());
185:
186: if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
187: $sampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
188: Mage_Downloadable_Model_Sample::getBaseTmpPath(),
189: Mage_Downloadable_Model_Sample::getBasePath(),
190: $files
191: );
192: $sampleModel->setSampleFile($sampleFileName);
193: }
194: $sampleModel->save();
195: }
196: }
197: if ($_deleteItems) {
198: Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems);
199: }
200: }
201: if (isset($data['link'])) {
202: $_deleteItems = array();
203: foreach ($data['link'] as $linkItem) {
204: if ($linkItem['is_delete'] == '1') {
205: if ($linkItem['link_id']) {
206: $_deleteItems[] = $linkItem['link_id'];
207: }
208: } else {
209: unset($linkItem['is_delete']);
210: if (!$linkItem['link_id']) {
211: unset($linkItem['link_id']);
212: }
213: $files = array();
214: if (isset($linkItem['file'])) {
215: $files = Mage::helper('core')->jsonDecode($linkItem['file']);
216: unset($linkItem['file']);
217: }
218: $sample = array();
219: if (isset($linkItem['sample'])) {
220: $sample = $linkItem['sample'];
221: unset($linkItem['sample']);
222: }
223: $linkModel = Mage::getModel('downloadable/link')
224: ->setData($linkItem)
225: ->setLinkType($linkItem['type'])
226: ->setProductId($product->getId())
227: ->setStoreId($product->getStoreId())
228: ->setWebsiteId($product->getStore()->getWebsiteId())
229: ->setProductWebsiteIds($product->getWebsiteIds());
230: if (null === $linkModel->getPrice()) {
231: $linkModel->setPrice(0);
232: }
233: if ($linkModel->getIsUnlimited()) {
234: $linkModel->setNumberOfDownloads(0);
235: }
236: $sampleFile = array();
237: if ($sample && isset($sample['type'])) {
238: if ($sample['type'] == 'url' && $sample['url'] != '') {
239: $linkModel->setSampleUrl($sample['url']);
240: }
241: $linkModel->setSampleType($sample['type']);
242: $sampleFile = Mage::helper('core')->jsonDecode($sample['file']);
243: }
244: if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
245: $linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
246: Mage_Downloadable_Model_Link::getBaseTmpPath(),
247: Mage_Downloadable_Model_Link::getBasePath(),
248: $files
249: );
250: $linkModel->setLinkFile($linkFileName);
251: }
252: if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
253: $linkSampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
254: Mage_Downloadable_Model_Link::getBaseSampleTmpPath(),
255: Mage_Downloadable_Model_Link::getBaseSamplePath(),
256: $sampleFile
257: );
258: $linkModel->setSampleFile($linkSampleFileName);
259: }
260: $linkModel->save();
261: }
262: }
263: if ($_deleteItems) {
264: Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems);
265: }
266: if ($this->getProduct($product)->getLinksPurchasedSeparately()) {
267: $this->getProduct($product)->setIsCustomOptionChanged();
268: }
269: }
270: }
271:
272: return $this;
273: }
274:
275: 276: 277: 278: 279: 280: 281: 282: 283:
284: protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
285: {
286: $result = parent::_prepareProduct($buyRequest, $product, $processMode);
287:
288: if (is_string($result)) {
289: return $result;
290: }
291:
292: $originalLinksPurchasedSeparately = null;
293: if ($this->getProduct($product)->getSkipCheckRequiredOption()) {
294: $originalLinksPurchasedSeparately = $this->getProduct($product)
295: ->getLinksPurchasedSeparately();
296: $this->getProduct($product)->setLinksPurchasedSeparately(false);
297: }
298: $preparedLinks = array();
299: if ($this->getProduct($product)->getLinksPurchasedSeparately()) {
300: if ($links = $buyRequest->getLinks()) {
301: foreach ($this->getLinks($product) as $link) {
302: if (in_array($link->getId(), $links)) {
303: $preparedLinks[] = $link->getId();
304: }
305: }
306: }
307: } else {
308: foreach ($this->getLinks($product) as $link) {
309: $preparedLinks[] = $link->getId();
310: }
311: }
312: if (null !== $originalLinksPurchasedSeparately) {
313: $this->getProduct($product)
314: ->setLinksPurchasedSeparately($originalLinksPurchasedSeparately);
315: }
316: if ($preparedLinks) {
317: $this->getProduct($product)->addCustomOption('downloadable_link_ids', implode(',', $preparedLinks));
318: return $result;
319: }
320: if ($this->getLinkSelectionRequired($product) && $this->_isStrictProcessMode($processMode)) {
321: return Mage::helper('downloadable')->__('Please specify product link(s).');
322: }
323: return $result;
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: public function checkProductBuyState($product = null)
334: {
335: parent::checkProductBuyState($product);
336: $product = $this->getProduct($product);
337: $option = $product->getCustomOption('info_buyRequest');
338: if ($option instanceof Mage_Sales_Model_Quote_Item_Option) {
339: $buyRequest = new Varien_Object(unserialize($option->getValue()));
340: if (!$buyRequest->hasLinks()) {
341: if (!$product->getLinksPurchasedSeparately()) {
342: $allLinksIds = Mage::getModel('downloadable/link')
343: ->getCollection()
344: ->addProductToFilter($product->getId())
345: ->getAllIds();
346: $buyRequest->setLinks($allLinksIds);
347: $product->addCustomOption('info_buyRequest', serialize($buyRequest->getData()));
348: } else {
349: Mage::throwException(
350: Mage::helper('downloadable')->__('Please specify product link(s).')
351: );
352: }
353: }
354: }
355: return $this;
356: }
357:
358: 359: 360: 361: 362: 363: 364:
365: public function getOrderOptions($product = null)
366: {
367: $options = parent::getOrderOptions($product);
368: if ($linkIds = $this->getProduct($product)->getCustomOption('downloadable_link_ids')) {
369: $linkOptions = array();
370: $links = $this->getLinks($product);
371: foreach (explode(',', $linkIds->getValue()) as $linkId) {
372: if (isset($links[$linkId])) {
373: $linkOptions[] = $linkId;
374: }
375: }
376: $options = array_merge($options, array('links' => $linkOptions));
377: }
378: $options = array_merge($options, array(
379: 'is_downloadable' => true,
380: 'real_product_type' => self::TYPE_DOWNLOADABLE
381: ));
382: return $options;
383: }
384:
385:
386:
387: 388: 389: 390: 391: 392:
393: public function beforeSave($product = null)
394: {
395: parent::beforeSave($product);
396: if ($this->getLinkSelectionRequired($product)) {
397: $this->getProduct($product)->setTypeHasRequiredOptions(true);
398: } else {
399: $this->getProduct($product)->setTypeHasRequiredOptions(false);
400: }
401:
402:
403: $linksExist = false;
404: if ($data = $product->getDownloadableData()) {
405: if (isset($data['link'])) {
406: foreach ($data['link'] as $linkItem) {
407: if (!isset($linkItem['is_delete']) || !$linkItem['is_delete']) {
408: $linksExist = true;
409: break;
410: }
411: }
412: }
413: }
414:
415: $this->getProduct($product)->setTypeHasOptions($linksExist);
416: $this->getProduct($product)->setLinksExist($linksExist);
417: }
418:
419: 420: 421: 422: 423: 424: 425:
426: public function getSearchableData($product = null)
427: {
428: $searchData = parent::getSearchableData($product);
429: $product = $this->getProduct($product);
430:
431: $linkSearchData = Mage::getSingleton('downloadable/link')
432: ->getSearchableData($product->getId(), $product->getStoreId());
433: if ($linkSearchData) {
434: $searchData = array_merge($searchData, $linkSearchData);
435: }
436:
437: $sampleSearchData = Mage::getSingleton('downloadable/sample')
438: ->getSearchableData($product->getId(), $product->getStoreId());
439: if ($sampleSearchData) {
440: $searchData = array_merge($searchData, $sampleSearchData);
441: }
442:
443: return $searchData;
444: }
445:
446: 447: 448: 449: 450: 451:
452: public function isSalable($product = null)
453: {
454: return $this->hasLinks($product) && parent::isSalable($product);
455: }
456:
457: 458: 459: 460: 461: 462: 463:
464: public function processBuyRequest($product, $buyRequest)
465: {
466: $links = $buyRequest->getLinks();
467: $links = (is_array($links)) ? array_filter($links, 'intval') : array();
468:
469: $options = array('links' => $links);
470:
471: return $options;
472: }
473:
474: 475: 476: 477: 478: 479:
480: public function canConfigure($product = null)
481: {
482: return $this->hasLinks($product) && $this->getProduct($product)->getLinksPurchasedSeparately();
483: }
484: }
485: