PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Mage/Adminhtml/Block/Catalog/Product/Edit.php

https://github.com/JackoPlane/magento2
PHP | 360 lines | 246 code | 36 blank | 78 comment | 19 complexity | 455c4539657984993a53a6ed86631edb 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. * Customer edit block
  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 extends Mage_Adminhtml_Block_Widget
  34. {
  35. protected $_template = 'catalog/product/edit.phtml';
  36. protected function _construct()
  37. {
  38. parent::_construct();
  39. $this->setId('product_edit');
  40. }
  41. /**
  42. * Retrieve currently edited product object
  43. *
  44. * @return Mage_Catalog_Model_Product
  45. */
  46. public function getProduct()
  47. {
  48. return Mage::registry('current_product');
  49. }
  50. /**
  51. * Add elements in layout
  52. *
  53. * @return Mage_Adminhtml_Block_Catalog_Product_Edit
  54. */
  55. protected function _prepareLayout()
  56. {
  57. if (!$this->getRequest()->getParam('popup')) {
  58. $this->addChild('back_button', 'Mage_Adminhtml_Block_Widget_Button', array(
  59. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Back'),
  60. 'title' => Mage::helper('Mage_Catalog_Helper_Data')->__('Back'),
  61. 'onclick' => 'setLocation(\''
  62. . $this->getUrl('*/*/', array('store' => $this->getRequest()->getParam('store', 0))) . '\')',
  63. 'class' => 'action-back'
  64. ));
  65. } else {
  66. $this->addChild('back_button', 'Mage_Adminhtml_Block_Widget_Button', array(
  67. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Close Window'),
  68. 'onclick' => 'window.close()',
  69. 'class' => 'cancel'
  70. ));
  71. }
  72. if (!$this->getProduct()->isReadonly()) {
  73. $this->addChild('reset_button', 'Mage_Adminhtml_Block_Widget_Button', array(
  74. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Reset'),
  75. 'onclick' => 'setLocation(\'' . $this->getUrl('*/*/*', array('_current' => true)) . '\')'
  76. ));
  77. }
  78. if (!$this->getRequest()->getParam('popup')) {
  79. if ($this->getProduct()->isDeleteable()) {
  80. $this->addChild('delete_button', 'Mage_Adminhtml_Block_Widget_Button', array(
  81. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Delete'),
  82. 'onclick' => 'confirmSetLocation(\''
  83. . Mage::helper('Mage_Catalog_Helper_Data')->__('Are you sure?') . '\', \'' . $this->getDeleteUrl() . '\')',
  84. 'class' => 'delete'
  85. ));
  86. }
  87. }
  88. if (!$this->getProduct()->isReadonly()) {
  89. $this->addChild('save-split-button', 'Mage_Backend_Block_Widget_Button_Split', array(
  90. 'id' => 'save-split-button',
  91. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Save'),
  92. 'class_name' => 'Mage_Backend_Block_Widget_Button_Split',
  93. 'button_class' => 'widget-button-save',
  94. 'options' => $this->_getSaveSplitButtonOptions()
  95. ));
  96. }
  97. return parent::_prepareLayout();
  98. }
  99. public function getBackButtonHtml()
  100. {
  101. return $this->getChildHtml('back_button');
  102. }
  103. public function getCancelButtonHtml()
  104. {
  105. return $this->getChildHtml('reset_button');
  106. }
  107. public function getSaveButtonHtml()
  108. {
  109. return $this->getChildHtml('save_button');
  110. }
  111. public function getSaveAndEditButtonHtml()
  112. {
  113. return $this->getChildHtml('save_and_edit_button');
  114. }
  115. public function getDeleteButtonHtml()
  116. {
  117. return $this->getChildHtml('delete_button');
  118. }
  119. public function getDuplicateButtonHtml()
  120. {
  121. return $this->getChildHtml('duplicate_button');
  122. }
  123. /**
  124. * Get Save Split Button html
  125. *
  126. * @return string
  127. */
  128. public function getSaveSplitButtonHtml()
  129. {
  130. return $this->getChildHtml('save-split-button');
  131. }
  132. public function getValidationUrl()
  133. {
  134. return $this->getUrl('*/*/validate', array('_current'=>true));
  135. }
  136. public function getSaveUrl()
  137. {
  138. return $this->getUrl('*/*/save', array('_current'=>true, 'back'=>null));
  139. }
  140. public function getSaveAndContinueUrl()
  141. {
  142. return $this->getUrl('*/*/save', array(
  143. '_current' => true,
  144. 'back' => 'edit',
  145. 'tab' => '{{tab_id}}',
  146. 'active_tab' => null
  147. ));
  148. }
  149. public function getProductId()
  150. {
  151. return $this->getProduct()->getId();
  152. }
  153. public function getProductSetId()
  154. {
  155. $setId = false;
  156. if (!($setId = $this->getProduct()->getAttributeSetId()) && $this->getRequest()) {
  157. $setId = $this->getRequest()->getParam('set', null);
  158. }
  159. return $setId;
  160. }
  161. public function getIsGrouped()
  162. {
  163. return $this->getProduct()->isGrouped();
  164. }
  165. public function getDeleteUrl()
  166. {
  167. return $this->getUrl('*/*/delete', array('_current'=>true));
  168. }
  169. public function getDuplicateUrl()
  170. {
  171. return $this->getUrl('*/*/duplicate', array('_current'=>true));
  172. }
  173. public function getHeader()
  174. {
  175. if ($this->getProduct()->getId()) {
  176. $header = $this->escapeHtml($this->getProduct()->getName());
  177. } else {
  178. $header = Mage::helper('Mage_Catalog_Helper_Data')->__('New Product');
  179. }
  180. return $header;
  181. }
  182. public function getAttributeSetName()
  183. {
  184. if ($setId = $this->getProduct()->getAttributeSetId()) {
  185. $set = Mage::getModel('Mage_Eav_Model_Entity_Attribute_Set')
  186. ->load($setId);
  187. return $set->getAttributeSetName();
  188. }
  189. return '';
  190. }
  191. public function getIsConfigured()
  192. {
  193. $result = true;
  194. $product = $this->getProduct();
  195. if ($product->isConfigurable()) {
  196. $superAttributes = $product->getTypeInstance()->getUsedProductAttributeIds($product);
  197. $result = !empty($superAttributes);
  198. }
  199. return $result;
  200. }
  201. public function getSelectedTabId()
  202. {
  203. return addslashes(htmlspecialchars($this->getRequest()->getParam('tab')));
  204. }
  205. /**
  206. * Get fields masks from config
  207. *
  208. * @return array
  209. */
  210. public function getFieldsAutogenerationMasks()
  211. {
  212. return $this->helper('Mage_Catalog_Helper_Product')->getFieldsAutogenerationMasks();
  213. }
  214. /**
  215. * Retrieve available placeholders
  216. *
  217. * @return array
  218. */
  219. public function getAttributesAllowedForAutogeneration()
  220. {
  221. return $this->helper('Mage_Catalog_Helper_Product')->getAttributesAllowedForAutogeneration();
  222. }
  223. /**
  224. * Get data for JS (product type transition)
  225. *
  226. * @return string
  227. */
  228. public function getTypeSwitcherData()
  229. {
  230. return Mage::helper('Mage_Core_Helper_Data')->jsonEncode(array(
  231. 'tab_id' => 'product_info_tabs_downloadable_items',
  232. 'is_virtual_id' => Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Weight::VIRTUAL_FIELD_HTML_ID,
  233. 'weight_id' => 'weight',
  234. 'current_type' => $this->getProduct()->getTypeId(),
  235. 'attributes' => $this->_getAttributes(),
  236. ));
  237. }
  238. /**
  239. * Get formed array with attribute codes and Apply To property
  240. *
  241. * @return array
  242. */
  243. protected function _getAttributes()
  244. {
  245. /** @var $product Mage_Catalog_Model_Product */
  246. $product = $this->getProduct();
  247. $attributes = array();
  248. foreach ($product->getAttributes() as $key => $attribute) {
  249. $attributes[$key] = $attribute->getApplyTo();
  250. }
  251. return $attributes;
  252. }
  253. /**
  254. * Get dropdown options for save split button
  255. *
  256. * @return array
  257. */
  258. protected function _getSaveSplitButtonOptions()
  259. {
  260. $options = array();
  261. if (!$this->getRequest()->getParam('popup')) {
  262. $options[] = array(
  263. 'id' => 'edit-button',
  264. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Save & Edit'),
  265. 'data_attribute' => array(
  266. 'mage-init' => array(
  267. 'button' => array('event' => 'saveAndContinueEdit', 'target' => '#product-edit-form'),
  268. ),
  269. ),
  270. 'default' => true,
  271. );
  272. }
  273. /** @var $limitation Mage_Catalog_Model_Product_Limitation */
  274. $limitation = Mage::getObjectManager()->get('Mage_Catalog_Model_Product_Limitation');
  275. if ($this->_isProductNew()) {
  276. $showAddNewButtons = !$limitation->isCreateRestricted(2);
  277. } else {
  278. $showAddNewButtons = !$limitation->isCreateRestricted();
  279. }
  280. if ($showAddNewButtons) {
  281. $options[] = array(
  282. 'id' => 'new-button',
  283. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Save & New'),
  284. 'data_attribute' => array(
  285. 'mage-init' => array(
  286. 'button' => array('event' => 'saveAndNew', 'target' => '#product-edit-form'),
  287. ),
  288. ),
  289. );
  290. if (!$this->getRequest()->getParam('popup') && $this->getProduct()->isDuplicable()) {
  291. $options[] = array(
  292. 'id' => 'duplicate-button',
  293. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Save & Duplicate'),
  294. 'data_attribute' => array(
  295. 'mage-init' => array(
  296. 'button' => array('event' => '', 'target' => '#product-edit-form'),
  297. ),
  298. ),
  299. 'onclick' => $this->_isProductNew() ? '' : 'setLocation(\'' . $this->getDuplicateUrl() . '\')',
  300. );
  301. }
  302. }
  303. $options[] = array(
  304. 'id' => 'close-button',
  305. 'label' => Mage::helper('Mage_Catalog_Helper_Data')->__('Save & Close'),
  306. 'data_attribute' => array(
  307. 'mage-init' => array(
  308. 'button' => array('event' => 'save', 'target' => '#product-edit-form'),
  309. ),
  310. ),
  311. );
  312. return $options;
  313. }
  314. /**
  315. * Check whether new product is being created
  316. *
  317. * @return bool
  318. */
  319. protected function _isProductNew()
  320. {
  321. $product = $this->getProduct();
  322. return !$product || !$product->getId();
  323. }
  324. }