Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Overloaded saveAction() in adminhtml ProductController but theres one problem…
 
novon
Jr. Member
 
Total Posts:  13
Joined:  2008-09-26
 

I needed to do some processing before actually saving the product data so I overloaded the controller like so:

Mockett/Mdeleteimage/controllers/Catalog/ProductController.php:

require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';
class 
Mockett_Mdeleteimage_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController{

    
public function saveAction(){
        
//exit('exiting in new controller... woo woo');
        
        
$data $this->getRequest()->getPost();
        if( 
$data ){
            $images 
explode("}, {"$data['product']['media_gallery']['images']);
            
//print_r($images);
            
            //$current
            
foreach( $images as $image ){
                $currentRecord 
$image;
                
$currentRecord str_replace("[{"""$currentRecord);
                
$currentRecord str_replace("}]"""$currentRecord);
                
                
$imageParams explode(", "$currentRecord);
                
                
$currentRemoveFlag 0;
                
$imgRelPath '';
                foreach(
$imageParams as $paramPair){                    
                    
list( $key$value ) = explode(": "$paramPair);
                    
                    
                    
//echo "key: $key, value: $value<br><br>";
                    
                    
if( trim($key) == '"file"' ){
                        $imgRelPath 
str_replace('"'''trim($value));
                    
}
                    
elseif( trim($key) == '"removed"' && trim($value) == '1' ){
                        $currentRemoveFlag 
1;
                    
}
                    
                    
//get the path of the current image
                
}
                
                
if( $currentRemoveFlag && !empty($imgRelPath) ){
                    $baseDir 
getcwd();
                    
$fullImgPath $baseDir "/media/catalog/product"$imgRelPath;
                    
$tmpImgPath $baseDir "/media/tmp/catalog/product" .$imgRelPath;
                    
                    if( 
file_exists($fullImgPath) ){
                        
if( !unlink( $fullImgPath ) ){
                            
exit("Failed to unlink image at: $fullImgPath");
                        
}
                    }
                    
                    
if( file_exists($tmpImgPath) ){
                        
if( !unlink( $tmpImgPath ) ){
                            
exit("Failed to unlink image at: $tmpImgPath");
                        
}
                    }
                    
                    
//echo $fullImgPath."<br>";
                    
                
}
            }
        }
        
        parent
::saveAction();
    
}
}

Mockett/Mdeleteimage/etc/config.xml:

<?xml version="1.0"?>
<config>
    <
modules>
        <
Mockett_Mdeleteimage>
            <
version>0.1.0</version>
        </
Mockett_Mdeleteimage>
    </
modules>
    <global>
        <
rewrite>
            <
mockett_mdeleteimage_adminhtml_catalog_product_save>
                <
from><![CDATA[#^/admin/catalog_product/save#]]></from>
                
<to>/mdeleteimage/catalog_product/save</to>
            </
mockett_mdeleteimage_adminhtml_catalog_product_save>
        </
rewrite>
    </global>
    <
admin>
        <
routers>
            <
mockett_mdeleteimage>
                <use>
admin</use>
                <
args>
                    <
module>Mockett_Mdeleteimage</module>
                    <
frontName>mdeleteimage</frontName>
                </
args>
            </
mockett_mdeleteimage>
        </
routers>
    </
admin>
</
config>

It works with one problem. Once I edit a product and click Save and Continue Editing, the rewrite comes into play and the url looks like this:

index.php/mdeleteimage/catalog_product/edit/id/4574/back/edit/tab/product_info_tabs_group_4/

I believe that this is breaking the layout (the product edit page now looks broken). I only meant to override the save action, now it looks like it is using my module for the edit action as well. How can I change the above redirect to only work for the save action?

 
Magento Community Magento Community
Magento Community
Magento Community
 
novon
Jr. Member
 
Total Posts:  13
Joined:  2008-09-26
 

I am able to fix the layout issue by changing the config.xml to look like this (notice the new rewrite rule):

<?xml version="1.0"?>
<config>
    <
modules>
        <
Mockett_Mdeleteimage>
            <
version>0.1.0</version>
        </
Mockett_Mdeleteimage>
    </
modules>
    <global>
        <
rewrite>
            <!-- 
This is an identifier for your rewrite that should be unique -->
            <!-- 
THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
            <
mockett_mdeleteimage_adminhtml_catalog_product>
                <
from><![CDATA[#^/mdeleteimage/catalog_product/#]]></from>
                
<to>/admin/catalog_product/</to>
            </
mockett_mdeleteimage_adminhtml_catalog_product>
            <
mockett_mdeleteimage_adminhtml_catalog_product_save>
                <
from><![CDATA[#^/admin/catalog_product/save/#]]></from>
                
<to>/mdeleteimage/catalog_product/save/</to>
            </
mockett_mdeleteimage_adminhtml_catalog_product_save>
        </
rewrite>
    </global>
    <
admin>
        <
routers>
            <
mockett_mdeleteimage>
                <use>
admin</use>
                <
args>
                    <
module>Mockett_Mdeleteimage</module>
                    <
frontName>mdeleteimage</frontName>
                </
args>
            </
mockett_mdeleteimage>
        </
routers>
    </
admin>
</
config>

However the url still shows my module being called. This is a problem because if I click just the Save button (instead of the Save and Continue Editing) I get taken to this url:

index.php/mdeleteimage/catalog_product/index/

This is bad because if I click in to edit another product I once again get something like this (which breaks the layout):

index.php/mdeleteimage/catalog_product/edit/id/4574/

 
Magento Community Magento Community
Magento Community
Magento Community
 
novon
Jr. Member
 
Total Posts:  13
Joined:  2008-09-26
 

Alright, I am able to fix the layout when clicking Save and Continue by adding this:

<mockett_mdeleteimage_catalog_product>
        <
update handle="admin_catalog_product"/>
    </
mockett_mdeleteimage_catalog_product>

to app/design/adminhtml/defualt/mockett/layout/main.xml. However, I am still trying to figure out how to call my module only for the save action instead of all actions.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top