1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Downloadable_Model_Link_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39: 40:
41: protected function _getValidator()
42: {
43: return Mage::getSingleton('downloadable/link_api_validator');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: protected function _uploadFile($fileInfo, $type)
54: {
55: $tmpPath = '';
56: if ($type == 'sample') {
57: $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
58: } elseif ($type == 'link') {
59: $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
60: } elseif ($type == 'link_samples') {
61: $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
62: }
63:
64: $result = array();
65: try {
66: $uploader = Mage::getModel('downloadable/link_api_uploader', $fileInfo);
67: $uploader->setAllowRenameFiles(true);
68: $uploader->setFilesDispersion(true);
69: $result = $uploader->save($tmpPath);
70:
71: if (isset($result['file'])) {
72: $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
73: Mage::helper('core/file_storage_database')->saveFile($fullPath);
74: }
75: } catch (Exception $e) {
76: if ($e->getMessage() != '') {
77: $this->_fault('upload_failed', $e->getMessage());
78: } else {
79: $this->_fault($e->getCode());
80: }
81: }
82:
83: $result['status'] = 'new';
84: $result['name'] = substr($result['file'], strrpos($result['file'], '/')+1);
85: return Mage::helper('core')->jsonEncode(array($result));
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function add($productId, $resource, $resourceType, $store = null, $identifierType = null)
99: {
100: try {
101: $this->_getValidator()->validateType($resourceType);
102: $this->_getValidator()->validateAttributes($resource, $resourceType);
103: } catch (Exception $e) {
104: $this->_fault('validation_error', $e->getMessage());
105: }
106:
107: $resource['is_delete'] = 0;
108: if ($resourceType == 'link') {
109: $resource['link_id'] = 0;
110: } elseif ($resourceType == 'sample') {
111: $resource['sample_id'] = 0;
112: }
113:
114: if ($resource['type'] == 'file') {
115: if (isset($resource['file'])) {
116: $resource['file'] = $this->_uploadFile($resource['file'], $resourceType);
117: }
118: unset($resource[$resourceType.'_url']);
119: } elseif ($resource['type'] == 'url') {
120: unset($resource['file']);
121: }
122:
123: if ($resourceType == 'link' && $resource['sample']['type'] == 'file') {
124: if (isset($resource['sample']['file'])) {
125: $resource['sample']['file'] = $this->_uploadFile($resource['sample']['file'], 'link_samples');
126: }
127: unset($resource['sample']['url']);
128: } elseif ($resourceType == 'link' && $resource['sample']['type'] == 'url') {
129: $resource['sample']['file'] = null;
130: }
131:
132: $product = $this->_getProduct($productId, $store, $identifierType);
133: try {
134: $downloadable = array($resourceType => array($resource));
135: $product->setDownloadableData($downloadable);
136: $product->save();
137: } catch (Exception $e) {
138: $this->_fault('save_error', $e->getMessage());
139: }
140:
141: return true;
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151:
152: public function items($productId, $store = null, $identifierType = null)
153: {
154: $product = $this->_getProduct($productId, $store, $identifierType);
155:
156: $linkArr = array();
157: $links = $product->getTypeInstance(true)->getLinks($product);
158: foreach ($links as $item) {
159: $tmpLinkItem = array(
160: 'link_id' => $item->getId(),
161: 'title' => $item->getTitle(),
162: 'price' => $item->getPrice(),
163: 'number_of_downloads' => $item->getNumberOfDownloads(),
164: 'is_shareable' => $item->getIsShareable(),
165: 'link_url' => $item->getLinkUrl(),
166: 'link_type' => $item->getLinkType(),
167: 'sample_file' => $item->getSampleFile(),
168: 'sample_url' => $item->getSampleUrl(),
169: 'sample_type' => $item->getSampleType(),
170: 'sort_order' => $item->getSortOrder()
171: );
172: $file = Mage::helper('downloadable/file')->getFilePath(
173: Mage_Downloadable_Model_Link::getBasePath(), $item->getLinkFile()
174: );
175:
176: if ($item->getLinkFile() && !is_file($file)) {
177: Mage::helper('core/file_storage_database')->saveFileToFilesystem($file);
178: }
179:
180: if ($item->getLinkFile() && is_file($file)) {
181: $name = Mage::helper('downloadable/file')->getFileFromPathFile($item->getLinkFile());
182: $tmpLinkItem['file_save'] = array(
183: array(
184: 'file' => $item->getLinkFile(),
185: 'name' => $name,
186: 'size' => filesize($file),
187: 'status' => 'old'
188: ));
189: }
190: $sampleFile = Mage::helper('downloadable/file')->getFilePath(
191: Mage_Downloadable_Model_Link::getBaseSamplePath(), $item->getSampleFile()
192: );
193: if ($item->getSampleFile() && is_file($sampleFile)) {
194: $tmpLinkItem['sample_file_save'] = array(
195: array(
196: 'file' => $item->getSampleFile(),
197: 'name' => Mage::helper('downloadable/file')->getFileFromPathFile($item->getSampleFile()),
198: 'size' => filesize($sampleFile),
199: 'status' => 'old'
200: ));
201: }
202: if ($item->getNumberOfDownloads() == '0') {
203: $tmpLinkItem['is_unlimited'] = 1;
204: }
205: if ($product->getStoreId() && $item->getStoreTitle()) {
206: $tmpLinkItem['store_title'] = $item->getStoreTitle();
207: }
208: if ($product->getStoreId() && Mage::helper('downloadable')->getIsPriceWebsiteScope()) {
209: $tmpLinkItem['website_price'] = $item->getWebsitePrice();
210: }
211: $linkArr[] = $tmpLinkItem;
212: }
213: unset($item);
214: unset($tmpLinkItem);
215: unset($links);
216:
217: $samples = $product->getTypeInstance(true)->getSamples($product)->getData();
218: return array('links' => $linkArr, 'samples' => $samples);
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function remove($linkId, $resourceType)
228: {
229: try {
230: $this->_getValidator()->validateType($resourceType);
231: } catch (Exception $e) {
232: $this->_fault('validation_error', $e->getMessage());
233: }
234:
235: switch($resourceType) {
236: case 'link':
237: $downloadableModel = Mage::getSingleton('downloadable/link');
238: break;
239: case 'sample':
240: $downloadableModel = Mage::getSingleton('downloadable/sample');
241: break;
242: }
243:
244: $downloadableModel->load($linkId);
245: if (is_null($downloadableModel->getId())) {
246: $this->_fault('link_was_not_found');
247: }
248:
249: try {
250: $downloadableModel->delete();
251: } catch (Exception $e) {
252: $this->_fault('remove_error', $e->getMessage());
253: }
254:
255: return true;
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265:
266: protected function _getProduct($productId, $store = null, $identifierType = null)
267: {
268: $product = parent::_getProduct($productId, $store, $identifierType);
269:
270: if ($product->getTypeId() !== Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
271: $this->_fault('product_not_downloadable');
272: }
273:
274: return $product;
275: }
276: }
277: