From a3901e821ae7ce66f2ef948f482e8eadd3848bde Mon Sep 17 00:00:00 2001
From: Uma <uma.s@vtiger.com>
Date: Tue, 14 Apr 2020 20:08:35 +0530
Subject: [PATCH] Fixes #213 #216 #210 Supported vtiger evenhandler for
 Relating entites

---
 include/utils/utils.php                       |  7 ++++
 modules/Migration/schema/720_to_721.php       |  8 +++++
 .../Vtiger/handlers/RelateEntitesHandler.php  | 34 +++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 modules/Vtiger/handlers/RelateEntitesHandler.php

diff --git a/include/utils/utils.php b/include/utils/utils.php
index 0029639a1..ad9ec269e 100755
--- a/include/utils/utils.php
+++ b/include/utils/utils.php
@@ -1632,11 +1632,18 @@ function DeleteEntity($module,$return_module,$focus,$record,$return_id) {
  * Function to related two records of different entity types
   */
 function relateEntities($focus, $sourceModule, $sourceRecordId, $destinationModule, $destinationRecordIds) {
+    $db = PearDatabase::getInstance();
+    $em = new VTEventsManager($db);
+    $data = array('sourceModule'=>$sourceModule, 'sourceRecordId'=>$sourceRecordId,
+                  'destinationModule'=>$destinationModule,'destinationRecordIds'=>$destinationRecordIds);
+    $em->triggerEvent("vtiger.entity.beforerelate", $data);
 	if(!is_array($destinationRecordIds)) $destinationRecordIds = Array($destinationRecordIds);
 	foreach($destinationRecordIds as $destinationRecordId) {
 		$focus->save_related_module($sourceModule, $sourceRecordId, $destinationModule, $destinationRecordId);
 		$focus->trackLinkedInfo($sourceModule, $sourceRecordId, $destinationModule, $destinationRecordId);
 	}
+    
+    $em->triggerEvent("vtiger.entity.afterrelate", $data);
 }
 
 /**
diff --git a/modules/Migration/schema/720_to_721.php b/modules/Migration/schema/720_to_721.php
index 0e09a716a..4d156c7fa 100644
--- a/modules/Migration/schema/720_to_721.php
+++ b/modules/Migration/schema/720_to_721.php
@@ -137,4 +137,12 @@ if (defined('VTIGER_UPGRADE')) {
         createUserSharingPrivilegesfile($userId);
     }
     echo "Re-calculated user privilege and sharing privileges files";
+    
+    //Adding beforeRelate and afterRelate event handlers
+    $em = new VTEventsManager($db);
+	$em->registerHandler('vtiger.entity.beforerelate', 'modules/Vtiger/handlers/RelateEntitesHandler.php', 'RelateEntitesHandler');
+	echo '<br>Succecssfully added before relate handler<br>';
+    
+    $em->registerHandler('vtiger.entity.afterrelate', 'modules/Vtiger/handlers/RelateEntitesHandler.php', 'RelateEntitesHandler');
+	echo '<br>Succecssfully added before relate handler<br>';
 }
\ No newline at end of file
diff --git a/modules/Vtiger/handlers/RelateEntitesHandler.php b/modules/Vtiger/handlers/RelateEntitesHandler.php
new file mode 100644
index 000000000..5ec06dada
--- /dev/null
+++ b/modules/Vtiger/handlers/RelateEntitesHandler.php
@@ -0,0 +1,34 @@
+<?php
+/* +**********************************************************************************
+ * The contents of this file are subject to the vtiger CRM Public License Version 1.1
+ * ("License"); You may not use this file except in compliance with the License
+ * The Original Code is: vtiger CRM Open Source
+ * The Initial Developer of the Original Code is vtiger.
+ * Portions created by vtiger are Copyright (C) vtiger.
+ * All Rights Reserved.
+ * ***********************************************************************************/
+
+require_once 'include/events/VTEventHandler.inc';
+
+class RelateEntitesHandler extends VTEventHandler {
+
+	function handleEvent($eventName, $entityData) {
+        global $log;
+		$log->debug("Entering function RelateEntitesHandler ($eventName)");
+		if ($eventName == 'vtiger.entity.beforerelate') {
+            $log->debug("Calling function triggerBeforeRelationsHandler ($eventName)");
+			$this->triggerBeforeRelationsHandler($entityData);
+		} else if ($eventName == 'vtiger.entity.afterrelate') {
+            $log->debug("Calling function triggerAfterRelationHandler ($eventName)");
+			$this->triggerAfterRelationHandler($entityData);
+		}
+	}
+    
+    public function triggerBeforeRelationsHandler($entityData) {
+        return true;
+    }
+    
+    public function triggerAfterRelationHandler($entityData) {
+        return true;
+    }
+}
\ No newline at end of file
-- 
GitLab