Jump to content

Uninstall controller override made in a module


Recommended Posts

Hi,

 

I created a new custom module wich overrides some functions of AdminImportController.php (I set to public the private function copyImg)

 

The module structure is

 

/modules/mymodule

/modules/mymodule/override/controllers/admin/AdminImportController.php

 

Inside file AdminImportController.php I put this code:

<?php

class AdminImportController extends AdminImportControllerCore
{
    public static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
    {
        $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

        switch ($entity) {
            default:
            case 'products':
                $image_obj = new Image($id_image);
                $path = $image_obj->getPathForCreation();
            break;
            case 'categories':
                $path = _PS_CAT_IMG_DIR_.(int)$id_entity;
            break;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_.(int)$id_entity;
            break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
            break;
        }
        ...
        ....
        .....

When I install my module the ovverride will be correcty put in /override/controllers/admin/AdminImportController.php with this code at the top of the function:

<?php
class AdminImportController extends AdminImportControllerCore
{
	/*
    * module: impresaimport
    * date: 2017-08-10 09:07:23
    * version: 1.0.0
    */
    public static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
    {
        $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
        switch ($entity) {
            default:
            case 'products':
                $image_obj = new Image($id_image);
                $path = $image_obj->getPathForCreation();
            break;
            case 'categories':
                $path = _PS_CAT_IMG_DIR_.(int)$id_entity;
            break;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_.(int)$id_entity;
            break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
            break;
        }
        ...
        ....
        .....

As you can see the Prestashop core put this comments

/*
* module: impresaimport
* date: 2017-08-10 09:07:23
* version: 1.0.0
*/

..so the override is correcty "installed"

 

If I uninstall the module the core doesn't remove the override (I know that the core doesn't remove the file) and the overrided function contain my code (public funcion declaration instead the default private)

 

The uninstall function of the module perform this code

public function uninstall()
	{
		if (!parent::uninstall() ||
			!Configuration::deleteByName('IMPRESAIMPORT_PRODOTTI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_COMBINAZIONI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_CLIENTI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_PRODUTTORI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_CARATTERISTICHE') ||
			!Configuration::deleteByName('IMPRESAIMPORT_CORRELATI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_ORDINI') ||
			!Configuration::deleteByName('IMPRESAIMPORT_ALLEGATI')
		)
			return false;

		return true;
	}

Any suggestion?

 

Thank you

Edited by OfficeGroup (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

Hello Rho_Bur,

 

thank you for your reply. I delayed answering because I was on vacation.

 

I agree with what you said, in fact, it is dangerous to delete a file that could also be used by other modules.

 

In my case I just needed to release a private function. I have solved creating a new public function that calls the private one of the core. So I'm sure that my function, even with the module uninstalled, will not be used and will not leave any problems

/*
* module: impresaimport
* date: 2017-08-10 16:42:58
* version: 1.0.0
*/
public static function p_copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
{
	return parent::copyImg($id_entity, $id_image, $url, $entity, $regenerate);
}

This way even if the override is not removed I'm sure my functions do not cause problems to the standard

Edited by OfficeGroup (see edit history)
Link to comment
Share on other sites

another thought is to add logic in your override code, so that you first check to ensure that your module is installed and enabled before executing.  If not installed, or disabled, then you would skip your override code, and just execute the parent code.

 

This way, if PS fails to remove your override code, you atleast have a backup

Link to comment
Share on other sites

  • 1 year later...
On 8/31/2017 at 2:31 PM, bellini13 said:

another thought is to add logic in your override code, so that you first check to ensure that your module is installed and enabled before executing.  If not installed, or disabled, then you would skip your override code, and just execute the parent code.

 

This way, if PS fails to remove your override code, you atleast have a backup

that i a good idea, i've tried to identify if the module is active and not running the override but i was not able to accomplish it

 

my ProductController.php override is:

 

<?php

class ProductController extends ProductControllerCore
{


    /**
     * Initialize product controller
     * @see FrontController::init()
     */
    public function init()
    {
        parent::init();

        if ($id_product = (int)Tools::getValue('id_product')) {
            $this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);
        }
		
		...
		...

    }
}

i tried to place inside the init() function the check:

 

if (Module::isEnabled('my_module_name') 

buit if the module is disabled all the init() in the override produce a white page so where i need to put the check so if the module is disabled/uninstalled the override is not executed?

 

Thanks

Edited by ANGELO Vintage (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...