-
Notifications
You must be signed in to change notification settings - Fork 144
Refactored Downloadable Files Import #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,13 @@ class AvS_FastSimpleImport_Model_Import_Entity_Product extends AvS_FastSimpleImp | |
| 'country_of_manufacture' | ||
| ); | ||
|
|
||
| /** | ||
| * Downloadable files uploader | ||
| * | ||
| * @var Mage_ImportExport_Model_Import_Uploader | ||
| */ | ||
| protected $_downloadableUploader; | ||
|
|
||
| public function __construct() | ||
| { | ||
| parent::__construct(); | ||
|
|
@@ -1612,29 +1619,46 @@ protected function _getUploader() | |
| } | ||
|
|
||
| /** | ||
| * @param $fileName | ||
| * @return bool | ||
| * Returns an object for upload downloadable files | ||
| * | ||
| * @return Mage_ImportExport_Model_Import_Uploader | ||
| */ | ||
| protected function moveDownloadableFile($fileName) | ||
| protected function _getDownloadableUploader() | ||
| { | ||
| $filePath = $this->_getUploader()->getTmpDir() . $fileName; | ||
| $basePath = Mage::getModel('downloadable/link')->getBasePath(); | ||
| $destDirectory = dirname(Mage::helper('downloadable/file')->getFilePath($basePath, $fileName)); | ||
| // make sure that the destination directory exists! | ||
| $ioObject = new Varien_Io_File(); | ||
| try { | ||
| $ioObject->open(array('path' => $destDirectory)); | ||
| } catch (Exception $e) { | ||
| $ioObject->mkdir($destDirectory, 0777, true); | ||
| $ioObject->open(array('path' => $destDirectory)); | ||
| } | ||
| $destFile = $basePath . DS . $fileName; | ||
| $sourceFile = realpath($filePath); | ||
| if ($sourceFile !== false) { | ||
| return copy($sourceFile, $destFile); | ||
| } else { | ||
| return false; | ||
| if (is_null($this->_downloadableUploader)) { | ||
| // make sure to pass null as a constructor argument, so that no upload file is set! | ||
| $this->_downloadableUploader = Mage::getModel('fastsimpleimport/import_uploader_downloadable', null); | ||
| $this->_downloadableUploader->init(); | ||
|
|
||
| $tmpDir = Mage::getConfig()->getOptions()->getMediaDir() . '/import'; | ||
| $destDir = Mage::getModel('downloadable/link')->getBasePath(); | ||
| if ( ! is_writable($destDir)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really what you want to do? Check if not writeable and then create it? I really have strong opinions against the usage of |
||
| @mkdir($destDir, 0777, true); | ||
| } | ||
| if ( ! file_exists($tmpDir)) { | ||
| @mkdir($tmpDir, 0777, true); | ||
| } | ||
| if ( ! $this->_downloadableUploader->setTmpDir($tmpDir)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you change the check for existence and writability you could supply better Exception messages. |
||
| Mage::throwException("File directory '{$tmpDir}' is not readable."); | ||
| } | ||
| if ( ! $this->_downloadableUploader->setDestDir($destDir)) { | ||
| Mage::throwException("File directory '{$destDir}' is not writable."); | ||
| } | ||
| } | ||
|
|
||
| return $this->_downloadableUploader; | ||
| } | ||
|
|
||
| /** | ||
| * @param $fileName | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function _moveDownloadableFile($fileName) | ||
| { | ||
| $res = $this->_getDownloadableUploader()->move($fileName); | ||
|
|
||
| return $res['file']; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -2012,11 +2036,11 @@ protected function _saveDownloadableLinks(array $downloadableData) | |
| return $this; | ||
| } | ||
|
|
||
| $downloadableLinkTableName = $this->_getDownloadableLinksTableName(); | ||
| $downloadableLinkTableName = $this->_getDownloadableLinksTableName(); | ||
| $downloadableLinkTitleTableName = $this->_getDownloadableLinksTitleTableName(); | ||
|
|
||
| foreach ($downloadableData as $productSku => $downloadableLink) { | ||
| $productId = $this->_newSku[$productSku]['entity_id']; | ||
| $productId = $this->_newSku[$productSku]['entity_id']; | ||
| $insertedDownloadableLinks = []; | ||
|
|
||
| if (Mage_ImportExport_Model_Import::BEHAVIOR_APPEND != $this->getBehavior()) { | ||
|
|
@@ -2032,32 +2056,27 @@ protected function _saveDownloadableLinks(array $downloadableData) | |
| ->where('product_id IN (?)', $productId) | ||
| ->where('link_file = (?)', $insertValue['file'])); | ||
|
|
||
| if (!in_array($insertValue['file'], $insertedDownloadableLinks) && !$alreadyImported) { | ||
| $valueArr = array( | ||
| 'product_id' => $productId, | ||
| 'link_file' => $insertValue['file'], | ||
| 'link_type' => 'file', | ||
| if ( ! in_array($insertValue['file'], $insertedDownloadableLinks) && ! $alreadyImported) { | ||
| $insertedDownloadableLinks[] = $insertValue['file']; | ||
| $linkFile = $this->_moveDownloadableFile($insertValue['file']); | ||
| $valueArr = array( | ||
| 'product_id' => $productId, | ||
| 'link_file' => $linkFile, | ||
| 'link_type' => 'file', | ||
| 'number_of_downloads' => $insertValue['number_of_downloads'], | ||
| ); | ||
|
|
||
| $this->_connection | ||
| ->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id')); | ||
| $this->_connection->insertOnDuplicate($downloadableLinkTableName, $valueArr, array('product_id')); | ||
|
|
||
| $linkId = $this->_connection->fetchOne('SELECT MAX(`link_id`) FROM ' . $downloadableLinkTableName); | ||
|
|
||
| $valueArr = array( | ||
| 'store_id' => $insertValue['store_id'], | ||
| 'title' => $insertValue['title'], | ||
| 'link_id' => $linkId, | ||
| 'store_id' => $insertValue['store_id'], | ||
| 'title' => $insertValue['title'], | ||
| 'link_id' => $linkId, | ||
| ); | ||
|
|
||
| $this->_connection | ||
| ->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id')); | ||
|
|
||
| $this->moveDownloadableFile($insertValue['file']); | ||
| $insertedDownloadableLinks[] = $insertValue['file']; | ||
|
|
||
|
|
||
| $this->_connection->insertOnDuplicate($downloadableLinkTitleTableName, $valueArr, array('link_id')); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| class AvS_FastSimpleImport_Model_Import_Uploader_Downloadable extends Mage_ImportExport_Model_Import_Uploader | ||
| { | ||
|
|
||
| // set this to an empty array, so that all mime types and file extensions are allowed | ||
| protected $_allowedMimeTypes = array(); | ||
|
|
||
| public function init() | ||
| { | ||
| parent::init(); | ||
|
|
||
| // we want to support any file type, so remove the image specific validators | ||
| $this->removeValidateCallback('catalog_product_image'); | ||
| $this->removeValidateCallback(Mage_Core_Model_File_Validator_Image::NAME); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usage of
DSinstead of/for compatibility reasons.