Guest User

file.php

a guest
Feb 28th, 2017
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class Aitoc_Aitcg_Model_Rewrite_Catalog_Product_Option_Type_File extends Mage_Catalog_Model_Product_Option_Type_File
  3. {
  4. private $_is15 = false;
  5.  
  6. public function __construct()
  7. {
  8. $this->_is15 = (version_compare(Mage::getVersion(), '1.5.0.0') >= 0) ? true : false;
  9. }
  10.  
  11. /**
  12. * Validate uploaded file
  13. *
  14. * @throws Mage_Core_Exception
  15. * @return Mage_Catalog_Model_Product_Option_Type_File
  16. */
  17. protected function _validateUploadedFile()
  18. {
  19. $option = $this->getOption();
  20.  
  21. if ( Mage::helper('aitcg/options')->checkAitOption( $option ) )
  22. {
  23. $values = Mage::app()->getRequest()->getParam('options');
  24. $optionValue = $values[$option->getId()];
  25. $runValidation = ($option->getIsRequire() || $this->_validateValue($option)) && !is_null($optionValue);
  26. if (!$runValidation) {
  27. $this->setUserValue(null);
  28. return $this;
  29. }
  30.  
  31. $optionValue = Mage::helper('core')->jsonDecode($optionValue);
  32. $this->_initFilesystem();
  33.  
  34. $model = Mage::getModel('aitcg/image');
  35. $src = Mage::getBaseDir('media') . DS
  36. . $model->getFullTempPath() . DS
  37. . $optionValue['config']['rand'] . '.'
  38. . Aitoc_Aitcg_Model_Image::FORMAT_PNG;
  39.  
  40. $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName(pathinfo(strtolower($src), PATHINFO_FILENAME));
  41. $dispersion = Mage_Core_Model_File_Uploader::getDispretionPath($fileName);
  42.  
  43. $filePath = $dispersion;
  44. $fileHash = md5(file_get_contents($src));
  45. $filePath .= DS . $fileHash . '.' . Aitoc_Aitcg_Model_Image::FORMAT_PNG;
  46. $fileFullPath = $this->getQuoteTargetDir() . $filePath;
  47.  
  48. $_width = 0;
  49. $_height = 0;
  50. $_fileSize = 0;
  51. if (is_readable($src)) {
  52. $_imageSize = getimagesize($src);
  53. if ($_imageSize) {
  54. $_width = $_imageSize[0];
  55. $_height = $_imageSize[1];
  56. }
  57. $_fileSize = filesize($src);
  58. }
  59.  
  60. $manager = Mage::getModel('aitcg/image_manager');
  61.  
  62. $template_id = $manager->addImage($values[$option->getId()], Mage::app()->getRequest()->getParam('product'), $option->getId(), $option) ;
  63.  
  64. $this->setUserValue(array(
  65. 'type' => 'image/png',
  66. 'title' => 'custom_product_preview.png',
  67. 'quote_path' => $this->getQuoteTargetDir(true) . $filePath,
  68. 'order_path' => $this->getOrderTargetDir(true) . $filePath,
  69. 'fullpath' => $fileFullPath,
  70. 'size' => $_fileSize,
  71. 'width' => $_width,
  72. 'height' => $_height,
  73. 'secret_key' => substr($fileHash, 0, 20),
  74.  
  75. Aitoc_Aitcg_Helper_Options::OPTION_DATA_KEY => array(
  76. 'template_id' => $template_id,
  77. Aitoc_Aitcg_Model_Sales_Order_Item_Converter::CUSTOMER_IMAGE_META_VERSION_KEY =>
  78. Aitoc_Aitcg_Model_Sales_Order_Item_Converter::CUSTOMER_IMAGE_META_VERSION)
  79. ));
  80.  
  81. $path = dirname($fileFullPath);
  82. $io = new Varien_Io_File();
  83. if (!$io->isWriteable($path) && !$io->mkdir($path, 0777, true)) {
  84. Mage::throwException(Mage::helper('catalog')->__("Cannot create writeable directory '%s'.", $path));
  85. }
  86.  
  87. @copy($src, $fileFullPath);
  88.  
  89. return $this;
  90. } else {
  91. return parent::_validateUploadedFile();
  92. }
  93.  
  94. }
  95.  
  96. protected function _validateFile($optionValue)
  97. {
  98. $option = $this->getOption();
  99. if (Mage::helper('aitcg/options')->checkAitOption($option))
  100. {
  101. return true;
  102. }
  103. return parent::_validateFile($optionValue);
  104. }
  105.  
  106. protected function _validateValue($option)
  107. {
  108. $options = $this->getRequest()->getOptions();
  109. if (isset($options[$option->getId()]) && $options[$option->getId()])
  110. {
  111. return true;
  112. }
  113. return false;
  114. }
  115.  
  116. /**
  117. * Return option html
  118. *
  119. * @param array $optionInfo
  120. * @return string
  121. */
  122. public function getCustomizedView($optionInfo)
  123. {
  124. try {
  125. if (isset($optionInfo['option_value'])) {
  126. return $this->_getOptionHtml($optionInfo);
  127. } elseif (isset($optionInfo['value'])) {
  128. return $optionInfo['value'];
  129. }
  130. } catch (Exception $e) {
  131. return $optionInfo['value'];
  132. }
  133. }
  134.  
  135. /**
  136. * Format File option html
  137. *
  138. * @param string|array $optionValue Serialized string of option data or its data array
  139. * @return string
  140. * @see Mage_Catalog_Model_Product_Option_Type_File::_getOptionHtml()
  141. */
  142. protected function _getOptionHtml($optionValue)
  143. {
  144. if ($this->_isCreateOrderRequest())
  145. {
  146. return parent::_getOptionHtml($optionValue);
  147. }
  148.  
  149. try {
  150. $option = $this->getOption();
  151. } catch (Exception $e) {
  152. $option = $this->_getOptionByValue($optionValue);
  153. }
  154.  
  155. $value = $this->_getValueOption($optionValue);
  156.  
  157. $isTempData = false;
  158. $data = array();
  159. if( Mage::helper('aitcg/options')->checkAitOption( $option ) && $option->getData("image_template_id") > 0)
  160. {
  161.  
  162. $model = Mage::getModel('aitcg/image');
  163.  
  164. $data = $this->_getImageData($model, $value);
  165. $data = $this->_getAreaData($option, $model, $data);
  166.  
  167. $isTempData = true;
  168.  
  169. }
  170. return $this->_sprintOption($value, $optionValue, $option, $data, $isTempData);
  171. }
  172.  
  173. protected function _getOptionByValue($optionInfo)
  174. {
  175. if (isset($optionInfo['option_value']))
  176. {
  177. $option = Mage::getModel('catalog/product_option')->load($optionInfo['option_id']);
  178. $product = Mage::getModel('catalog/product')->load($option->getProductId());
  179.  
  180. return $product->getOptionById($option->getId());
  181. }
  182. }
  183.  
  184. protected function _sprintOption($value, $optionValue, $option, $data, $isTempData)
  185. {
  186.  
  187. $js = "";
  188. if($isTempData)
  189. {
  190. $sBlockName = $this->_getBlockName();
  191. $js = Mage::app()->getLayout()
  192. ->createBlock($sBlockName, null, $data)
  193. ->setProduct( $option->getProduct() )
  194. ->setOption( $option )
  195. ->toHtml();
  196.  
  197. return $js;
  198. }
  199. else
  200. {
  201. return $js . sprintf('<a href="%s" target="_blank">%s</a> %s',
  202. $this->_getOptionDownloadUrl($value['url']['route'], $value['url']['params']),
  203. Mage::helper('core')->htmlEscape($value['title']),
  204. $this->_getSizes($optionValue)
  205. );
  206. }
  207. }
  208.  
  209. protected function _getBlockName()
  210. {
  211. $request = Mage::app()->getRequest();
  212. if( $request->getModuleName() == 'checkout' && $request->getControllerName() == 'cart') {
  213. //allowing moving/uploading images only at cart
  214. return 'aitcg/checkout_cart_item_option_cgfile';
  215. }
  216. return 'aitcg/checkout_cart_item_option_cgfile_lite';
  217. }
  218.  
  219. protected function _getImageData($model, $value)
  220. {
  221. $request = Mage::app()->getRequest();
  222. $data = array();
  223. if( isset($value['aitcg_data']['template_id']) && $value['aitcg_data']['template_id'] > 0 )
  224. {
  225. $model->load($value['aitcg_data']['template_id']);
  226. if(!$model->isEmpty())
  227. {
  228. $data = $model->getFullData();
  229. }
  230. }
  231. return $data;
  232. }
  233.  
  234. protected function _getAreaData($option, $model, $data = array())
  235. {
  236. $request = Mage::app()->getRequest();
  237. if( $request->getModuleName() == 'checkout' && $request->getControllerName() == 'cart') {
  238. //allowing moving/uploading images only at cart
  239. if($this->hasData('quote_item')) {
  240. $options = $this->getQuoteItem()->getOptions();
  241. } else {
  242. $options = $this->getQuoteItemOption()->getItem()->getOptions();
  243. }
  244. foreach($options as $buyRequest ) {
  245. if($buyRequest['code'] == 'info_buyRequest') {
  246. break;
  247. }
  248. }
  249. }
  250.  
  251. if(!isset($data['area_size_x'])) {
  252. $data['area_size_x'] = $option->getAreaSizeX();
  253. $data['area_size_y'] = $option->getAreaSizeY();
  254. $data['area_offset_x']= $option->getAreaOffsetX();
  255. $data['area_offset_y']= $option->getAreaOffsetY();
  256. }
  257.  
  258. $data["image"] = $model->getMediaImage( $option->getProductId(), $option->getImageTemplateId(), Aitoc_Aitcg_Helper_Data::INTEGRATION_POPUP );
  259.  
  260. $this->setData('aitcg_model', $data);
  261.  
  262. if(isset($buyRequest))
  263. $data['buy_request'] = $buyRequest;
  264. if($model->hasDataChanges()) {
  265. $model->save();
  266. }
  267. return $data;
  268. }
  269.  
  270. protected function _getSizes($optionValue)
  271. {
  272. $value = $this->_getValueOption($optionValue);
  273. if (isset($value['width'],$value['height']) && $value['width'] > 0 && $value['height'] > 0)
  274. {
  275. $sizes = $value['width'] . ' x ' . $value['height'] . ' ' . Mage::helper('catalog')->__('px.');
  276. } else
  277. {
  278. $sizes = '';
  279. }
  280. return $sizes;
  281. }
  282.  
  283. protected function _getValueOption($optionValue)
  284. {
  285. if (isset($optionValue['option_value']))
  286. {
  287. $optionValue = $optionValue['option_value'];
  288. }
  289. if(is_array($optionValue))
  290. {
  291. return $optionValue;
  292. } else
  293. {
  294. try
  295. {
  296. return unserialize($optionValue);
  297. } catch (Exception $e) {
  298. return $optionValue;
  299. }
  300. }
  301. }
  302.  
  303. protected function _isCreateOrderRequest()
  304. {
  305. return Mage::helper('aitcg')->requestIsSuitable(array(
  306. 'checkout/onepage/saveOrder',
  307. 'checkout/multishipping/overviewPost'
  308. ));
  309. }
  310. }
RAW Paste Data