1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option extends Mage_Adminhtml_Block_Widget
36: {
37: protected $_product;
38:
39: protected $_productInstance;
40:
41: protected $_values;
42:
43: protected $_itemCount = 1;
44:
45: 46: 47:
48: public function __construct()
49: {
50: parent::__construct();
51: $this->setTemplate('catalog/product/edit/options/option.phtml');
52: $this->setCanReadPrice(true);
53: $this->setCanEditPrice(true);
54: }
55:
56: public function getItemCount()
57: {
58: return $this->_itemCount;
59: }
60:
61: public function setItemCount($itemCount)
62: {
63: $this->_itemCount = max($this->_itemCount, $itemCount);
64: return $this;
65: }
66:
67: 68: 69: 70: 71:
72: public function getProduct()
73: {
74: if (!$this->_productInstance) {
75: if ($product = Mage::registry('product')) {
76: $this->_productInstance = $product;
77: } else {
78: $this->_productInstance = Mage::getSingleton('catalog/product');
79: }
80: }
81:
82: return $this->_productInstance;
83: }
84:
85: public function setProduct($product)
86: {
87: $this->_productInstance = $product;
88: return $this;
89: }
90:
91: 92: 93: 94: 95:
96: public function getFieldName()
97: {
98: return 'product[options]';
99: }
100:
101: 102: 103: 104: 105:
106: public function getFieldId()
107: {
108: return 'product_option';
109: }
110:
111: 112: 113: 114: 115:
116: public function isReadonly()
117: {
118: return $this->getProduct()->getOptionsReadonly();
119: }
120:
121: protected function _prepareLayout()
122: {
123: $this->setChild('delete_button',
124: $this->getLayout()->createBlock('adminhtml/widget_button')
125: ->setData(array(
126: 'label' => Mage::helper('catalog')->__('Delete Option'),
127: 'class' => 'delete delete-product-option '
128: ))
129: );
130:
131: $path = 'global/catalog/product/options/custom/groups';
132:
133: foreach (Mage::getConfig()->getNode($path)->children() as $group) {
134: $this->setChild($group->getName() . '_option_type',
135: $this->getLayout()->createBlock(
136: (string) Mage::getConfig()->getNode($path . '/' . $group->getName() . '/render')
137: )
138: );
139: }
140:
141: return parent::_prepareLayout();
142: }
143:
144: public function getAddButtonId()
145: {
146: $buttonId = $this->getLayout()
147: ->getBlock('admin.product.options')
148: ->getChild('add_button')->getId();
149: return $buttonId;
150: }
151:
152: public function getDeleteButtonHtml()
153: {
154: return $this->getChildHtml('delete_button');
155: }
156:
157: public function getTypeSelectHtml()
158: {
159: $select = $this->getLayout()->createBlock('adminhtml/html_select')
160: ->setData(array(
161: 'id' => $this->getFieldId().'_{{id}}_type',
162: 'class' => 'select select-product-option-type required-option-select'
163: ))
164: ->setName($this->getFieldName().'[{{id}}][type]')
165: ->setOptions(Mage::getSingleton('adminhtml/system_config_source_product_options_type')->toOptionArray());
166:
167: return $select->getHtml();
168: }
169:
170: public function getRequireSelectHtml()
171: {
172: $select = $this->getLayout()->createBlock('adminhtml/html_select')
173: ->setData(array(
174: 'id' => $this->getFieldId().'_{{id}}_is_require',
175: 'class' => 'select'
176: ))
177: ->setName($this->getFieldName().'[{{id}}][is_require]')
178: ->setOptions(Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray());
179:
180: return $select->getHtml();
181: }
182:
183: 184: 185: 186: 187:
188: public function getTemplatesHtml()
189: {
190: $canEditPrice = $this->getCanEditPrice();
191: $canReadPrice = $this->getCanReadPrice();
192: $this->getChild('select_option_type')
193: ->setCanReadPrice($canReadPrice)
194: ->setCanEditPrice($canEditPrice);
195:
196: $this->getChild('file_option_type')
197: ->setCanReadPrice($canReadPrice)
198: ->setCanEditPrice($canEditPrice);
199:
200: $this->getChild('date_option_type')
201: ->setCanReadPrice($canReadPrice)
202: ->setCanEditPrice($canEditPrice);
203:
204: $this->getChild('text_option_type')
205: ->setCanReadPrice($canReadPrice)
206: ->setCanEditPrice($canEditPrice);
207:
208: $templates = $this->getChildHtml('text_option_type') . "\n" .
209: $this->getChildHtml('file_option_type') . "\n" .
210: $this->getChildHtml('select_option_type') . "\n" .
211: $this->getChildHtml('date_option_type');
212:
213: return $templates;
214: }
215:
216: public function getOptionValues()
217: {
218: $optionsArr = array_reverse($this->getProduct()->getOptions(), true);
219:
220:
221: if (!$this->_values) {
222: $showPrice = $this->getCanReadPrice();
223: $values = array();
224: $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
225: foreach ($optionsArr as $option) {
226:
227:
228: $this->setItemCount($option->getOptionId());
229:
230: $value = array();
231:
232: $value['id'] = $option->getOptionId();
233: $value['item_count'] = $this->getItemCount();
234: $value['option_id'] = $option->getOptionId();
235: $value['title'] = $this->htmlEscape($option->getTitle());
236: $value['type'] = $option->getType();
237: $value['is_require'] = $option->getIsRequire();
238: $value['sort_order'] = $option->getSortOrder();
239: $value['can_edit_price'] = $this->getCanEditPrice();
240:
241: if ($this->getProduct()->getStoreId() != '0') {
242: $value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title',
243: is_null($option->getStoreTitle()));
244: $value['scopeTitleDisabled'] = is_null($option->getStoreTitle())?'disabled':null;
245: }
246:
247: if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
248:
249:
250:
251: $i = 0;
252: $itemCount = 0;
253: foreach ($option->getValues() as $_value) {
254:
255: $value['optionValues'][$i] = array(
256: 'item_count' => max($itemCount, $_value->getOptionTypeId()),
257: 'option_id' => $_value->getOptionId(),
258: 'option_type_id' => $_value->getOptionTypeId(),
259: 'title' => $this->htmlEscape($_value->getTitle()),
260: 'price' => ($showPrice)
261: ? $this->getPriceValue($_value->getPrice(), $_value->getPriceType()) : '',
262: 'price_type' => ($showPrice) ? $_value->getPriceType() : 0,
263: 'sku' => $this->htmlEscape($_value->getSku()),
264: 'sort_order' => $_value->getSortOrder(),
265: );
266:
267: if ($this->getProduct()->getStoreId() != '0') {
268: $value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml(
269: $_value->getOptionId(), 'title', is_null($_value->getStoreTitle()),
270: $_value->getOptionTypeId());
271: $value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle())
272: ? 'disabled' : null;
273: if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
274: $value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml(
275: $_value->getOptionId(), 'price', is_null($_value->getstorePrice()),
276: $_value->getOptionTypeId());
277: $value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice())
278: ? 'disabled' : null;
279: }
280: }
281: $i++;
282: }
283: } else {
284: $value['price'] = ($showPrice)
285: ? $this->getPriceValue($option->getPrice(), $option->getPriceType()) : '';
286: $value['price_type'] = $option->getPriceType();
287: $value['sku'] = $this->htmlEscape($option->getSku());
288: $value['max_characters'] = $option->getMaxCharacters();
289: $value['file_extension'] = $option->getFileExtension();
290: $value['image_size_x'] = $option->getImageSizeX();
291: $value['image_size_y'] = $option->getImageSizeY();
292: if ($this->getProduct()->getStoreId() != '0' &&
293: $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
294: $value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(),
295: 'price', is_null($option->getStorePrice()));
296: $value['scopePriceDisabled'] = is_null($option->getStorePrice())?'disabled':null;
297: }
298: }
299: $values[] = new Varien_Object($value);
300: }
301: $this->_values = $values;
302: }
303:
304: return $this->_values;
305: }
306:
307: 308: 309: 310: 311: 312: 313: 314: 315:
316: public function getCheckboxScopeHtml($id, $name, $checked=true, $select_id='-1')
317: {
318: $checkedHtml = '';
319: if ($checked) {
320: $checkedHtml = ' checked="checked"';
321: }
322: $selectNameHtml = '';
323: $selectIdHtml = '';
324: if ($select_id != '-1') {
325: $selectNameHtml = '[values][' . $select_id . ']';
326: $selectIdHtml = 'select_' . $select_id . '_';
327: }
328: $checkbox = '<input type="checkbox" id="' . $this->getFieldId() . '_' . $id . '_' .
329: $selectIdHtml . $name . '_use_default" class="product-option-scope-checkbox" name="' .
330: $this->getFieldName() . '['.$id.']' . $selectNameHtml . '[scope][' . $name . ']" value="1" ' .
331: $checkedHtml . '/>';
332: $checkbox .= '<label class="normal" for="' . $this->getFieldId() . '_' . $id . '_' .
333: $selectIdHtml . $name . '_use_default">' . $this->__('Use Default Value') . '</label>';
334: return $checkbox;
335: }
336:
337: public function getPriceValue($value, $type)
338: {
339: if ($type == 'percent') {
340: return number_format($value, 2, null, '');
341: } elseif ($type == 'fixed') {
342: return number_format($value, 2, null, '');
343: }
344: }
345: }
346: