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

added case triggers

parent ac23d891
No related branches found
No related tags found
No related merge requests found
......@@ -59,7 +59,6 @@ class CRM_Civirules_Trigger_Post extends CRM_Civirules_Trigger {
if (!in_array($op,$extensionConfig->getValidTriggerOperations())) {
return;
}
//find matching rules for this objectName and op
$triggers = CRM_Civirules_BAO_Rule::findRulesByObjectNameAndOp($objectName, $op);
foreach($triggers as $trigger) {
......
......@@ -88,4 +88,9 @@ class CRM_Civirules_Upgrader extends CRM_Civirules_Upgrader_Base {
CRM_Core_DAO::executeQuery("ALTER TABLE `civirule_rule_action` ADD COLUMN `ignore_condition_with_delay` TINYINT NULL default 0 AFTER `delay`");
return true;
}
public function upgrade_1005() {
CRM_Core_DAO::executeQuery("update `civirule_trigger` SET `class_name` = 'CRM_CivirulesPostTrigger_Case' where `object_name` = 'Case'");
return true;
}
}
\ No newline at end of file
<?php
class CRM_CivirulesPostTrigger_Case extends CRM_Civirules_Trigger_Post {
/**
* Returns an array of entities on which the trigger reacts
*
* @return CRM_Civirules_TriggerData_EntityDefinition
*/
protected function reactOnEntity() {
return new CRM_Civirules_TriggerData_EntityDefinition($this->objectName, $this->objectName, $this->getDaoClassName(), 'Case');
}
/**
* 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_Case_DAO_Case';
}
/**
* Trigger a rule for this trigger
*
* @param $op
* @param $objectName
* @param $objectId
* @param $objectRef
*/
public function triggerTrigger($op, $objectName, $objectId, $objectRef) {
$triggerData = $this->getTriggerDataFromPost($op, $objectName, $objectId, $objectRef);
//trigger for each client
$clients = CRM_Case_BAO_Case::getCaseClients($objectId);
foreach($clients as $client) {
$roleData = array();
$roleData['contact_id'] = $client;
$roleData['is_client'] = true;
$triggerData->setEntityData('CaseRole', $roleData);
$triggerData->setEntityData('Relationship', null);
CRM_Civirules_Engine::triggerRule($this, clone $triggerData);
}
//trigger for each case role
$relatedContacts = CRM_Case_BAO_Case::getRelatedContacts($objectId);
foreach($relatedContacts as $contact) {
$roleData = array();
$roleData['contact_id'] = $contact['contact_id'];
$roleData['is_client'] = false;
$triggerData->setEntityData('CaseRole', $roleData);
$relationshipData = null;
$relationship = new CRM_Contact_BAO_Relationship();
$relationship->contact_id_b = $contact['contact_id'];
$relationship->case_id = $objectId;
if ($relationship->find(true)) {
CRM_Core_DAO::storeValues($relationship, $relationshipData);
}
$triggerData->setEntityData('Relationship', null);
CRM_Civirules_Engine::triggerRule($this, clone $triggerData);
}
}
/**
* Returns an array of additional entities provided in this trigger
*
* @return array of CRM_Civirules_TriggerData_EntityDefinition
*/
protected function getAdditionalEntities() {
$entities = parent::getAdditionalEntities();
$entities[] = new CRM_Civirules_TriggerData_EntityDefinition('CaseRole', 'CaseRole', 'CRM_CivirulesPostTrigger_DataSpecification_CaseRole', 'CaseRole');
$entities[] = new CRM_Civirules_TriggerData_EntityDefinition('Relationship', 'Relationship', 'CRM_Contact_DAO_Relationship' , 'Relationship');
return $entities;
}
}
\ No newline at end of file
<?php
/**
* Helper class to specify structure of CaseRoles. Which consists of a case_id, contact_id and a role
* Class CRM_CivirulesPostTrigger_DAO_CaseRole
*/
class CRM_CivirulesPostTrigger_DataSpecification_CaseRole {
/**
* static instance to hold the field values
*
* @var array
*/
static $_fields = null;
/**
* static instance to hold the keys used in $_fields for each field.
*
* @var array
*/
static $_fieldKeys = null;
/**
* Returns all the column names of this table
*
* @return array
*/
static function &fields()
{
if (!(self::$_fields)) {
self::$_fields = array(
'contact_id' => array(
'name' => 'contact_id',
'type' => CRM_Utils_Type::T_INT,
'description' => 'Contact ID of contact record given case belongs to.',
'required' => true,
'FKClassName' => 'CRM_Contact_DAO_Contact',
),
'is_client' => array(
'name' => 'is_client',
'type' => CRM_Utils_Type::T_BOOLEAN,
),
);
}
return self::$_fields;
}
/**
* Returns an array containing, for each field, the arary key used for that
* field in self::$_fields.
*
* @return array
*/
static function &fieldKeys()
{
if (!(self::$_fieldKeys)) {
self::$_fieldKeys = array(
'contact_id' => 'contact_id',
'is_client' => 'is_client',
);
}
return self::$_fieldKeys;
}
}
\ No newline at end of file
......@@ -6,9 +6,9 @@ VALUES
('new_address', 'Address is added', 'Address', 'create', null, CURDATE(), 1),
('changed_address', 'Address is changed', 'Address', 'edit', null, CURDATE(), 1),
('deleted_address', 'Address is deleted', 'Address', 'delete', null, CURDATE(), 1),
('new_case', 'Case is added', 'Case', 'create', null, CURDATE(), 1),
('changed_case', 'Case is changed', 'Case', 'edit', null, CURDATE(), 1),
('deleted_case', 'Case is deleted', 'Case', 'delete', null, CURDATE(), 1),
('new_case', 'Case is added', 'Case', 'create', 'CRM_CivirulesPostTrigger_Case', CURDATE(), 1),
('changed_case', 'Case is changed', 'Case', 'edit', 'CRM_CivirulesPostTrigger_Case', CURDATE(), 1),
('deleted_case', 'Case is deleted', 'Case', 'delete', 'CRM_CivirulesPostTrigger_Case', CURDATE(), 1),
('new_contact', 'Contact of any type is added', 'Contact', 'create', 'CRM_CivirulesPostTrigger_Contact', CURDATE(), 1),
('changed_contact', 'Contact of any type is changed', 'Contact', 'edit', 'CRM_CivirulesPostTrigger_Contact', CURDATE(), 1),
('deleted_contact', 'Contact of any type is deleted', 'Contact', 'delete', 'CRM_CivirulesPostTrigger_Contact', CURDATE(), 1),
......
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