×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: user3cff7
Added: Jan 16, 2016 2:03 AM
Views: 7
Tags: no tags
chinh sua api product attribute media
  1. <?php
  2. /**
  3.  * Modify Catalog product media api
  4.  * @author Ho Ngoc Hang<kemly.vn@gmail.com>
  5.  */
  6.  
  7. class SydneyEcommerce_OderApi_Model_Catalog_Product_Attribute_Media_Api_V2 extends Mage_Catalog_Model_Product_Attribute_Media_Api_V2
  8. {
  9.    
  10.     /**
  11.      * Create new image for product and return image filename
  12.      *
  13.      * @param int|string $productId
  14.      * @param array $data
  15.      * @param string|int $store
  16.      * @return string
  17.      */
  18.     public function create($productId, $data, $store = null, $identifierType = null)
  19.     {
  20.         $data = $this->_prepareImageData($data);
  21.        
  22.         $file_name = $data['file']['content'];
  23.         $image = 'media/import/'.$file_name;
  24.         $imagedata = @file_get_contents($image);
  25.         $image_base64 = @base64_encode($imagedata);
  26.        
  27.         $product = $this->_initProduct($productId, $store, $identifierType);
  28.  
  29.         $gallery = $this->_getGalleryAttribute($product);
  30.  
  31.         if (!isset($data['file']) || !isset($data['file']['mime']) || !isset($data['file']['content'])) {
  32.             $this->_fault('data_invalid', Mage::helper('catalog')->__('The image is not specified.'));
  33.         }
  34.  
  35.         if (!isset($this->_mimeTypes[$data['file']['mime']])) {
  36.             $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
  37.         }
  38.  
  39.         $fileContent = @base64_decode($image_base64, true);
  40.         if (!$fileContent) {
  41.             $this->_fault('data_invalid', Mage::helper('catalog')->__('The image contents is not valid base64 data.'));
  42.         }
  43.  
  44.         unset($image_base64);
  45.  
  46.         $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
  47.  
  48.         if (isset($data['file']['name']) && $data['file']['name']) {
  49.             $fileName  = $data['file']['name'];
  50.         } else {
  51.             $fileName  = 'image';
  52.         }
  53.         $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
  54.  
  55.         $ioAdapter = new Varien_Io_File();
  56.         try {
  57.             // Create temporary directory for api
  58.             $ioAdapter->checkAndCreateFolder($tmpDirectory);
  59.             $ioAdapter->open(array('path'=>$tmpDirectory));
  60.             // Write image file
  61.             $ioAdapter->write($fileName, $fileContent, 0666);
  62.             unset($fileContent);
  63.  
  64.             // try to create Image object - it fails with Exception if image is not supported
  65.             try {
  66.                 new Varien_Image($tmpDirectory . DS . $fileName);
  67.             } catch (Exception $e) {
  68.                 // Remove temporary directory
  69.                 $ioAdapter->rmdir($tmpDirectory, true);
  70.  
  71.                 throw new Mage_Core_Exception($e->getMessage());
  72.             }
  73.  
  74.             // Adding image to gallery
  75.             $file = $gallery->getBackend()->addImage(
  76.                 $product,
  77.                 $tmpDirectory . DS . $fileName,
  78.                 null,
  79.                 true
  80.             );
  81.  
  82.             // Remove temporary directory
  83.             $ioAdapter->rmdir($tmpDirectory, true);
  84.  
  85.             $gallery->getBackend()->updateImage($product, $file, $data);
  86.  
  87.             if (isset($data['types'])) {
  88.                 $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
  89.             }
  90.  
  91.             $product->save();
  92.         } catch (Mage_Core_Exception $e) {
  93.             $this->_fault('not_created', $e->getMessage());
  94.         } catch (Exception $e) {
  95.             $this->_fault('not_created', Mage::helper('catalog')->__('Cannot create image.'));
  96.         }
  97.  
  98.         return $gallery->getBackend()->getRenamedImage($file);
  99.     }
  100.    
  101.     /**
  102.      * Update image data
  103.      *
  104.      * @param int|string $productId
  105.      * @param string $file
  106.      * @param array $data
  107.      * @param string|int $store
  108.      * @return boolean
  109.      */
  110.     public function update($productId, $file, $data, $store = null, $identifierType = null)
  111.     {
  112.         $data = $this->_prepareImageData($data);
  113.        
  114.         $file_name = $data['file']['content'];
  115.         $image = 'media/import/'.$file_name;
  116.         $imagedata = @file_get_contents($image);
  117.         $image_base64 = @base64_encode($imagedata);
  118.  
  119.         $product = $this->_initProduct($productId, $store, $identifierType);
  120.  
  121.         $gallery = $this->_getGalleryAttribute($product);
  122.  
  123.         if (!$gallery->getBackend()->getImage($product, $file)) {
  124.             $this->_fault('not_exists');
  125.         }
  126.  
  127.         if (isset($data['file']['mime']) && isset($data['file']['content'])) {
  128.             if (!isset($this->_mimeTypes[$data['file']['mime']])) {
  129.                 $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
  130.             }
  131.  
  132.             $fileContent = @base64_decode($image_base64, true);
  133.             if (!$fileContent) {
  134.                 $this->_fault('data_invalid', Mage::helper('catalog')->__('Image content is not valid base64 data.'));
  135.             }
  136.  
  137.             unset($image_base64);
  138.  
  139.             $ioAdapter = new Varien_Io_File();
  140.             try {
  141.                 $fileName = Mage::getBaseDir('media'). DS . 'catalog' . DS . 'product' . $file;
  142.                 $ioAdapter->open(array('path'=>dirname($fileName)));
  143.                 $ioAdapter->write(basename($fileName), $fileContent, 0666);
  144.  
  145.             } catch(Exception $e) {
  146.                 $this->_fault('not_created', Mage::helper('catalog')->__('Can\'t create image.'));
  147.             }
  148.         }
  149.  
  150.         $gallery->getBackend()->updateImage($product, $file, $data);
  151.  
  152.         if (isset($data['types']) && is_array($data['types'])) {
  153.             $oldTypes = array();
  154.             foreach ($product->getMediaAttributes() as $attribute) {
  155.                 if ($product->getData($attribute->getAttributeCode()) == $file) {
  156.                      $oldTypes[] = $attribute->getAttributeCode();
  157.                 }
  158.             }
  159.  
  160.             $clear = array_diff($oldTypes, $data['types']);
  161.  
  162.             if (count($clear) > 0) {
  163.                 $gallery->getBackend()->clearMediaAttribute($product, $clear);
  164.             }
  165.  
  166.             $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
  167.         }
  168.  
  169.         try {
  170.             $product->save();
  171.         } catch (Mage_Core_Exception $e) {
  172.             $this->_fault('not_updated', $e->getMessage());
  173.         }
  174.  
  175.         return true;
  176.     }
  177.  
  178. }
  179.