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_Catalog_Model_Api2_Product_Image_Rest_Admin_V1 extends Mage_Catalog_Model_Api2_Product_Image_Rest
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _create(array $data)
44: {
45:
46: $validator = Mage::getModel('catalog/api2_product_image_validator_image');
47: if (!$validator->isValidData($data)) {
48: foreach ($validator->getErrors() as $error) {
49: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
50: }
51: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
52: }
53: $imageFileContent = @base64_decode($data['file_content'], true);
54: if (!$imageFileContent) {
55: $this->_critical('The image content must be valid base64 encoded data',
56: Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
57: }
58: unset($data['file_content']);
59:
60: $apiTempDir = Mage::getBaseDir('var') . DS . 'api' . DS . Mage::getSingleton('api/session')->getSessionId();
61: $imageFileName = $this->_getFileName($data);
62:
63: try {
64: $ioAdapter = new Varien_Io_File();
65: $ioAdapter->checkAndCreateFolder($apiTempDir);
66: $ioAdapter->open(array('path' => $apiTempDir));
67: $ioAdapter->write($imageFileName, $imageFileContent, 0666);
68: unset($imageFileContent);
69:
70:
71: try {
72: new Varien_Image($apiTempDir . DS . $imageFileName);
73: } catch (Exception $e) {
74: $ioAdapter->rmdir($apiTempDir, true);
75: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
76: }
77: $product = $this->_getProduct();
78: $imageFileUri = $this->_getMediaGallery()
79: ->addImage($product, $apiTempDir . DS . $imageFileName, null, false, false);
80: $ioAdapter->rmdir($apiTempDir, true);
81:
82: $this->_getMediaGallery()->updateImage($product, $imageFileUri, $data);
83:
84: if (isset($data['types'])) {
85: $this->_getMediaGallery()->setMediaAttribute($product, $data['types'], $imageFileUri);
86: }
87: $product->save();
88: return $this->_getImageLocation($this->_getCreatedImageId($imageFileUri));
89: } catch (Mage_Core_Exception $e) {
90: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
91: } catch (Exception $e) {
92: $this->_critical(self::RESOURCE_UNKNOWN_ERROR);
93: }
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: protected function _getCreatedImageId($imageFileUri)
104: {
105: $imageId = null;
106:
107: $imageData = Mage::getResourceModel('catalog/product_attribute_backend_media')
108: ->loadGallery($this->_getProduct(), $this->_getMediaGallery());
109: foreach ($imageData as $image) {
110: if ($image['file'] == $imageFileUri) {
111: $imageId = $image['value_id'];
112: break;
113: }
114: }
115: if (!$imageId) {
116: $this->_critical('Unknown error during image save', Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
117: }
118: return $imageId;
119: }
120:
121: 122: 123: 124: 125: 126:
127: protected function _retrieve()
128: {
129: $result = array();
130: $imageId = (int) $this->getRequest()->getParam('image');
131: $galleryData = $this->_getProduct()->getData(self::GALLERY_ATTRIBUTE_CODE);
132: if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
133: $this->_critical('Product image not found', Mage_Api2_Model_Server::HTTP_NOT_FOUND);
134: }
135: foreach ($galleryData['images'] as &$image) {
136: if ($image['value_id'] == $imageId) {
137: $result = $this->_formatImageData($image);
138: break;
139: }
140: }
141: if (empty($result)) {
142: $this->_critical('Product image not found', Mage_Api2_Model_Server::HTTP_NOT_FOUND);
143: }
144: return $result;
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: protected function _update(array $data)
155: {
156: $imageId = (int)$this->getRequest()->getParam('image');
157: $imageFileUri = $this->_getImageFileById($imageId);
158: $product = $this->_getProduct();
159: $this->_getMediaGallery()->updateImage($product, $imageFileUri, $data);
160: if (isset($data['types']) && is_array($data['types'])) {
161: $assignedTypes = $this->_getImageTypesAssignedToProduct($imageFileUri);
162: $typesToBeCleared = array_diff($assignedTypes, $data['types']);
163: if (count($typesToBeCleared) > 0) {
164: $this->_getMediaGallery()->clearMediaAttribute($product, $typesToBeCleared);
165: }
166: $this->_getMediaGallery()->setMediaAttribute($product, $data['types'], $imageFileUri);
167: }
168: try {
169: $product->save();
170: } catch (Mage_Core_Exception $e) {
171: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
172: } catch (Exception $e) {
173: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
174: }
175: }
176:
177: 178: 179: 180: 181:
182: protected function _delete()
183: {
184: $imageId = (int)$this->getRequest()->getParam('image');
185: $product = $this->_getProduct();
186: $imageFileUri = $this->_getImageFileById($imageId);
187: $this->_getMediaGallery()->removeImage($product, $imageFileUri);
188: try {
189: $product->save();
190: } catch (Mage_Core_Exception $e) {
191: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
192: } catch (Exception $e) {
193: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
194: }
195: }
196:
197: 198: 199: 200: 201: 202:
203: protected function _retrieveCollection()
204: {
205: $images = array();
206: $galleryData = $this->_getProduct()->getData(self::GALLERY_ATTRIBUTE_CODE);
207: if (isset($galleryData['images']) && is_array($galleryData['images'])) {
208: foreach ($galleryData['images'] as $image) {
209: $images[] = $this->_formatImageData($image);
210: }
211: }
212: return $images;
213: }
214:
215: 216: 217: 218: 219: 220:
221: protected function _getImageLocation($imageId)
222: {
223:
224: $apiTypeRoute = Mage::getModel('api2/route_apiType');
225:
226: $chain = $apiTypeRoute->chain(
227: new Zend_Controller_Router_Route($this->getConfig()->getRouteWithEntityTypeAction($this->getResourceType()))
228: );
229: $params = array(
230: 'api_type' => $this->getRequest()->getApiType(),
231: 'id' => $this->getRequest()->getParam('id'),
232: 'image' => $imageId
233: );
234: $uri = $chain->assemble($params);
235: return '/' . $uri;
236: }
237: }
238: