PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Resource/Product/Option/Value.php

https://bitbucket.org/jokusafet/magento2
PHP | 354 lines | 238 code | 41 blank | 75 comment | 33 complexity | edc746e4c5f158d91deba628a7130104 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) 2012 X.commerce, 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 custom option resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Option_Value extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Define main table and initialize connection
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('catalog_product_option_type_value', 'option_type_id');
  42. }
  43. /**
  44. * Proceed operations after object is saved
  45. * Save options store data
  46. *
  47. * @param Mage_Core_Model_Abstract $object
  48. * @return Mage_Core_Model_Resource_Db_Abstract
  49. */
  50. protected function _afterSave(Mage_Core_Model_Abstract $object)
  51. {
  52. $this->_saveValuePrices($object);
  53. $this->_saveValueTitles($object);
  54. return parent::_afterSave($object);
  55. }
  56. /**
  57. * Save option value price data
  58. *
  59. * @param Mage_Core_Model_Abstract $object
  60. */
  61. protected function _saveValuePrices(Mage_Core_Model_Abstract $object)
  62. {
  63. $priceTable = $this->getTable('catalog_product_option_type_price');
  64. $price = (float)sprintf('%F', $object->getPrice());
  65. $priceType = $object->getPriceType();
  66. if (!$object->getData('scope', 'price')) {
  67. //save for store_id = 0
  68. $select = $this->_getReadAdapter()->select()
  69. ->from($priceTable, 'option_type_id')
  70. ->where('option_type_id = ?', (int)$object->getId())
  71. ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
  72. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  73. if ($optionTypeId) {
  74. if ($object->getStoreId() == '0') {
  75. $bind = array(
  76. 'price' => $price,
  77. 'price_type' => $priceType
  78. );
  79. $where = array(
  80. 'option_type_id = ?' => $optionTypeId,
  81. 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
  82. );
  83. $this->_getWriteAdapter()->update($priceTable, $bind, $where);
  84. }
  85. } else {
  86. $bind = array(
  87. 'option_type_id' => (int)$object->getId(),
  88. 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
  89. 'price' => $price,
  90. 'price_type' => $priceType
  91. );
  92. $this->_getWriteAdapter()->insert($priceTable, $bind);
  93. }
  94. }
  95. $scope = (int)Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
  96. if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
  97. && !$object->getData('scope', 'price')) {
  98. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  99. $storeIds = Mage::app()->getStore($object->getStoreId())
  100. ->getWebsite()
  101. ->getStoreIds();
  102. if (is_array($storeIds)) {
  103. foreach ($storeIds as $storeId) {
  104. if ($priceType == 'fixed') {
  105. $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
  106. /** @var $currencyModel Mage_Directory_Model_Currency */
  107. $currencyModel = Mage::getModel('Mage_Directory_Model_Currency');
  108. $currencyModel->load($baseCurrency);
  109. $rate = $currencyModel->getRate($storeCurrency);
  110. if (!$rate) {
  111. $rate = 1;
  112. }
  113. $newPrice = $price * $rate;
  114. } else {
  115. $newPrice = $price;
  116. }
  117. $select = $this->_getReadAdapter()->select()
  118. ->from($priceTable, 'option_type_id')
  119. ->where('option_type_id = ?', (int)$object->getId())
  120. ->where('store_id = ?', (int)$storeId);
  121. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  122. if ($optionTypeId) {
  123. $bind = array(
  124. 'price' => $newPrice,
  125. 'price_type' => $priceType
  126. );
  127. $where = array(
  128. 'option_type_id = ?' => (int)$optionTypeId,
  129. 'store_id = ?' => (int)$storeId
  130. );
  131. $this->_getWriteAdapter()->update($priceTable, $bind, $where);
  132. } else {
  133. $bind = array(
  134. 'option_type_id' => (int)$object->getId(),
  135. 'store_id' => (int)$storeId,
  136. 'price' => $newPrice,
  137. 'price_type' => $priceType
  138. );
  139. $this->_getWriteAdapter()->insert($priceTable, $bind);
  140. }
  141. }// end of foreach()
  142. }
  143. } else if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
  144. $where = array(
  145. 'option_type_id = ?' => (int)$object->getId(),
  146. 'store_id = ?' => (int)$object->getStoreId(),
  147. );
  148. $this->_getWriteAdapter()->delete($priceTable, $where);
  149. }
  150. }
  151. /**
  152. * Save option value title data
  153. *
  154. * @param Mage_Core_Model_Abstract $object
  155. */
  156. protected function _saveValueTitles(Mage_Core_Model_Abstract $object)
  157. {
  158. $titleTable = $this->getTable('catalog_product_option_type_title');
  159. if (!$object->getData('scope', 'title')) {
  160. $select = $this->_getReadAdapter()->select()
  161. ->from($titleTable, array('option_type_id'))
  162. ->where('option_type_id = ?', (int)$object->getId())
  163. ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
  164. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  165. if ($optionTypeId) {
  166. if ($object->getStoreId() == '0') {
  167. $where = array(
  168. 'option_type_id = ?' => (int)$optionTypeId,
  169. 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
  170. );
  171. $bind = array(
  172. 'title' => $object->getTitle()
  173. );
  174. $this->_getWriteAdapter()->update($titleTable, $bind, $where);
  175. }
  176. } else {
  177. $bind = array(
  178. 'option_type_id' => (int)$object->getId(),
  179. 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
  180. 'title' => $object->getTitle()
  181. );
  182. $this->_getWriteAdapter()->insert($titleTable, $bind);
  183. }
  184. }
  185. if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
  186. $select = $this->_getReadAdapter()->select()
  187. ->from($titleTable, array('option_type_id'))
  188. ->where('option_type_id = ?', (int)$object->getId())
  189. ->where('store_id = ?', (int)$object->getStoreId());
  190. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  191. if ($optionTypeId) {
  192. $bind = array(
  193. 'title' => $object->getTitle()
  194. );
  195. $where = array(
  196. 'option_type_id = ?' => (int)$optionTypeId,
  197. 'store_id = ?' => (int)$object->getStoreId()
  198. );
  199. $this->_getWriteAdapter()->update($titleTable, $bind, $where);
  200. } else {
  201. $bind = array(
  202. 'option_type_id' => (int)$object->getId(),
  203. 'store_id' => (int)$object->getStoreId(),
  204. 'title' => $object->getTitle()
  205. );
  206. $this->_getWriteAdapter()->insert($titleTable, $bind);
  207. }
  208. } else if ($object->getData('scope', 'title')) {
  209. $where = array(
  210. 'option_type_id = ?' => (int) $object->getId(),
  211. 'store_id = ?' => (int) $object->getStoreId(),
  212. );
  213. $this->_getWriteAdapter()->delete($titleTable, $where);
  214. }
  215. }
  216. /**
  217. * Delete values by option id
  218. *
  219. * @param int $optionId
  220. * @return Mage_Catalog_Model_Resource_Product_Option_Value
  221. */
  222. public function deleteValue($optionId)
  223. {
  224. $statement = $this->_getReadAdapter()->select()
  225. ->from($this->getTable('catalog_product_option_type_value'))
  226. ->where('option_id = ?', $optionId);
  227. $rowSet = $this->_getReadAdapter()->fetchAll($statement);
  228. foreach ($rowSet as $optionType) {
  229. $this->deleteValues($optionType['option_type_id']);
  230. }
  231. $this->_getWriteAdapter()->delete(
  232. $this->getMainTable(),
  233. array(
  234. 'option_id = ?' => $optionId,
  235. )
  236. );
  237. return $this;
  238. }
  239. /**
  240. * Delete values by option type
  241. *
  242. * @param int $optionTypeId
  243. */
  244. public function deleteValues($optionTypeId)
  245. {
  246. $condition = array(
  247. 'option_type_id = ?' => $optionTypeId
  248. );
  249. $this->_getWriteAdapter()->delete(
  250. $this->getTable('catalog_product_option_type_price'),
  251. $condition
  252. );
  253. $this->_getWriteAdapter()->delete(
  254. $this->getTable('catalog_product_option_type_title'),
  255. $condition
  256. );
  257. }
  258. /**
  259. * Duplicate product options value
  260. *
  261. * @param Mage_Catalog_Model_Product_Option_Value $object
  262. * @param int $oldOptionId
  263. * @param int $newOptionId
  264. * @return Mage_Catalog_Model_Product_Option_Value
  265. */
  266. public function duplicate(Mage_Catalog_Model_Product_Option_Value $object, $oldOptionId, $newOptionId)
  267. {
  268. $writeAdapter = $this->_getWriteAdapter();
  269. $readAdapter = $this->_getReadAdapter();
  270. $select = $readAdapter->select()
  271. ->from($this->getMainTable())
  272. ->where('option_id = ?', $oldOptionId);
  273. $valueData = $readAdapter->fetchAll($select);
  274. $valueCond = array();
  275. foreach ($valueData as $data) {
  276. $optionTypeId = $data[$this->getIdFieldName()];
  277. unset($data[$this->getIdFieldName()]);
  278. $data['option_id'] = $newOptionId;
  279. $writeAdapter->insert($this->getMainTable(), $data);
  280. $valueCond[$optionTypeId] = $writeAdapter->lastInsertId($this->getMainTable());
  281. }
  282. unset($valueData);
  283. foreach ($valueCond as $oldTypeId => $newTypeId) {
  284. // price
  285. $priceTable = $this->getTable('catalog_product_option_type_price');
  286. $columns= array(
  287. new Zend_Db_Expr($newTypeId),
  288. 'store_id', 'price', 'price_type'
  289. );
  290. $select = $readAdapter->select()
  291. ->from($priceTable, array())
  292. ->where('option_type_id = ?', $oldTypeId)
  293. ->columns($columns);
  294. $insertSelect = $writeAdapter->insertFromSelect($select, $priceTable,
  295. array('option_type_id', 'store_id', 'price', 'price_type'));
  296. $writeAdapter->query($insertSelect);
  297. // title
  298. $titleTable = $this->getTable('catalog_product_option_type_title');
  299. $columns= array(
  300. new Zend_Db_Expr($newTypeId),
  301. 'store_id', 'title'
  302. );
  303. $select = $this->_getReadAdapter()->select()
  304. ->from($titleTable, array())
  305. ->where('option_type_id = ?', $oldTypeId)
  306. ->columns($columns);
  307. $insertSelect = $writeAdapter->insertFromSelect($select, $titleTable,
  308. array('option_type_id', 'store_id', 'title'));
  309. $writeAdapter->query($insertSelect);
  310. }
  311. return $object;
  312. }
  313. }