Skip to content
Snippets Groups Projects
Commit 8164001c authored by Jitendra Purohit's avatar Jitendra Purohit
Browse files

Fix entity to be triggerred without being the main entity in civirule

parent c5e62a19
No related branches found
No related tags found
No related merge requests found
......@@ -12,26 +12,31 @@ class CRM_CivirulesActions_Zapier_Trigger extends CRM_Civirules_Action {
*/
public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$params = $this->getActionParameters();
$entity = $triggerData->getEntity();
$entityParams = [
'Contact' => ['*', 'custom.*', 'address_primary.*', 'email_primary.*', 'phone_primary.*'],
'Membership' => ['*', 'custom.*', 'contact_id.*'],
'Participant' => ['*', 'custom.*', 'contact_id.*', 'event_id.*'],
];
if (isset($entityParams[$entity])) {
$entityData = $triggerData->getEntityData($entity);
$actionEntity = CRM_Zapier_Utils::getZapActionEntity($params['zap_trigger']);
$apiValues = civicrm_api4($entity, 'get', [
'select' => $entityParams[$entity],
if (isset($entityParams[$actionEntity])) {
$entityData = $triggerData->getEntityData($actionEntity);
if (empty($entityData['id'])) {
return;
}
$apiParams = [
'select' => $entityParams[$actionEntity],
'checkPermissions' => FALSE,
'where' => [
['id', '=', $entityData['id']],
],
])->first();
];
$apiValues = civicrm_api4($actionEntity, 'get', $apiParams)->first();
$hookURL = CRM_Zapier_Utils::getZapHook($params['zap_trigger']);
if (!empty($hookURL) && !empty($apiValues)) {
CRM_Zapier_Utils::triggerZap('POST', $hookURL, $apiValues);
CRM_Zapier_Utils::triggerZap($hookURL, $apiValues);
}
}
}
......
......@@ -6,16 +6,24 @@ class CRM_Zapier_Utils {
* Returns a list of zap options
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public static function getZapOptions() {
return [
'create_contact' => 'Create Contact',
'update_participant' => 'Update Participant',
'membership_created' => 'Create Membership',
'create_contact' => ts('Create Contact'),
'update_participant' => ts('Update Participant'),
'membership_created' => ts('Create Membership'),
];
}
public static function getZapActionEntity($name) {
$actions = [
'create_contact' => 'Contact',
'update_participant' => 'Participant',
'membership_created' => 'Membership',
];
return $actions[$name] ?? NULL;
}
/**
* Save hook URLs in the database.
*/
......@@ -56,18 +64,26 @@ class CRM_Zapier_Utils {
}
public static function unserialize_recursive($val) {
//$pattern = "/.*\{(.*)\}/";
if(self::is_serialized($val)){
// $pattern = "/.*\{(.*)\}/";
if (self::is_serialized($val)){
$val = trim($val);
$ret = unserialize($val);
if (is_array($ret)) {
foreach($ret as &$r) $r = self::unserialize_recursive($r);
foreach($ret as &$r) {
$r = self::unserialize_recursive($r);
}
}
return $ret;
} elseif (is_array($val)) {
foreach($val as &$r) $r = self::unserialize_recursive($r);
}
elseif (is_array($val)) {
foreach($val as &$r) {
$r = self::unserialize_recursive($r);
}
return $val;
}
else {
return $val;
} else { return $val; }
}
}
public static function is_serialized($val) {
......@@ -78,34 +94,19 @@ class CRM_Zapier_Utils {
return false;
}
/**
*
* @param $method
* @param $url
* @param $data
*/
public static function triggerZap($method, $url, $data = []) {
public static function triggerZap($url, $data = []) {
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
if (is_array($data)) {
$data = http_build_query($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
if (is_array($data)) {
$data = http_build_query($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_URL, $url);
......@@ -118,4 +119,4 @@ class CRM_Zapier_Utils {
return json_decode($result);
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment