Skip to content
Snippets Groups Projects
Unverified Commit 531c72d1 authored by Jaap Jansma's avatar Jaap Jansma Committed by GitHub
Browse files

Merge pull request #205 from eyeonall/feature/custom_field_trigger_change

make custom fields accessible for field change comparison
parents 44a2a08f 60393dba
No related branches found
No related tags found
No related merge requests found
......@@ -102,6 +102,26 @@ abstract class CRM_Civirules_TriggerData_TriggerData {
return array();
}
/**
* Returns an array of custom fields in param format
*
* @param $custom_field_id
* @return array
*/
public function getEntityCustomData() {
$customFields = array();
if ( ! isset($this->custom_data) ) {
return $customFields;
} elseif ( ! is_array($this->custom_data) ) {
return $customFields;
}
foreach ($this->custom_data as $custom_field_id => $custom_field_value ) {
$customFields['custom_' . $custom_field_id] = $this->getCustomFieldValue($custom_field_id);
}
return $customFields;
}
/**
* Sets data for an entity
*
......@@ -155,4 +175,5 @@ abstract class CRM_Civirules_TriggerData_TriggerData {
}
return null;
}
}
\ No newline at end of file
......@@ -53,6 +53,22 @@ class CRM_Civirules_Utils_PreData {
} catch (Exception $e) {
return;
}
// add custom data fields
try {
$customData = civicrm_api3('CustomValue', 'get', array(
'sequential' => 1,
'entity_id' => $id,
'entity_table' => ucfirst($entity),
));
} catch (Exception $e ) {
$customData = array();
}
if ( empty($customData['is_error']) && ! empty($customData['count']) ) {
foreach ($customData['values'] as $customField ) {
$data['custom_' . $customField['id']] = $customField['latest'];
}
}
self::setPreData($entity, $id, $data);
}
......
......@@ -38,16 +38,19 @@ abstract class CRM_CivirulesConditions_Generic_FieldChanged extends CRM_Civirule
if (!$triggerData instanceof CRM_Civirules_TriggerData_Interface_OriginalData) {
return false;
}
$entity = $this->getEntity();
if ($entity != $triggerData->getOriginalEntity()) {
if ( strtolower($entity) != strtolower($triggerData->getOriginalEntity()) ) {
return false;
}
// we need to check to see if the data being submitted actually contains the field we are comparing. if not, return false, no change
if ( ! array_key_exists($this->getField(), $triggerData->getEntityData($entity)) ) {
$compareField = $this->getField();
$compareEntityData = $triggerData->getEntityData($entity);
$compareEntityCustomData = $triggerData->getEntityCustomData();
if ( array_key_exists($compareField, $compareEntityData) || array_key_exists($compareField, $compareEntityCustomData) ) {
$fieldData = $this->getFieldData($triggerData);
} else {
return false;
}
$fieldData = $this->getFieldData($triggerData);
$originalData = $this->getOriginalFieldData($triggerData);
if (empty($fieldData) && empty($originalData)) {
......@@ -97,10 +100,17 @@ abstract class CRM_CivirulesConditions_Generic_FieldChanged extends CRM_Civirule
protected function getFieldData(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$entity = $this->getEntity();
$data = $triggerData->getEntityData($entity);
$field = $this->getField();
if (isset($data[$field])) {
return $this->transformFieldData($data[$field]);
}
$customFieldId = str_replace("custom_", '', $field, $customField);
if ( $customField ) {
$value = $triggerData->getCustomFieldValue($customFieldId);
return $this->transformFieldData($value);
}
return null;
}
......@@ -113,7 +123,7 @@ abstract class CRM_CivirulesConditions_Generic_FieldChanged extends CRM_Civirule
*/
protected function getOriginalFieldData(CRM_Civirules_TriggerData_Interface_OriginalData $triggerData) {
$entity = $this->getEntity();
if ($triggerData->getOriginalEntity() != $entity) {
if ( strtolower($triggerData->getOriginalEntity()) != strtolower($entity) ) {
return null;
}
......
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