Mage_Downloadable_DownloadController Class Reference

Inheritance diagram for Mage_Downloadable_DownloadController:

Mage_Core_Controller_Front_Action Mage_Core_Controller_Varien_Action

List of all members.

Public Member Functions

 sampleAction ()
 linkSampleAction ()
 linkAction ()

Protected Member Functions

 _getSession ()
 _getCustomerSession ()
 _processDownload ($resource, $resourceType)


Detailed Description

Definition at line 34 of file DownloadController.php.


Member Function Documentation

_getCustomerSession (  )  [protected]

Return customer session object

Returns:
Mage_Customer_Model_Session

Definition at line 52 of file DownloadController.php.

00053     {
00054         return Mage::getSingleton('customer/session');
00055     }

_getSession (  )  [protected]

Return core session object

Returns:
Mage_Core_Model_Session

Definition at line 42 of file DownloadController.php.

00043     {
00044         return Mage::getSingleton('core/session');
00045     }

_processDownload ( resource,
resourceType 
) [protected]

Definition at line 57 of file DownloadController.php.

00058     {
00059         $helper = Mage::helper('downloadable/download');
00060         /* @var $helper Mage_Downloadable_Helper_Download */
00061 
00062         $helper->setResource($resource, $resourceType);
00063 
00064         $fileName       = $helper->getFilename();
00065         $contentType    = $helper->getContentType();
00066 
00067         $this->getResponse()
00068             ->setHttpResponseCode(200)
00069             ->setHeader('Pragma', 'public', true)
00070             ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
00071             ->setHeader('Content-type', $contentType, true);
00072 
00073         if ($fileSize = $helper->getFilesize()) {
00074             $this->getResponse()
00075                 ->setHeader('Content-Length', $fileSize);
00076         }
00077 
00078         if ($contentDisposition = $helper->getContentDisposition()) {
00079             $this->getResponse()
00080                 ->setHeader('Content-Disposition', $contentDisposition . '; filename='.$fileName);
00081         }
00082 
00083         $this->getResponse()
00084             ->clearBody();
00085         $this->getResponse()
00086             ->sendHeaders();
00087 
00088         $helper->output();
00089     }

linkAction (  ) 

Download link action

Definition at line 154 of file DownloadController.php.

