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_Product_Attribute_Media_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39:
40: const ATTRIBUTE_CODE = 'media_gallery';
41:
42: 43: 44: 45: 46:
47: protected $_mimeTypes = array(
48: 'image/jpeg' => 'jpg',
49: 'image/gif' => 'gif',
50: 'image/png' => 'png'
51: );
52:
53: public function __construct()
54: {
55: $this->_storeIdSessionField = 'product_store_id';
56: }
57:
58: 59: 60: 61: 62: 63: 64:
65: public function items($productId, $store = null, $identifierType = null)
66: {
67: $product = $this->_initProduct($productId, $store, $identifierType);
68:
69: $gallery = $this->_getGalleryAttribute($product);
70:
71: $galleryData = $product->getData(self::ATTRIBUTE_CODE);
72:
73: if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
74: return array();
75: }
76:
77: $result = array();
78:
79: foreach ($galleryData['images'] as &$image) {
80: $result[] = $this->_imageToArray($image, $product);
81: }
82:
83: return $result;
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93:
94: public function info($productId, $file, $store = null, $identifierType = null)
95: {
96: $product = $this->_initProduct($productId, $store, $identifierType);
97:
98: $gallery = $this->_getGalleryAttribute($product);
99:
100: if (!$image = $gallery->getBackend()->getImage($product, $file)) {
101: $this->_fault('not_exists');
102: }
103:
104: return $this->_imageToArray($image, $product);
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114:
115: public function create($productId, $data, $store = null, $identifierType = null)
116: {
117: $data = $this->_prepareImageData($data);
118:
119: $product = $this->_initProduct($productId, $store, $identifierType);
120:
121: $gallery = $this->_getGalleryAttribute($product);
122:
123: if (!isset($data['file']) || !isset($data['file']['mime']) || !isset($data['file']['content'])) {
124: $this->_fault('data_invalid', Mage::helper('catalog')->__('The image is not specified.'));
125: }
126:
127: if (!isset($this->_mimeTypes[$data['file']['mime']])) {
128: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
129: }
130:
131: $fileContent = @base64_decode($data['file']['content'], true);
132: if (!$fileContent) {
133: $this->_fault('data_invalid', Mage::helper('catalog')->__('The image contents is not valid base64 data.'));
134: }
135:
136: unset($data['file']['content']);
137:
138: $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
139:
140: if (isset($data['file']['name']) && $data['file']['name']) {
141: $fileName = $data['file']['name'];
142: } else {
143: $fileName = 'image';
144: }
145: $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
146:
147: $ioAdapter = new Varien_Io_File();
148: try {
149:
150: $ioAdapter->checkAndCreateFolder($tmpDirectory);
151: $ioAdapter->open(array('path'=>$tmpDirectory));
152:
153: $ioAdapter->write($fileName, $fileContent, 0666);
154: unset($fileContent);
155:
156:
157: try {
158: new Varien_Image($tmpDirectory . DS . $fileName);
159: } catch (Exception $e) {
160:
161: $ioAdapter->rmdir($tmpDirectory, true);
162:
163: throw new Mage_Core_Exception($e->getMessage());
164: }
165:
166:
167: $file = $gallery->getBackend()->addImage(
168: $product,
169: $tmpDirectory . DS . $fileName,
170: null,
171: true
172: );
173:
174:
175: $ioAdapter->rmdir($tmpDirectory, true);
176:
177: $gallery->getBackend()->updateImage($product, $file, $data);
178:
179: if (isset($data['types'])) {
180: $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
181: }
182:
183: $product->save();
184: } catch (Mage_Core_Exception $e) {
185: $this->_fault('not_created', $e->getMessage());
186: } catch (Exception $e) {
187: $this->_fault('not_created', Mage::helper('catalog')->__('Cannot create image.'));
188: }
189:
190: return $gallery->getBackend()->getRenamedImage($file);
191: }
192:
193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public function update($productId, $file, $data, $store = null, $identifierType = null)
203: {
204: $data = $this->_prepareImageData($data);
205:
206: $product = $this->_initProduct($productId, $store, $identifierType);
207:
208: $gallery = $this->_getGalleryAttribute($product);
209:
210: if (!$gallery->getBackend()->getImage($product, $file)) {
211: $this->_fault('not_exists');
212: }
213:
214: if (isset($data['file']['mime']) && isset($data['file']['content'])) {
215: if (!isset($this->_mimeTypes[$data['file']['mime']])) {
216: $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
217: }
218:
219: $fileContent = @base64_decode($data['file']['content'], true);
220: if (!$fileContent) {
221: $this->_fault('data_invalid', Mage::helper('catalog')->__('Image content is not valid base64 data.'));
222: }
223:
224: unset($data['file']['content']);
225:
226: $ioAdapter = new Varien_Io_File();
227: try {
228: $fileName = Mage::getBaseDir('media'). DS . 'catalog' . DS . 'product' . $file;
229: $ioAdapter->open(array('path'=>dirname($fileName)));
230: $ioAdapter->write(basename($fileName), $fileContent, 0666);
231:
232: } catch(Exception $e) {
233: $this->_fault('not_created', Mage::helper('catalog')->__('Can\'t create image.'));
234: }
235: }
236:
237: $gallery->getBackend()->updateImage($product, $file, $data);
238:
239: if (isset($data['types']) && is_array($data['types'])) {
240: $oldTypes = array();
241: foreach ($product->getMediaAttributes() as $attribute) {
242: if ($product->getData($attribute->getAttributeCode()) == $file) {
243: $oldTypes[] = $attribute->getAttributeCode();
244: }
245: }
246:
247: $clear = array_diff($oldTypes, $data['types']);
248:
249: if (count($clear) > 0) {
250: $gallery->getBackend()->clearMediaAttribute($product, $clear);
251: }
252:
253: $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
254: }
255:
256: try {
257: $product->save();
258: } catch (Mage_Core_Exception $e) {
259: $this->_fault('not_updated', $e->getMessage());
260: }
261:
262: return true;
263: }
264:
265: 266: 267: 268: 269: 270: 271:
272: public function remove($productId, $file, $identifierType = null)
273: {
274: $product = $this->_initProduct($productId, null, $identifierType);
275:
276: $gallery = $this->_getGalleryAttribute($product);
277:
278: if (!$gallery->getBackend()->getImage($product, $file)) {
279: $this->_fault('not_exists');
280: }
281:
282: $gallery->getBackend()->removeImage($product, $file);
283:
284: try {
285: $product->save();
286: } catch (Mage_Core_Exception $e) {
287: $this->_fault('not_removed', $e->getMessage());
288: }
289:
290: return true;
291: }
292:
293:
294: 295: 296: 297: 298: 299:
300: public function types($setId)
301: {
302: $attributes = Mage::getModel('catalog/product')->getResource()
303: ->loadAllAttributes()
304: ->getSortedAttributes($setId);
305:
306: $result = array();
307:
308: foreach ($attributes as $attribute) {
309:
310: if ($attribute->isInSet($setId)
311: && $attribute->getFrontendInput() == 'media_image') {
312: if ($attribute->isScopeGlobal()) {
313: $scope = 'global';
314: } elseif ($attribute->isScopeWebsite()) {
315: $scope = 'website';
316: } else {
317: $scope = 'store';
318: }
319:
320: $result[] = array(
321: 'code' => $attribute->getAttributeCode(),
322: 'scope' => $scope
323: );
324: }
325: }
326:
327: return $result;
328: }
329:
330: 331: 332: 333: 334: 335:
336: protected function _prepareImageData($data)
337: {
338: return $data;
339: }
340:
341: 342: 343: 344: 345: 346:
347: protected function _getGalleryAttribute($product)
348: {
349: $attributes = $product->getTypeInstance(true)
350: ->getSetAttributes($product);
351:
352: if (!isset($attributes[self::ATTRIBUTE_CODE])) {
353: $this->_fault('not_media');
354: }
355:
356: return $attributes[self::ATTRIBUTE_CODE];
357: }
358:
359: 360: 361: 362: 363: 364:
365: protected function _getMediaConfig()
366: {
367: return Mage::getSingleton('catalog/product_media_config');
368: }
369:
370: 371: 372: 373: 374: 375: 376:
377: protected function _imageToArray(&$image, $product)
378: {
379: $result = array(
380: 'file' => $image['file'],
381: 'label' => $image['label'],
382: 'position' => $image['position'],
383: 'exclude' => $image['disabled'],
384: 'url' => $this->_getMediaConfig()->getMediaUrl($image['file']),
385: 'types' => array()
386: );
387:
388:
389: foreach ($product->getMediaAttributes() as $attribute) {
390: if ($product->getData($attribute->getAttributeCode()) == $image['file']) {
391: $result['types'][] = $attribute->getAttributeCode();
392: }
393: }
394:
395: return $result;
396: }
397:
398: 399: 400: 401: 402: 403: 404: 405:
406: protected function _initProduct($productId, $store = null, $identifierType = null)
407: {
408: $product = Mage::helper('catalog/product')->getProduct($productId, $this->_getStoreId($store), $identifierType);
409: if (!$product->getId()) {
410: $this->_fault('product_not_exists');
411: }
412:
413: return $product;
414: }
415: }
416: