How can i unregisterHandler (BeforeSave and AfterSave) using PHP Script (Vtlib Function) from my CRM vtiger 7

151 Views Asked by At

I hope all of you are doing well. I have Register Handler(Before Save and After Save) used this script.we have run this script in Modules/CustomExtension/CustomExtension.php. Handler register are working fine and create Handler table also perfect.

    static function register_eventhandler() 
    {
    global $adb;
    $moduleInstance = Vtiger_Module::getInstance('Modules Name');
                //Here HCacf Custom Extension Name
                        //Before Save           
    Vtiger_Event::register($moduleInstance,'vtiger.entity.beforesave',
    'HCacfHandler','modules/HCacf/HCacfHandler.php');
                            // After Save
    Vtiger_Event::register($moduleInstance,'vtiger.entity.aftersave',
    'HCacfHandler','modules/HCacf/HCacfHandler.php'); 
2

There are 2 best solutions below

0
Ruben Estrada On

You would have to do a query for that. something like this should work:


static public function unregisterHandler($handlerPath)
{
    $adb = PearDatabse::getInstance();
    $adb->pquery("DELETE FROM vtiger_eventhandlers WHERE handler_path=?", [$handlerPath]);
}

In this case we should assume that handler_path is unique in the vtiger_eventhandlers table, which in practice, is not:

SHOW CREATE TABLE vtiger_eventhandlers\G

#result:
CREATE TABLE `vtiger_eventhandlers` (
  `eventhandler_id` int(11) NOT NULL AUTO_INCREMENT,
  `event_name` varchar(100) NOT NULL,
  `handler_path` varchar(400) NOT NULL,
  `handler_class` varchar(100) NOT NULL,
  `cond` text,
  `is_active` int(1) NOT NULL,
  `dependent_on` varchar(255) DEFAULT '[]',
  PRIMARY KEY (`eventhandler_id`,`event_name`,`handler_class`),
  UNIQUE KEY `eventhandler_idx` (`eventhandler_id`)
) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8

But you could add further conditions to your WHERE clause as you see necessary

0
Milan Malani On

It looks like you are trying to create a custom module and want to add event handlers on Importing module and remove those handlers when your custom module is disabled.

You can add code like this in your Custom module class file "Modules/CustomExtension/CustomExtension.php"

function vtlib_handler($modulename, $event_type) {
     if ($event_type == 'module.postinstall') {
        $this->register_eventhandler();
    } else if ($event_type == 'module.disabled') {
        $this->unregister_eventHandler();
    } else if ($event_type == 'module.enabled') {
        $this->register_eventhandler();
    } else if ($event_type == 'module.preuninstall') {
        // TODO Handle actions before this module is uninstalled.
    } else if ($event_type == 'module.preupdate') {
        // TODO Handle actions before this module is updated.
    } else if ($event_type == 'module.postupdate') {
        $this->registerLookupEvents();
        // TODO Handle actions before this module is updated.
    }
}

/**
 * To unregister phone lookup events 
 */
function register_eventhandler(){
    global $log;
    $adb = PearDatabase::getInstance();
    $EventManager = new VTEventsManager($adb);
    $className = 'HandlerClassName';
    $EventManager->unregisterHandler($className);
    $log->fatal('Lookup Events Unregistered');
}

You can refer to the other module events to manage your handlers.