PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Type/Configurable/Attribute.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 283 lines | 170 code | 26 blank | 87 comment | 32 complexity | 237cc9c927a4056737961f1452c5dd58 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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 super product attribute 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_Eav_Mysql4_Product_Type_Configurable_Attribute extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Label table name cache
  37. *
  38. * @var string
  39. */
  40. protected $_labelTable;
  41. /**
  42. * Price table name cache
  43. *
  44. * @var string
  45. */
  46. protected $_priceTable;
  47. /**
  48. * Inititalize connection and define tables
  49. *
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('catalog/product_super_attribute', 'product_super_attribute_id');
  54. $this->_labelTable = $this->getTable('catalog/product_super_attribute_label');
  55. $this->_priceTable = $this->getTable('catalog/product_super_attribute_pricing');
  56. }
  57. /**
  58. * Retrieve Catalog Helper
  59. *
  60. * @return Mage_Catalog_Helper_Data
  61. */
  62. public function getCatalogHelper()
  63. {
  64. return Mage::helper('catalog');
  65. }
  66. /**
  67. * Load attribute labels
  68. *
  69. * @deprecated
  70. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  71. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute
  72. */
  73. public function loadLabel($attribute)
  74. {
  75. return $this;
  76. }
  77. /**
  78. * Load prices
  79. *
  80. * @deprecated
  81. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  82. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute
  83. */
  84. public function loadPrices($attribute)
  85. {
  86. return $this;
  87. }
  88. /**
  89. * Save Custom labels for Attribute name
  90. *
  91. * @param Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute
  92. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute
  93. */
  94. public function saveLabel($attribute)
  95. {
  96. $select = $this->_getWriteAdapter()->select()
  97. ->from($this->_labelTable, 'value_id')
  98. ->where('product_super_attribute_id=?', $attribute->getId())
  99. ->where('store_id=?', (int)$attribute->getStoreId());
  100. if ($valueId = $this->_getWriteAdapter()->fetchOne($select)) {
  101. $this->_getWriteAdapter()->update($this->_labelTable,array(
  102. 'use_default' => (int) $attribute->getUseDefault(),
  103. 'value'=>$attribute->getLabel()
  104. ),
  105. $this->_getWriteAdapter()->quoteInto('value_id=?', $valueId)
  106. );
  107. }
  108. else {
  109. $this->_getWriteAdapter()->insert($this->_labelTable, array(
  110. 'product_super_attribute_id' => $attribute->getId(),
  111. 'store_id' => (int) $attribute->getStoreId(),
  112. 'use_default' => (int) $attribute->getUseDefault(),
  113. 'value' => $attribute->getLabel()
  114. ));
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Save Options prices (Depends from price save scope)
  120. *
  121. * @param Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute
  122. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute
  123. */
  124. public function savePrices($attribute)
  125. {
  126. $write = $this->_getWriteAdapter();
  127. // define website id scope
  128. if ($this->getCatalogHelper()->isPriceGlobal()) {
  129. $websiteId = 0;
  130. } else {
  131. $websiteId = Mage::app()->getStore($attribute->getStoreId())->getWebsite()->getId();
  132. }
  133. $values = $attribute->getValues();
  134. if (!is_array($values)) {
  135. $values = array();
  136. }
  137. $new = array();
  138. $old = array();
  139. // retrieve old values
  140. $select = $write->select()
  141. ->from($this->_priceTable)
  142. ->where('product_super_attribute_id=?', $attribute->getId())
  143. ->where('website_id=?', $websiteId);
  144. $rowSet = $write->fetchAll($select);
  145. foreach ($rowSet as $row) {
  146. $key = join('-', array($row['website_id'], $row['value_index']));
  147. if (!isset($old[$key])) {
  148. $old[$key] = $row;
  149. } else {
  150. // delete invalid (duplicate row)
  151. $where = $write->quoteInto('value_id=?', $row['value_id']);
  152. $write->delete($this->_priceTable, $where);
  153. }
  154. }
  155. // prepare new values
  156. foreach ($values as $v) {
  157. if (empty($v['value_index'])) {
  158. continue;
  159. }
  160. $key = join('-', array($websiteId, $v['value_index']));
  161. $new[$key] = array(
  162. 'value_index' => $v['value_index'],
  163. 'pricing_value' => $v['pricing_value'],
  164. 'is_percent' => $v['is_percent'],
  165. 'website_id' => $websiteId,
  166. 'use_default' => !empty($v['use_default_value']) ? true : false
  167. );
  168. }
  169. $insert = array();
  170. $update = array();
  171. $delete = array();
  172. foreach ($old as $k => $v) {
  173. if (!isset($new[$k])) {
  174. $delete[] = $v['value_id'];
  175. }
  176. }
  177. foreach ($new as $k => $v) {
  178. $needInsert = false;
  179. $needUpdate = false;
  180. $needDelete = false;
  181. $isGlobal = true;
  182. if (!$this->getCatalogHelper()->isPriceGlobal() && $websiteId != 0) {
  183. $isGlobal = false;
  184. }
  185. $hasValue = ($isGlobal && !empty($v['pricing_value']))
  186. || (!$isGlobal && !$v['use_default']);
  187. if (isset($old[$k])) {
  188. // data changed
  189. $dataChanged = ($old[$k]['is_percent'] != $v['is_percent'])
  190. || ($old[$k]['pricing_value'] != $v['pricing_value']);
  191. if (!$hasValue) {
  192. $needDelete = true;
  193. } else if ($dataChanged) {
  194. $needUpdate = true;
  195. }
  196. } else if ($hasValue) {
  197. $needInsert = true;
  198. }
  199. if (!$isGlobal && empty($v['pricing_value'])) {
  200. $v['pricing_value'] = 0;
  201. $v['is_percent'] = 0;
  202. }
  203. if ($needInsert) {
  204. $insert[] = array(
  205. 'product_super_attribute_id' => $attribute->getId(),
  206. 'value_index' => $v['value_index'],
  207. 'is_percent' => $v['is_percent'],
  208. 'pricing_value' => $v['pricing_value'],
  209. 'website_id' => $websiteId
  210. );
  211. }
  212. if ($needUpdate) {
  213. $update[$old[$k]['value_id']] = array(
  214. 'is_percent' => $v['is_percent'],
  215. 'pricing_value' => $v['pricing_value']
  216. );
  217. }
  218. if ($needDelete) {
  219. $delete[] = $old[$k]['value_id'];
  220. }
  221. }
  222. if (!empty($delete)) {
  223. $where = $write->quoteInto('value_id IN(?)', $delete);
  224. $write->delete($this->_priceTable, $where);
  225. }
  226. if (!empty($update)) {
  227. foreach ($update as $valueId => $bind) {
  228. $where = $write->quoteInto('value_id=?', $valueId);
  229. $write->update($this->_priceTable, $bind, $where);
  230. }
  231. }
  232. if (!empty($insert)) {
  233. $write->insertMultiple($this->_priceTable, $insert);
  234. }
  235. return $this;
  236. }
  237. /**
  238. * Retrieve Used in Configurable Products Attributes
  239. *
  240. * @param int $setId The specific attribute set
  241. * @return array
  242. */
  243. public function getUsedAttributes($setId)
  244. {
  245. $select = $this->_getReadAdapter()->select()
  246. ->distinct(true)
  247. ->from(array('e' => $this->getTable('catalog/product')), null)
  248. ->join(
  249. array('a' => $this->getMainTable()),
  250. 'e.entity_id=a.product_id',
  251. array('attribute_id')
  252. )
  253. ->where('e.attribute_set_id=?', $setId)
  254. ->where('e.type_id=?', Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
  255. return $this->_getReadAdapter()->fetchCol($select);
  256. }
  257. }