00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Catalog_Product_AttributeController extends Mage_Adminhtml_Controller_Action
00036 {
00037
00038 protected $_entityTypeId;
00039
00040 public function preDispatch()
00041 {
00042 parent::preDispatch();
00043 $this->_entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
00044 }
00045
00046 protected function _initAction()
00047 {
00048 if($this->getRequest()->getParam('popup')) {
00049 $this->loadLayout('popup');
00050 } else {
00051 $this->loadLayout()
00052 ->_setActiveMenu('catalog/attributes')
00053 ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
00054 ->_addBreadcrumb(Mage::helper('catalog')->__('Manage Product Attributes'), Mage::helper('catalog')->__('Manage Product Attributes'))
00055 ;
00056 }
00057 return $this;
00058 }
00059
00060 public function indexAction()
00061 {
00062 $this->_initAction()
00063 ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute'))
00064 ->renderLayout();
00065 }
00066
00067 public function newAction()
00068 {
00069 $this->_forward('edit');
00070 }
00071
00072 public function editAction()
00073 {
00074 $id = $this->getRequest()->getParam('attribute_id');
00075 $model = Mage::getModel('catalog/entity_attribute');
00076
00077 if ($id) {
00078 $model->load($id);
00079
00080 if (! $model->getId()) {
00081 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('This attribute no longer exists'));
00082 $this->_redirect('*/*/');
00083 return;
00084 }
00085
00086
00087 if ($model->getEntityTypeId() != $this->_entityTypeId) {
00088 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot edit this attribute'));
00089 $this->_redirect('*/*/');
00090 return;
00091 }
00092 }
00093
00094
00095 $data = Mage::getSingleton('adminhtml/session')->getAttributeData(true);
00096 if (! empty($data)) {
00097 $model->setData($data);
00098 }
00099
00100 Mage::register('entity_attribute', $model);
00101
00102 $this->_initAction()
00103 ->_addBreadcrumb($id ? Mage::helper('catalog')->__('Edit Product Attribute') : Mage::helper('catalog')->__('New Product Attribute'), $id ? Mage::helper('catalog')->__('Edit Product Attribute') : Mage::helper('catalog')->__('New Product Attribute'))
00104 ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_edit')->setData('action', $this->getUrl('*/catalog_product_attribute/save')))
00105 ->_addLeft($this->getLayout()->createBlock('adminhtml/catalog_product_attribute_edit_tabs'))
00106 ->_addJs(
00107 $this->getLayout()->createBlock('adminhtml/template')
00108 ->setIsPopup((bool)$this->getRequest()->getParam('popup'))
00109 ->setTemplate('catalog/product/attribute/js.phtml')
00110 )
00111 ->renderLayout();
00112 }
00113
00114 public function validateAction()
00115 {
00116 $response = new Varien_Object();
00117 $response->setError(false);
00118
00119 $attributeCode = $this->getRequest()->getParam('attribute_code');
00120 $attributeId = $this->getRequest()->getParam('attribute_id');
00121 $attribute = Mage::getModel('catalog/entity_attribute')
00122 ->loadByCode($this->_entityTypeId, $attributeCode);
00123
00124 if ($attribute->getId() && !$attributeId) {
00125 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Attribute with the same code already exists'));
00126 $this->_initLayoutMessages('adminhtml/session');
00127 $response->setError(true);
00128 $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
00129 }
00130
00131 $this->getResponse()->setBody($response->toJson());
00132 }
00133
00134 public function saveAction()
00135 {
00136 if ($data = $this->getRequest()->getPost()) {
00137 $redirectBack = $this->getRequest()->getParam('back', false);
00138 $model = Mage::getModel('catalog/entity_attribute');
00139
00140
00141 if ($id = $this->getRequest()->getParam('attribute_id')) {
00142
00143 $model->load($id);
00144
00145
00146 if ($model->getEntityTypeId() != $this->_entityTypeId) {
00147 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot update this attribute'));
00148 Mage::getSingleton('adminhtml/session')->setAttributeData($data);
00149 $this->_redirect('*/*/');
00150 return;
00151 }
00152
00153 $data['attribute_code'] = $model->getAttributeCode();
00154 $data['is_user_defined'] = $model->getIsUserDefined();
00155 $data['frontend_input'] = $model->getFrontendInput();
00156 }
00157
00158 if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
00159 $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
00160 }
00161
00162 $defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
00163 if ($defaultValueField) {
00164 $data['default_value'] = $this->getRequest()->getParam($defaultValueField);
00165 }
00166
00167 if(!isset($data['apply_to'])) {
00168 $data['apply_to'] = array();
00169 }
00170
00171
00172
00173
00174 if (isset($data['frontend_input']) && $data['frontend_input'] == 'multiselect') {
00175 $data['backend_model'] = 'eav/entity_attribute_backend_array';
00176 }
00177
00178 $model->addData($data);
00179
00180
00181 if (!$id) {
00182 $model->setEntityTypeId($this->_entityTypeId);
00183 $model->setIsUserDefined(1);
00184 }
00185
00186
00187 if($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
00188
00189 $model->setAttributeSetId($this->getRequest()->getParam('set'));
00190 $model->setAttributeGroupId($this->getRequest()->getParam('group'));
00191 }
00192
00193 try {
00194 $model->save();
00195 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Product attribute was successfully saved'));
00196
00197
00198
00199
00200 Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
00201 Mage::getSingleton('adminhtml/session')->setAttributeData(false);
00202 if ($this->getRequest()->getParam('popup')) {
00203 $this->_redirect('adminhtml/catalog_product/addAttribute', array(
00204 'id' => $this->getRequest()->getParam('product'),
00205 'attribute'=> $model->getId(),
00206 '_current' => true
00207 ));
00208 } elseif ($redirectBack) {
00209 $this->_redirect('*/*/edit', array('attribute_id' => $model->getId(),'_current'=>true));
00210 } else {
00211 $this->_redirect('*/*/', array());
00212 }
00213 return;
00214 } catch (Exception $e) {
00215 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00216 Mage::getSingleton('adminhtml/session')->setAttributeData($data);
00217 $this->_redirect('*/*/edit', array('_current' => true));
00218 return;
00219 }
00220 }
00221 $this->_redirect('*/*/');
00222 }
00223
00224 public function deleteAction()
00225 {
00226 if ($id = $this->getRequest()->getParam('attribute_id')) {
00227 $model = Mage::getModel('catalog/entity_attribute');
00228
00229
00230 $model->load($id);
00231 if ($model->getEntityTypeId() != $this->_entityTypeId) {
00232 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('You cannot delete this attribute'));
00233 $this->_redirect('*/*/');
00234 return;
00235 }
00236
00237 try {
00238 $model->delete();
00239 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Product attribute was successfully deleted'));
00240 $this->_redirect('*/*/');
00241 return;
00242 }
00243 catch (Exception $e) {
00244 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00245 $this->_redirect('*/*/edit', array('attribute_id' => $this->getRequest()->getParam('attribute_id')));
00246 return;
00247 }
00248 }
00249 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Unable to find an attribute to delete'));
00250 $this->_redirect('*/*/');
00251 }
00252
00253 protected function _isAllowed()
00254 {
00255 return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes');
00256 }
00257 }