@@ -114,6 +114,13 @@ class AvS_FastSimpleImport_Model_Import_Entity_Product extends AvS_FastSimpleImp
114
114
'country_of_manufacture '
115
115
);
116
116
117
+ /**
118
+ * Downloadable files uploader
119
+ *
120
+ * @var AvS_FastSimpleImport_Model_Import_Uploader_Downloadable
121
+ */
122
+ protected $ _downloadableUploader ;
123
+
117
124
public function __construct ()
118
125
{
119
126
parent ::__construct ();
@@ -1618,8 +1625,8 @@ protected function _getUploader()
1618
1625
$ this ->_fileUploader ->init ();
1619
1626
$ this ->_fileUploader ->removeValidateCallback ('catalog_product_image ' );
1620
1627
1621
- $ tmpDir = Mage::getConfig ()->getOptions ()->getMediaDir () . ' / import ' ;
1622
- $ destDir = Mage::getConfig ()->getOptions ()->getMediaDir () . ' / catalog/ product ' ;
1628
+ $ tmpDir = Mage::getConfig ()->getOptions ()->getMediaDir () . DS . ' import ' ;
1629
+ $ destDir = Mage::getConfig ()->getOptions ()->getMediaDir () . DS . ' catalog ' . DS . ' product ' ;
1623
1630
if (!is_writable ($ destDir )) {
1624
1631
@mkdir ($ destDir , 0777 , true );
1625
1632
}
@@ -1637,30 +1644,44 @@ protected function _getUploader()
1637
1644
return $ this ->_fileUploader ;
1638
1645
}
1639
1646
1647
+ /** Returns an object for upload downloadable files
1648
+ *
1649
+ * @return AvS_FastSimpleImport_Model_Import_Uploader_Downloadable
1650
+ */
1651
+ protected function _getDownloadableUploader ()
1652
+ {
1653
+ if (is_null ($ this ->_downloadableUploader )) {
1654
+ // make sure to pass null as a constructor argument, so that no upload file is set!
1655
+ $ this ->_downloadableUploader = Mage::getModel ('fastsimpleimport/import_uploader_downloadable ' , null );
1656
+ $ this ->_downloadableUploader ->init ();
1657
+ $ tmpDir = Mage::getConfig ()->getOptions ()->getMediaDir () . DS . 'import ' ;
1658
+ $ destDir = Mage::getModel ('downloadable/link ' )->getBasePath ();
1659
+ if ( ! is_writable ($ destDir )) {
1660
+ @mkdir ($ destDir , 0777 , true );
1661
+ }
1662
+ if ( ! file_exists ($ tmpDir )) {
1663
+ @mkdir ($ tmpDir , 0777 , true );
1664
+ }
1665
+ if ( ! $ this ->_downloadableUploader ->setTmpDir ($ tmpDir )) {
1666
+
1667
+ Mage::throwException ("File directory ' {$ tmpDir }' is not readable. " );
1668
+ }
1669
+ if ( ! $ this ->_downloadableUploader ->setDestDir ($ destDir )) {
1670
+ Mage::throwException ("File directory ' {$ destDir }' is not writable. " );
1671
+ }
1672
+ }
1673
+ return $ this ->_downloadableUploader ;
1674
+ }
1675
+
1640
1676
/**
1641
1677
* @param $fileName
1642
- * @return bool
1678
+ *
1679
+ * @return string
1643
1680
*/
1644
- protected function moveDownloadableFile ($ fileName )
1681
+ protected function _moveDownloadableFile ($ fileName )
1645
1682
{
1646
- $ filePath = $ this ->_getUploader ()->getTmpDir () . $ fileName ;
1647
- $ basePath = Mage::getModel ('downloadable/link ' )->getBasePath ();
1648
- $ destDirectory = dirname (Mage::helper ('downloadable/file ' )->getFilePath ($ basePath , $ fileName ));
1649
- // make sure that the destination directory exists!
1650
- $ ioObject = new Varien_Io_File ();
1651
- try {
1652
- $ ioObject ->open (array ('path ' => $ destDirectory ));
1653
- } catch (Exception $ e ) {
1654
- $ ioObject ->mkdir ($ destDirectory , 0777 , true );
1655
- $ ioObject ->open (array ('path ' => $ destDirectory ));
1656
- }
1657
- $ destFile = $ basePath . DS . $ fileName ;
1658
- $ sourceFile = realpath ($ filePath );
1659
- if ($ sourceFile !== false ) {
1660
- return copy ($ sourceFile , $ destFile );
1661
- } else {
1662
- return false ;
1663
- }
1683
+ $ res = $ this ->_getDownloadableUploader ()->move ($ fileName );
1684
+ return $ res ['file ' ];
1664
1685
}
1665
1686
1666
1687
/**
@@ -2066,6 +2087,15 @@ protected function _saveDownloadableLinks(array $downloadableData)
2066
2087
->where ("$ fieldToSelect = (?) " , $ insertValue [$ linkType ]));
2067
2088
2068
2089
if (!in_array ($ insertValue [$ linkType ], $ insertedDownloadableLinks ) && !$ alreadyImported ) {
2090
+
2091
+ if (array_key_exists ($ fieldToSelect , $ insertValue )) {
2092
+ $ insertedDownloadableLinks [] = $ insertValue [$ fieldToSelect ];
2093
+ }
2094
+
2095
+ if ($ linkType == 'file ' ) {
2096
+ $ this ->_moveDownloadableFile ($ insertValue [$ fieldToSelect ]);
2097
+ }
2098
+
2069
2099
$ valueArr = array (
2070
2100
'product_id ' => $ productId ,
2071
2101
'number_of_downloads ' => $ insertValue ['number_of_downloads ' ],
@@ -2079,8 +2109,7 @@ protected function _saveDownloadableLinks(array $downloadableData)
2079
2109
$ valueArr ['link_type ' ] = 'url ' ;
2080
2110
}
2081
2111
2082
- $ this ->_connection
2083
- ->insertOnDuplicate ($ downloadableLinkTableName , $ valueArr , array ('product_id ' ));
2112
+ $ this ->_connection ->insertOnDuplicate ($ downloadableLinkTableName , $ valueArr , array ('product_id ' ));
2084
2113
2085
2114
$ linkId = $ this ->_connection ->fetchOne ('SELECT MAX(`link_id`) FROM ' . $ downloadableLinkTableName );
2086
2115
@@ -2090,15 +2119,8 @@ protected function _saveDownloadableLinks(array $downloadableData)
2090
2119
'link_id ' => $ linkId ,
2091
2120
);
2092
2121
2093
- $ this ->_connection
2094
- ->insertOnDuplicate ($ downloadableLinkTitleTableName , $ valueArr , array ('link_id ' ));
2122
+ $ this ->_connection ->insertOnDuplicate ($ downloadableLinkTitleTableName , $ valueArr , array ('link_id ' ));
2095
2123
2096
- if ($ linkType == 'file ' ) {
2097
- $ this ->moveDownloadableFile ($ insertValue [$ fieldToSelect ]);
2098
- }
2099
- if (array_key_exists ($ fieldToSelect , $ insertValue )) {
2100
- $ insertedDownloadableLinks [] = $ insertValue [$ fieldToSelect ];
2101
- }
2102
2124
}
2103
2125
}
2104
2126
}
0 commit comments