/** * Upload file controller action * * @return \Magento\Framework\Controller\ResultInterface */ public function executeInternal() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = $this->_sample->getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = $this->_link->getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = $this->_link->getBaseSampleTmpPath(); } try { $uploader = $this->uploaderFactory->create(['fileId' => $type]); $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader); if (!$result) { throw new \Exception('File can not be moved from temporary folder to the destination folder.'); } /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']); $result['path'] = str_replace('\\', '/', $result['path']); if (isset($result['file'])) { $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/'); $this->storageDatabase->saveFile($relativePath); } $result['cookie'] = ['name' => $this->_getSession()->getName(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()]; } catch (\Exception $e) { $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; } return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result); }
/** * Check is link shareable or not * * @param \Magento\Downloadable\Model\Link|Item $link * @return bool */ public function getIsShareable($link) { $shareable = false; switch ($link->getIsShareable()) { case \Magento\Downloadable\Model\Link::LINK_SHAREABLE_YES: case \Magento\Downloadable\Model\Link::LINK_SHAREABLE_NO: $shareable = (bool) $link->getIsShareable(); break; case \Magento\Downloadable\Model\Link::LINK_SHAREABLE_CONFIG: $shareable = (bool) $this->_scopeConfig->isSetFlag(\Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } return $shareable; }
public function testGetLinkData() { $expectingFileData = array('file' => array('file' => 'file/link.gif', 'name' => '<a href="final_url">link.gif</a>', 'size' => '1.1', 'status' => 'old'), 'sample_file' => array('file' => 'file/sample.gif', 'name' => '<a href="final_url">sample.gif</a>', 'size' => '1.1', 'status' => 'old')); $this->productModel->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable')); $this->productModel->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->downloadableProductModel)); $this->productModel->expects($this->any())->method('getStoreId')->will($this->returnValue(0)); $this->downloadableProductModel->expects($this->any())->method('getLinks')->will($this->returnValue(array($this->downloadableLinkModel))); $this->coreRegistry->expects($this->any())->method('registry')->will($this->returnValue($this->productModel)); $this->downloadableLinkModel->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->downloadableLinkModel->expects($this->any())->method('getTitle')->will($this->returnValue('Link Title')); $this->downloadableLinkModel->expects($this->any())->method('getPrice')->will($this->returnValue('10')); $this->downloadableLinkModel->expects($this->any())->method('getNumberOfDownloads')->will($this->returnValue('6')); $this->downloadableLinkModel->expects($this->any())->method('getLinkUrl')->will($this->returnValue(null)); $this->downloadableLinkModel->expects($this->any())->method('getLinkType')->will($this->returnValue('file')); $this->downloadableLinkModel->expects($this->any())->method('getSampleFile')->will($this->returnValue('file/sample.gif')); $this->downloadableLinkModel->expects($this->any())->method('getSampleType')->will($this->returnValue('file')); $this->downloadableLinkModel->expects($this->any())->method('getSortOrder')->will($this->returnValue(0)); $this->downloadableLinkModel->expects($this->any())->method('getLinkFile')->will($this->returnValue('file/link.gif')); $this->downloadableLinkModel->expects($this->any())->method('getStoreTitle')->will($this->returnValue('Store Title')); $this->escaper->expects($this->any())->method('escapeHtml')->will($this->returnValue('Link Title')); $this->fileHelper->expects($this->any())->method('getFilePath')->will($this->returnValue('/file/path/link.gif')); $this->fileHelper->expects($this->any())->method('ensureFileInFilesystem')->will($this->returnValue(true)); $this->fileHelper->expects($this->any())->method('getFileSize')->will($this->returnValue('1.1')); $this->urlBuilder->expects($this->any())->method('getUrl')->will($this->returnValue('final_url')); $linkData = $this->block->getLinkData(); foreach ($linkData as $link) { $fileSave = $link->getFileSave(0); $sampleFileSave = $link->getSampleFileSave(0); $this->assertEquals($expectingFileData['file'], $fileSave); $this->assertEquals($expectingFileData['sample_file'], $sampleFileSave); } }
/** * Add Link File info into $linkData * * @param array $linkData * @param LinkInterface $link * @return array */ protected function addLinkFile(array $linkData, LinkInterface $link) { $linkFile = $link->getLinkFile(); if ($linkFile) { $file = $this->downloadableFile->getFilePath($this->linkModel->getBasePath(), $linkFile); if ($this->downloadableFile->ensureFileInFilesystem($file)) { $linkData['file'][0] = ['file' => $linkFile, 'name' => $this->downloadableFile->getFileFromPathFile($linkFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/link', ['id' => $link->getId(), 'type' => 'link', '_secure' => true])]; } } return $linkData; }
/** * Retrieve destination directory for given content type * * @param string $contentType * @return string * @throws \InvalidArgumentException */ protected function getDestinationDirectory($contentType) { switch ($contentType) { case 'link_file': $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseTmpPath()); break; case 'link_sample_file': $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseSampleTmpPath()); break; case 'sample': $directory = $this->mediaDirectory->getAbsolutePath($this->sampleConfig->getBaseTmpPath()); break; default: throw new \InvalidArgumentException('Invalid downloadable file content type.'); } return $directory; }
/** * Return array of links * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getLinkData() { $linkArr = []; if ($this->getProduct()->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $linkArr; } $links = $this->getProduct()->getTypeInstance()->getLinks($this->getProduct()); $priceWebsiteScope = $this->getIsPriceWebsiteScope(); $fileHelper = $this->_downloadableFile; foreach ($links as $item) { $tmpLinkItem = ['link_id' => $item->getId(), 'title' => $this->escapeHtml($item->getTitle()), 'price' => $this->getCanReadPrice() ? $this->getPriceValue($item->getPrice()) : '', 'number_of_downloads' => $item->getNumberOfDownloads(), 'is_shareable' => $item->getIsShareable(), 'link_url' => $item->getLinkUrl(), 'link_type' => $item->getLinkType(), 'sample_file' => $item->getSampleFile(), 'sample_url' => $item->getSampleUrl(), 'sample_type' => $item->getSampleType(), 'sort_order' => $item->getSortOrder()]; $linkFile = $item->getLinkFile(); if ($linkFile) { $file = $fileHelper->getFilePath($this->_link->getBasePath(), $linkFile); $fileExist = $fileHelper->ensureFileInFilesystem($file); if ($fileExist) { $name = '<a href="' . $this->getUrl('adminhtml/downloadable_product_edit/link', ['id' => $item->getId(), 'type' => 'link', '_secure' => true]) . '">' . $fileHelper->getFileFromPathFile($linkFile) . '</a>'; $tmpLinkItem['file_save'] = [['file' => $linkFile, 'name' => $name, 'size' => $fileHelper->getFileSize($file), 'status' => 'old']]; } } $sampleFile = $item->getSampleFile(); if ($sampleFile) { $file = $fileHelper->getFilePath($this->_link->getBaseSamplePath(), $sampleFile); $fileExist = $fileHelper->ensureFileInFilesystem($file); if ($fileExist) { $name = '<a href="' . $this->getUrl('adminhtml/downloadable_product_edit/link', ['id' => $item->getId(), 'type' => 'sample', '_secure' => true]) . '">' . $fileHelper->getFileFromPathFile($sampleFile) . '</a>'; $tmpLinkItem['sample_file_save'] = [['file' => $item->getSampleFile(), 'name' => $name, 'size' => $fileHelper->getFileSize($file), 'status' => 'old']]; } } if ($item->getNumberOfDownloads() == '0') { $tmpLinkItem['is_unlimited'] = ' checked="checked"'; } if ($this->getProduct()->getStoreId() && $item->getStoreTitle()) { $tmpLinkItem['store_title'] = $item->getStoreTitle(); } if ($this->getProduct()->getStoreId() && $priceWebsiteScope) { $tmpLinkItem['website_price'] = $item->getWebsitePrice(); } $linkArr[] = new \Magento\Framework\DataObject($tmpLinkItem); } return $linkArr; }
/** * @dataProvider executeDataProvider * @param string $fileType */ public function testExecuteUrl($fileType) { $this->request->expects($this->at(0))->method('getParam')->with('id', 0)->will($this->returnValue(1)); $this->request->expects($this->at(1))->method('getParam')->with('type', 0)->will($this->returnValue($fileType)); $this->response->expects($this->once())->method('setHttpResponseCode')->will($this->returnSelf()); $this->response->expects($this->once())->method('clearBody')->will($this->returnSelf()); $this->response->expects($this->any())->method('setHeader')->will($this->returnSelf()); $this->response->expects($this->once())->method('sendHeaders')->will($this->returnSelf()); $this->objectManager->expects($this->at(1))->method('get')->with('Magento\\Downloadable\\Helper\\Download')->will($this->returnValue($this->downloadHelper)); $this->downloadHelper->expects($this->once())->method('setResource')->will($this->returnSelf()); $this->downloadHelper->expects($this->once())->method('getFilename')->will($this->returnValue('link.jpg')); $this->downloadHelper->expects($this->once())->method('getContentType')->will($this->returnSelf('url')); $this->downloadHelper->expects($this->once())->method('getFileSize')->will($this->returnValue(null)); $this->downloadHelper->expects($this->once())->method('getContentDisposition')->will($this->returnValue(null)); $this->downloadHelper->expects($this->once())->method('output')->will($this->returnSelf()); $this->linkModel->expects($this->once())->method('load')->will($this->returnSelf()); $this->linkModel->expects($this->once())->method('getId')->will($this->returnValue('1')); $this->linkModel->expects($this->once())->method('get' . $fileType . 'Type')->will($this->returnValue('url')); $this->linkModel->expects($this->once())->method('get' . $fileType . 'Url')->will($this->returnValue('http://url.magento.com')); $this->objectManager->expects($this->once())->method('create')->will($this->returnValue($this->linkModel)); $this->link->execute(); }
/** * Returns whether link checked by default or not * * @param Link $link * @return bool * @SuppressWarnings(PHPMD.BooleanGetMethodName) */ public function getIsLinkChecked($link) { $configValue = $this->getProduct()->getPreconfiguredValues()->getLinks(); if (!$configValue || !is_array($configValue)) { return false; } return $configValue && in_array($link->getId(), $configValue); }
/** * Delete data by item(s) * * @param \Magento\Downloadable\Model\Link|array|int $items * @return $this */ public function deleteItems($items) { $connection = $this->getConnection(); if ($items instanceof \Magento\Downloadable\Model\Link) { $where = ['link_id = ?' => $items->getId()]; } elseif (is_array($items)) { $where = ['link_id in (?)' => $items]; } else { $where = ['sample_id = ?' => $items]; } $connection->delete($this->getMainTable(), $where); $connection->delete($this->getTable('downloadable_link_title'), $where); $connection->delete($this->getTable('downloadable_link_price'), $where); return $this; }
/** * Subroutine for build link * * @param \Magento\Downloadable\Model\Link $resourceData * @param \Magento\Downloadable\Api\Data\LinkInterface $dataObject * @return null */ protected function setBasicFields($resourceData, $dataObject) { $dataObject->setId($resourceData->getId()); $storeTitle = $resourceData->getStoreTitle(); $title = $resourceData->getTitle(); if (!empty($storeTitle)) { $dataObject->setTitle($storeTitle); } else { $dataObject->setTitle($title); } $dataObject->setSortOrder($resourceData->getSortOrder()); $dataObject->setSampleType($resourceData->getSampleType()); $dataObject->setSampleFile($resourceData->getSampleFile()); $dataObject->setSampleUrl($resourceData->getSampleUrl()); }
/** * @param Link $link * @return \Magento\Framework\Pricing\Amount\AmountInterface */ public function getLinkAmount(Link $link) { return $this->calculator->getAmount($link->getPrice(), $link->getProduct()); }
/** * @param Link $link * @return \Magento\Framework\Pricing\Amount\AmountInterface */ public function getLinkAmount(Link $link) { $price = $link->getPrice(); $convertedPrice = $this->priceCurrency->convertAndRound($price); return $this->calculator->getAmount($convertedPrice, $link->getProduct()); }
/** * Delete data by item(s) * * @param \Magento\Downloadable\Model\Link|array|int $items * @return $this */ public function deleteItems($items) { $writeAdapter = $this->_getWriteAdapter(); $where = []; if ($items instanceof \Magento\Downloadable\Model\Link) { $where = ['link_id = ?' => $items->getId()]; } elseif (is_array($items)) { $where = ['link_id in (?)' => $items]; } else { $where = ['sample_id = ?' => $items]; } if ($where) { $writeAdapter->delete($this->getMainTable(), $where); $writeAdapter->delete($this->getTable('downloadable_link_title'), $where); $writeAdapter->delete($this->getTable('downloadable_link_price'), $where); } return $this; }
/** * Build file info data object * * @param string $entityType 'link' or 'sample' * @param \Magento\Downloadable\Model\Link|\Magento\Downloadable\Model\Sample $resourceData * @return \Magento\Downloadable\Service\V1\DownloadableLink\Data\DownloadableResourceInfo|null */ protected function entityInfoGenerator($entityType, $resourceData) { $type = $resourceData->getData($entityType . '_type'); if (empty($type)) { return null; } $this->resourceBuilder->populateWithArray([]); $this->resourceBuilder->setType($type); $this->resourceBuilder->setUrl($resourceData->getData($entityType . '_url')); $this->resourceBuilder->setFile($resourceData->getData($entityType . '_file')); return $this->resourceBuilder->create(); }