Skip to content
Snippets Groups Projects
Commit b7cb6bbc authored by jaapjansma's avatar jaapjansma
Browse files

fixed issue with GroupContact event

parent 053dcb51
Branches
Tags
No related merge requests found
......@@ -44,8 +44,6 @@ class CRM_Civirules_Event_Post extends CRM_Civirules_Event {
return $daoClassName;
}
/**
* Method post
*
......@@ -62,6 +60,41 @@ class CRM_Civirules_Event_Post extends CRM_Civirules_Event {
return;
}
//find matching rules for this objectName and op
$events = CRM_Civirules_BAO_Rule::findRulesByObjectNameAndOp($objectName, $op);
foreach($events as $event) {
if ($event instanceof CRM_Civirules_Event_Post) {
$event->triggerEvent($op, $objectName, $objectId, $objectRef);
}
}
}
/**
* Trigger a rule for this event
*
* @param $op
* @param $objectName
* @param $objectId
* @param $objectRef
*/
public function triggerEvent($op, $objectName, $objectId, $objectRef) {
$eventData = $this->getEventDataFromPost($op, $objectName, $objectId, $objectRef);
CRM_Civirules_Engine::triggerRule($this, clone $eventData);
}
/**
* Get event data belonging to this specific post event
*
* Sub classes could override this method. E.g. a post on GroupContact doesn't give on object of GroupContact
* it rather gives an array with contact Id's
*
* @param $op
* @param $objectName
* @param $objectId
* @param $objectRef
* @return CRM_Civirules_EventData_Edit|CRM_Civirules_EventData_Post
*/
protected function getEventDataFromPost($op, $objectName, $objectId, $objectRef) {
$entity = CRM_Civirules_Utils_ObjectName::convertToEntity($objectName);
//set data
......@@ -79,12 +112,7 @@ class CRM_Civirules_Event_Post extends CRM_Civirules_Event {
} else {
$eventData = new CRM_Civirules_EventData_Post($entity, $objectId, $data);
}
//find matching rules for this objectName and op
$events = CRM_Civirules_BAO_Rule::findRulesByObjectNameAndOp($objectName, $op);
foreach($events as $event) {
CRM_Civirules_Engine::triggerRule($event, clone $eventData);
}
return $eventData;
}
......
......@@ -139,7 +139,7 @@ class CRM_Civirules_Form_RuleCondition extends CRM_Core_Form {
foreach($eventObject->getProvidedEntities() as $entityDef) {
$availableEntities[] = strtolower($entityDef->entity);
}
foreach($requiredEntities as $entity) {
if (!in_array(strtolower($entity), $availableEntities)) {
$errors['rule_condition_select'] = ts('This condition is not available with event %1', array(1 => $event->label));
......
......@@ -59,9 +59,21 @@ abstract class CRM_CivirulesActions_Generic_Api extends CRM_Civirules_Action {
* @param $action
* @param $parameters
* @access protected
* @throws Exception on api error
*/
protected function executeApiAction($entity, $action, $parameters) {
civicrm_api3($entity, $action, $parameters);
try {
civicrm_api3($entity, $action, $parameters);
} catch (Exception $e) {
$formattedParams = '';
foreach($parameters as $key => $param) {
if (strlen($formattedParams)) {
$formattedParams .= ', ';
}
$formattedParams .= $key.' = '.$param;
}
throw new Exception('Civirules api action exception '.$entity.'.'.$action.' ('.$formattedParams.')');
}
}
}
\ No newline at end of file
<?php
class CRM_CivirulesPostEvent_GroupContact extends CRM_Civirules_Event_Post {
/**
* Returns an array of entities on which the event reacts
*
* @return CRM_Civirules_EventData_EntityDefinition
*/
protected function reactOnEntity() {
return new CRM_Civirules_EventData_EntityDefinition($this->objectName, $this->objectName, $this->getDaoClassName(), 'GroupContact');
}
/**
* Return the name of the DAO Class. If a dao class does not exist return an empty value
*
* @return string
*/
protected function getDaoClassName() {
return 'CRM_Contact_DAO_GroupContact';
}
/**
* Trigger a rule for this event
*
* @param $op
* @param $objectName
* @param $objectId
* @param $objectRef
*/
public function triggerEvent($op, $objectName, $objectId, $objectRef) {
//in case of GroupContact $objectRef consist of an array of contactIds
//so convert this array to group contact objects
//we do this by a query on the group_contact table to retrieve the latest records for this group and contact
$sql = "SELECT MAX(`id`), `group_id`, `contact_id`, `status`, `location_id`, `email_id`
FROM `civicrm_group_contact`
WHERE `group_id` = %1 AND `contact_id` IN (".implode(", ", $objectRef).")
GROUP BY `contact_id`";
$params[1] = array($objectId, 'Integer');
$dao = CRM_Core_DAO::executeQuery($sql, $params, true, 'CRM_Contact_DAO_GroupContact');
while ($dao->fetch()) {
$data = array();
CRM_Core_DAO::storeValues($dao, $data);
$eventData = $this->getEventDataFromPost($op, $objectName, $objectId, $data);
CRM_Civirules_Engine::triggerRule($this, clone $eventData);
}
}
}
\ No newline at end of file
......@@ -52,8 +52,8 @@ VALUES
('new_group', 'Group is added', 'Group', 'create', null, CURDATE(), 1),
('changed_group', 'Group is changed', 'Group', 'edit', null, CURDATE(), 1),
('deleted_group', 'Group is deleted', 'Group', 'delete', null, CURDATE(), 1),
('new_group_contact', 'Contact is added to Group', 'GroupContact', 'create', null, CURDATE(), 1),
('deleted_group_contact', 'Contact is removed from Group', 'GroupContact', 'delete', null, CURDATE(), 1),
('new_group_contact', 'Contact is added to Group', 'GroupContact', 'create', 'CRM_CivirulesPostEvent_GroupContact', CURDATE(), 1),
('deleted_group_contact', 'Contact is removed from Group', 'GroupContact', 'delete', 'CRM_CivirulesPostEvent_GroupContact', CURDATE(), 1),
('new_membership', 'Membership is added', 'Membership', 'create', null, CURDATE(), 1),
('changed_membership', 'Membership is changed', 'Membership', 'edit', null, CURDATE(), 1),
('deleted_membership', 'Membership is deleted', 'Membership', 'delete', null, CURDATE(), 1),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment