PageRenderTime 143ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php

https://github.com/rgranadino/magento-mirror
PHP | 405 lines | 213 code | 69 blank | 123 comment | 32 complexity | 3efbe69bd276907d910c4973a1d490e3 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product media api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Attribute_Media_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. /**
  36. * Attribute code for media gallery
  37. *
  38. */
  39. const ATTRIBUTE_CODE = 'media_gallery';
  40. /**
  41. * Allowed mime types for image
  42. *
  43. * @var array
  44. */
  45. protected $_mimeTypes = array(
  46. 'image/jpeg' => 'jpg',
  47. 'image/gif' => 'gif',
  48. 'image/png' => 'png'
  49. );
  50. public function __construct()
  51. {
  52. $this->_storeIdSessionField = 'product_store_id';
  53. }
  54. /**
  55. * Retrieve images for product
  56. *
  57. * @param int|string $productId
  58. * @param string|int $store
  59. * @return array
  60. */
  61. public function items($productId, $store = null, $identifierType = null)
  62. {
  63. $product = $this->_initProduct($productId, $store, $identifierType);
  64. $gallery = $this->_getGalleryAttribute($product);
  65. $galleryData = $product->getData(self::ATTRIBUTE_CODE);
  66. if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
  67. return array();
  68. }
  69. $result = array();
  70. foreach ($galleryData['images'] as &$image) {
  71. $result[] = $this->_imageToArray($image, $product);
  72. }
  73. return $result;
  74. }
  75. /**
  76. * Retrieve image data
  77. *
  78. * @param int|string $productId
  79. * @param string $file
  80. * @param string|int $store
  81. * @return array
  82. */
  83. public function info($productId, $file, $store = null, $identifierType = null)
  84. {
  85. $product = $this->_initProduct($productId, $store, $identifierType);
  86. $gallery = $this->_getGalleryAttribute($product);
  87. if (!$image = $gallery->getBackend()->getImage($product, $file)) {
  88. $this->_fault('not_exists');
  89. }
  90. return $this->_imageToArray($image, $product);
  91. }
  92. /**
  93. * Create new image for product and return image filename
  94. *
  95. * @param int|string $productId
  96. * @param array $data
  97. * @param string|int $store
  98. * @return string
  99. */
  100. public function create($productId, $data, $store = null, $identifierType = null)
  101. {
  102. $data = $this->_prepareImageData($data);
  103. $product = $this->_initProduct($productId, $store, $identifierType);
  104. $gallery = $this->_getGalleryAttribute($product);
  105. if (!isset($data['file']) || !isset($data['file']['mime']) || !isset($data['file']['content'])) {
  106. $this->_fault('data_invalid', Mage::helper('catalog')->__('The image is not specified.'));
  107. }
  108. if (!isset($this->_mimeTypes[$data['file']['mime']])) {
  109. $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
  110. }
  111. $fileContent = @base64_decode($data['file']['content'], true);
  112. if (!$fileContent) {
  113. $this->_fault('data_invalid', Mage::helper('catalog')->__('The image contents is not valid base64 data.'));
  114. }
  115. unset($data['file']['content']);
  116. $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
  117. if (isset($data['file']['name']) && $data['file']['name']) {
  118. $fileName = $data['file']['name'];
  119. } else {
  120. $fileName = 'image';
  121. }
  122. $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
  123. $ioAdapter = new Varien_Io_File();
  124. try {
  125. // Create temporary directory for api
  126. $ioAdapter->checkAndCreateFolder($tmpDirectory);
  127. $ioAdapter->open(array('path'=>$tmpDirectory));
  128. // Write image file
  129. $ioAdapter->write($fileName, $fileContent, 0666);
  130. unset($fileContent);
  131. // Adding image to gallery
  132. $file = $gallery->getBackend()->addImage(
  133. $product,
  134. $tmpDirectory . DS . $fileName,
  135. null,
  136. true
  137. );
  138. // Remove temporary directory
  139. $ioAdapter->rmdir($tmpDirectory, true);
  140. $gallery->getBackend()->updateImage($product, $file, $data);
  141. if (isset($data['types'])) {
  142. $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
  143. }
  144. $product->save();
  145. } catch (Mage_Core_Exception $e) {
  146. $this->_fault('not_created', $e->getMessage());
  147. } catch (Exception $e) {
  148. $this->_fault('not_created', Mage::helper('catalog')->__('Cannot create image.'));
  149. }
  150. return $gallery->getBackend()->getRenamedImage($file);
  151. }
  152. /**
  153. * Update image data
  154. *
  155. * @param int|string $productId
  156. * @param string $file
  157. * @param array $data
  158. * @param string|int $store
  159. * @return boolean
  160. */
  161. public function update($productId, $file, $data, $store = null, $identifierType = null)
  162. {
  163. $data = $this->_prepareImageData($data);
  164. $product = $this->_initProduct($productId, $store, $identifierType);
  165. $gallery = $this->_getGalleryAttribute($product);
  166. if (!$gallery->getBackend()->getImage($product, $file)) {
  167. $this->_fault('not_exists');
  168. }
  169. if (isset($data['file']['mime']) && isset($data['file']['content'])) {
  170. if (!isset($this->_mimeTypes[$data['file']['mime']])) {
  171. $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
  172. }
  173. $fileContent = @base64_decode($data['file']['content'], true);
  174. if (!$fileContent) {
  175. $this->_fault('data_invalid', Mage::helper('catalog')->__('Image content is not valid base64 data.'));
  176. }
  177. unset($data['file']['content']);
  178. $ioAdapter = new Varien_Io_File();
  179. try {
  180. $fileName = Mage::getBaseDir('media'). DS . 'catalog' . DS . 'product' . $file;
  181. $ioAdapter->open(array('path'=>dirname($fileName)));
  182. $ioAdapter->write(basename($fileName), $fileContent, 0666);
  183. } catch(Exception $e) {
  184. $this->_fault('not_created', Mage::helper('catalog')->__('Can\'t create image.'));
  185. }
  186. }
  187. $gallery->getBackend()->updateImage($product, $file, $data);
  188. if (isset($data['types']) && is_array($data['types'])) {
  189. $oldTypes = array();
  190. foreach ($product->getMediaAttributes() as $attribute) {
  191. if ($product->getData($attribute->getAttributeCode()) == $file) {
  192. $oldTypes[] = $attribute->getAttributeCode();
  193. }
  194. }
  195. $clear = array_diff($oldTypes, $data['types']);
  196. if (count($clear) > 0) {
  197. $gallery->getBackend()->clearMediaAttribute($product, $clear);
  198. }
  199. $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
  200. }
  201. try {
  202. $product->save();
  203. } catch (Mage_Core_Exception $e) {
  204. $this->_fault('not_updated', $e->getMessage());
  205. }
  206. return true;
  207. }
  208. /**
  209. * Remove image from product
  210. *
  211. * @param int|string $productId
  212. * @param string $file
  213. * @return boolean
  214. */
  215. public function remove($productId, $file, $identifierType = null)
  216. {
  217. $product = $this->_initProduct($productId, null, $identifierType);
  218. $gallery = $this->_getGalleryAttribute($product);
  219. if (!$gallery->getBackend()->getImage($product, $file)) {
  220. $this->_fault('not_exists');
  221. }
  222. $gallery->getBackend()->removeImage($product, $file);
  223. try {
  224. $product->save();
  225. } catch (Mage_Core_Exception $e) {
  226. $this->_fault('not_removed', $e->getMessage());
  227. }
  228. return true;
  229. }
  230. /**
  231. * Retrieve image types (image, small_image, thumbnail, etc...)
  232. *
  233. * @param int $setId
  234. * @return array
  235. */
  236. public function types($setId)
  237. {
  238. $attributes = Mage::getModel('catalog/product')->getResource()
  239. ->loadAllAttributes()
  240. ->getSortedAttributes($setId);
  241. $result = array();
  242. foreach ($attributes as $attribute) {
  243. /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  244. if ($attribute->isInSet($setId)
  245. && $attribute->getFrontendInput() == 'media_image') {
  246. if ($attribute->isScopeGlobal()) {
  247. $scope = 'global';
  248. } elseif ($attribute->isScopeWebsite()) {
  249. $scope = 'website';
  250. } else {
  251. $scope = 'store';
  252. }
  253. $result[] = array(
  254. 'code' => $attribute->getAttributeCode(),
  255. 'scope' => $scope
  256. );
  257. }
  258. }
  259. return $result;
  260. }
  261. /**
  262. * Prepare data to create or update image
  263. *
  264. * @param array $data
  265. * @return array
  266. */
  267. protected function _prepareImageData($data)
  268. {
  269. return $data;
  270. }
  271. /**
  272. * Retrieve gallery attribute from product
  273. *
  274. * @param Mage_Catalog_Model_Product $product
  275. * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute|boolean
  276. */
  277. protected function _getGalleryAttribute($product)
  278. {
  279. $attributes = $product->getTypeInstance(true)
  280. ->getSetAttributes($product);
  281. if (!isset($attributes[self::ATTRIBUTE_CODE])) {
  282. $this->_fault('not_media');
  283. }
  284. return $attributes[self::ATTRIBUTE_CODE];
  285. }
  286. /**
  287. * Retrie
  288. * ve media config
  289. *
  290. * @return Mage_Catalog_Model_Product_Media_Config
  291. */
  292. protected function _getMediaConfig()
  293. {
  294. return Mage::getSingleton('catalog/product_media_config');
  295. }
  296. /**
  297. * Converts image to api array data
  298. *
  299. * @param array $image
  300. * @param Mage_Catalog_Model_Product $product
  301. * @return array
  302. */
  303. protected function _imageToArray(&$image, $product)
  304. {
  305. $result = array(
  306. 'file' => $image['file'],
  307. 'label' => $image['label'],
  308. 'position' => $image['position'],
  309. 'exclude' => $image['disabled'],
  310. 'url' => $this->_getMediaConfig()->getMediaUrl($image['file']),
  311. 'types' => array()
  312. );
  313. foreach ($product->getMediaAttributes() as $attribute) {
  314. if ($product->getData($attribute->getAttributeCode()) == $image['file']) {
  315. $result['types'][] = $attribute->getAttributeCode();
  316. }
  317. }
  318. return $result;
  319. }
  320. /**
  321. * Retrieve product
  322. *
  323. * @param int|string $productId
  324. * @param string|int $store
  325. * @param string $identifierType
  326. * @return Mage_Catalog_Model_Product
  327. */
  328. protected function _initProduct($productId, $store = null, $identifierType = null)
  329. {
  330. $product = Mage::helper('catalog/product')->getProduct($productId, $this->_getStoreId($store), $identifierType);
  331. if (!$product->getId()) {
  332. $this->_fault('product_not_exists');
  333. }
  334. return $product;
  335. }
  336. } // Class Mage_Catalog_Model_Product_Attribute_Media_Api End