PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/JackoPlane/magento2
PHP | 352 lines | 228 code | 44 blank | 80 comment | 24 complexity | b3e0319ecf8eac6016531c837abf8d61 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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) 2013 X.commerce, 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. protected $_template = 'catalog/product/edit/options/option.phtml';
  40. /**
  41. * Class constructor
  42. */
  43. protected function _construct()
  44. {
  45. parent::_construct();
  46. $this->setCanReadPrice(true);
  47. $this->setCanEditPrice(true);
  48. }
  49. public function getItemCount()
  50. {
  51. return $this->_itemCount;
  52. }
  53. public function setItemCount($itemCount)
  54. {
  55. $this->_itemCount = max($this->_itemCount, $itemCount);
  56. return $this;
  57. }
  58. /**
  59. * Get Product
  60. *
  61. * @return Mage_Catalog_Model_Product
  62. */
  63. public function getProduct()
  64. {
  65. if (!$this->_productInstance) {
  66. if ($product = Mage::registry('product')) {
  67. $this->_productInstance = $product;
  68. } else {
  69. $this->_productInstance = Mage::getSingleton('Mage_Catalog_Model_Product');
  70. }
  71. }
  72. return $this->_productInstance;
  73. }
  74. public function setProduct($product)
  75. {
  76. $this->_productInstance = $product;
  77. return $this;
  78. }
  79. /**
  80. * Retrieve options field name prefix
  81. *
  82. * @return string
  83. */
  84. public function getFieldName()
  85. {
  86. return 'product[options]';
  87. }
  88. /**
  89. * Retrieve options field id prefix
  90. *
  91. * @return string
  92. */
  93. public function getFieldId()
  94. {
  95. return 'product_option';
  96. }
  97. /**
  98. * Check block is readonly
  99. *
  100. * @return boolean
  101. */
  102. public function isReadonly()
  103. {
  104. return $this->getProduct()->getOptionsReadonly();
  105. }
  106. protected function _prepareLayout()
  107. {
  108. $path = 'global/catalog/product/options/custom/groups';
  109. foreach (Mage::getConfig()->getNode($path)->children() as $group) {
  110. $this->addChild(
  111. $group->getName() . '_option_type',
  112. (string)Mage::getConfig()->getNode($path . '/' . $group->getName() . '/render')
  113. );
  114. }
  115. return parent::_prepareLayout();
  116. }
  117. public function getAddButtonId()
  118. {
  119. $buttonId = $this->getLayout()
  120. ->getBlock('admin.product.options')
  121. ->getChildBlock('add_button')->getId();
  122. return $buttonId;
  123. }
  124. public function getTypeSelectHtml()
  125. {
  126. $select = $this->getLayout()->createBlock('Mage_Adminhtml_Block_Html_Select')
  127. ->setData(array(
  128. 'id' => $this->getFieldId() . '_${id}_type',
  129. 'class' => 'select select-product-option-type required-option-select',
  130. ))
  131. ->setName($this->getFieldName() . '[${id}][type]')
  132. ->setOptions(Mage::getSingleton('Mage_Catalog_Model_Config_Source_Product_Options_Type')->toOptionArray());
  133. return $select->getHtml();
  134. }
  135. public function getRequireSelectHtml()
  136. {
  137. $select = $this->getLayout()->createBlock('Mage_Adminhtml_Block_Html_Select')
  138. ->setData(array(
  139. 'id' => $this->getFieldId() . '_${id}_is_require',
  140. 'class' => 'select'
  141. ))
  142. ->setName($this->getFieldName() . '[${id}][is_require]')
  143. ->setOptions(Mage::getSingleton('Mage_Backend_Model_Config_Source_Yesno')->toOptionArray());
  144. return $select->getHtml();
  145. }
  146. /**
  147. * Retrieve html templates for different types of product custom options
  148. *
  149. * @return string
  150. */
  151. public function getTemplatesHtml()
  152. {
  153. $canEditPrice = $this->getCanEditPrice();
  154. $canReadPrice = $this->getCanReadPrice();
  155. $this->getChildBlock('select_option_type')
  156. ->setCanReadPrice($canReadPrice)
  157. ->setCanEditPrice($canEditPrice);
  158. $this->getChildBlock('file_option_type')
  159. ->setCanReadPrice($canReadPrice)
  160. ->setCanEditPrice($canEditPrice);
  161. $this->getChildBlock('date_option_type')
  162. ->setCanReadPrice($canReadPrice)
  163. ->setCanEditPrice($canEditPrice);
  164. $this->getChildBlock('text_option_type')
  165. ->setCanReadPrice($canReadPrice)
  166. ->setCanEditPrice($canEditPrice);
  167. $templates = $this->getChildHtml('text_option_type') . "\n" .
  168. $this->getChildHtml('file_option_type') . "\n" .
  169. $this->getChildHtml('select_option_type') . "\n" .
  170. $this->getChildHtml('date_option_type');
  171. return $templates;
  172. }
  173. public function getOptionValues()
  174. {
  175. $optionsArr = $this->getProduct()->getOptions();
  176. if (!$this->_values || $this->getIgnoreCaching()) {
  177. $showPrice = $this->getCanReadPrice();
  178. $values = array();
  179. $scope = (int)Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
  180. foreach ($optionsArr as $option) {
  181. /* @var $option Mage_Catalog_Model_Product_Option */
  182. $this->setItemCount($option->getOptionId());
  183. $value = array();
  184. $value['id'] = $option->getOptionId();
  185. $value['item_count'] = $this->getItemCount();
  186. $value['option_id'] = $option->getOptionId();
  187. $value['title'] = $this->escapeHtml($option->getTitle());
  188. $value['type'] = $option->getType();
  189. $value['is_require'] = $option->getIsRequire();
  190. $value['sort_order'] = $option->getSortOrder();
  191. $value['can_edit_price'] = $this->getCanEditPrice();
  192. if ($this->getProduct()->getStoreId() != '0') {
  193. $value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title',
  194. is_null($option->getStoreTitle()));
  195. $value['scopeTitleDisabled'] = is_null($option->getStoreTitle()) ? 'disabled' : null;
  196. }
  197. if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
  198. $i = 0;
  199. $itemCount = 0;
  200. foreach ($option->getValues() as $_value) {
  201. /* @var $_value Mage_Catalog_Model_Product_Option_Value */
  202. $value['optionValues'][$i] = array(
  203. 'item_count' => max($itemCount, $_value->getOptionTypeId()),
  204. 'option_id' => $_value->getOptionId(),
  205. 'option_type_id' => $_value->getOptionTypeId(),
  206. 'title' => $this->escapeHtml($_value->getTitle()),
  207. 'price' => ($showPrice)
  208. ? $this->getPriceValue($_value->getPrice(), $_value->getPriceType()) : '',
  209. 'price_type' => ($showPrice) ? $_value->getPriceType() : 0,
  210. 'sku' => $this->escapeHtml($_value->getSku()),
  211. 'sort_order' => $_value->getSortOrder(),
  212. );
  213. if ($this->getProduct()->getStoreId() != '0') {
  214. $value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml(
  215. $_value->getOptionId(), 'title', is_null($_value->getStoreTitle()),
  216. $_value->getOptionTypeId());
  217. $value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle())
  218. ? 'disabled' : null;
  219. if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
  220. $value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml(
  221. $_value->getOptionId(), 'price', is_null($_value->getstorePrice()),
  222. $_value->getOptionTypeId());
  223. $value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice())
  224. ? 'disabled' : null;
  225. }
  226. }
  227. $i++;
  228. }
  229. } else {
  230. $value['price'] = ($showPrice)
  231. ? $this->getPriceValue($option->getPrice(), $option->getPriceType()) : '';
  232. $value['price_type'] = $option->getPriceType();
  233. $value['sku'] = $this->escapeHtml($option->getSku());
  234. $value['max_characters'] = $option->getMaxCharacters();
  235. $value['file_extension'] = $option->getFileExtension();
  236. $value['image_size_x'] = $option->getImageSizeX();
  237. $value['image_size_y'] = $option->getImageSizeY();
  238. if ($this->getProduct()->getStoreId() != '0'
  239. && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
  240. ) {
  241. $value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'price',
  242. is_null($option->getStorePrice()));
  243. $value['scopePriceDisabled'] = is_null($option->getStorePrice()) ? 'disabled' : null;
  244. }
  245. }
  246. $values[] = new Varien_Object($value);
  247. }
  248. $this->_values = $values;
  249. }
  250. return $this->_values;
  251. }
  252. /**
  253. * Retrieve html of scope checkbox
  254. *
  255. * @param string $id
  256. * @param string $name
  257. * @param boolean $checked
  258. * @param string $select_id
  259. * @return string
  260. */
  261. public function getCheckboxScopeHtml($id, $name, $checked = true, $select_id = '-1')
  262. {
  263. $checkedHtml = '';
  264. if ($checked) {
  265. $checkedHtml = ' checked="checked"';
  266. }
  267. $selectNameHtml = '';
  268. $selectIdHtml = '';
  269. if ($select_id != '-1') {
  270. $selectNameHtml = '[values][' . $select_id . ']';
  271. $selectIdHtml = 'select_' . $select_id . '_';
  272. }
  273. $useDefault = '<div class="field-service">'
  274. . '<label for="' . $this->getFieldId() . '_' . $id . '_' . $selectIdHtml . $name . '" class="use-default">'
  275. . '<input value="1" type="checkbox" class="use-default-control"'
  276. . 'name="' . $this->getFieldName() . '[' . $id . ']' . $selectNameHtml . '[scope][' . $name . ']"'
  277. . 'id="' . $this->getFieldId() . '_' . $id . '_' . $selectIdHtml . $name . '_use_default"' . $checkedHtml
  278. .' /><span class="use-default-label">' . Mage::helper('Mage_Catalog_Helper_Data')->__('Use Default')
  279. . '</span></label></div>';
  280. return $useDefault;
  281. }
  282. public function getPriceValue($value, $type)
  283. {
  284. if ($type == 'percent') {
  285. return number_format($value, 2, null, '');
  286. } elseif ($type == 'fixed') {
  287. return number_format($value, 2, null, '');
  288. }
  289. }
  290. /**
  291. * Return product grid url for custom options import popup
  292. *
  293. * @return string
  294. */
  295. public function getProductGridUrl()
  296. {
  297. return $this->getUrl('*/*/optionsImportGrid');
  298. }
  299. /**
  300. * Return custom options getter URL for ajax queries
  301. *
  302. * @return string
  303. */
  304. public function getCustomOptionsUrl()
  305. {
  306. return $this->getUrl('*/*/customOptions');
  307. }
  308. }