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

/app/code/core/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php

https://github.com/Polansk/magento-pt_br
PHP | 257 lines | 176 code | 38 blank | 43 comment | 29 complexity | 4f7f8a2f69ff4989427aa0b15a23a265 MD5 | raw file
Possible License(s): GPL-2.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) 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. * Catalog product attribute controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Catalog_Product_AttributeController extends Mage_Adminhtml_Controller_Action
  34. {
  35. protected $_entityTypeId;
  36. public function preDispatch()
  37. {
  38. parent::preDispatch();
  39. $this->_entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
  40. }
  41. protected function _initAction()
  42. {
  43. if($this->getRequest()->getParam('popup')) {
  44. $this->loadLayout('popup');
  45. } else {
  46. $this->loadLayout()
  47. ->_setActiveMenu('catalog/attributes')
  48. ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
  49. ->_addBreadcrumb(Mage::helper('catalog')->__('Manage Product Attributes'), Mage::helper('catalog')->__('Manage Product Attributes'))
  50. ;
  51. }
  52. return $this;
  53. }
  54. public function indexAction()
  55. {
  56. $this->_initAction()
  57. ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute'))
  58. ->renderLayout();
  59. }
  60. public function newAction()
  61. {
  62. $this->_forward('edit');
  63. }
  64. public function editAction()
  65. {
  66. $id = $this->getRequest()->getParam('attribute_id');
  67. $model = Mage::getModel('catalog/entity_attribute');
  68. if ($id) {
  69. $model->load($id);
  70. if (! $model->getId()) {
  71. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('This attribute no longer exists'));
  72. $this->_redirect('*/*/');
  73. return;
  74. }
  75. // entity type check
  76. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  77. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot edit this attribute'));
  78. $this->_redirect('*/*/');
  79. return;
  80. }
  81. }
  82. // set entered data if was error when we do save
  83. $data = Mage::getSingleton('adminhtml/session')->getAttributeData(true);
  84. if (! empty($data)) {
  85. $model->setData($data);
  86. }
  87. Mage::register('entity_attribute', $model);
  88. $this->_initAction()
  89. ->_addBreadcrumb($id ? Mage::helper('catalog')->__('Edit Product Attribute') : Mage::helper('catalog')->__('New Product Attribute'), $id ? Mage::helper('catalog')->__('Edit Product Attribute') : Mage::helper('catalog')->__('New Product Attribute'))
  90. ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_edit')->setData('action', $this->getUrl('*/catalog_product_attribute/save')))
  91. ->_addLeft($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_edit_tabs'))
  92. ->_addJs(
  93. $this->getLayout()->createBlock('adminhtml/template')
  94. ->setIsPopup((bool)$this->getRequest()->getParam('popup'))
  95. ->setTemplate('catalog/product/attribute/js.phtml')
  96. )
  97. ->renderLayout();
  98. }
  99. public function validateAction()
  100. {
  101. $response = new Varien_Object();
  102. $response->setError(false);
  103. $attributeCode = $this->getRequest()->getParam('attribute_code');
  104. $attributeId = $this->getRequest()->getParam('attribute_id');
  105. $attribute = Mage::getModel('catalog/entity_attribute')
  106. ->loadByCode($this->_entityTypeId, $attributeCode);
  107. if ($attribute->getId() && !$attributeId) {
  108. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Attribute with the same code already exists'));
  109. $this->_initLayoutMessages('adminhtml/session');
  110. $response->setError(true);
  111. $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
  112. }
  113. $this->getResponse()->setBody($response->toJson());
  114. }
  115. public function saveAction()
  116. {
  117. if ($data = $this->getRequest()->getPost()) {
  118. $redirectBack = $this->getRequest()->getParam('back', false);
  119. $model = Mage::getModel('catalog/entity_attribute');
  120. /* @var $model Mage_Catalog_Model_Entity_Attribute */
  121. if ($id = $this->getRequest()->getParam('attribute_id')) {
  122. $model->load($id);
  123. // entity type check
  124. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  125. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot update this attribute'));
  126. Mage::getSingleton('adminhtml/session')->setAttributeData($data);
  127. $this->_redirect('*/*/');
  128. return;
  129. }
  130. $data['attribute_code'] = $model->getAttributeCode();
  131. $data['is_user_defined'] = $model->getIsUserDefined();
  132. $data['frontend_input'] = $model->getFrontendInput();
  133. }
  134. if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
  135. $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
  136. }
  137. $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
  138. if ($defaultValueField) {
  139. $data['default_value'] = $this->getRequest()->getParam($defaultValueField);
  140. }
  141. if(!isset($data['apply_to'])) {
  142. $data['apply_to'] = array();
  143. }
  144. /**
  145. * @todo need specify relations for properties
  146. */
  147. if (isset($data['frontend_input']) && $data['frontend_input'] == 'multiselect') {
  148. $data['backend_model'] = 'eav/entity_attribute_backend_array';
  149. }
  150. $model->addData($data);
  151. if (!$id) {
  152. $model->setEntityTypeId($this->_entityTypeId);
  153. $model->setIsUserDefined(1);
  154. }
  155. if($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
  156. // For creating product attribute on product page we need specify attribute set and group
  157. $model->setAttributeSetId($this->getRequest()->getParam('set'));
  158. $model->setAttributeGroupId($this->getRequest()->getParam('group'));
  159. }
  160. try {
  161. $model->save();
  162. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Product attribute was successfully saved'));
  163. /**
  164. * Clear translation cache because attribute labels are stored in translation
  165. */
  166. Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
  167. Mage::getSingleton('adminhtml/session')->setAttributeData(false);
  168. if ($this->getRequest()->getParam('popup')) {
  169. $this->_redirect('adminhtml/catalog_product/addAttribute', array(
  170. 'id' => $this->getRequest()->getParam('product'),
  171. 'attribute'=> $model->getId(),
  172. '_current' => true
  173. ));
  174. } elseif ($redirectBack) {
  175. $this->_redirect('*/*/edit', array('attribute_id' => $model->getId(),'_current'=>true));
  176. } else {
  177. $this->_redirect('*/*/', array());
  178. }
  179. return;
  180. } catch (Exception $e) {
  181. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  182. Mage::getSingleton('adminhtml/session')->setAttributeData($data);
  183. $this->_redirect('*/*/edit', array('_current' => true));
  184. return;
  185. }
  186. }
  187. $this->_redirect('*/*/');
  188. }
  189. public function deleteAction()
  190. {
  191. if ($id = $this->getRequest()->getParam('attribute_id')) {
  192. $model = Mage::getModel('catalog/entity_attribute');
  193. // entity type check
  194. $model->load($id);
  195. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  196. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot delete this attribute'));
  197. $this->_redirect('*/*/');
  198. return;
  199. }
  200. try {
  201. $model->delete();
  202. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Product attribute was successfully deleted'));
  203. $this->_redirect('*/*/');
  204. return;
  205. }
  206. catch (Exception $e) {
  207. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  208. $this->_redirect('*/*/edit', array('attribute_id' => $this->getRequest()->getParam('attribute_id')));
  209. return;
  210. }
  211. }
  212. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Unable to find an attribute to delete'));
  213. $this->_redirect('*/*/');
  214. }
  215. protected function _isAllowed()
  216. {
  217. return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes');
  218. }
  219. }