PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 364 lines | 255 code | 50 blank | 59 comment | 41 complexity | d4e455c4b9cb68d92003d9c3cca4155e 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. * 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. $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. //options
  140. if (!empty($data['option']['value'])) {
  141. foreach ($data['option']['value'] as &$options) {
  142. foreach ($options as &$label) {
  143. $label = $helperCatalog->stripTags($label);
  144. }
  145. }
  146. }
  147. //default value
  148. if (!empty($data['default_value'])) {
  149. $data['default_value'] = $helperCatalog->stripTags($data['default_value']);
  150. }
  151. if (!empty($data['default_value_text'])) {
  152. $data['default_value_text'] = $helperCatalog->stripTags($data['default_value_text']);
  153. }
  154. if (!empty($data['default_value_textarea'])) {
  155. $data['default_value_textarea'] = $helperCatalog->stripTags($data['default_value_textarea']);
  156. }
  157. }
  158. return $data;
  159. }
  160. public function saveAction()
  161. {
  162. $data = $this->getRequest()->getPost();
  163. if ($data) {
  164. /** @var $session Mage_Admin_Model_Session */
  165. $session = Mage::getSingleton('adminhtml/session');
  166. $redirectBack = $this->getRequest()->getParam('back', false);
  167. /* @var $model Mage_Catalog_Model_Entity_Attribute */
  168. $model = Mage::getModel('catalog/resource_eav_attribute');
  169. /* @var $helper Mage_Catalog_Helper_Product */
  170. $helper = Mage::helper('catalog/product');
  171. $id = $this->getRequest()->getParam('attribute_id');
  172. //validate attribute_code
  173. if (isset($data['attribute_code'])) {
  174. $validatorAttrCode = new Zend_Validate_Regex(array('pattern' => '/^[a-z_]{1,255}$/'));
  175. if (!$validatorAttrCode->isValid($data['attribute_code'])) {
  176. $session->addError(
  177. $helper->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'));
  178. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  179. return;
  180. }
  181. }
  182. //validate frontend_input
  183. if (isset($data['frontend_input'])) {
  184. /** @var $validatorInputType Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype_Validator */
  185. $validatorInputType = Mage::getModel('eav/adminhtml_system_config_source_inputtype_validator');
  186. if (!$validatorInputType->isValid($data['frontend_input'])) {
  187. foreach ($validatorInputType->getMessages() as $message) {
  188. $session->addError($message);
  189. }
  190. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  191. return;
  192. }
  193. }
  194. if ($id) {
  195. $model->load($id);
  196. if (!$model->getId()) {
  197. $session->addError(
  198. Mage::helper('catalog')->__('This Attribute no longer exists'));
  199. $this->_redirect('*/*/');
  200. return;
  201. }
  202. // entity type check
  203. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  204. $session->addError(
  205. Mage::helper('catalog')->__('This attribute cannot be updated.'));
  206. $session->setAttributeData($data);
  207. $this->_redirect('*/*/');
  208. return;
  209. }
  210. $data['attribute_code'] = $model->getAttributeCode();
  211. $data['is_user_defined'] = $model->getIsUserDefined();
  212. $data['frontend_input'] = $model->getFrontendInput();
  213. } else {
  214. /**
  215. * @todo add to helper and specify all relations for properties
  216. */
  217. $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
  218. $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
  219. }
  220. if (!isset($data['is_configurable'])) {
  221. $data['is_configurable'] = 0;
  222. }
  223. if (!isset($data['is_filterable'])) {
  224. $data['is_filterable'] = 0;
  225. }
  226. if (!isset($data['is_filterable_in_search'])) {
  227. $data['is_filterable_in_search'] = 0;
  228. }
  229. if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
  230. $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
  231. }
  232. $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
  233. if ($defaultValueField) {
  234. $data['default_value'] = $this->getRequest()->getParam($defaultValueField);
  235. }
  236. if(!isset($data['apply_to'])) {
  237. $data['apply_to'] = array();
  238. }
  239. //filter
  240. $data = $this->_filterPostData($data);
  241. $model->addData($data);
  242. if (!$id) {
  243. $model->setEntityTypeId($this->_entityTypeId);
  244. $model->setIsUserDefined(1);
  245. }
  246. if ($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
  247. // For creating product attribute on product page we need specify attribute set and group
  248. $model->setAttributeSetId($this->getRequest()->getParam('set'));
  249. $model->setAttributeGroupId($this->getRequest()->getParam('group'));
  250. }
  251. try {
  252. $model->save();
  253. $session->addSuccess(
  254. Mage::helper('catalog')->__('The product attribute has been saved.'));
  255. /**
  256. * Clear translation cache because attribute labels are stored in translation
  257. */
  258. Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
  259. $session->setAttributeData(false);
  260. if ($this->getRequest()->getParam('popup')) {
  261. $this->_redirect('adminhtml/catalog_product/addAttribute', array(
  262. 'id' => $this->getRequest()->getParam('product'),
  263. 'attribute'=> $model->getId(),
  264. '_current' => true
  265. ));
  266. } elseif ($redirectBack) {
  267. $this->_redirect('*/*/edit', array('attribute_id' => $model->getId(),'_current'=>true));
  268. } else {
  269. $this->_redirect('*/*/', array());
  270. }
  271. return;
  272. } catch (Exception $e) {
  273. $session->addError($e->getMessage());
  274. $session->setAttributeData($data);
  275. $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
  276. return;
  277. }
  278. }
  279. $this->_redirect('*/*/');
  280. }
  281. public function deleteAction()
  282. {
  283. if ($id = $this->getRequest()->getParam('attribute_id')) {
  284. $model = Mage::getModel('catalog/resource_eav_attribute');
  285. // entity type check
  286. $model->load($id);
  287. if ($model->getEntityTypeId() != $this->_entityTypeId) {
  288. Mage::getSingleton('adminhtml/session')->addError(
  289. Mage::helper('catalog')->__('This attribute cannot be deleted.'));
  290. $this->_redirect('*/*/');
  291. return;
  292. }
  293. try {
  294. $model->delete();
  295. Mage::getSingleton('adminhtml/session')->addSuccess(
  296. Mage::helper('catalog')->__('The product attribute has been deleted.'));
  297. $this->_redirect('*/*/');
  298. return;
  299. }
  300. catch (Exception $e) {
  301. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  302. $this->_redirect('*/*/edit', array('attribute_id' => $this->getRequest()->getParam('attribute_id')));
  303. return;
  304. }
  305. }
  306. Mage::getSingleton('adminhtml/session')->addError(
  307. Mage::helper('catalog')->__('Unable to find an attribute to delete.'));
  308. $this->_redirect('*/*/');
  309. }
  310. protected function _isAllowed()
  311. {
  312. return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes');
  313. }
  314. }