Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Downloadable_Adminhtml_Downloadable_FileController
  • Mage_Downloadable_Adminhtml_Downloadable_Product_EditController
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Links
  • Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Samples
  • Mage_Downloadable_Block_Adminhtml_Sales_Items_Column_Downloadable_Name
  • Mage_Downloadable_Block_Catalog_Product_Links
  • Mage_Downloadable_Block_Catalog_Product_Samples
  • Mage_Downloadable_Block_Catalog_Product_View_Type
  • Mage_Downloadable_Block_Checkout_Cart_Item_Renderer
  • Mage_Downloadable_Block_Checkout_Success
  • Mage_Downloadable_Block_Customer_Products_List
  • Mage_Downloadable_Block_Sales_Order_Email_Items_Downloadable
  • Mage_Downloadable_Block_Sales_Order_Email_Items_Order_Downloadable
  • Mage_Downloadable_Block_Sales_Order_Item_Renderer_Downloadable
  • Mage_Downloadable_CustomerController
  • Mage_Downloadable_DownloadController
  • Mage_Downloadable_Helper_Catalog_Product_Configuration
  • Mage_Downloadable_Helper_Data
  • Mage_Downloadable_Helper_Download
  • Mage_Downloadable_Helper_File
  • Mage_Downloadable_Model_CatalogIndex_Data_Downloadable
  • Mage_Downloadable_Model_Link
  • Mage_Downloadable_Model_Link_Api
  • Mage_Downloadable_Model_Link_Api_Uploader
  • Mage_Downloadable_Model_Link_Api_V2
  • Mage_Downloadable_Model_Link_Api_Validator
  • Mage_Downloadable_Model_Link_Purchased
  • Mage_Downloadable_Model_Link_Purchased_Item
  • Mage_Downloadable_Model_Mysql4_Indexer_Price
  • Mage_Downloadable_Model_Mysql4_Link
  • Mage_Downloadable_Model_Mysql4_Link_Collection
  • Mage_Downloadable_Model_Mysql4_Link_Purchased
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Collection
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Item
  • Mage_Downloadable_Model_Mysql4_Link_Purchased_Item_Collection
  • Mage_Downloadable_Model_Mysql4_Sample
  • Mage_Downloadable_Model_Mysql4_Sample_Collection
  • Mage_Downloadable_Model_Observer
  • Mage_Downloadable_Model_Product_Price
  • Mage_Downloadable_Model_Product_Type
  • Mage_Downloadable_Model_Resource_Indexer_Price
  • Mage_Downloadable_Model_Resource_Link
  • Mage_Downloadable_Model_Resource_Link_Collection
  • Mage_Downloadable_Model_Resource_Link_Purchased
  • Mage_Downloadable_Model_Resource_Link_Purchased_Collection
  • Mage_Downloadable_Model_Resource_Link_Purchased_Item
  • Mage_Downloadable_Model_Resource_Link_Purchased_Item_Collection
  • Mage_Downloadable_Model_Resource_Sample
  • Mage_Downloadable_Model_Resource_Sample_Collection
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Abstract
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Creditmemo
  • Mage_Downloadable_Model_Sales_Order_Pdf_Items_Invoice
  • Mage_Downloadable_Model_Sample
  • Mage_Downloadable_Model_System_Config_Source_Contentdisposition
  • Mage_Downloadable_Model_System_Config_Source_Orderitemstatus
  • Overview
  • Package
  • Class
  • Tree
  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_Downloadable
 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:  * Downloadable product type model
 29:  *
 30:  * @category    Mage
 31:  * @package     Mage_Downloadable
 32:  * @author      Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Downloadable_Model_Product_Type extends Mage_Catalog_Model_Product_Type_Virtual
 35: {
 36:     const TYPE_DOWNLOADABLE = 'downloadable';
 37: 
 38:     /**
 39:      * Get downloadable product links
 40:      *
 41:      * @param Mage_Catalog_Model_Product $product
 42:      * @return array
 43:      */
 44:     public function getLinks($product = null)
 45:     {
 46:         $product = $this->getProduct($product);
 47:         /* @var Mage_Catalog_Model_Product $product */
 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:                 /* @var Mage_Downloadable_Model_Link $link */
 56: 
 57:                 $link->setProduct($product);
 58:                 $linksCollectionById[$link->getId()] = $link;
 59:             }
 60:             $product->setDownloadableLinks($linksCollectionById);
 61:         }
 62:         return $product->getDownloadableLinks();
 63:     }
 64: 
 65:     /**
 66:      * Check if product has links
 67:      *
 68:      * @param Mage_Catalog_Model_Product $product
 69:      * @return boolean
 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:      * Check if product has options
 81:      *
 82:      * @param Mage_Catalog_Model_Product $product
 83:      * @return boolean
 84:      */
 85:     public function hasOptions($product = null)
 86:     {
 87:         //return true;
 88:         return $this->getProduct($product)->getLinksPurchasedSeparately()
 89:             || parent::hasOptions($product);
 90:     }
 91: 
 92:     /**
 93:      * Check if product has required options
 94:      *
 95:      * @param Mage_Catalog_Model_Product $product
 96:      * @return bool
 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:      * Check if product cannot be purchased with no links selected
108:      *
109:      * @param Mage_Catalog_Model_Product $product
110:      * @return boolean
111:      */
112:     public function getLinkSelectionRequired($product = null)
113:     {
114:         return $this->getProduct($product)->getLinksPurchasedSeparately();
115:     }
116: 
117:     /**
118:      * Get downloadable product samples
119:      *
120:      * @param Mage_Catalog_Model_Product $product
121:      * @return Mage_Downloadable_Model_Mysql4_Sample_Collection
122:      */
123:     public function getSamples($product = null)
124:     {
125:         $product = $this->getProduct($product);
126:         /* @var Mage_Catalog_Model_Product $product */
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:      * Check if product has samples
139:      *
140:      * @param Mage_Catalog_Model_Product $product
141:      * @return boolean
142:      */
143:     public function hasSamples($product = null)
144:     {
145:         return count($this->getSamples($product)) > 0;
146:     }
147: 
148:     /**
149:      * Save Product downloadable information (links and samples)
150:      *
151:      * @param Mage_Catalog_Model_Product $product
152:      * @return Mage_Downloadable_Model_Product_Type
153:      */
154:     public function save($product = null)
155:     {
156:         parent::save($product);
157: 
158:         $product = $this->getProduct($product);
159:         /* @var Mage_Catalog_Model_Product $product */
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:      * Prepare product and its configuration to be added to some products list.
277:      * Perform standard preparation process and then prepare options for downloadable links.
278:      *
279:      * @param Varien_Object $buyRequest
280:      * @param Mage_Catalog_Model_Product $product
281:      * @param string $processMode
282:      * @return array|string
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:         // if adding product from admin area we add all links to product
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:      * Check if product can be bought
328:      *
329:      * @param Mage_Catalog_Model_Product $product
330:      * @return Mage_Bundle_Model_Product_Type
331:      * @throws Mage_Core_Exception
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:      * Prepare additional options/information for order item which will be
360:      * created from this product
361:      *
362:      * @param Mage_Catalog_Model_Product $product
363:      * @return array
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:      * Setting flag if dowenloadable product can be or not in complex product
389:      * based on link can be purchased separately or not
390:      *
391:      * @param Mage_Catalog_Model_Product $product
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:         // Update links_exist attribute value
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:      * Retrieve additional searchable data from type instance
421:      * Using based on product id and store_id data
422:      *
423:      * @param Mage_Catalog_Model_Product $product
424:      * @return array
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:      * Check is product available for sale
448:      *
449:      * @param Mage_Catalog_Model_Product $product
450:      * @return bool
451:      */
452:     public function isSalable($product = null)
453:     {
454:         return $this->hasLinks($product) && parent::isSalable($product);
455:     }
456: 
457:     /**
458:      * Prepare selected options for downloadable product
459:      *
460:      * @param  Mage_Catalog_Model_Product $product
461:      * @param  Varien_Object $buyRequest
462:      * @return array
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:      * Check if downloadable product has links and they can be purchased separately
476:      *
477:      * @param Mage_Catalog_Model_Product $product
478:      * @return bool
479:      */
480:     public function canConfigure($product = null)
481:     {
482:         return $this->hasLinks($product) && $this->getProduct($product)->getLinksPurchasedSeparately();
483:     }
484: }
485: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0