-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeController.php
339 lines (309 loc) · 12.5 KB
/
AttributeController.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Magento Enterprise Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Enterprise Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/enterprise-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Enterprise
* @package Enterprise_Customer
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/enterprise-edition
*/
/**
* Controller for Customer Attributes Management
*/
class Enterprise_Customer_Adminhtml_Customer_AttributeController
extends Mage_Adminhtml_Controller_Action
{
/**
* Customer Address Entity Type instance
*
* @var Mage_Eav_Model_Entity_Type
*/
protected $_entityType;
/**
* Return Customer Address Entity Type instance
*
* @return Mage_Eav_Model_Entity_Type
*/
protected function _getEntityType()
{
if (is_null($this->_entityType)) {
$this->_entityType = Mage::getSingleton('eav/config')->getEntityType('customer');
}
return $this->_entityType;
}
/**
* Load layout, set breadcrumbs
*
* @return Enterprise_Customer_Adminhtml_Customer_AttributeController
*/
protected function _initAction()
{
$this->loadLayout()
->_setActiveMenu('customer/attributes')
->_addBreadcrumb(
Mage::helper('enterprise_customer')->__('Customer'),
Mage::helper('enterprise_customer')->__('Customer'))
->_addBreadcrumb(
Mage::helper('enterprise_customer')->__('Manage Customer Attributes'),
Mage::helper('enterprise_customer')->__('Manage Customer Attributes'));
return $this;
}
/**
* Retrieve customer attribute object
*
* @return Mage_Customer_Model_Attribute
*/
protected function _initAttribute()
{
$attribute = Mage::getModel('customer/attribute');
$websiteId = $this->getRequest()->getParam('website');
if ($websiteId) {
$attribute->setWebsite($websiteId);
}
return $attribute;
}
/**
* Attributes grid
*
*/
public function indexAction()
{
$this->_title($this->__('Manage Customer Attributes'));
$this->_initAction()
->renderLayout();
}
/**
* Create new attribute action
*
*/
public function newAction()
{
$this->addActionLayoutHandles();
$this->_forward('edit');
}
/**
* Edit attribute action
*
*/
public function editAction()
{
/* @var $attributeObject Mage_Customer_Model_Attribute */
$attributeId = $this->getRequest()->getParam('attribute_id');
$attributeObject = $this->_initAttribute()
->setEntityTypeId($this->_getEntityType()->getId());
$this->_title($this->__('Manage Customer Attributes'));
if ($attributeId) {
$attributeObject->load($attributeId);
if (!$attributeObject->getId()) {
$this->_getSession()
->addError(Mage::helper('enterprise_customer')->__('Attribute is no longer exists.'));
$this->_redirect('*/*/');
return;
}
if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
$this->_getSession()->addError(Mage::helper('enterprise_customer')->__('You cannot edit this attribute.'));
$this->_redirect('*/*/');
return;
}
$this->_title($attributeObject->getFrontendLabel());
} else {
$this->_title($this->__('New Attribute'));
}
$attributeData = $this->_getSession()->getAttributeData(true);
if (!empty($attributeData)) {
$attributeObject->setData($attributeData);
}
Mage::register('entity_attribute', $attributeObject);
$label = $attributeObject->getId()
? Mage::helper('enterprise_customer')->__('Edit Customer Attribute')
: Mage::helper('enterprise_customer')->__('New Customer Attribute');
$this->_initAction()
->_addBreadcrumb($label, $label)
->renderLayout();
}
/**
* Validate attribute action
*
*/
public function validateAction()
{
$response = new Varien_Object();
$response->setError(false);
$attributeId = $this->getRequest()->getParam('attribute_id');
if (!$attributeId) {
$attributeCode = $this->getRequest()->getParam('attribute_code');
$attributeObject = $this->_initAttribute()
->loadByCode($this->_getEntityType()->getId(), $attributeCode);
if ($attributeObject->getId()) {
$this->_getSession()->addError(
Mage::helper('enterprise_customer')->__('Attribute with the same code already exists')
);
$this->_initLayoutMessages('adminhtml/session');
$response->setError(true);
$response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
}
}
$this->getResponse()->setBody($response->toJson());
}
/**
* Save attribute action
*
*/
public function saveAction()
{
$data = $this->getRequest()->getPost();
if ($this->getRequest()->isPost() && $data) {
/* @var $attributeObject Mage_Customer_Model_Attribute */
$attributeObject = $this->_initAttribute();
/* @var $helper Enterprise_Customer_Helper_Data */
$helper = Mage::helper('enterprise_customer');
$attributeId = $this->getRequest()->getParam('attribute_id');
if ($attributeId) {
$attributeObject->load($attributeId);
if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()) {
$this->_getSession()->addError(
Mage::helper('enterprise_customer')->__('You cannot edit this attribute.')
);
$this->_getSession()->addAttributeData($data);
$this->_redirect('*/*/');
return;
}
$data['attribute_code'] = $attributeObject->getAttributeCode();
$data['is_user_defined'] = $attributeObject->getIsUserDefined();
$data['frontend_input'] = $attributeObject->getFrontendInput();
$data['is_user_defined'] = $attributeObject->getIsUserDefined();
$data['is_system'] = $attributeObject->getIsSystem();
} else {
$data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
$data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
$data['backend_type'] = $helper->getAttributeBackendTypeByInputType($data['frontend_input']);
$data['is_user_defined'] = 1;
$data['is_system'] = 0;
// add set and group info
$data['attribute_set_id'] = $this->_getEntityType()->getDefaultAttributeSetId();
$data['attribute_group_id'] = Mage::getModel('eav/entity_attribute_set')
->getDefaultGroupId($data['attribute_set_id']);
}
if (isset($data['used_in_forms'])) {
$data['used_in_forms'][] = 'adminhtml_customer';
}
$defaultValueField = $helper->getAttributeDefaultValueByInput($data['frontend_input']);
if ($defaultValueField) {
$scopeKeyPrefix = ($this->getRequest()->getParam('website') ? 'scope_' : '');
$data[$scopeKeyPrefix . 'default_value'] = $this->getRequest()->getParam($scopeKeyPrefix . $defaultValueField);
}
$data['entity_type_id'] = $this->_getEntityType()->getId();
$data['validate_rules'] = $helper->getAttributeValidateRules($data['frontend_input'], $data);
$attributeObject->addData($data);
/**
* Check "Use Default Value" checkboxes values
*/
if ($useDefaults = $this->getRequest()->getPost('use_default')) {
foreach ($useDefaults as $key) {
$attributeObject->setData('scope_' . $key, null);
}
}
try {
$attributeObject->save();
Mage::dispatchEvent('enterprise_customer_attribute_save', array(
'attribute' => $attributeObject
));
$this->_getSession()->addSuccess(
Mage::helper('enterprise_customer')->__('The customer attribute has been saved.')
);
$this->_getSession()->setAttributeData(false);
if ($this->getRequest()->getParam('back', false)) {
$this->_redirect('*/*/edit', array(
'attribute_id' => $attributeObject->getId(),
'_current' => true
));
} else {
$this->_redirect('*/*/');
}
return;
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_getSession()->setAttributeData($data);
$this->_redirect('*/*/edit', array('_current' => true));
return;
} catch (Exception $e) {
$this->_getSession()->addException($e,
Mage::helper('enterprise_customer')->__('An error occurred while saving the customer attribute.')
);
$this->_getSession()->setAttributeData($data);
$this->_redirect('*/*/edit', array('_current' => true));
return;
}
}
$this->_redirect('*/*/');
return;
}
/**
* Delete attribute action
*
*/
public function deleteAction()
{
$attributeId = $this->getRequest()->getParam('attribute_id');
if ($attributeId) {
$attributeObject = $this->_initAttribute()->load($attributeId);
if ($attributeObject->getEntityTypeId() != $this->_getEntityType()->getId()
|| !$attributeObject->getIsUserDefined())
{
$this->_getSession()->addError(
Mage::helper('enterprise_customer')->__('You cannot delete this attribute.')
);
$this->_redirect('*/*/');
return;
}
try {
$attributeObject->delete();
Mage::dispatchEvent('enterprise_customer_attribute_delete', array(
'attribute' => $attributeObject
));
$this->_getSession()->addSuccess(
Mage::helper('enterprise_customer')->__('The customer attribute has been deleted.')
);
$this->_redirect('*/*/');
return;
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
return;
} catch (Exception $e) {
$this->_getSession()->addException($e,
Mage::helper('enterprise_customer')->__('An error occurred while deleting the customer attribute.')
);
$this->_redirect('*/*/edit', array('attribute_id' => $attributeId, '_current' => true));
return;
}
}
$this->_redirect('*/*/');
return;
}
/**
* Check whether attributes management functionality is allowed
*
* @return bool
*/
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('admin/customer/attributes/customer_attributes');
}
}