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 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:
28: /**
29: * Catalog product price attribute backend model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Product_Attribute_Backend_Price extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
36: {
37: /**
38: * Set Attribute instance
39: * Rewrite for redefine attribute scope
40: *
41: * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
42: * @return Mage_Catalog_Model_Product_Attribute_Backend_Price
43: */
44: public function setAttribute($attribute)
45: {
46: parent::setAttribute($attribute);
47: $this->setScope($attribute);
48: return $this;
49: }
50:
51: /**
52: * Redefine Attribute scope
53: *
54: * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
55: * @return Mage_Catalog_Model_Product_Attribute_Backend_Price
56: */
57: public function setScope($attribute)
58: {
59: if (Mage::helper('catalog')->isPriceGlobal()) {
60: $attribute->setIsGlobal(Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL);
61: }
62: else {
63: $attribute->setIsGlobal(Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE);
64: }
65:
66: return $this;
67: }
68:
69: /**
70: * After Save Attribute manipulation
71: *
72: * @param Mage_Catalog_Model_Product $object
73: * @return Mage_Catalog_Model_Product_Attribute_Backend_Price
74: */
75: public function afterSave($object)
76: {
77: $value = $object->getData($this->getAttribute()->getAttributeCode());
78: /**
79: * Orig value is only for existing objects
80: */
81: $oridData = $object->getOrigData();
82: $origValueExist = $oridData && array_key_exists($this->getAttribute()->getAttributeCode(), $oridData);
83: if ($object->getStoreId() != 0 || !$value || $origValueExist) {
84: return $this;
85: }
86:
87: if ($this->getAttribute()->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE) {
88: $baseCurrency = Mage::app()->getBaseCurrencyCode();
89:
90: $storeIds = $object->getStoreIds();
91: if (is_array($storeIds)) {
92: foreach ($storeIds as $storeId) {
93: $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
94: if ($storeCurrency == $baseCurrency) {
95: continue;
96: }
97: $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
98: if (!$rate) {
99: $rate = 1;
100: }
101: $newValue = $value * $rate;
102: $object->addAttributeUpdate($this->getAttribute()->getAttributeCode(), $newValue, $storeId);
103: }
104: }
105: }
106:
107: return $this;
108: }
109: }
110: