PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/htdocs/magento/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Options/Option.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 284 lines | 199 code | 40 blank | 45 comment | 23 complexity | 732fdfa21900fc122527bc2c7d56af3e MD5 | raw file
  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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * customers defined options
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option extends Mage_Adminhtml_Block_Widget
  34. {
  35. protected $_product;
  36. protected $_productInstance;
  37. protected $_name = 'product[options]';
  38. protected $_id = 'product_option';
  39. protected $_values;
  40. protected $_itemCount = 1;
  41. public function __construct()
  42. {
  43. parent::__construct();
  44. $this->setTemplate('catalog/product/edit/options/option.phtml');
  45. }
  46. public function getItemCount()
  47. {
  48. return $this->_itemCount;
  49. }
  50. public function setItemCount($itemCount)
  51. {
  52. $this->_itemCount = max($this->_itemCount, $itemCount);
  53. return $this;
  54. }
  55. /**
  56. * Get Product
  57. *
  58. * @return Mage_Catalog_Model_Product
  59. */
  60. public function getProduct()
  61. {
  62. if (!$this->_productInstance) {
  63. if ($product = Mage::registry('product')) {
  64. $this->_productInstance = $product;
  65. } else {
  66. $this->_productInstance = Mage::getSingleton('catalog/product');
  67. }
  68. }
  69. return $this->_productInstance;
  70. }
  71. public function setProduct($product)
  72. {
  73. $this->_productInstance = $product;
  74. return $this;
  75. }
  76. public function getFieldName()
  77. {
  78. return $this->_name;
  79. }
  80. public function getFieldId()
  81. {
  82. return $this->_id;
  83. }
  84. /**
  85. * Check block is readonly
  86. *
  87. * @return boolean
  88. */
  89. public function isReadonly()
  90. {
  91. return $this->getProduct()->getOptionsReadonly();
  92. }
  93. protected function _prepareLayout()
  94. {
  95. $this->setChild('delete_button',
  96. $this->getLayout()->createBlock('adminhtml/widget_button')
  97. ->setData(array(
  98. 'label' => Mage::helper('catalog')->__('Delete Option'),
  99. 'class' => 'delete delete-product-option '
  100. ))
  101. );
  102. $path = 'global/catalog/product/options/custom/groups';
  103. foreach (Mage::getConfig()->getNode($path)->children() as $group) {
  104. $this->setChild($group->getName() . '_option_type',
  105. $this->getLayout()->createBlock(
  106. (string) Mage::getConfig()->getNode($path . '/' . $group->getName() . '/render')
  107. )
  108. );
  109. }
  110. return parent::_prepareLayout();
  111. }
  112. public function getAddButtonId()
  113. {
  114. $buttonId = $this->getLayout()
  115. ->getBlock('admin.product.options')
  116. ->getChild('add_button')->getId();
  117. return $buttonId;
  118. }
  119. public function getDeleteButtonHtml()
  120. {
  121. return $this->getChildHtml('delete_button');
  122. }
  123. public function getTypeSelectHtml()
  124. {
  125. $select = $this->getLayout()->createBlock('adminhtml/html_select')
  126. ->setData(array(
  127. 'id' => $this->getFieldId().'_{{id}}_type',
  128. 'class' => 'select select-product-option-type required-option-select'
  129. ))
  130. ->setName($this->getFieldName().'[{{id}}][type]')
  131. ->setOptions(Mage::getSingleton('adminhtml/system_config_source_product_options_type')->toOptionArray());
  132. return $select->getHtml();
  133. }
  134. public function getRequireSelectHtml()
  135. {
  136. $select = $this->getLayout()->createBlock('adminhtml/html_select')
  137. ->setData(array(
  138. 'id' => $this->getFieldId().'_{{id}}_is_require',
  139. 'class' => 'select'
  140. ))
  141. ->setName($this->getFieldName().'[{{id}}][is_require]')
  142. ->setOptions(Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray());
  143. return $select->getHtml();
  144. }
  145. public function getTemplatesHtml()
  146. {
  147. $templates = $this->getChildHtml('text_option_type') . "\n" .
  148. $this->getChildHtml('file_option_type') . "\n" .
  149. $this->getChildHtml('select_option_type') . "\n" .
  150. $this->getChildHtml('date_option_type');
  151. return $templates;
  152. }
  153. public function getOptionValues()
  154. {
  155. $optionsArr = array_reverse($this->getProduct()->getOptions(), true);
  156. // $optionsArr = $this->getProduct()->getOptions();
  157. if (!$this->_values) {
  158. $values = array();
  159. $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
  160. foreach ($optionsArr as $option) {
  161. /* @var $option Mage_Catalog_Model_Product_Option */
  162. $this->setItemCount($option->getOptionId());
  163. $value = array();
  164. $value['id'] = $option->getOptionId();
  165. $value['item_count'] = $this->getItemCount();
  166. $value['option_id'] = $option->getOptionId();
  167. $value['title'] = $this->htmlEscape($option->getTitle());
  168. $value['type'] = $option->getType();
  169. $value['is_require'] = $option->getIsRequire();
  170. $value['sort_order'] = $option->getSortOrder();
  171. if ($this->getProduct()->getStoreId() != '0') {
  172. $value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title', is_null($option->getStoreTitle()));
  173. $value['scopeTitleDisabled'] = is_null($option->getStoreTitle())?'disabled':null;
  174. }
  175. if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
  176. // $valuesArr = array_reverse($option->getValues(), true);
  177. $i = 0;
  178. $itemCount = 0;
  179. foreach ($option->getValues() as $_value) {
  180. /* @var $_value Mage_Catalog_Model_Product_Option_Value */
  181. $value['optionValues'][$i] = array(
  182. 'item_count' => max($itemCount, $_value->getOptionTypeId()),
  183. 'option_id' => $_value->getOptionId(),
  184. 'option_type_id' => $_value->getOptionTypeId(),
  185. 'title' => $this->htmlEscape($_value->getTitle()),
  186. 'price' => $this->getPriceValue($_value->getPrice(), $_value->getPriceType()),
  187. 'price_type' => $_value->getPriceType(),
  188. 'sku' => $this->htmlEscape($_value->getSku()),
  189. 'sort_order' => $_value->getSortOrder(),
  190. );
  191. if ($this->getProduct()->getStoreId() != '0') {
  192. $value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($_value->getOptionId(), 'title', is_null($_value->getStoreTitle()), $_value->getOptionTypeId());
  193. $value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle())?'disabled':null;
  194. if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
  195. $value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml($_value->getOptionId(), 'price', is_null($_value->getstorePrice()), $_value->getOptionTypeId());
  196. $value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice())?'disabled':null;
  197. }
  198. }
  199. $i++;
  200. }
  201. } else {
  202. $value['price'] = $this->getPriceValue($option->getPrice(), $option->getPriceType());
  203. $value['price_type'] = $option->getPriceType();
  204. $value['sku'] = $this->htmlEscape($option->getSku());
  205. $value['max_characters'] = $option->getMaxCharacters();
  206. $value['file_extension'] = $option->getFileExtension();
  207. $value['image_size_x'] = $option->getImageSizeX();
  208. $value['image_size_y'] = $option->getImageSizeY();
  209. if ($this->getProduct()->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
  210. $value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'price', is_null($option->getStorePrice()));
  211. $value['scopePriceDisabled'] = is_null($option->getStorePrice())?'disabled':null;
  212. }
  213. }
  214. $values[] = new Varien_Object($value);
  215. }
  216. $this->_values = $values;
  217. }
  218. return $this->_values;
  219. }
  220. public function getCheckboxScopeHtml($id, $name, $checked=true, $select_id='-1')
  221. {
  222. $checkedHtml = '';
  223. if ($checked) {
  224. $checkedHtml = ' checked="checked"';
  225. }
  226. $selectNameHtml = '';
  227. $selectIdHtml = '';
  228. if ($select_id != '-1') {
  229. $selectNameHtml = '[values]['.$select_id.']';
  230. $selectIdHtml = 'select_'.$select_id.'_';
  231. }
  232. $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.'/>';
  233. $checkbox .= '<label class="normal" for="'.$this->getFieldId().'_'.$id.'_'.$selectIdHtml.$name.'_use_default">Use Default Value</label>';
  234. return $checkbox;
  235. }
  236. public function getPriceValue($value, $type)
  237. {
  238. if ($type == 'percent') {
  239. return number_format($value, 2, null, '');
  240. } elseif ($type == 'fixed') {
  241. return number_format($value, 2, null, '');
  242. }
  243. }
  244. }