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

https://github.com/rgranadino/magento-mirror · PHP · 351 lines · 236 code · 41 blank · 74 comment · 33 complexity · 2cbd240f595b339135509a32d34dfb5c 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 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. * Proceeed 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. $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
  107. if (!$rate) {
  108. $rate = 1;
  109. }
  110. $newPrice = $price * $rate;
  111. } else {
  112. $newPrice = $price;
  113. }
  114. $select = $this->_getReadAdapter()->select()
  115. ->from($priceTable, 'option_type_id')
  116. ->where('option_type_id = ?', (int)$object->getId())
  117. ->where('store_id = ?', (int)$storeId);
  118. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  119. if ($optionTypeId) {
  120. $bind = array(
  121. 'price' => $newPrice,
  122. 'price_type' => $priceType
  123. );
  124. $where = array(
  125. 'option_type_id = ?' => (int)$optionTypeId,
  126. 'store_id = ?' => (int)$storeId
  127. );
  128. $this->_getWriteAdapter()->update($priceTable, $bind, $where);
  129. } else {
  130. $bind = array(
  131. 'option_type_id' => (int)$object->getId(),
  132. 'store_id' => (int)$storeId,
  133. 'price' => $newPrice,
  134. 'price_type' => $priceType
  135. );
  136. $this->_getWriteAdapter()->insert($priceTable, $bind);
  137. }
  138. }// end of foreach()
  139. }
  140. } else if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
  141. $where = array(
  142. 'option_type_id = ?' => (int)$object->getId(),
  143. 'store_id = ?' => (int)$object->getStoreId(),
  144. );
  145. $this->_getWriteAdapter()->delete($priceTable, $where);
  146. }
  147. }
  148. /**
  149. * Save option value title data
  150. *
  151. * @param Mage_Core_Model_Abstract $object
  152. */
  153. protected function _saveValueTitles(Mage_Core_Model_Abstract $object)
  154. {
  155. $titleTable = $this->getTable('catalog/product_option_type_title');
  156. if (!$object->getData('scope', 'title')) {
  157. $select = $this->_getReadAdapter()->select()
  158. ->from($titleTable, array('option_type_id'))
  159. ->where('option_type_id = ?', (int)$object->getId())
  160. ->where('store_id = ?', 0);
  161. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  162. if ($optionTypeId) {
  163. if ($object->getStoreId() == '0') {
  164. $where = array(
  165. 'option_type_id = ?' => (int)$optionTypeId,
  166. 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
  167. );
  168. $bind = array(
  169. 'title' => $object->getTitle()
  170. );
  171. $this->_getWriteAdapter()->update($titleTable, $bind, $where);
  172. }
  173. } else {
  174. $bind = array(
  175. 'option_type_id' => (int)$object->getId(),
  176. 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
  177. 'title' => $object->getTitle()
  178. );
  179. $this->_getWriteAdapter()->insert($titleTable, $bind);
  180. }
  181. }
  182. if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
  183. $select = $this->_getReadAdapter()->select()
  184. ->from($titleTable, array('option_type_id'))
  185. ->where('option_type_id = ?', (int)$object->getId())
  186. ->where('store_id = ?', (int)$object->getStoreId());
  187. $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
  188. if ($optionTypeId) {
  189. $bind = array(
  190. 'title' => $object->getTitle()
  191. );
  192. $where = array(
  193. 'option_type_id = ?' => (int)$optionTypeId,
  194. 'store_id = ?' => (int)$object->getStoreId()
  195. );
  196. $this->_getWriteAdapter()->update($titleTable, $bind, $where);
  197. } else {
  198. $bind = array(
  199. 'option_type_id' => (int)$object->getId(),
  200. 'store_id' => (int)$object->getStoreId(),
  201. 'title' => $object->getTitle()
  202. );
  203. $this->_getWriteAdapter()->insert($titleTable, $bind);
  204. }
  205. } else if ($object->getData('scope', 'title')) {
  206. $where = array(
  207. 'option_type_id = ?' => (int)$optionTypeId,
  208. 'store_id = ?' => (int)$object->getStoreId()
  209. );
  210. $this->_getWriteAdapter()->delete($titleTable, $where);
  211. }
  212. }
  213. /**
  214. * Delete values by option id
  215. *
  216. * @param int $optionId
  217. * @return Mage_Catalog_Model_Resource_Product_Option_Value
  218. */
  219. public function deleteValue($optionId)
  220. {
  221. $statement = $this->_getReadAdapter()->select()
  222. ->from($this->getTable('catalog/product_option_type_value'))
  223. ->where('option_id = ?', $optionId);
  224. $rowSet = $this->_getReadAdapter()->fetchAll($statement);
  225. foreach ($rowSet as $optionType) {
  226. $this->deleteValues($optionType['option_type_id']);
  227. }
  228. $this->_getWriteAdapter()->delete(
  229. $this->getMainTable(),
  230. array(
  231. 'option_id = ?' => $optionId,
  232. )
  233. );
  234. return $this;
  235. }
  236. /**
  237. * Delete values by option type
  238. *
  239. * @param int $optionTypeId
  240. */
  241. public function deleteValues($optionTypeId)
  242. {
  243. $condition = array(
  244. 'option_type_id = ?' => $optionTypeId
  245. );
  246. $this->_getWriteAdapter()->delete(
  247. $this->getTable('catalog/product_option_type_price'),
  248. $condition
  249. );
  250. $this->_getWriteAdapter()->delete(
  251. $this->getTable('catalog/product_option_type_title'),
  252. $condition
  253. );
  254. }
  255. /**
  256. * Duplicate product options value
  257. *
  258. * @param Mage_Catalog_Model_Product_Option_Value $object
  259. * @param int $oldOptionId
  260. * @param int $newOptionId
  261. * @return Mage_Catalog_Model_Product_Option_Value
  262. */
  263. public function duplicate(Mage_Catalog_Model_Product_Option_Value $object, $oldOptionId, $newOptionId)
  264. {
  265. $writeAdapter = $this->_getWriteAdapter();
  266. $readAdapter = $this->_getReadAdapter();
  267. $select = $readAdapter->select()
  268. ->from($this->getMainTable())
  269. ->where('option_id = ?', $oldOptionId);
  270. $valueData = $readAdapter->fetchAll($select);
  271. $valueCond = array();
  272. foreach ($valueData as $data) {
  273. $optionTypeId = $data[$this->getIdFieldName()];
  274. unset($data[$this->getIdFieldName()]);
  275. $data['option_id'] = $newOptionId;
  276. $writeAdapter->insert($this->getMainTable(), $data);
  277. $valueCond[$optionTypeId] = $writeAdapter->lastInsertId($this->getMainTable());
  278. }
  279. unset($valueData);
  280. foreach ($valueCond as $oldTypeId => $newTypeId) {
  281. // price
  282. $priceTable = $this->getTable('catalog/product_option_type_price');
  283. $columns= array(
  284. new Zend_Db_Expr($newTypeId),
  285. 'store_id', 'price', 'price_type'
  286. );
  287. $select = $readAdapter->select()
  288. ->from($priceTable, array())
  289. ->where('option_type_id = ?', $oldTypeId)
  290. ->columns($columns);
  291. $insertSelect = $writeAdapter->insertFromSelect($select, $priceTable,
  292. array('option_type_id', 'store_id', 'price', 'price_type'));
  293. $writeAdapter->query($insertSelect);
  294. // title
  295. $titleTable = $this->getTable('catalog/product_option_type_title');
  296. $columns= array(
  297. new Zend_Db_Expr($newTypeId),
  298. 'store_id', 'title'
  299. );
  300. $select = $this->_getReadAdapter()->select()
  301. ->from($titleTable, array())
  302. ->where('option_type_id = ?', $oldTypeId)
  303. ->columns($columns);
  304. $insertSelect = $writeAdapter->insertFromSelect($select, $titleTable,
  305. array('option_type_id', 'store_id', 'title'));
  306. $writeAdapter->query($insertSelect);
  307. }
  308. return $object;
  309. }
  310. }