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: abstract class Mage_Catalog_Model_Api2_Product_Image_Rest extends Mage_Catalog_Model_Api2_Product_Rest
35: {
36: 37: 38:
39: const GALLERY_ATTRIBUTE_CODE = 'media_gallery';
40:
41: 42: 43: 44: 45:
46: protected $_mimeTypes = array(
47: 'image/jpg' => 'jpg',
48: 'image/jpeg' => 'jpg',
49: 'image/gif' => 'gif',
50: 'image/png' => 'png'
51: );
52:
53: 54: 55: 56: 57: 58:
59: protected function _retrieve()
60: {
61: $imageData = array();
62: $imageId = (int)$this->getRequest()->getParam('image');
63: $galleryData = $this->_getProduct()->getData(self::GALLERY_ATTRIBUTE_CODE);
64:
65: if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
66: $this->_critical(self::RESOURCE_NOT_FOUND);
67: }
68: foreach ($galleryData['images'] as $image) {
69: if ($image['value_id'] == $imageId && !$image['disabled']) {
70: $imageData = $this->_formatImageData($image);
71: break;
72: }
73: }
74: if (empty($imageData)) {
75: $this->_critical(self::RESOURCE_NOT_FOUND);
76: }
77: return $imageData;
78: }
79:
80: 81: 82: 83: 84:
85: protected function _retrieveCollection()
86: {
87: $images = array();
88: $galleryData = $this->_getProduct()->getData(self::GALLERY_ATTRIBUTE_CODE);
89: if (isset($galleryData['images']) && is_array($galleryData['images'])) {
90: foreach ($galleryData['images'] as $image) {
91: if (!$image['disabled']) {
92: $images[] = $this->_formatImageData($image);
93: }
94: }
95: }
96: return $images;
97: }
98:
99: 100: 101: 102: 103: 104:
105: protected function _getMediaGallery()
106: {
107: $attributes = $this->_getProduct()->getTypeInstance(true)->getSetAttributes($this->_getProduct());
108:
109: if (!isset($attributes[self::GALLERY_ATTRIBUTE_CODE])
110: || !$attributes[self::GALLERY_ATTRIBUTE_CODE] instanceof Mage_Eav_Model_Entity_Attribute_Abstract
111: ) {
112: $this->_critical('Requested product does not support images', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
113: }
114: $galleryAttribute = $attributes[self::GALLERY_ATTRIBUTE_CODE];
115:
116: $mediaGallery = $galleryAttribute->getBackend();
117: return $mediaGallery;
118: }
119:
120: 121: 122: 123: 124: 125:
126: protected function _formatImageData($image)
127: {
128: $result = array(
129: 'id' => $image['value_id'],
130: 'label' => $image['label'],
131: 'position' => $image['position'],
132: 'exclude' => $image['disabled'],
133: 'url' => $this->_getMediaConfig()->getMediaUrl($image['file']),
134: 'types' => $this->_getImageTypesAssignedToProduct($image['file'])
135: );
136: return $result;
137: }
138:
139: 140: 141: 142: 143: 144:
145: protected function _getImageTypesAssignedToProduct($imageFile)
146: {
147: $types = array();
148: foreach ($this->_getProduct()->getMediaAttributes() as $attribute) {
149: if ($this->_getProduct()->getData($attribute->getAttributeCode()) == $imageFile) {
150: $types[] = $attribute->getAttributeCode();
151: }
152: }
153: return $types;
154: }
155:
156: 157: 158: 159: 160:
161: protected function _getMediaConfig()
162: {
163: return Mage::getSingleton('catalog/product_media_config');
164: }
165:
166: 167: 168: 169: 170: 171:
172: protected function _getFileName($data)
173: {
174: $fileName = 'image';
175: if (isset($data['file_name']) && $data['file_name']) {
176: $fileName = $data['file_name'];
177: }
178: $fileName .= '.' . $this->_getExtensionByMimeType($data['file_mime_type']);
179: return $fileName;
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: protected function _getExtensionByMimeType($mimeType)
190: {
191: if (!array_key_exists($mimeType, $this->_mimeTypes)) {
192: $this->_critical('Unsuppoted image MIME type', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
193: }
194: return $this->_mimeTypes[$mimeType];
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: protected function _getImageFileById($imageId)
205: {
206: $file = null;
207: $mediaGalleryData = $this->_getProduct()->getData('media_gallery');
208: if (!isset($mediaGalleryData['images'])) {
209: $this->_critical(self::RESOURCE_NOT_FOUND);
210: }
211: foreach ($mediaGalleryData['images'] as $image) {
212: if ($image['value_id'] == $imageId) {
213: $file = $image['file'];
214: break;
215: }
216: }
217: if (!($file && $this->_getMediaGallery()->getImage($this->_getProduct(), $file))) {
218: $this->_critical(self::RESOURCE_NOT_FOUND);
219: }
220: return $file;
221: }
222: }
223: