Jump to content

Module Reset How to code


El Patron

Recommended Posts

When coding a module I've accounted for all Prestashop action buttons in back office-->modules-->my module, i.e. enable/disable/install

 

but what about reset? I suppose our modules get control when the reset action is clicked yes?

 

 

   public function reset()
    {
    if (!parent::reset())
	    return false;
    if( !$this->resetCode()){
	    return false;
    }	   
    return Module::enableByName($this->name);
    }

 

I did not see anyone using reset on a search (function reset())

 

Thanks in advance

Link to comment
Share on other sites

  • 8 years later...

Hi, i know this is an old thread but since i have the same question i think its ok. My reset() function in a module is not being called after reseting it from the module manager in the BO.

I'm using version 1.7.6.

This is my reset function:

public function reset()
    {
        return $this->uninstall(false) && $this->install(false);
    }

 

Now, i believe that the code that handles this is the one in the ModuleManager($keep_data = false by default):

public function reset($name, $keep_data = false)
    {
        if (!$this->adminModuleProvider->isAllowedAccess('install') || !$this->adminModuleProvider->isAllowedAccess('uninstall', $name)) {
            throw new Exception(
                $this->translator->trans(
                    'You are not allowed to reset the module %module%.',
                    array(
                        '%module%' => $name,
                    ),
                    'Admin.Modules.Notification'
                )
            );
        }

        $this->checkIsInstalled($name);

        $module = $this->moduleRepository->getModule($name);

        try {
            if ((bool) $keep_data && method_exists($module->getInstance(), 'reset')) {
                $this->dispatch(ModuleManagementEvent::UNINSTALL, $module);
                $status = $module->onReset();
                $this->dispatch(ModuleManagementEvent::INSTALL, $module);
            } else {
                $status = ($this->uninstall($name) && $this->install($name));
            }
        } catch (Exception $e) {
            throw new Exception(
                $this->translator->trans(
                    'Error when resetting module %module%. %error_details%',
                    array(
                        '%module%' => $name,
                        '%error_details%' => $e->getMessage(),
                    ),
                    'Admin.Modules.Notification'
                ),
                0,
                $e
            );
        }

        return $status;
    }

 

But i can't find a way to pass $keep_data = true to this one. Didn't find an option in the BO to keep the configurations.

Can anyone please point me in the right direction?

 

Link to comment
Share on other sites

  • 2 years later...

v 1.7.2.4
I need reset for register new hooks in dev process. 
You can use this in your module: (maybe, it legacy variant)

	public function onclickOption($opt, $href)
	{
		if ($opt == 'reset') {
			return	$this->uninstall(false) && $this->install(false);
		}
	}

How it works? In classes/module/Module.php:1399 (vendor code)

$item->onclick_option = method_exists($module, 'onclickOption') ? true : false;

if ($item->onclick_option) {
	$href = Context::getContext()->link->getAdminLink('Module', true) . '&module_name=' . $tmp_module->name . '&tab_module=' . $tmp_module->tab;
	$item->onclick_option_content = array();
	$option_tab = array('desactive', 'reset', 'configure', 'delete');

	foreach ($option_tab as $opt) {
		$item->onclick_option_content[$opt] = $tmp_module->onclickOption($opt, $href);
	}
}


And you can use function reset();
 

 

Edited by Rijen (see edit history)
  • Like 1
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...