Example #1
0
 /**
  * Upload file controller action
  */
 public function uploadAction()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     try {
         $uploader = new Mage_Core_Model_File_Uploader($type);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $result = $uploader->save($tmpPath);
         /**
          * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
          */
         $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']);
         $result['path'] = str_replace(DS, "/", $result['path']);
         if (isset($result['file'])) {
             $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
             Mage::helper('core/file_storage_database')->saveFile($fullPath);
         }
         $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain());
     } catch (Exception $e) {
         $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode());
     }
     $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
 }
Example #2
0
 /**
  * Upload file controller action
  */
 public function uploadAction()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     try {
         $uploader = new Mage_Core_Model_File_Uploader($type);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $result = $uploader->save($tmpPath);
         if (isset($result['file'])) {
             $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
             Mage::helper('Mage_Core_Helper_File_Storage_Database')->saveFile($fullPath);
         }
         $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain());
     } catch (Exception $e) {
         $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode());
     }
     $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode($result));
 }
Example #3
0
 /**
  * Decode file from base64 and upload it to donwloadable 'tmp' folder
  *
  * @param array $fileInfo
  * @param string $type
  * @return string
  */
 protected function _uploadFile($fileInfo, $type)
 {
     $tmpPath = '';
     if ($type == 'sample') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'link') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     try {
         $uploader = Mage::getModel('downloadable/link_api_uploader', $fileInfo);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $result = $uploader->save($tmpPath);
         if (isset($result['file'])) {
             $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
             Mage::helper('core/file_storage_database')->saveFile($fullPath);
         }
     } catch (Exception $e) {
         if ($e->getMessage() != '') {
             $this->_fault('upload_failed', $e->getMessage());
         } else {
             $this->_fault($e->getCode());
         }
     }
     $result['status'] = 'new';
     $result['name'] = substr($result['file'], strrpos($result['file'], '/') + 1);
     return Mage::helper('core')->jsonEncode(array($result));
 }
 /**
  * Upload file controller action
  */
 public function uploadAction()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     try {
         $uploader = new Varien_File_Uploader($type);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $result = $uploader->save($tmpPath);
         $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain());
     } catch (Exception $e) {
         $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode());
     }
     $this->getResponse()->setBody(Zend_Json::encode($result));
 }
Example #5
0
 /**
  * Save Product downloadable information (links and samples)
  *
  * @param Mage_Catalog_Model_Product $product
  * @return Mage_Downloadable_Model_Product_Type
  */
 public function save($product = null)
 {
     parent::save($product);
     $product = $this->getProduct($product);
     /* @var Mage_Catalog_Model_Product $product */
     if ($data = $product->getDownloadableData()) {
         if (isset($data['sample'])) {
             $_deleteItems = array();
             foreach ($data['sample'] as $sampleItem) {
                 if ($sampleItem['is_delete'] == '1') {
                     if ($sampleItem['sample_id']) {
                         $_deleteItems[] = $sampleItem['sample_id'];
                     }
                 } else {
                     unset($sampleItem['is_delete']);
                     if (!$sampleItem['sample_id']) {
                         unset($sampleItem['sample_id']);
                     }
                     $sampleModel = Mage::getModel('downloadable/sample');
                     $files = array();
                     if (isset($sampleItem['file'])) {
                         $files = Mage::helper('core')->jsonDecode($sampleItem['file']);
                         unset($sampleItem['file']);
                     }
                     $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId());
                     if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
                         $sampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Sample::getBaseTmpPath(), Mage_Downloadable_Model_Sample::getBasePath(), $files);
                         $sampleModel->setSampleFile($sampleFileName);
                     }
                     $sampleModel->save();
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems);
             }
         }
         if (isset($data['link'])) {
             $_deleteItems = array();
             foreach ($data['link'] as $linkItem) {
                 if ($linkItem['is_delete'] == '1') {
                     if ($linkItem['link_id']) {
                         $_deleteItems[] = $linkItem['link_id'];
                     }
                 } else {
                     unset($linkItem['is_delete']);
                     if (!$linkItem['link_id']) {
                         unset($linkItem['link_id']);
                     }
                     $files = array();
                     if (isset($linkItem['file'])) {
                         $files = Mage::helper('core')->jsonDecode($linkItem['file']);
                         unset($linkItem['file']);
                     }
                     $sample = array();
                     if (isset($linkItem['sample'])) {
                         $sample = $linkItem['sample'];
                         unset($linkItem['sample']);
                     }
                     $linkModel = Mage::getModel('downloadable/link')->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds());
                     if (null === $linkModel->getPrice()) {
                         $linkModel->setPrice(0);
                     }
                     if ($linkModel->getIsUnlimited()) {
                         $linkModel->setNumberOfDownloads(0);
                     }
                     $sampleFile = array();
                     if ($sample && isset($sample['type'])) {
                         if ($sample['type'] == 'url' && $sample['url'] != '') {
                             $linkModel->setSampleUrl($sample['url']);
                         }
                         $linkModel->setSampleType($sample['type']);
                         $sampleFile = Mage::helper('core')->jsonDecode($sample['file']);
                     }
                     if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
                         $linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseTmpPath(), Mage_Downloadable_Model_Link::getBasePath(), $files);
                         $linkModel->setLinkFile($linkFileName);
                     }
                     if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
                         $linkSampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseSampleTmpPath(), Mage_Downloadable_Model_Link::getBaseSamplePath(), $sampleFile);
                         $linkModel->setSampleFile($linkSampleFileName);
                     }
                     $linkModel->save();
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems);
             }
             if ($this->getProduct($product)->getLinksPurchasedSeparately()) {
                 $this->getProduct($product)->setIsCustomOptionChanged();
             }
         }
     }
     return $this;
 }