00155     {
00156         $id = $this->getRequest()->getParam('id', 0);
00157         $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')->load($id, 'link_hash');
00158         if (! $linkPurchasedItem->getId() ) {
00159             $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link doesn't exist."));
00160             return $this->_redirect('*/customer/products');
00161         }
00162         if (!Mage::helper('downloadable')->getIsShareable($linkPurchasedItem)) {
00163             $customerId = $this->_getCustomerSession()->getCustomerId();
00164             if (!$customerId) {
00165                 $product = Mage::getModel('catalog/product')->load($linkPurchasedItem->getProductId());
00166                 if ($product->getId()) {
00167                     $notice = Mage::helper('downloadable')->__(
00168                         'Please log in to download your product or purchase <a href="%s">%s</a>.',
00169                         $product->getProductUrl(), $product->getName()
00170                     );
00171                 } else {
00172                     $notice = Mage::helper('downloadable')->__('Please log in to download your product.');
00173                 }
00174                 $this->_getCustomerSession()->addNotice($notice);
00175                 $this->_getCustomerSession()->authenticate($this);
00176                 $this->_getCustomerSession()->setBeforeAuthUrl(Mage::getUrl('downloadable/customer/products/'), array('_secure' => true));
00177                 return ;
00178             }
00179             $linkPurchased = Mage::getModel('downloadable/link_purchased')->load($linkPurchasedItem->getPurchasedId());
00180             if ($linkPurchased->getCustomerId() != $customerId) {
00181                 $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__("Requested link doesn't exist."));
00182                 return $this->_redirect('*/customer/products');
00183             }
00184         }
00185         $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought() - $linkPurchasedItem->getNumberOfDownloadsUsed();
00186         if ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE
00187             && ($downloadsLeft || $linkPurchasedItem->getNumberOfDownloadsBought() == 0)) {
00188             $resource = '';
00189             $resourceType = '';
00190             if ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00191                 $resource = $linkPurchasedItem->getLinkUrl();
00192                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00193             } elseif ($linkPurchasedItem->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00194                 $resource = Mage::helper('downloadable/file')->getFilePath(
00195                     Mage_Downloadable_Model_Link::getBasePath(), $linkPurchasedItem->getLinkFile()
00196                 );
00197                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00198             }
00199             try {
00200                 $this->_processDownload($resource, $resourceType);
00201                 $linkPurchasedItem->setNumberOfDownloadsUsed(
00202                     $linkPurchasedItem->getNumberOfDownloadsUsed()+1
00203                 );
00204                 if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0
00205                     && !($linkPurchasedItem->getNumberOfDownloadsBought() - $linkPurchasedItem->getNumberOfDownloadsUsed())) {
00206                     $linkPurchasedItem->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED);
00207                 }
00208                 $linkPurchasedItem->save();
00209                 exit(0);
00210             }
00211             catch (Exception $e) {
00212                 $this->_getCustomerSession()->addError(
00213                     Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.')
00214                 );
00215             }
00216         } elseif ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED) {
00217             $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('Link has expired.'));
00218         } elseif ($linkPurchasedItem->getStatus() == Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING) {
00219             $this->_getCustomerSession()->addNotice(Mage::helper('downloadable')->__('Link is not available.'));
00220         } else {
00221             $this->_getCustomerSession()->addError(
00222                 Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.')
00223             );
00224         }
00225         return $this->_redirect('*/customer/products');
00226     }

linkSampleAction (  ) 

Download link's sample action

Definition at line 125 of file DownloadController.php.

00126     {
00127         $linkId = $this->getRequest()->getParam('link_id', 0);
00128         $link = Mage::getModel('downloadable/link')->load($linkId);
00129         if ($link->getId()) {
00130             $resource = '';
00131             $resourceType = '';
00132             if ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00133                 $resource = $link->getSampleUrl();
00134                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00135             } elseif ($link->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00136                 $resource = Mage::helper('downloadable/file')->getFilePath(
00137                     Mage_Downloadable_Model_Link::getBaseSamplePath(), $link->getSampleFile()
00138                 );
00139                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00140             }
00141             try {
00142                 $this->_processDownload($resource, $resourceType);
00143                 exit(0);
00144             } catch (Mage_Core_Exception $e) {
00145                 $this->_getCustomerSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
00146             }
00147         }
00148         return $this->_redirectReferer();
00149     }

sampleAction (  ) 

Download sample action

Definition at line 95 of file DownloadController.php.

00096     {
00097         $sampleId = $this->getRequest()->getParam('sample_id', 0);
00098         $sample = Mage::getModel('downloadable/sample')->load($sampleId);
00099         if ($sample->getId()) {
00100             $resource = '';
00101             $resourceType = '';
00102             if ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_URL) {
00103                 $resource = $sample->getSampleUrl();
00104                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_URL;
00105             } elseif ($sample->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00106                 $resource = Mage::helper('downloadable/file')->getFilePath(
00107                     Mage_Downloadable_Model_Sample::getBasePath(), $sample->getSampleFile()
00108                 );
00109                 $resourceType = Mage_Downloadable_Helper_Download::LINK_TYPE_FILE;
00110             }
00111             try {
00112                 $this->_processDownload($resource, $resourceType);
00113                 exit(0);
00114             } catch (Mage_Core_Exception $e) {
00115                 $this->_getSession()->addError(Mage::helper('downloadable')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
00116             }
00117         }
00118         return $this->_redirectReferer();
00119     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:08 2009 for Magento by  doxygen 1.5.8