00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option extends Mage_Adminhtml_Block_Widget
00036 {
00037 protected $_product;
00038
00039 protected $_productInstance;
00040
00041 protected $_name = 'product[options]';
00042 protected $_id = 'product_option';
00043
00044 protected $_values;
00045
00046 protected $_itemCount = 1;
00047
00048 public function __construct()
00049 {
00050 parent::__construct();
00051 $this->setTemplate('catalog/product/edit/options/option.phtml');
00052 }
00053
00054 public function getItemCount()
00055 {
00056 return $this->_itemCount;
00057 }
00058
00059 public function setItemCount($itemCount)
00060 {
00061 $this->_itemCount = max($this->_itemCount, $itemCount);
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070 public function getProduct()
00071 {
00072 if (!$this->_productInstance) {
00073 if ($product = Mage::registry('product')) {
00074 $this->_productInstance = $product;
00075 } else {
00076 $this->_productInstance = Mage::getSingleton('catalog/product');
00077 }
00078 }
00079
00080 return $this->_productInstance;
00081 }
00082
00083 public function setProduct($product)
00084 {
00085 $this->_productInstance = $product;
00086 return $this;
00087 }
00088
00089 public function getFieldName()
00090 {
00091 return $this->_name;
00092 }
00093
00094 public function getFieldId()
00095 {
00096 return $this->_id;
00097 }
00098
00099
00100
00101
00102
00103
00104 public function isReadonly()
00105 {
00106 return $this->getProduct()->getOptionsReadonly();
00107 }
00108
00109 protected function _prepareLayout()
00110 {
00111 $this->setChild('delete_button',
00112 $this->getLayout()->createBlock('adminhtml/widget_button')
00113 ->setData(array(
00114 'label' => Mage::helper('catalog')->__('Delete Option'),
00115 'class' => 'delete delete-product-option '
00116 ))
00117 );
00118
00119 $path = 'global/catalog/product/options/custom/groups';
00120
00121 foreach (Mage::getConfig()->getNode($path)->children() as $group) {
00122 $this->setChild($group->getName() . '_option_type',
00123 $this->getLayout()->createBlock(
00124 (string) Mage::getConfig()->getNode($path . '/' . $group->getName() . '/render')
00125 )
00126 );
00127 }
00128
00129 return parent::_prepareLayout();
00130 }
00131
00132 public function getAddButtonId()
00133 {
00134 $buttonId = $this->getLayout()
00135 ->getBlock('admin.product.options')
00136 ->getChild('add_button')->getId();
00137 return $buttonId;
00138 }
00139
00140 public function getDeleteButtonHtml()
00141 {
00142 return $this->getChildHtml('delete_button');
00143 }
00144
00145 public function getTypeSelectHtml()
00146 {
00147 $select = $this->getLayout()->createBlock('adminhtml/html_select')
00148 ->setData(array(
00149 'id' => $this->getFieldId().'_{{id}}_type',
00150 'class' => 'select select-product-option-type required-option-select'
00151 ))
00152 ->setName($this->getFieldName().'[{{id}}][type]')
00153 ->setOptions(Mage::getSingleton('adminhtml/system_config_source_product_options_type')->toOptionArray());
00154
00155 return $select->getHtml();
00156 }
00157
00158 public function getRequireSelectHtml()
00159 {
00160 $select = $this->getLayout()->createBlock('adminhtml/html_select')
00161 ->setData(array(
00162 'id' => $this->getFieldId().'_{{id}}_is_require',
00163 'class' => 'select'
00164 ))
00165 ->setName($this->getFieldName().'[{{id}}][is_require]')
00166 ->setOptions(Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray());
00167
00168 return $select->getHtml();
00169 }
00170
00171 public function getTemplatesHtml()
00172 {
00173 $templates = $this->getChildHtml('text_option_type') . "\n" .
00174 $this->getChildHtml('file_option_type') . "\n" .
00175 $this->getChildHtml('select_option_type') . "\n" .
00176 $this->getChildHtml('date_option_type');
00177
00178 return $templates;
00179 }
00180
00181 public function getOptionValues()
00182 {
00183 $optionsArr = array_reverse($this->getProduct()->getOptions(), true);
00184
00185
00186 if (!$this->_values) {
00187 $values = array();
00188 $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
00189 foreach ($optionsArr as $option) {
00190
00191
00192 $this->setItemCount($option->getOptionId());
00193
00194 $value = array();
00195
00196 $value['id'] = $option->getOptionId();
00197 $value['item_count'] = $this->getItemCount();
00198 $value['option_id'] = $option->getOptionId();
00199 $value['title'] = $this->htmlEscape($option->getTitle());
00200 $value['type'] = $option->getType();
00201 $value['is_require'] = $option->getIsRequire();
00202 $value['sort_order'] = $option->getSortOrder();
00203
00204 if ($this->getProduct()->getStoreId() != '0') {
00205 $value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title', is_null($option->getStoreTitle()));
00206 $value['scopeTitleDisabled'] = is_null($option->getStoreTitle())?'disabled':null;
00207 }
00208
00209 if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
00210
00211
00212
00213 $i = 0;
00214 $itemCount = 0;
00215 foreach ($option->getValues() as $_value) {
00216
00217 $value['optionValues'][$i] = array(
00218 'item_count' => max($itemCount, $_value->getOptionTypeId()),
00219 'option_id' => $_value->getOptionId(),
00220 'option_type_id' => $_value->getOptionTypeId(),
00221 'title' => $this->htmlEscape($_value->getTitle()),
00222 'price' => $this->getPriceValue($_value->getPrice(), $_value->getPriceType()),
00223 'price_type' => $_value->getPriceType(),
00224 'sku' => $this->htmlEscape($_value->getSku()),
00225 'sort_order' => $_value->getSortOrder(),
00226 );
00227
00228 if ($this->getProduct()->getStoreId() != '0') {
00229 $value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($_value->getOptionId(), 'title', is_null($_value->getStoreTitle()), $_value->getOptionTypeId());
00230 $value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle())?'disabled':null;
00231 if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
00232 $value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml($_value->getOptionId(), 'price', is_null($_value->getstorePrice()), $_value->getOptionTypeId());
00233 $value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice())?'disabled':null;
00234 }
00235 }
00236 $i++;
00237 }
00238 } else {
00239 $value['price'] = $this->getPriceValue($option->getPrice(), $option->getPriceType());
00240 $value['price_type'] = $option->getPriceType();
00241 $value['sku'] = $this->htmlEscape($option->getSku());
00242 $value['max_characters'] = $option->getMaxCharacters();
00243 $value['file_extension'] = $option->getFileExtension();
00244 $value['image_size_x'] = $option->getImageSizeX();
00245 $value['image_size_y'] = $option->getImageSizeY();
00246 if ($this->getProduct()->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
00247 $value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'price', is_null($option->getStorePrice()));
00248 $value['scopePriceDisabled'] = is_null($option->getStorePrice())?'disabled':null;
00249 }
00250 }
00251 $values[] = new Varien_Object($value);
00252 }
00253 $this->_values = $values;
00254 }
00255
00256 return $this->_values;
00257 }
00258
00259 public function getCheckboxScopeHtml($id, $name, $checked=true, $select_id='-1')
00260 {
00261 $checkedHtml = '';
00262 if ($checked) {
00263 $checkedHtml = ' checked="checked"';
00264 }
00265 $selectNameHtml = '';
00266 $selectIdHtml = '';
00267 if ($select_id != '-1') {
00268 $selectNameHtml = '[values]['.$select_id.']';
00269 $selectIdHtml = 'select_'.$select_id.'_';
00270 }
00271 $checkbox = '<input type="checkbox" id="'.$this->getFieldId().'_'.$id.'_'.$selectIdHtml.$name.'_use_default" class="product-option-scope-checkbox" name="'.$this->getFieldName().'['.$id.']'.$selectNameHtml.'[scope]['.$name.']" value="1" '.$checkedHtml.'/>';
00272 $checkbox .= '<label class="normal" for="'.$this->getFieldId().'_'.$id.'_'.$selectIdHtml.$name.'_use_default">Use Default Value</label>';
00273 return $checkbox;
00274 }
00275
00276 public function getPriceValue($value, $type)
00277 {
00278 if ($type == 'percent') {
00279 return number_format($value, 2, null, '');
00280 } elseif ($type == 'fixed') {
00281 return number_format($value, 2, null, '');
00282 }
00283 }
00284 }