Example #6
0
 /**
  * Decode file from base64 and upload it to donwloadable 'tmp' folder
  *
  * @param array $fileInfo
  * @param string $type
  * @return string
  */
 protected function _uploadFile($fileInfo, $type)
 {
     $tmpPath = '';
     if ($type == 'sample') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'link') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     $url = $fileInfo['url'];
     $remoteFileName = $fileInfo['name'];
     $ioAdapter = new Varien_Io_File();
     $ioAdapter->checkAndCreateFolder($tmpPath);
     $ioAdapter->open(array('path' => $tmpPath));
     $fileName = $tmpPath . DS . Varien_File_Uploader::getCorrectFileName($remoteFileName);
     if ($ioAdapter->cp($url, $fileName)) {
         Mage::helper('core/file_storage_database')->saveFile($fileName);
     }
     $result['file'] = $remoteFileName;
     $result['status'] = 'new';
     $result['name'] = $remoteFileName;
     return Mage::helper('core')->jsonEncode(array($result));
 }
Example #7
0
 protected function _processDownloadableProduct($product, &$importData)
 {
     // comment if --------------------------
     //if ($new) {
     $filearrayforimports = array();
     $downloadableitems = array();
     $downloadableitemsoptionscount = 0;
     //THIS IS FOR DOWNLOADABLE OPTIONS
     $commadelimiteddata = explode('|', $importData['downloadable_options']);
     foreach ($commadelimiteddata as $data) {
         $configBundleOptionsCodes = $this->userCSVDataAsArray($data);
         $downloadableitems['link'][$downloadableitemsoptionscount]['is_delete'] = 0;
         $downloadableitems['link'][$downloadableitemsoptionscount]['link_id'] = 0;
         $downloadableitems['link'][$downloadableitemsoptionscount]['title'] = $configBundleOptionsCodes[0];
         $downloadableitems['link'][$downloadableitemsoptionscount]['price'] = $configBundleOptionsCodes[1];
         $downloadableitems['link'][$downloadableitemsoptionscount]['number_of_downloads'] = $configBundleOptionsCodes[2];
         $downloadableitems['link'][$downloadableitemsoptionscount]['is_shareable'] = 2;
         if (isset($configBundleOptionsCodes[5])) {
             #$downloadableitems['link'][$downloadableitemsoptionscount]['sample'] = '';
             $downloadableitems['link'][$downloadableitemsoptionscount]['sample'] = array('file' => '[]', 'type' => 'url', 'url' => '' . $configBundleOptionsCodes[5] . '');
         } else {
             $downloadableitems['link'][$downloadableitemsoptionscount]['sample'] = '';
         }
         $downloadableitems['link'][$downloadableitemsoptionscount]['file'] = '';
         $downloadableitems['link'][$downloadableitemsoptionscount]['type'] = $configBundleOptionsCodes[3];
         #$downloadableitems['link'][$downloadableitemsoptionscount]['link_url'] = $configBundleOptionsCodes[4];
         if ($configBundleOptionsCodes[3] == "file") {
             #$filearrayforimport = array('file'  => 'media/import/mypdf.pdf' , 'name'  => 'asdad.txt', 'size'  => '316', 'status'  => 'old');
             #$document_directory =  Mage :: getBaseDir( 'media' ) . DS . 'import' . DS;
             #echo "DIRECTORY: " . $document_directory;
             #$filearrayforimport = '[{"file": "/home/discou33/public_html/media/import/mypdf.pdf", "name": "mypdf.pdf", "status": "new"}]';
             #$filearrayforimport = '[{"file": "mypdf.pdf", "name": "quickstart.pdf", "size": 324075, "status": "new"}]';
             #$product->setLinksPurchasedSeparately(0);
             #$product->setLinksPurchasedSeparately(false);
             #$files = Zend_Json::decode($filearrayforimport);
             #$files = "mypdf.pdf";
             //--------------- upload file ------------------
             $document_directory = Mage::getBaseDir('media') . DS . 'import' . DS . $this->__vendorName . DS;
             $files = '' . $configBundleOptionsCodes[4] . '';
             $link_file = $document_directory . $files;
             $file = realpath($link_file);
             if (!$file || !file_exists($file)) {
                 Mage::throwException(Mage::helper('catalog')->__($rowInfo . 'Link  file ' . $file . ' not exists'));
             }
             $pathinfo = pathinfo($file);
             $linkfile = Varien_File_Uploader::getCorrectFileName($pathinfo['basename']);
             $dispretionPath = Varien_File_Uploader::getDispretionPath($linkfile);
             $linkfile = $dispretionPath . DS . $linkfile;
             $linkfile = $dispretionPath . DS . Varien_File_Uploader::getNewFileName(Mage_Downloadable_Model_Link::getBaseTmpPath() . DS . $linkfile);
             $ioAdapter = new Varien_Io_File();
             $ioAdapter->setAllowCreateFolders(true);
             $distanationDirectory = dirname(Mage_Downloadable_Model_Link::getBaseTmpPath() . DS . $linkfile);
             try {
                 $ioAdapter->open(array('path' => $distanationDirectory));
                 $ioAdapter->cp($file, Mage_Downloadable_Model_Link::getBaseTmpPath() . DS . $linkfile);
                 $ioAdapter->chmod(Mage_Downloadable_Model_Link::getBaseTmpPath() . DS . $linkfile, 0777);
             } catch (exception $e) {
                 Mage::throwException(Mage::helper('catalog')->__('Failed to move file: %s', $e->getMessage()));
             }
             //{"file": "/2/_/2.jpg", "name": "2.jpg", "size": 23407, "status": "new"}
             $linkfile = str_replace(DS, '/', $linkfile);
             $filearrayforimports = array(array('file' => $linkfile, 'name' => $pathinfo['filename'] . '.' . $pathinfo['extension'], 'status' => 'new', 'size' => filesize($file)));
             if (isset($configBundleOptionsCodes[5])) {
                 if ($configBundleOptionsCodes[5] == 0) {
                     $linkspurchasedstatus = 0;
                     $linkspurchasedstatustext = false;
                 } else {
                     $linkspurchasedstatus = 1;
                     $linkspurchasedstatustext = true;
                 }
                 $product->setLinksPurchasedSeparately($linkspurchasedstatus);
                 $product->setLinksPurchasedSeparately($linkspurchasedstatustext);
             }
             //$downloadableitems['link'][$downloadableitemsoptionscount]['link_file'] = $linkfile;
             $downloadableitems['link'][$downloadableitemsoptionscount]['file'] = Mage::helper('core')->jsonEncode($filearrayforimports);
         } else {
             if ($configBundleOptionsCodes[3] == "url") {
                 $downloadableitems['link'][$downloadableitemsoptionscount]['link_url'] = $configBundleOptionsCodes[4];
             }
         }
         $downloadableitems['link'][$downloadableitemsoptionscount]['sort_order'] = 0;
         $product->setDownloadableData($downloadableitems);
         $downloadableitemsoptionscount += 1;
     }
     #print_r($downloadableitems);
     //}
 }
 /**
  * Enter description here...
  *
  * @return Mage_Downloadable_Model_Product_Type
  */
 public function save()
 {
     parent::save();
     $product = $this->getProduct();
     /* @var Mage_Catalog_Model_Product $product */
     if ($data = $product->getDownloadableData()) {
         if (isset($data['sample'])) {
             $_deleteItems = array();
             foreach ($data['sample'] as $sampleItem) {
                 if ($sampleItem['is_delete'] == '1') {
                     if ($sampleItem['sample_id']) {
                         $_deleteItems[] = $sampleItem['sample_id'];
                     }
                 } else {
                     unset($sampleItem['is_delete']);
                     if (!$sampleItem['sample_id']) {
                         unset($sampleItem['sample_id']);
                     }
                     $sampleModel = Mage::getModel('downloadable/sample');
                     $files = array();
                     if (isset($sampleItem['file'])) {
                         $files = Zend_Json::decode($sampleItem['file']);
                         unset($sampleItem['file']);
                     }
                     $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId());
                     $fileStatusNew = false;
                     if (isset($files[0]) && $sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
                         $sampleModel->setSampleFile($files[0]['file']);
                         if ($files[0]['status'] == 'new') {
                             $fileStatusNew = true;
                         }
                     }
                     $sampleModel->save();
                     if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $fileStatusNew) {
                         try {
                             Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Sample::getBaseTmpPath(), Mage_Downloadable_Model_Sample::getBasePath(), $files[0]['file']);
                         } catch (Exception $e) {
                             Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
                         }
                     }
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems);
             }
         }
         if (isset($data['link'])) {
             $_deleteItems = array();
             foreach ($data['link'] as $linkItem) {
                 if ($linkItem['is_delete'] == '1') {
                     if ($linkItem['link_id']) {
                         $_deleteItems[] = $linkItem['link_id'];
                     }
                 } else {
                     unset($linkItem['is_delete']);
                     if (!$linkItem['link_id']) {
                         unset($linkItem['link_id']);
                     }
                     $files = array();
                     if (isset($linkItem['file'])) {
                         $files = Zend_Json::decode($linkItem['file']);
                         unset($linkItem['file']);
                     }
                     $sample = array();
                     if (isset($linkItem['sample'])) {
                         $sample = $linkItem['sample'];
                         unset($linkItem['sample']);
                     }
                     $linkModel = Mage::getModel('downloadable/link')->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId());
                     if (null === $linkModel->getPrice()) {
                         $linkModel->setPrice(0);
                     }
                     if ($linkModel->getIsUnlimited()) {
                         $linkModel->setNumberOfDownloads(0);
                     }
                     $fileStatusNew = false;
                     if (isset($files[0]) && $linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
                         $linkModel->setLinkFile($files[0]['file']);
                         if ($files[0]['status'] == 'new') {
                             $fileStatusNew = true;
                         }
                     }
                     $sampleFileNew = false;
                     if ($sample && isset($sample['type'])) {
                         if ($sample['type'] == 'url' && $sample['url'] != '') {
                             $linkModel->setSampleUrl($sample['url'])->setSampleType($sample['type']);
                         }
                         $sampleFile = Zend_Json::decode($sample['file']);
                         if ($sample['type'] == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && isset($sampleFile[0])) {
                             $linkModel->setSampleFile($sampleFile[0]['file'])->setSampleType($sample['type']);
                             if ($sampleFile[0]['status'] == 'new') {
                                 $sampleFileNew = true;
                             }
                         }
                     }
                     $linkModel->save();
                     if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $fileStatusNew) {
                         try {
                             Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseTmpPath(), Mage_Downloadable_Model_Link::getBasePath(), $files[0]['file']);
                         } catch (Exception $e) {
                             Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
                         }
                     }
                     if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $sampleFileNew) {
                         try {
                             Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseSampleTmpPath(), Mage_Downloadable_Model_Link::getBaseSamplePath(), $sampleFile[0]['file']);
                         } catch (Exception $e) {
                             Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
                         }
                     }
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems);
             }
         }
     }
     return $this;
 }
Example #9
0
 /**
  * Get temporary path for downloadable product
  *
  * @param array $key            
  * @return array $tmpPathResult
  */
 public function getTmpPathForDownloadable($key)
 {
     $type = $tmpPath = '';
     $tmpPathResult = array();
     if (substr($key, 0, 5) == 'sampl') {
         $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
         $type = 'samples';
     }
     if (substr($key, 0, 5) == 'links') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
         $type = 'links';
     }
     if (substr($key, 0, 5) == 'l_sam') {
         $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
         $type = 'link_samples';
     }
     $tmpPathResult['type'] = $type;
     $tmpPathResult['tmp_path'] = $tmpPath;
     return $tmpPathResult;
 }