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

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

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