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: class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Simple
35: extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Attributes
36: {
37: 38: 39: 40: 41:
42: protected $_product = null;
43:
44: protected function _prepareForm()
45: {
46: $form = new Varien_Data_Form();
47:
48: $form->setFieldNameSuffix('simple_product');
49: $form->setDataObject($this->_getProduct());
50:
51: $fieldset = $form->addFieldset('simple_product', array(
52: 'legend' => Mage::helper('catalog')->__('Quick simple product creation')
53: ));
54: $this->_addElementTypes($fieldset);
55: $attributesConfig = array(
56: 'autogenerate' => array('name', 'sku'),
57: 'additional' => array('name', 'sku', 'visibility', 'status')
58: );
59:
60: $availableTypes = array('text', 'select', 'multiselect', 'textarea', 'price', 'weight');
61:
62: $attributes = Mage::getModel('catalog/product')
63: ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
64: ->setAttributeSetId($this->_getProduct()->getAttributeSetId())
65: ->getAttributes();
66:
67:
68: foreach ($attributes as $attribute) {
69: if (($attribute->getIsRequired()
70: && $attribute->getApplyTo()
71:
72: && !in_array(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, $attribute->getApplyTo())
73:
74: && !in_array($attribute->getId(),
75: $this->_getProduct()->getTypeInstance(true)->getUsedProductAttributeIds($this->_getProduct()))
76: )
77:
78: || in_array($attribute->getAttributeCode(), $attributesConfig['additional'])
79: ) {
80: $inputType = $attribute->getFrontend()->getInputType();
81: if (!in_array($inputType, $availableTypes)) {
82: continue;
83: }
84: $attributeCode = $attribute->getAttributeCode();
85: $attribute->setAttributeCode('simple_product_' . $attributeCode);
86: $element = $fieldset->addField(
87: 'simple_product_' . $attributeCode,
88: $inputType,
89: array(
90: 'label' => $attribute->getFrontend()->getLabel(),
91: 'name' => $attributeCode,
92: 'required' => $attribute->getIsRequired(),
93: )
94: )->setEntityAttribute($attribute);
95:
96: if (in_array($attributeCode, $attributesConfig['autogenerate'])) {
97: $element->setDisabled('true');
98: $element->setValue($this->_getProduct()->getData($attributeCode));
99: $element->setAfterElementHtml(
100: '<input type="checkbox" id="simple_product_' . $attributeCode . '_autogenerate" '
101: . 'name="simple_product[' . $attributeCode . '_autogenerate]" value="1" '
102: . 'onclick="toggleValueElements(this, this.parentNode)" checked="checked" /> '
103: . '<label for="simple_product_' . $attributeCode . '_autogenerate" >'
104: . Mage::helper('catalog')->__('Autogenerate')
105: . '</label>'
106: );
107: }
108:
109:
110: if ($inputType == 'select' || $inputType == 'multiselect') {
111: $element->setValues($attribute->getFrontend()->getSelectOptions());
112: }
113: }
114:
115: }
116:
117:
118: $usedAttributes = $this->_getProduct()->getTypeInstance(true)->getUsedProductAttributes($this->_getProduct());
119: foreach ($usedAttributes as $attribute) {
120: $attributeCode = $attribute->getAttributeCode();
121: $fieldset->addField( 'simple_product_' . $attributeCode, 'select', array(
122: 'label' => $attribute->getFrontend()->getLabel(),
123: 'name' => $attributeCode,
124: 'values' => $attribute->getSource()->getAllOptions(true, true),
125: 'required' => true,
126: 'class' => 'validate-configurable',
127: 'onchange' => 'superProduct.showPricing(this, \'' . $attributeCode . '\')'
128: ));
129:
130: $fieldset->addField('simple_product_' . $attributeCode . '_pricing_value', 'hidden', array(
131: 'name' => 'pricing[' . $attributeCode . '][value]'
132: ));
133:
134: $fieldset->addField('simple_product_' . $attributeCode . '_pricing_type', 'hidden', array(
135: 'name' => 'pricing[' . $attributeCode . '][is_percent]'
136: ));
137: }
138:
139:
140: $fieldset->addField('simple_product_inventory_qty', 'text', array(
141: 'label' => Mage::helper('catalog')->__('Qty'),
142: 'name' => 'stock_data[qty]',
143: 'class' => 'validate-number',
144: 'required' => true,
145: 'value' => 0
146: ));
147:
148: $fieldset->addField('simple_product_inventory_is_in_stock', 'select', array(
149: 'label' => Mage::helper('catalog')->__('Stock Availability'),
150: 'name' => 'stock_data[is_in_stock]',
151: 'values' => array(
152: array('value'=>1, 'label'=> Mage::helper('catalog')->__('In Stock')),
153: array('value'=>0, 'label'=> Mage::helper('catalog')->__('Out of Stock'))
154: ),
155: 'value' => 1
156: ));
157:
158: $stockHiddenFields = array(
159: 'use_config_min_qty' => 1,
160: 'use_config_min_sale_qty' => 1,
161: 'use_config_max_sale_qty' => 1,
162: 'use_config_backorders' => 1,
163: 'use_config_notify_stock_qty' => 1,
164: 'is_qty_decimal' => 0
165: );
166:
167: foreach ($stockHiddenFields as $fieldName=>$fieldValue) {
168: $fieldset->addField('simple_product_inventory_' . $fieldName, 'hidden', array(
169: 'name' => 'stock_data[' . $fieldName .']',
170: 'value' => $fieldValue
171: ));
172: }
173:
174:
175: $fieldset->addField('create_button', 'note', array(
176: 'text' => $this->getButtonHtml(
177: Mage::helper('catalog')->__('Quick Create'),
178: 'superProduct.quickCreateNewProduct()',
179: 'save'
180: )
181: ));
182:
183: $this->setForm($form);
184: }
185:
186: 187: 188: 189: 190:
191: protected function _getProduct()
192: {
193: if (!$this->_product) {
194: $this->_product = Mage::registry('current_product');
195: }
196: return $this->_product;
197: }
198: }
199: