Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Link.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Downloadable\Helper\Download as DownloadHelper;
11 
12 class Link extends \Magento\Catalog\Controller\Adminhtml\Product\Edit
13 {
17  protected function _createLink()
18  {
19  return $this->_objectManager->create(\Magento\Downloadable\Model\Link::class);
20  }
21 
25  protected function _getLink()
26  {
27  return $this->_objectManager->get(\Magento\Downloadable\Model\Link::class);
28  }
29 
37  protected function _processDownload($resource, $resourceType)
38  {
39  /* @var $helper \Magento\Downloadable\Helper\Download */
40  $helper = $this->_objectManager->get(\Magento\Downloadable\Helper\Download::class);
41  $helper->setResource($resource, $resourceType);
42 
43  $fileName = $helper->getFilename();
44  $contentType = $helper->getContentType();
45 
47  $response = $this->getResponse();
48  $response->setHttpResponseCode(
49  200
50  )->setHeader(
51  'Pragma',
52  'public',
53  true
54  )->setHeader(
55  'Cache-Control',
56  'must-revalidate, post-check=0, pre-check=0',
57  true
58  )->setHeader(
59  'Content-type',
60  $contentType,
61  true
62  );
63 
64  if ($fileSize = $helper->getFileSize()) {
65  $response->setHeader('Content-Length', $fileSize);
66  }
67  //Setting disposition as state in the config or forcing it for HTML.
69  $contentDisposition = $helper->getContentDisposition();
70  if (!$contentDisposition || $contentType === 'text/html') {
71  $contentDisposition = 'attachment';
72  }
73  $response->setHeader(
74  'Content-Disposition',
75  $contentDisposition . '; filename=' . $fileName
76  );
77  //Rendering
78  $response->clearBody();
79  $response->sendHeaders();
80 
81  $helper->output();
82  }
83 
89  public function execute()
90  {
91  $linkId = $this->getRequest()->getParam('id', 0);
92  $type = $this->getRequest()->getParam('type', 0);
94  $link = $this->_createLink()->load($linkId);
95  if ($link->getId()) {
96  $resource = '';
97  $resourceType = '';
98  if ($type == 'link') {
99  if ($link->getLinkType() == DownloadHelper::LINK_TYPE_URL) {
100  $resource = $link->getLinkUrl();
101  $resourceType = DownloadHelper::LINK_TYPE_URL;
102  } elseif ($link->getLinkType() == DownloadHelper::LINK_TYPE_FILE) {
103  $resource = $this->_objectManager->get(
104  \Magento\Downloadable\Helper\File::class
105  )->getFilePath(
106  $this->_getLink()->getBasePath(),
107  $link->getLinkFile()
108  );
109  $resourceType = DownloadHelper::LINK_TYPE_FILE;
110  }
111  } else {
112  if ($link->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
113  $resource = $link->getSampleUrl();
114  $resourceType = DownloadHelper::LINK_TYPE_URL;
115  } elseif ($link->getSampleType() == DownloadHelper::LINK_TYPE_FILE) {
116  $resource = $this->_objectManager->get(
117  \Magento\Downloadable\Helper\File::class
118  )->getFilePath(
119  $this->_getLink()->getBaseSamplePath(),
120  $link->getSampleFile()
121  );
122  $resourceType = DownloadHelper::LINK_TYPE_FILE;
123  }
124  }
125  try {
126  $this->_processDownload($resource, $resourceType);
127  } catch (\Magento\Framework\Exception\LocalizedException $e) {
128  $this->messageManager->addError(__('Something went wrong while getting the requested content.'));
129  }
130  }
131  }
132 }
$response
Definition: 404.php:11
$helper
Definition: iframe.phtml:13
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15