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_Adminhtml
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: * Adminhtml group price item abstract renderer
29: *
30: * @category Mage
31: * @package Mage_Adminhtml
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract
35: extends Mage_Adminhtml_Block_Widget
36: implements Varien_Data_Form_Element_Renderer_Interface
37: {
38: /**
39: * Form element instance
40: *
41: * @var Varien_Data_Form_Element_Abstract
42: */
43: protected $_element;
44:
45: /**
46: * Customer groups cache
47: *
48: * @var array
49: */
50: protected $_customerGroups;
51:
52: /**
53: * Websites cache
54: *
55: * @var array
56: */
57: protected $_websites;
58:
59: /**
60: * Retrieve current product instance
61: *
62: * @return Mage_Catalog_Model_Product
63: */
64: public function getProduct()
65: {
66: return Mage::registry('product');
67: }
68:
69: /**
70: * Render HTML
71: *
72: * @param Varien_Data_Form_Element_Abstract $element
73: * @return string
74: */
75: public function render(Varien_Data_Form_Element_Abstract $element)
76: {
77: $this->setElement($element);
78: return $this->toHtml();
79: }
80:
81: /**
82: * Set form element instance
83: *
84: * @param Varien_Data_Form_Element_Abstract $element
85: * @return Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract
86: */
87: public function setElement(Varien_Data_Form_Element_Abstract $element)
88: {
89: $this->_element = $element;
90: return $this;
91: }
92:
93: /**
94: * Retrieve form element instance
95: *
96: * @return Varien_Data_Form_Element_Abstract
97: */
98: public function getElement()
99: {
100: return $this->_element;
101: }
102:
103: /**
104: * Prepare group price values
105: *
106: * @return array
107: */
108: public function getValues()
109: {
110: $values = array();
111: $data = $this->getElement()->getValue();
112:
113: if (is_array($data)) {
114: $values = $this->_sortValues($data);
115: }
116:
117: foreach ($values as &$value) {
118: $value['readonly'] = ($value['website_id'] == 0)
119: && $this->isShowWebsiteColumn()
120: && !$this->isAllowChangeWebsite();
121: }
122:
123: return $values;
124: }
125:
126: /**
127: * Sort values
128: *
129: * @param array $data
130: * @return array
131: */
132: protected function _sortValues($data)
133: {
134: return $data;
135: }
136:
137: /**
138: * Retrieve allowed customer groups
139: *
140: * @param int|null $groupId return name by customer group id
141: * @return array|string
142: */
143: public function getCustomerGroups($groupId = null)
144: {
145: if ($this->_customerGroups === null) {
146: if (!Mage::helper('catalog')->isModuleEnabled('Mage_Customer')) {
147: return array();
148: }
149: $collection = Mage::getModel('customer/group')->getCollection();
150: $this->_customerGroups = $this->_getInitialCustomerGroups();
151:
152: foreach ($collection as $item) {
153: /** @var $item Mage_Customer_Model_Group */
154: $this->_customerGroups[$item->getId()] = $item->getCustomerGroupCode();
155: }
156: }
157:
158: if ($groupId !== null) {
159: return isset($this->_customerGroups[$groupId]) ? $this->_customerGroups[$groupId] : array();
160: }
161:
162: return $this->_customerGroups;
163: }
164:
165: /**
166: * Retrieve list of initial customer groups
167: *
168: * @return array
169: */
170: protected function _getInitialCustomerGroups()
171: {
172: return array();
173: }
174:
175: /**
176: * Retrieve number of websites
177: *
178: * @return int
179: */
180: public function getWebsiteCount()
181: {
182: return count($this->getWebsites());
183: }
184:
185: /**
186: * Show website column and switcher for group price table
187: *
188: * @return bool
189: */
190: public function isMultiWebsites()
191: {
192: return !Mage::app()->isSingleStoreMode();
193: }
194:
195: /**
196: * Retrieve allowed for edit websites
197: *
198: * @return array
199: */
200: public function getWebsites()
201: {
202: if (!is_null($this->_websites)) {
203: return $this->_websites;
204: }
205:
206: $this->_websites = array(
207: 0 => array(
208: 'name' => Mage::helper('catalog')->__('All Websites'),
209: 'currency' => Mage::app()->getBaseCurrencyCode()
210: )
211: );
212:
213: if (!$this->isScopeGlobal() && $this->getProduct()->getStoreId()) {
214: /** @var $website Mage_Core_Model_Website */
215: $website = Mage::app()->getStore($this->getProduct()->getStoreId())->getWebsite();
216:
217: $this->_websites[$website->getId()] = array(
218: 'name' => $website->getName(),
219: 'currency' => $website->getBaseCurrencyCode()
220: );
221: } elseif (!$this->isScopeGlobal()) {
222: $websites = Mage::app()->getWebsites(false);
223: $productWebsiteIds = $this->getProduct()->getWebsiteIds();
224: foreach ($websites as $website) {
225: /** @var $website Mage_Core_Model_Website */
226: if (!in_array($website->getId(), $productWebsiteIds)) {
227: continue;
228: }
229: $this->_websites[$website->getId()] = array(
230: 'name' => $website->getName(),
231: 'currency' => $website->getBaseCurrencyCode()
232: );
233: }
234: }
235:
236: return $this->_websites;
237: }
238:
239: /**
240: * Retrieve default value for customer group
241: *
242: * @return int
243: */
244: public function getDefaultCustomerGroup()
245: {
246: return Mage_Customer_Model_Group::CUST_GROUP_ALL;
247: }
248:
249: /**
250: * Retrieve default value for website
251: *
252: * @return int
253: */
254: public function getDefaultWebsite()
255: {
256: if ($this->isShowWebsiteColumn() && !$this->isAllowChangeWebsite()) {
257: return Mage::app()->getStore($this->getProduct()->getStoreId())->getWebsiteId();
258: }
259: return 0;
260: }
261:
262: /**
263: * Retrieve 'add group price item' button HTML
264: *
265: * @return string
266: */
267: public function getAddButtonHtml()
268: {
269: return $this->getChildHtml('add_button');
270: }
271:
272: /**
273: * Retrieve customized price column header
274: *
275: * @param string $default
276: * @return string
277: */
278: public function getPriceColumnHeader($default)
279: {
280: if ($this->hasData('price_column_header')) {
281: return $this->getData('price_column_header');
282: } else {
283: return $default;
284: }
285: }
286:
287: /**
288: * Retrieve customized price column header
289: *
290: * @param string $default
291: * @return string
292: */
293: public function getPriceValidation($default)
294: {
295: if ($this->hasData('price_validation')) {
296: return $this->getData('price_validation');
297: } else {
298: return $default;
299: }
300: }
301:
302: /**
303: * Retrieve Group Price entity attribute
304: *
305: * @return Mage_Catalog_Model_Resource_Eav_Attribute
306: */
307: public function getAttribute()
308: {
309: return $this->getElement()->getEntityAttribute();
310: }
311:
312: /**
313: * Check group price attribute scope is global
314: *
315: * @return bool
316: */
317: public function isScopeGlobal()
318: {
319: return $this->getAttribute()->isScopeGlobal();
320: }
321:
322: /**
323: * Show group prices grid website column
324: *
325: * @return bool
326: */
327: public function isShowWebsiteColumn()
328: {
329: if ($this->isScopeGlobal() || Mage::app()->isSingleStoreMode()) {
330: return false;
331: }
332: return true;
333: }
334:
335: /**
336: * Check is allow change website value for combination
337: *
338: * @return bool
339: */
340: public function isAllowChangeWebsite()
341: {
342: if (!$this->isShowWebsiteColumn() || $this->getProduct()->getStoreId()) {
343: return false;
344: }
345: return true;
346: }
347: }
348: