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_SetController extends Mage_Adminhtml_Controller_Action
36: {
37: public function indexAction()
38: {
39: $this->_title($this->__('Catalog'))
40: ->_title($this->__('Attributes'))
41: ->_title($this->__('Manage Attribute Sets'));
42:
43: $this->_setTypeId();
44:
45: $this->loadLayout();
46: $this->_setActiveMenu('catalog/sets');
47:
48: $this->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'));
49: $this->_addBreadcrumb(
50: Mage::helper('catalog')->__('Manage Attribute Sets'),
51: Mage::helper('catalog')->__('Manage Attribute Sets'));
52:
53: $this->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_set_toolbar_main'));
54: $this->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_set_grid'));
55:
56: $this->renderLayout();
57: }
58:
59: public function editAction()
60: {
61: $this->_title($this->__('Catalog'))
62: ->_title($this->__('Attributes'))
63: ->_title($this->__('Manage Attribute Sets'));
64:
65: $this->_setTypeId();
66: $attributeSet = Mage::getModel('eav/entity_attribute_set')
67: ->load($this->getRequest()->getParam('id'));
68:
69: if (!$attributeSet->getId()) {
70: $this->_redirect('*/*/index');
71: return;
72: }
73:
74: $this->_title($attributeSet->getId() ? $attributeSet->getAttributeSetName() : $this->__('New Set'));
75:
76: Mage::register('current_attribute_set', $attributeSet);
77:
78: $this->loadLayout();
79: $this->_setActiveMenu('catalog/sets');
80: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
81:
82: $this->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'));
83: $this->_addBreadcrumb(
84: Mage::helper('catalog')->__('Manage Product Sets'),
85: Mage::helper('catalog')->__('Manage Product Sets'));
86:
87: $this->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_set_main'));
88:
89: $this->renderLayout();
90: }
91:
92: public function setGridAction()
93: {
94: $this->_setTypeId();
95: $this->getResponse()->setBody(
96: $this->getLayout()
97: ->createBlock('adminhtml/catalog_product_attribute_set_grid')
98: ->toHtml());
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public function saveAction()
109: {
110: $entityTypeId = $this->_getEntityTypeId();
111: $hasError = false;
112: $attributeSetId = $this->getRequest()->getParam('id', false);
113: $isNewSet = $this->getRequest()->getParam('gotoEdit', false) == '1';
114:
115:
116: $model = Mage::getModel('eav/entity_attribute_set')
117: ->setEntityTypeId($entityTypeId);
118:
119:
120: $helper = Mage::helper('adminhtml');
121:
122: try {
123: if ($isNewSet) {
124:
125: $name = $helper->stripTags($this->getRequest()->getParam('attribute_set_name'));
126: $model->setAttributeSetName(trim($name));
127: } else {
128: if ($attributeSetId) {
129: $model->load($attributeSetId);
130: }
131: if (!$model->getId()) {
132: Mage::throwException(Mage::helper('catalog')->__('This attribute set no longer exists.'));
133: }
134: $data = Mage::helper('core')->jsonDecode($this->getRequest()->getPost('data'));
135:
136:
137: $data['attribute_set_name'] = $helper->stripTags($data['attribute_set_name']);
138:
139: $model->organizeData($data);
140: }
141:
142: $model->validate();
143: if ($isNewSet) {
144: $model->save();
145: $model->initFromSkeleton($this->getRequest()->getParam('skeleton_set'));
146: }
147: $model->save();
148: $this->_getSession()->addSuccess(Mage::helper('catalog')->__('The attribute set has been saved.'));
149: } catch (Mage_Core_Exception $e) {
150: $this->_getSession()->addError($e->getMessage());
151: $hasError = true;
152: } catch (Exception $e) {
153: $this->_getSession()->addException($e,
154: Mage::helper('catalog')->__('An error occurred while saving the attribute set.'));
155: $hasError = true;
156: }
157:
158: if ($isNewSet) {
159: if ($hasError) {
160: $this->_redirect('*/*/add');
161: } else {
162: $this->_redirect('*/*/edit', array('id' => $model->getId()));
163: }
164: } else {
165: $response = array();
166: if ($hasError) {
167: $this->_initLayoutMessages('adminhtml/session');
168: $response['error'] = 1;
169: $response['message'] = $this->getLayout()->getMessagesBlock()->getGroupedHtml();
170: } else {
171: $response['error'] = 0;
172: $response['url'] = $this->getUrl('*/*/');
173: }
174: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
175: }
176: }
177:
178: public function addAction()
179: {
180: $this->_title($this->__('Catalog'))
181: ->_title($this->__('Attributes'))
182: ->_title($this->__('Manage Attribute Sets'))
183: ->_title($this->__('New Set'));
184:
185: $this->_setTypeId();
186:
187: $this->loadLayout();
188: $this->_setActiveMenu('catalog/sets');
189:
190: $this->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_set_toolbar_add'));
191:
192: $this->renderLayout();
193: }
194:
195: public function deleteAction()
196: {
197: $setId = $this->getRequest()->getParam('id');
198: try {
199: Mage::getModel('eav/entity_attribute_set')
200: ->setId($setId)
201: ->delete();
202:
203: $this->_getSession()->addSuccess($this->__('The attribute set has been removed.'));
204: $this->getResponse()->setRedirect($this->getUrl('*/*/'));
205: } catch (Exception $e) {
206: $this->_getSession()->addError($this->__('An error occurred while deleting this set.'));
207: $this->_redirectReferer();
208: }
209: }
210:
211: 212: 213: 214:
215: protected function _setTypeId()
216: {
217: Mage::register('entityType',
218: Mage::getModel('catalog/product')->getResource()->getTypeId());
219: }
220:
221: protected function _isAllowed()
222: {
223: return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/sets');
224: }
225:
226: 227: 228: 229: 230:
231: protected function _getEntityTypeId()
232: {
233: if (is_null(Mage::registry('entityType'))) {
234: $this->_setTypeId();
235: }
236: return Mage::registry('entityType');
237: }
238: }
239: