PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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