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_Action_AttributeController extends Mage_Adminhtml_Controller_Action
00036 {
00037
00038 protected function _construct()
00039 {
00040
00041 $this->setUsedModuleName('Mage_Catalog');
00042 }
00043
00044 public function editAction()
00045 {
00046 if (!$this->_validateProducts()) {
00047 return;
00048 }
00049
00050 $this->loadLayout();
00051 $this->renderLayout();
00052 }
00053
00054 public function saveAction()
00055 {
00056 if (!$this->_validateProducts()) {
00057 return;
00058 }
00059
00060
00061 $inventoryData = $this->getRequest()->getParam('inventory', array());
00062 $attributesData = $this->getRequest()->getParam('attributes', array());
00063 $websiteRemoveData = $this->getRequest()->getParam('remove_website_ids', array());
00064 $websiteAddData = $this->getRequest()->getParam('add_website_ids', array());
00065
00066
00067 foreach (Mage::helper('cataloginventory')->getConfigItemOptions() as $option) {
00068 if (isset($inventoryData[$option]) && !isset($inventoryData['use_config_' . $option])) {
00069 $inventoryData['use_config_' . $option] = 0;
00070 }
00071 }
00072
00073 try {
00074 if ($attributesData) {
00075 $product = Mage::getModel('catalog/product');
00076 if ($inventoryData) {
00077 $stockItem = Mage::getModel('cataloginventory/stock_item');
00078 }
00079 foreach ($this->_getHelper()->getProductIds() as $productId) {
00080 $product->setData(array());
00081 $product->setStoreId($this->_getHelper()->getSelectedStoreId())
00082 ->load($productId)
00083 ->setIsMassupdate(true)
00084 ->setExcludeUrlRewrite(true);
00085
00086 if (!$product->getId()) {
00087 continue;
00088 }
00089
00090 $product->addData($attributesData);
00091 if ($inventoryData) {
00092 $product->setStockData($inventoryData);
00093 }
00094
00095 $dataChanged = false;
00096 foreach ($attributesData as $k => $v) {
00097 if ($product->dataHasChangedFor($k)) {
00098 $dataChanged = true;
00099 }
00100 }
00101
00102 if ($dataChanged) {
00103 $product->save();
00104 }
00105 elseif ($inventoryData) {
00106 $stockItem->setData(array());
00107 $stockItem->loadByProduct($productId)
00108 ->setProductId($productId);
00109 $stockDataChanged = false;
00110 foreach ($inventoryData as $k => $v) {
00111 $stockItem->setDataUsingMethod($k, $v);
00112 if ($stockItem->dataHasChangedFor($k)) {
00113 $stockDataChanged = true;
00114 }
00115 }
00116 if ($stockDataChanged) {
00117 $stockItem->save();
00118 }
00119 }
00120 }
00121 }
00122 elseif ($inventoryData) {
00123 $stockItem = Mage::getModel('cataloginventory/stock_item');
00124
00125 foreach ($this->_getHelper()->getProductIds() as $productId) {
00126 $stockItem->setData(array());
00127 $stockItem->loadByProduct($productId)
00128 ->setProductId($productId);
00129
00130 $stockDataChanged = false;
00131 foreach ($inventoryData as $k => $v) {
00132 $stockItem->setDataUsingMethod($k, $v);
00133 if ($stockItem->dataHasChangedFor($k)) {
00134 $stockDataChanged = true;
00135 }
00136 }
00137 if ($stockDataChanged) {
00138 $stockItem->save();
00139 }
00140 }
00141 }
00142
00143 if ($websiteAddData || $websiteRemoveData) {
00144 $productWebsite = Mage::getModel('catalog/product_website');
00145
00146
00147 if ($websiteRemoveData) {
00148 $productWebsite->removeProducts($websiteRemoveData, $this->_getHelper()->getProductIds());
00149 }
00150 if ($websiteAddData) {
00151 $productWebsite->addProducts($websiteAddData, $this->_getHelper()->getProductIds());
00152 }
00153
00154 Mage::dispatchEvent(
00155 'catalog_product_to_website_change',
00156 array('products'=>$this->_getHelper()->getProductIds())
00157 );
00158
00159 $this->_getSession()->addNotice(
00160 $this->__('Please refresh "Catalog Rewrites" and "Layered Navigation Indices" in System -> <a href="%s">Cache Management</a>', $this->getUrl('adminhtml/system_cache'))
00161 );
00162 }
00163
00164 $this->_getSession()->addSuccess(
00165 $this->__('Total of %d record(s) were successfully updated',
00166 count($this->_getHelper()->getProductIds()))
00167 );
00168 }
00169 catch (Mage_Core_Exception $e) {
00170 $this->_getSession()->addError($e->getMessage());
00171 }
00172 catch (Exception $e) {
00173 $this->_getSession()->addException($e, $this->__('There was an error while updating product(s) attributes'));
00174 }
00175
00176 $this->_redirect('*/catalog_product/', array('store'=>$this->_getHelper()->getSelectedStoreId()));
00177 }
00178
00179
00180
00181
00182
00183
00184 protected function _validateProducts()
00185 {
00186 if (!is_array($this->_getHelper()->getProductIds())) {
00187 $this->_getSession()->addError($this->__('Please select products for attributes update'));
00188 $this->_redirect('*/catalog_product/', array('_current'=>true));
00189 return false;
00190 }
00191
00192 return true;
00193 }
00194
00195
00196
00197
00198
00199
00200 protected function _getHelper()
00201 {
00202 return Mage::helper('adminhtml/catalog_product_edit_action_attribute');
00203 }
00204
00205 protected function _isAllowed()
00206 {
00207 return Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes');
00208 }
00209 }