1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_Adminhtml_Catalog_Product_AttributeController extends Mage_Adminhtml_Controller_Action
36: {
37:
38: protected $_entityTypeId;
39:
40: public function preDispatch()
41: {
42: parent::preDispatch();
43: $this->_entityTypeId = Mage::getModel('eav/entity')->setType(Mage_Catalog_Model_Product::ENTITY)->getTypeId();
44: }
45:
46: protected function _initAction()
47: {
48: $this->_title($this->__('Catalog'))
49: ->_title($this->__('Attributes'))
50: ->_title($this->__('Manage Attributes'));
51:
52: if($this->getRequest()->getParam('popup')) {
53: $this->loadLayout('popup');
54: } else {
55: $this->loadLayout()
56: ->_setActiveMenu('catalog/attributes')
57: ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
58: ->_addBreadcrumb(
59: Mage::helper('catalog')->__('Manage Product Attributes'),
60: Mage::helper('catalog')->__('Manage Product Attributes'))
61: ;
62: }
63: return $this;
64: }
65:
66: public function indexAction()
67: {
68: $this->_initAction()
69: ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute'))
70: ->renderLayout();
71: }
72:
73: public function newAction()
74: {
75: $this->_forward('edit');
76: }
77:
78: public function editAction()
79: {
80: $id = $this->getRequest()->getParam('attribute_id');
81: $model = Mage::getModel('catalog/resource_eav_attribute')
82: ->setEntityTypeId($this->_entityTypeId);
83: if ($id) {
84: $model->load($id);
85:
86: if (! $model->getId()) {
87: Mage::getSingleton('adminhtml/session')->addError(
88: Mage::helper('catalog')->__('This attribute no longer exists'));
89: $this->_redirect('*/*/');
90: return;
91: }
92:
93:
94: if ($model->getEntityTypeId() != $this->_entityTypeId) {
95: Mage::getSingleton('adminhtml/session')->addError(
96: Mage::helper('catalog')->__('This attribute cannot be edited.'));
97: $this->_redirect('*/*/');
98: return;
99: }
100: }
101:
102:
103: $data = Mage::getSingleton('adminhtml/session')->getAttributeData(true);
104: if (! empty($data)) {
105: $model->addData($data);
106: }
107:
108: Mage::register('entity_attribute', $model);
109:
110: $this->_initAction();
111:
112: $this->_title($id ? $model->getName() : $this->__('New Attribute'));
113:
114: $item = $id ? Mage::helper('catalog')->__('Edit Product Attribute')
115: : Mage::helper('catalog')->__('New Product Attribute');
116:
117: $this->_addBreadcrumb($item, $item);
118:
119: $this->getLayout()->getBlock('attribute_edit_js')
120: ->setIsPopup((bool)$this->getRequest()->getParam('popup'));
121:
122: $this->renderLayout();
123:
124: }
125:
126: public function validateAction()
127: {
128: $response = new Varien_Object();
129: $response->setError(false);
130:
131: $attributeCode = $this->getRequest()->getParam('attribute_code');
132: $attributeId = $this->getRequest()->getParam('attribute_id');
133: $attribute = Mage::getModel('catalog/resource_eav_attribute')
134: ->loadByCode($this->_entityTypeId, $attributeCode);
135:
136: if ($attribute->getId() && !$attributeId) {
137: Mage::getSingleton('adminhtml/session')->addError(
138: Mage::helper('catalog')->__('Attribute with the same code already exists'));
139: $this->_initLayoutMessages('adminhtml/session');
140: $response->setError(true);
141: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
142: }
143:
144: $this->getResponse()->setBody($response->toJson());
145: }
146:
147: 148: 149: 150: 151: 152:
153: protected function _filterPostData($data)
154: {
155: if ($data) {
156:
157: $helperCatalog = Mage::helper('catalog');
158:
159: foreach ($data['frontend_label'] as & $value) {
160: if ($value) {
161: $value = $helperCatalog->stripTags($value);
162: }
163: }
164: }
165: return $data;
166: }
167:
168: public function saveAction()
169: {
170: $data = $this->getRequest()->getPost();
171: if ($data) {
172:
173: $session = Mage::getSingleton('adminhtml/session');
174:
175: $redirectBack = $this->getRequest()->getParam('back', false);
176:
177: $model = Mage::getModel('catalog/resource_eav_attribute');
178:
179: $helper = Mage::helper('catalog/product');
180:
181: $id = $this->getRequest()->getParam('attribute_id');
182:
183:
184: if (isset($data['attribute_code'])) {
185: $validatorAttrCode = new Zend_Validate_Regex(array('pattern' => '/^[a-z][a-z_0-9]{1,254}$/'));
186: if (!$validatorAttrCode->isValid($data['attribute_code'])) {
187: $session->addError(
188: 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.')
189: );
190: $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
191: return;
192: }
193: }
194:
195:
196:
197: if (isset($data['frontend_input'])) {
198:
199: $validatorInputType = Mage::getModel('eav/adminhtml_system_config_source_inputtype_validator');
200: if (!$validatorInputType->isValid($data['frontend_input'])) {
201: foreach ($validatorInputType->getMessages() as $message) {
202: $session->addError($message);
203: }
204: $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
205: return;
206: }
207: }
208:
209: if ($id) {
210: $model->load($id);
211:
212: if (!$model->getId()) {
213: $session->addError(
214: Mage::helper('catalog')->__('This Attribute no longer exists'));
215: $this->_redirect('*/*/');
216: return;
217: }
218:
219:
220: if ($model->getEntityTypeId() != $this->_entityTypeId) {
221: $session->addError(
222: Mage::helper('catalog')->__('This attribute cannot be updated.'));
223: $session->setAttributeData($data);
224: $this->_redirect('*/*/');
225: return;
226: }
227:
228: $data['attribute_code'] = $model->getAttributeCode();
229: $data['is_user_defined'] = $model->getIsUserDefined();
230: $data['frontend_input'] = $model->getFrontendInput();
231: } else {
232: 233: 234:
235: $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
236: $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
237: }
238:
239: if (!isset($data['is_configurable'])) {
240: $data['is_configurable'] = 0;
241: }
242: if (!isset($data['is_filterable'])) {
243: $data['is_filterable'] = 0;
244: }
245: if (!isset($data['is_filterable_in_search'])) {
246: $data['is_filterable_in_search'] = 0;
247: }
248:
249: if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
250: $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
251: }
252:
253: $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
254: if ($defaultValueField) {
255: $data['default_value'] = $this->getRequest()->getParam($defaultValueField);
256: }
257:
258: if(!isset($data['apply_to'])) {
259: $data['apply_to'] = array();
260: }
261:
262:
263: $data = $this->_filterPostData($data);
264: $model->addData($data);
265:
266: if (!$id) {
267: $model->setEntityTypeId($this->_entityTypeId);
268: $model->setIsUserDefined(1);
269: }
270:
271:
272: if ($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
273:
274: $model->setAttributeSetId($this->getRequest()->getParam('set'));
275: $model->setAttributeGroupId($this->getRequest()->getParam('group'));
276: }
277:
278: try {
279: $model->save();
280: $session->addSuccess(
281: Mage::helper('catalog')->__('The product attribute has been saved.'));
282:
283: 284: 285:
286: Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
287: $session->setAttributeData(false);
288: if ($this->getRequest()->getParam('popup')) {
289: $this->_redirect('adminhtml/catalog_product/addAttribute', array(
290: 'id' => $this->getRequest()->getParam('product'),
291: 'attribute'=> $model->getId(),
292: '_current' => true
293: ));
294: } elseif ($redirectBack) {
295: $this->_redirect('*/*/edit', array('attribute_id' => $model->getId(),'_current'=>true));
296: } else {
297: $this->_redirect('*/*/', array());
298: }
299: return;
300: } catch (Exception $e) {
301: $session->addError($e->getMessage());
302: $session->setAttributeData($data);
303: $this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
304: return;
305: }
306: }
307: $this->_redirect('*/*/');
308: }
309:
310: public function deleteAction()
311: {
312: if ($id = $this->getRequest()->getParam('attribute_id')) {
313: $model = Mage::getModel('catalog/resource_eav_attribute');
314:
315:
316: $model->load($id);
317: if ($model->getEntityTypeId() != $this->_entityTypeId) {
318: Mage::getSingleton('adminhtml/session')->addError(
319: Mage::helper('catalog')->__('This attribute cannot be deleted.'));
320: $this->_redirect('*/*/');
321: return;
322: }
323:
324: try {
325: $model->delete();
326: Mage::getSingleton('adminhtml/session')->addSuccess(
327: Mage::helper('catalog')->__('The product attribute has been deleted.'));
328: $this->_redirect('*/*/');
329: return;
330: }
331: catch (Exception $e) {
332: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
333: $this->_redirect('*/*/edit', array('attribute_id' => $this->getRequest()->getParam('attribute_id')));
334: return;
335: }
336: }
337: Mage::getSingleton('adminhtml/session')->addError(
338: Mage::helper('catalog')->__('Unable to find an attribute to delete.'));
339: $this->_redirect('*/*/');
340: }
341:
342: protected function _isAllowed()
343: {
344: return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes');
345: }
346: }
347: