Guest User

converter

a guest
Feb 28th, 2017
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Aitoc_Aitcg_Model_Sales_Order_Item_Converter extends Mage_Core_Model_Abstract
  4. {
  5. const CUSTOMER_IMAGE_META_VERSION = '1.0';
  6.  
  7. const CUSTOMER_IMAGE_META_VERSION_KEY = 'metadata_version';
  8.  
  9. public function getConvertedOrderItem()
  10. {
  11. $productOptions = $this->getOrderItem()->getProductOptions();
  12. if (isset($productOptions['options']))
  13. {
  14. foreach ($productOptions['options'] as $optionData)
  15. {
  16. $this->getConvertedOptionValue($optionData);
  17. }
  18. }
  19.  
  20. return $this->getOrderItem();
  21. }
  22.  
  23. public function getConvertedOptionValue($optionValue)
  24. {
  25. if (isset($optionValue['option_id']) && Mage::helper('aitcg/options')->checkAitOption($this->_getOption($optionValue['option_id'])))
  26. {
  27. if ($this->_checkOptionValue($optionValue))
  28. {
  29. $optionVersionKey = $this->_getOptionMetadataVersion($optionValue);
  30. $currentVersionKey = $this->_getVersionReplacementKey(self::CUSTOMER_IMAGE_META_VERSION);
  31. $method = '_from' . $optionVersionKey . 'To' . $currentVersionKey;
  32. return $this->$method($optionValue);
  33. }
  34. }
  35.  
  36. return $optionValue;
  37. }
  38.  
  39. protected function _fromNonversionedTo10($optionValue)
  40. {
  41. $item = $this->getOrderItem();
  42. $productOptions = $item->getProductOptions();
  43. foreach ($productOptions['options'] as $key => $optionData)
  44. {
  45. if ($optionData['option_id'] == $optionValue['option_id'])
  46. {
  47. $option = $this->_getOption($optionData['option_id']);
  48. $value = unserialize($optionData['option_value']);
  49. $templateId = $value['template_id'];
  50. $group = $option->groupFactory($option->getType())->setOption($option);
  51. $confItemOption = array(
  52. 'type' => 'image/png',
  53. 'title' => Mage::helper('aitcg')->__('Please enable the Aitoc Custom Product Preview extension, then you will see an image option.'),
  54. 'quote_path' => '',
  55. 'order_path' => '',
  56. 'fullpath' => '',
  57. 'size' => '',
  58. 'width' => '',
  59. 'height' => '',
  60. 'secret_key' => '',
  61. Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY => array(
  62. 'template_id' => $templateId,
  63. self::CUSTOMER_IMAGE_META_VERSION_KEY => self::CUSTOMER_IMAGE_META_VERSION
  64. )
  65. );
  66. $productOptions['info_buyRequest']['options'][$option->getId()] = $confItemOption;
  67. $userData = array(
  68. 'label' => $option->getTitle(),
  69. 'value' => $group->getFormattedOptionValue(serialize($confItemOption)),
  70. 'print_value' => $group->getPrintableOptionValue(serialize($confItemOption)),
  71. 'option_id' => $option->getId(),
  72. 'option_type' => Aitoc_Aitcg_Model_Rewrite_Catalog_Product_Option::OPTION_GROUP_FILE,
  73. 'option_value' => serialize($confItemOption),
  74. 'custom_view' => $group->isCustomizedView()
  75. );
  76. $productOptions['options'][$key] = $userData;
  77. $this->_convertImageData($templateId);
  78. }
  79. }
  80. $item->setProductOptions($productOptions)->save();
  81. $this->setOrderItem($item);
  82. return $userData;
  83. }
  84.  
  85. protected function _convertImageData($templateId)
  86. {
  87. $image = Mage::getModel('aitcg/image')->load($templateId);
  88. $data = Mage::helper('core')->jsonDecode($image->getImgData());
  89. if (isset($data['data']))
  90. {
  91. return;
  92. }
  93. $result = array();
  94. foreach ($data as $item)
  95. {
  96. if (($item['preserveAspectRatio'] == 'xMidYMid') && ($imageSize = $this->_getImageSize($item)))
  97. {
  98. $d0 = $item['width']/$item['height'];
  99. $d1 = $imageSize['width']/$imageSize['height'];
  100. if ($d1 > $d0)
  101. {
  102. $h = $imageSize['height'] * $item['width'] / $imageSize['width'];
  103. $item['y'] = $item['y'] + ($item['height'] - $h) / 2;
  104. $item['height'] = $h;
  105. } else {
  106. $w = $imageSize['width'] * $item['height'] / $imageSize['height'];
  107. $item['x'] = $item['x'] + ($item['width'] - $w) / 2;
  108. $item['width'] = $w;
  109. }
  110. $item['preserveAspectRatio'] = 'none';
  111. }
  112. $item['creator'] = array(
  113. 'type' => 'UserImage',
  114. 'params' => null,
  115. 'isNew' => false
  116. );
  117. $r = isset($item['rotation']) ? $item['rotation'] : 0;
  118. $item['transform'] =
  119. 'r' . $r . ',' . ($item['x'] + $item['width']/2) . ',' . ($item['y'] + $item['height']/2) .
  120. 't' . $item['x'] . ',' . $item['y'];
  121. $item['x'] = 0;
  122. $item['y'] = 0;
  123. $item['r'] = 0;
  124. unset($item['rotation']);
  125. $result[] = $item;
  126. }
  127.  
  128. $image
  129. ->setImgData(Mage::helper('core')->jsonEncode(array('data' => $result)))
  130. ->save();
  131. }
  132.  
  133. protected function _getImageSize($image)
  134. {
  135. $imageSize = getimagesize($image['src']);
  136. if ($imageSize)
  137. {
  138. return array(
  139. 'width' => $imageSize[0],
  140. 'height' => $imageSize[1]
  141. );
  142. }
  143. return false;
  144. }
  145.  
  146. protected function _checkOptionValue($value)
  147. {
  148. $data = unserialize($value['option_value']);
  149. if (isset($data[Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY])
  150. &&
  151. version_compare(
  152. $data[Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY][self::CUSTOMER_IMAGE_META_VERSION_KEY],
  153. self::CUSTOMER_IMAGE_META_VERSION,
  154. 'ge'
  155. ))
  156. {
  157. return false;
  158. } else {
  159. return true;
  160. }
  161. }
  162.  
  163. protected function _getOptionMetadataVersion($value)
  164. {
  165. $data = unserialize($value['option_value']);
  166. if (isset($data[Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY]) &&
  167. isset($data[Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY][self::CUSTOMER_IMAGE_META_VERSION_KEY]))
  168. {
  169. $optionVersion = $data[Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY][self::CUSTOMER_IMAGE_META_VERSION_KEY];
  170. } else {
  171. $optionVersion = 'Nonversioned';
  172. }
  173. return $this->_getVersionReplacementKey($optionVersion);
  174. }
  175.  
  176. protected function _getVersionReplacementKey($version)
  177. {
  178. return str_replace('.', '', $version);
  179. }
  180.  
  181. protected function _getOption($optionId)
  182. {
  183. $productOptions = $this->getOrderItem()->getProductOptions();
  184. $product = Mage::getModel('catalog/product')->load($productOptions['info_buyRequest']['product']);
  185. return $product->getOptionById($optionId);
  186. }
  187.  
  188. }
RAW Paste Data