PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/src/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php

https://bitbucket.org/kdms/sh-magento
PHP | 346 lines | 240 code | 49 blank | 57 comment | 37 complexity | 417268fb553ca3b4d33c3f7c0deec4bb MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  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(Mage_Catalog_Model_Product::ENTITY)->getTypeId();
  40. }
  41. protected function _initAction()
  42. {
  43. $this->_title($this->__('Catalog'))
  44. ->_title($this->__('Attributes'))
  45. ->_title($this->__('Manage Attributes'));
  46. if($this->getRequest()->getParam('popup')) {
  47. $this->loadLayout('popup');
  48. } else {
  49. $this->loadLayout()
  50. ->_setActiveMenu('catalog/attributes')
  51. ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
  52. ->_addBreadcrumb(
  53. Mage::helper('catalog')->__('Manage Product Attributes'),
  54. Mage::helper('catalog')->__('Manage Product Attributes'))
  55. ;
  56. }
  57. return $this;
  58. }
  59. public function indexAction()
  60. {
  61. $this->_initAction()
  62. ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute'))
  63. ->renderLayout();
  64. }
  65. public function newAction()
  66. {
  67. $this->_forward('edit');
  68. }
  69. public function editAction()
  70. {
  71. $id = $this->getRequest()->getParam('attribute_id');
  72. $model = Mage::getModel('catalog/resource_eav_attribute')
  73. ->setEntityTypeId($this->_entityTypeId);
  74. if ($id) {
  75. $model->load($id);
  76. if (! $model->getId()) {
  77. Mage::getSingleton('adminhtml/session')->addError(
  78. Mage::helper('catalog')->__('This attribute no longer exists'));
  79. $this->_redirect('*/*/');
  80. return;
  81. }
  82. // entity type check
  83. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  84. Mage::getSingleton('adminhtml/session')->addError(
  85. Mage::helper('catalog')->__('This attribute cannot be edited.'));
  86. $this->_redirect('*/*/');
  87. return;
  88. }
  89. }
  90. // set entered data if was error when we do save
  91. $data = Mage::getSingleton('adminhtml/session')->getAttributeData(true);
  92. if (! empty($data)) {
  93. $model->addData($data);
  94. }
  95. Mage::register('entity_attribute', $model);
  96. $this->_initAction();
  97. $this->_title($id ? $model->getName() : $this->__('New Attribute'));
  98. $item = $id ? Mage::helper('catalog')->__('Edit Product Attribute')
  99. : Mage::helper('catalog')->__('New Product Attribute');
  100. $this->_addBreadcrumb($item, $item);
  101. $this->getLayout()->getBlock('attribute_edit_js')
  102. ->setIsPopup((bool)$this->getRequest()->getParam('popup'));
  103. $this->renderLayout();
  104. }
  105. public function validateAction()
  106. {
  107. $response = new Varien_Object();
  108. $response->setError(false);
  109. $attributeCode = $this->getRequest()->getParam('attribute_code');
  110. $attributeId = $this->getRequest()->getParam('attribute_id');
  111. $attribute = Mage::getModel('catalog/resource_eav_attribute')
  112. ->loadByCode($this->_entityTypeId, $attributeCode);
  113. if ($attribute->getId() && !$attributeId) {
  114. Mage::getSingleton('adminhtml/session')->addError(
  115. Mage::helper('catalog')->__('Attribute with the same code already exists'));
  116. $this->_initLayoutMessages('adminhtml/session');
  117. $response->setError(true);
  118. $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
  119. }
  120. $this->getResponse()->setBody($response->toJson());
  121. }
  122. /**
  123. * Filter post data
  124. *
  125. * @param array $data
  126. * @return array
  127. */
  128. protected function _filterPostData($data)
  129. {
  130. if ($data) {
  131. /** @var $helperCatalog Mage_Catalog_Helper_Data */
  132. $helperCatalog = Mage::helper('catalog');
  133. //labels
  134. foreach ($data['frontend_label'] as & $value) {
  135. if ($value) {
  136. $value = $helperCatalog->stripTags($value);
  137. }
  138. }
  139. }
  140. return $data;
  141. }
  142. public function saveAction()
  143. {
  144. $data = $this->getRequest()->getPost();
  145. if ($data) {
  146. /** @var $session Mage_Admin_Model_Session */
  147. $session = Mage::getSingleton('adminhtml/session');
  148. $redirectBack = $this->getRequest()->getParam('back', false);
  149. /* @var $model Mage_Catalog_Model_Entity_Attribute */
  150. $model = Mage::getModel('catalog/resource_eav_attribute');
  151. /* @var $helper Mage_Catalog_Helper_Product */
  152. $helper = Mage::helper('catalog/product');
  153. $id = $this->getRequest()->getParam('attribute_id');
  154. //validate attribute_code
  155. if (isset($data['attribute_code'])) {
  156. $validatorAttrCode = new Zend_Validate_Regex(array('pattern' => '/^[a-z][a-z_0-9]{1,254}$/'));
  157. if (!$validatorAttrCode->isValid($data['attribute_code'])) {
  158. $session->addError(
  159. Mage::helper('catalog')->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.')
  160. );
  161. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  162. return;
  163. }
  164. }
  165. //validate frontend_input
  166. if (isset($data['frontend_input'])) {
  167. /** @var $validatorInputType Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype_Validator */
  168. $validatorInputType = Mage::getModel('eav/adminhtml_system_config_source_inputtype_validator');
  169. if (!$validatorInputType->isValid($data['frontend_input'])) {
  170. foreach ($validatorInputType->getMessages() as $message) {
  171. $session->addError($message);
  172. }
  173. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  174. return;
  175. }
  176. }
  177. if ($id) {
  178. $model->load($id);
  179. if (!$model->getId()) {
  180. $session->addError(
  181. Mage::helper('catalog')->__('This Attribute no longer exists'));
  182. $this->_redirect('*/*/');
  183. return;
  184. }
  185. // entity type check
  186. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  187. $session->addError(
  188. Mage::helper('catalog')->__('This attribute cannot be updated.'));
  189. $session->setAttributeData($data);
  190. $this->_redirect('*/*/');
  191. return;
  192. }
  193. $data['attribute_code'] = $model->getAttributeCode();
  194. $data['is_user_defined'] = $model->getIsUserDefined();
  195. $data['frontend_input'] = $model->getFrontendInput();
  196. } else {
  197. /**
  198. * @todo add to helper and specify all relations for properties
  199. */
  200. $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
  201. $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
  202. }
  203. if (!isset($data['is_configurable'])) {
  204. $data['is_configurable'] = 0;
  205. }
  206. if (!isset($data['is_filterable'])) {
  207. $data['is_filterable'] = 0;
  208. }
  209. if (!isset($data['is_filterable_in_search'])) {
  210. $data['is_filterable_in_search'] = 0;
  211. }
  212. if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
  213. $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
  214. }
  215. $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
  216. if ($defaultValueField) {
  217. $data['default_value'] = $this->getRequest()->getParam($defaultValueField);
  218. }
  219. if(!isset($data['apply_to'])) {
  220. $data['apply_to'] = array();
  221. }
  222. //filter
  223. $data = $this->_filterPostData($data);
  224. $model->addData($data);
  225. if (!$id) {
  226. $model->setEntityTypeId($this->_entityTypeId);
  227. $model->setIsUserDefined(1);
  228. }
  229. if ($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
  230. // For creating product attribute on product page we need specify attribute set and group
  231. $model->setAttributeSetId($this->getRequest()->getParam('set'));
  232. $model->setAttributeGroupId($this->getRequest()->getParam('group'));
  233. }
  234. try {
  235. $model->save();
  236. $session->addSuccess(
  237. Mage::helper('catalog')->__('The product attribute has been saved.'));
  238. /**
  239. * Clear translation cache because attribute labels are stored in translation
  240. */
  241. Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
  242. $session->setAttributeData(false);
  243. if ($this->getRequest()->getParam('popup')) {
  244. $this->_redirect('adminhtml/catalog_product/addAttribute', array(
  245. 'id' => $this->getRequest()->getParam('product'),
  246. 'attribute'=> $model->getId(),
  247. '_current' => true
  248. ));
  249. } elseif ($redirectBack) {
  250. $this->_redirect('*/*/edit', array('attribute_id' => $model->getId(),'_current'=>true));
  251. } else {
  252. $this->_redirect('*/*/', array());
  253. }
  254. return;
  255. } catch (Exception $e) {
  256. $session->addError($e->getMessage());
  257. $session->setAttributeData($data);
  258. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  259. return;
  260. }
  261. }
  262. $this->_redirect('*/*/');
  263. }
  264. public function deleteAction()
  265. {
  266. if ($id = $this->getRequest()->getParam('attribute_id')) {
  267. $model = Mage::getModel('catalog/resource_eav_attribute');
  268. // entity type check
  269. $model->load($id);
  270. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  271. Mage::getSingleton('adminhtml/session')->addError(
  272. Mage::helper('catalog')->__('This attribute cannot be deleted.'));
  273. $this->_redirect('*/*/');
  274. return;
  275. }
  276. try {
  277. $model->delete();
  278. Mage::getSingleton('adminhtml/session')->addSuccess(
  279. Mage::helper('catalog')->__('The product attribute has been deleted.'));
  280. $this->_redirect('*/*/');
  281. return;
  282. }
  283. catch (Exception $e) {
  284. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  285. $this->_redirect('*/*/edit', array('attribute_id' => $this->getRequest()->getParam('attribute_id')));
  286. return;
  287. }
  288. }
  289. Mage::getSingleton('adminhtml/session')->addError(
  290. Mage::helper('catalog')->__('Unable to find an attribute to delete.'));
  291. $this->_redirect('*/*/');
  292. }
  293. protected function _isAllowed()
  294. {
  295. return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes');
  296. }
  297. }