PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Downloadable/Model/Link/Api.php

https://bitbucket.org/jokusafet/magento2
PHP | 277 lines | 178 code | 24 blank | 75 comment | 37 complexity | 211af9386826153315e0149c854238d3 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Downloadable
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Downloadable links API model
  28. *
  29. * @category Mage
  30. * @package Mage_Downloadable
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Downloadable_Model_Link_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. /**
  36. * Return validator instance
  37. *
  38. * @return Mage_Downloadable_Model_Link_Api_Validator
  39. */
  40. protected function _getValidator()
  41. {
  42. return Mage::getSingleton('Mage_Downloadable_Model_Link_Api_Validator');
  43. }
  44. /**
  45. * Decode file from base64 and upload it to donwloadable 'tmp' folder
  46. *
  47. * @param array $fileInfo
  48. * @param string $type
  49. * @return string
  50. */
  51. protected function _uploadFile($fileInfo, $type)
  52. {
  53. $tmpPath = '';
  54. if ($type == 'sample') {
  55. $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
  56. } elseif ($type == 'link') {
  57. $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
  58. } elseif ($type == 'link_samples') {
  59. $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
  60. }
  61. $result = array();
  62. try {
  63. $uploader = Mage::getModel('Mage_Downloadable_Model_Link_Api_Uploader', array('file' => $fileInfo));
  64. $uploader->setAllowRenameFiles(true);
  65. $uploader->setFilesDispersion(true);
  66. $result = $uploader->save($tmpPath);
  67. if (isset($result['file'])) {
  68. $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
  69. Mage::helper('Mage_Core_Helper_File_Storage_Database')->saveFile($fullPath);
  70. }
  71. } catch (Exception $e) {
  72. if ($e->getMessage() != '') {
  73. $this->_fault('upload_failed', $e->getMessage());
  74. } else {
  75. $this->_fault($e->getCode());
  76. }
  77. }
  78. $result['status'] = 'new';
  79. $result['name'] = substr($result['file'], strrpos($result['file'], '/')+1);
  80. return Mage::helper('Mage_Core_Helper_Data')->jsonEncode(array($result));
  81. }
  82. /**
  83. * Add downloadable content to product
  84. *
  85. * @param int|string $productId
  86. * @param array $resource
  87. * @param string $resourceType
  88. * @param string|int|null $store
  89. * @param string|null $identifierType ('sku'|'id')
  90. * @return boolean
  91. */
  92. public function add($productId, $resource, $resourceType, $store = null, $identifierType = null)
  93. {
  94. try {
  95. $this->_getValidator()->validateType($resourceType);
  96. $this->_getValidator()->validateAttributes($resource, $resourceType);
  97. } catch (Exception $e) {
  98. $this->_fault('validation_error', $e->getMessage());
  99. }
  100. $resource['is_delete'] = 0;
  101. if ($resourceType == 'link') {
  102. $resource['link_id'] = 0;
  103. } elseif ($resourceType == 'sample') {
  104. $resource['sample_id'] = 0;
  105. }
  106. if ($resource['type'] == 'file') {
  107. if (isset($resource['file'])) {
  108. $resource['file'] = $this->_uploadFile($resource['file'], $resourceType);
  109. }
  110. unset($resource[$resourceType.'_url']);
  111. } elseif ($resource['type'] == 'url') {
  112. unset($resource['file']);
  113. }
  114. if ($resourceType == 'link' && $resource['sample']['type'] == 'file') {
  115. if (isset($resource['sample']['file'])) {
  116. $resource['sample']['file'] = $this->_uploadFile($resource['sample']['file'], 'link_samples');
  117. }
  118. unset($resource['sample']['url']);
  119. } elseif ($resourceType == 'link' && $resource['sample']['type'] == 'url') {
  120. $resource['sample']['file'] = null;
  121. }
  122. $product = $this->_getProduct($productId, $store, $identifierType);
  123. try {
  124. $downloadable = array($resourceType => array($resource));
  125. $product->setDownloadableData($downloadable);
  126. $product->save();
  127. } catch (Exception $e) {
  128. $this->_fault('save_error', $e->getMessage());
  129. }
  130. return true;
  131. }
  132. /**
  133. * Retrieve downloadable product links
  134. *
  135. * @param int|string $productId
  136. * @param string|int $store
  137. * @param string $identifierType ('sku'|'id')
  138. * @return array
  139. */
  140. public function items($productId, $store = null, $identifierType = null)
  141. {
  142. $product = $this->_getProduct($productId, $store, $identifierType);
  143. $linkArr = array();
  144. $links = $product->getTypeInstance()->getLinks($product);
  145. $fileHelper = Mage::helper('Mage_Downloadable_Helper_File');
  146. foreach ($links as $item) {
  147. $tmpLinkItem = array(
  148. 'link_id' => $item->getId(),
  149. 'title' => $item->getTitle(),
  150. 'price' => $item->getPrice(),
  151. 'number_of_downloads' => $item->getNumberOfDownloads(),
  152. 'is_shareable' => $item->getIsShareable(),
  153. 'link_url' => $item->getLinkUrl(),
  154. 'link_type' => $item->getLinkType(),
  155. 'sample_file' => $item->getSampleFile(),
  156. 'sample_url' => $item->getSampleUrl(),
  157. 'sample_type' => $item->getSampleType(),
  158. 'sort_order' => $item->getSortOrder()
  159. );
  160. $file = $fileHelper->getFilePath(
  161. Mage_Downloadable_Model_Link::getBasePath(), $item->getLinkFile()
  162. );
  163. if ($item->getLinkFile() && !is_file($file)) {
  164. Mage::helper('Mage_Core_Helper_File_Storage_Database')->saveFileToFilesystem($file);
  165. }
  166. if ($item->getLinkFile() && is_file($file)) {
  167. $name = $fileHelper->getFileFromPathFile($item->getLinkFile());
  168. $tmpLinkItem['file_save'] = array(
  169. array(
  170. 'file' => $item->getLinkFile(),
  171. 'name' => $name,
  172. 'size' => filesize($file),
  173. 'status' => 'old'
  174. ));
  175. }
  176. $sampleFile = $fileHelper->getFilePath(
  177. Mage_Downloadable_Model_Link::getBaseSamplePath(), $item->getSampleFile()
  178. );
  179. if ($item->getSampleFile() && is_file($sampleFile)) {
  180. $tmpLinkItem['sample_file_save'] = array(
  181. array(
  182. 'file' => $item->getSampleFile(),
  183. 'name' => $fileHelper->getFileFromPathFile($item->getSampleFile()),
  184. 'size' => filesize($sampleFile),
  185. 'status' => 'old'
  186. ));
  187. }
  188. if ($item->getNumberOfDownloads() == '0') {
  189. $tmpLinkItem['is_unlimited'] = 1;
  190. }
  191. if ($product->getStoreId() && $item->getStoreTitle()) {
  192. $tmpLinkItem['store_title'] = $item->getStoreTitle();
  193. }
  194. if ($product->getStoreId() && Mage::helper('Mage_Downloadable_Helper_Data')->getIsPriceWebsiteScope()) {
  195. $tmpLinkItem['website_price'] = $item->getWebsitePrice();
  196. }
  197. $linkArr[] = $tmpLinkItem;
  198. }
  199. unset($item);
  200. unset($tmpLinkItem);
  201. unset($links);
  202. $samples = $product->getTypeInstance()->getSamples($product)->getData();
  203. return array('links' => $linkArr, 'samples' => $samples);
  204. }
  205. /**
  206. * Remove downloadable product link
  207. * @param string $linkId
  208. * @param string $resourceType
  209. * @return bool
  210. */
  211. public function remove($linkId, $resourceType)
  212. {
  213. try {
  214. $this->_getValidator()->validateType($resourceType);
  215. } catch (Exception $e) {
  216. $this->_fault('validation_error', $e->getMessage());
  217. }
  218. switch($resourceType) {
  219. case 'link':
  220. $downloadableModel = Mage::getSingleton('Mage_Downloadable_Model_Link');
  221. break;
  222. case 'sample':
  223. $downloadableModel = Mage::getSingleton('Mage_Downloadable_Model_Sample');
  224. break;
  225. }
  226. $downloadableModel->load($linkId);
  227. if (is_null($downloadableModel->getId())) {
  228. $this->_fault('link_was_not_found');
  229. }
  230. try {
  231. $downloadableModel->delete();
  232. } catch (Exception $e) {
  233. $this->_fault('remove_error', $e->getMessage());
  234. }
  235. return true;
  236. }
  237. /**
  238. * Return loaded downloadable product instance
  239. *
  240. * @param int|string $productId (SKU or ID)
  241. * @param int|string $store
  242. * @param string $identifierType
  243. * @return Mage_Catalog_Model_Product
  244. */
  245. protected function _getProduct($productId, $store = null, $identifierType = null)
  246. {
  247. $product = parent::_getProduct($productId, $store, $identifierType);
  248. if ($product->getTypeId() !== Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
  249. $this->_fault('product_not_downloadable');
  250. }
  251. return $product;
  252. }
  253. }