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_Action_AttributeController extends Mage_Adminhtml_Controller_Action
36: {
37:
38: protected function _construct()
39: {
40:
41: $this->setUsedModuleName('Mage_Catalog');
42: }
43:
44: public function editAction()
45: {
46: if (!$this->_validateProducts()) {
47: return;
48: }
49:
50: $this->loadLayout();
51: $this->renderLayout();
52: }
53:
54: 55: 56:
57: public function saveAction()
58: {
59: if (!$this->_validateProducts()) {
60: return;
61: }
62:
63:
64: $inventoryData = $this->getRequest()->getParam('inventory', array());
65: $attributesData = $this->getRequest()->getParam('attributes', array());
66: $websiteRemoveData = $this->getRequest()->getParam('remove_website_ids', array());
67: $websiteAddData = $this->getRequest()->getParam('add_website_ids', array());
68:
69:
70: foreach (Mage::helper('cataloginventory')->getConfigItemOptions() as $option) {
71: if (isset($inventoryData[$option]) && !isset($inventoryData['use_config_' . $option])) {
72: $inventoryData['use_config_' . $option] = 0;
73: }
74: }
75:
76: try {
77: if ($attributesData) {
78: $dateFormat = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
79: $storeId = $this->_getHelper()->getSelectedStoreId();
80:
81: foreach ($attributesData as $attributeCode => $value) {
82: $attribute = Mage::getSingleton('eav/config')
83: ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
84: if (!$attribute->getAttributeId()) {
85: unset($attributesData[$attributeCode]);
86: continue;
87: }
88: if ($attribute->getBackendType() == 'datetime') {
89: if (!empty($value)) {
90: $filterInput = new Zend_Filter_LocalizedToNormalized(array(
91: 'date_format' => $dateFormat
92: ));
93: $filterInternal = new Zend_Filter_NormalizedToLocalized(array(
94: 'date_format' => Varien_Date::DATE_INTERNAL_FORMAT
95: ));
96: $value = $filterInternal->filter($filterInput->filter($value));
97: } else {
98: $value = null;
99: }
100: $attributesData[$attributeCode] = $value;
101: } elseif ($attribute->getFrontendInput() == 'multiselect') {
102:
103: $isChanged = (bool)$this->getRequest()->getPost($attributeCode . '_checkbox');
104: if (!$isChanged) {
105: unset($attributesData[$attributeCode]);
106: continue;
107: }
108: if (is_array($value)) {
109: $value = implode(',', $value);
110: }
111: $attributesData[$attributeCode] = $value;
112: }
113: }
114:
115: Mage::getSingleton('catalog/product_action')
116: ->updateAttributes($this->_getHelper()->getProductIds(), $attributesData, $storeId);
117: }
118: if ($inventoryData) {
119:
120: $stockItem = Mage::getModel('cataloginventory/stock_item');
121: $stockItem->setProcessIndexEvents(false);
122: $stockItemSaved = false;
123:
124: foreach ($this->_getHelper()->getProductIds() as $productId) {
125: $stockItem->setData(array());
126: $stockItem->loadByProduct($productId)
127: ->setProductId($productId);
128:
129: $stockDataChanged = false;
130: foreach ($inventoryData as $k => $v) {
131: $stockItem->setDataUsingMethod($k, $v);
132: if ($stockItem->dataHasChangedFor($k)) {
133: $stockDataChanged = true;
134: }
135: }
136: if ($stockDataChanged) {
137: $stockItem->save();
138: $stockItemSaved = true;
139: }
140: }
141:
142: if ($stockItemSaved) {
143: Mage::getSingleton('index/indexer')->indexEvents(
144: Mage_CatalogInventory_Model_Stock_Item::ENTITY,
145: Mage_Index_Model_Event::TYPE_SAVE
146: );
147: }
148: }
149:
150: if ($websiteAddData || $websiteRemoveData) {
151:
152: $actionModel = Mage::getSingleton('catalog/product_action');
153: $productIds = $this->_getHelper()->getProductIds();
154:
155: if ($websiteRemoveData) {
156: $actionModel->updateWebsites($productIds, $websiteRemoveData, 'remove');
157: }
158: if ($websiteAddData) {
159: $actionModel->updateWebsites($productIds, $websiteAddData, 'add');
160: }
161:
162: 163: 164:
165: Mage::dispatchEvent('catalog_product_to_website_change', array(
166: 'products' => $productIds
167: ));
168:
169: $this->_getSession()->addNotice(
170: $this->__('Please refresh "Catalog URL Rewrites" and "Product Attributes" in System -> <a href="%s">Index Management</a>', $this->getUrl('adminhtml/process/list'))
171: );
172: }
173:
174: $this->_getSession()->addSuccess(
175: $this->__('Total of %d record(s) were updated', count($this->_getHelper()->getProductIds()))
176: );
177: }
178: catch (Mage_Core_Exception $e) {
179: $this->_getSession()->addError($e->getMessage());
180: }
181: catch (Exception $e) {
182: $this->_getSession()->addException($e, $this->__('An error occurred while updating the product(s) attributes.'));
183: }
184:
185: $this->_redirect('*/catalog_product/', array('store'=>$this->_getHelper()->getSelectedStoreId()));
186: }
187:
188: 189: 190: 191: 192:
193: protected function _validateProducts()
194: {
195: $error = false;
196: $productIds = $this->_getHelper()->getProductIds();
197: if (!is_array($productIds)) {
198: $error = $this->__('Please select products for attributes update');
199: } else if (!Mage::getModel('catalog/product')->isProductsHasSku($productIds)) {
200: $error = $this->__('Some of the processed products have no SKU value defined. Please fill it prior to performing operations on these products.');
201: }
202:
203: if ($error) {
204: $this->_getSession()->addError($error);
205: $this->_redirect('*/catalog_product/', array('_current'=>true));
206: }
207:
208: return !$error;
209: }
210:
211: 212: 213: 214: 215:
216: protected function _getHelper()
217: {
218: return Mage::helper('adminhtml/catalog_product_edit_action_attribute');
219: }
220:
221: protected function _isAllowed()
222: {
223: return Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes');
224: }
225:
226: 227: 228: 229:
230: public function validateAction()
231: {
232: $response = new Varien_Object();
233: $response->setError(false);
234: $attributesData = $this->getRequest()->getParam('attributes', array());
235: $data = new Varien_Object();
236:
237: try {
238: if ($attributesData) {
239: $dateFormat = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
240: $storeId = $this->_getHelper()->getSelectedStoreId();
241:
242: foreach ($attributesData as $attributeCode => $value) {
243: $attribute = Mage::getSingleton('eav/config')
244: ->getAttribute('catalog_product', $attributeCode);
245: if (!$attribute->getAttributeId()) {
246: unset($attributesData[$attributeCode]);
247: continue;
248: }
249: $data->setData($attributeCode, $value);
250: $attribute->getBackend()->validate($data);
251: }
252: }
253: } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
254: $response->setError(true);
255: $response->setAttribute($e->getAttributeCode());
256: $response->setMessage($e->getMessage());
257: } catch (Mage_Core_Exception $e) {
258: $response->setError(true);
259: $response->setMessage($e->getMessage());
260: } catch (Exception $e) {
261: $this->_getSession()->addException($e, $this->__('An error occurred while updating the product(s) attributes.'));
262: $this->_initLayoutMessages('adminhtml/session');
263: $response->setError(true);
264: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
265: }
266:
267: $this->getResponse()->setBody($response->toJson());
268: }
269: }
270: