/** * Method processAction to execute the action * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @access public * */ public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) { $contactId = $triggerData->getContactId(); $processContact = false; // retrieve contact type of contact $contactParams = array('id' => $contactId, 'return' => 'contact_type'); $contactType = civicrm_api3('Contact', 'Getvalue', $contactParams); // retrieve contact type Donor and only execute if the same $donorType = civicrm_api3('ContactType', 'Getsingle', array('name' => 'Donor')); try { switch ($contactType) { case 'Individual': if ($donorType['parent_id'] = 1) { $processContact = true; } break; case 'Household': if ($donorType['parent_id'] = 2) { $processContact = true; } break; case 'Organization': if ($donorType['parent_id'] = 3) { $processContact = true; } break; } } catch (CiviCRM_API3_Exception $ex) { } if ($processContact) { $newParams = array('id' => $contactId, 'contact_sub_type' => 'Donor'); civicrm_api3('Contact', 'Create', $newParams); } }
/** * Returns the value of the field for the condition * For example: I want to check if age > 50, this function would return the 50 * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return * @access protected * @abstract */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $entity = $this->conditionParams['entity']; $field = $this->conditionParams['field']; $data = $triggerData->getEntityData($entity); if (isset($data[$field])) { return $this->normalizeValue($data[$field]); } if (strpos($field, 'custom_') === 0) { $custom_field_id = str_replace("custom_", "", $field); try { $params['entityID'] = $data['id']; $params[$field] = 1; $values = CRM_Core_BAO_CustomValueTable::getValues($params); if (!empty($values[$field])) { return $this->normalizeValue($values[$field]); } elseif (!empty($values['error_message'])) { $custom_values = $triggerData->getCustomFieldValues($custom_field_id); if (!empty($custom_values)) { return $this->normalizeValue(reset($custom_values)); } } } catch (Exception $e) { //do nothing } } return null; }
/** * Returns an array with parameters used for processing an action * * @param array $params * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return array $params * @access protected */ protected function alterApiParameters($params, CRM_Civirules_TriggerData_TriggerData $triggerData) { $action_params = $this->getActionParameters(); //this function could be overridden in subclasses to alter parameters to meet certain criteraia $params['target_contact_id'] = $triggerData->getContactId(); $params['activity_type_id'] = $action_params['activity_type_id']; $params['status_id'] = $action_params['status_id']; $params['subject'] = $action_params['subject']; if (!empty($action_params['assignee_contact_id'])) { $assignee = array(); if (is_array($action_params['assignee_contact_id'])) { foreach ($action_params['assignee_contact_id'] as $contact_id) { if ($contact_id) { $assignee[] = $contact_id; } } } else { $assignee[] = $action_params['assignee_contact_id']; } if (count($assignee)) { $params['assignee_contact_id'] = $action_params['assignee_contact_id']; } else { $params['assignee_contact_id'] = ''; } } return $params; }
/** * Method is mandatory and checks if the condition is met * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $contactId = $triggerData->getContactId(); $donorHasAny = FALSE; $recurringParams = array('contact_id' => $contactId, 'is_test' => 0); try { $foundRecurring = civicrm_api3('ContributionRecur', 'Get', $recurringParams); foreach ($foundRecurring['values'] as $recurring) { if (CRM_Civirules_Utils::endDateLaterThanToday($recurring['end_date']) == TRUE || !isset($recurring['end_date'])) { $donorHasAny = TRUE; } } if ($donorHasAny) { if ($this->conditionParams['has_recurring']) { $isConditionValid = TRUE; } else { $isConditionValid = FALSE; } } else { if ($this->conditionParams['has_recurring']) { $isConditionValid = FALSE; } else { $isConditionValid = TRUE; } } } catch (CiviCRM_API3_Exception $ex) { if ($this->conditionParams['has_recurring']) { $isConditionValid = FALSE; } else { $isConditionValid = TRUE; } } return $isConditionValid; }
/** * Returns an array with parameters used for processing an action * * @param array $params * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return array $params * @access protected */ protected function alterApiParameters($params, CRM_Civirules_TriggerData_TriggerData $triggerData) { //this function could be overridden in subclasses to alter parameters to meet certain criteraia $params['entity_id'] = $triggerData->getContactId(); $params['entity_table'] = 'civicrm_contact'; return $params; }
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $groupContact = $triggerData->getEntityData('GroupContact'); if ($groupContact['group_id'] == $this->conditionParams['group_id']) { return true; } return false; }
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $contribution = $triggerData->getEntityData('Contribution'); if ($contribution['contribution_status_id'] == $this->conditionParams['contribution_status_id']) { return true; } return false; }
/** * Method to check if the condition is valid, will check if the contact * has an activity of the selected type * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $ActivityContact = $triggerData->getEntityData('ActivityContact'); if ($ActivityContact['record_type_id'] == $this->conditionParams['record_type_id']) { return true; } return false; }
/** * Method to check if the condition is valid, will check if the contact * has an activity of the selected type * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $activity = $triggerData->getEntityData('Activity'); if ($activity['activity_type_id'] == $this->conditionParams['activity_type_id']) { return true; } return false; }
public static function addCustomDataToTriggerData(CRM_Civirules_TriggerData_TriggerData $triggerData) { foreach (self::$customValues as $field_id => $values) { foreach ($values as $id => $value) { $triggerData->setCustomFieldValue($field_id, $id, $value); } } }
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $relationship = $triggerData->getEntityData('Relationship'); if (empty($relationship)) { return true; } return false; }
/** * Returns the value of the field for the condition * For example: I want to check if age > 50, this function would return the 50 * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return mixed * @access protected */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $field = 'status_id'; $data = $triggerData->getEntityData('Case'); if (isset($data[$field])) { return $data[$field]; } return null; }
/** * Method processAction to execute the action * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @access public * */ public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) { $contactId = $triggerData->getContactId(); //we cannot delete domain contacts if (CRM_Contact_BAO_Contact::checkDomainContact($contactId)) { return; } CRM_Contact_BAO_Contact::deleteContact($contactId); }
/** * Returns value of the field * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return mixed * @access protected */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $contribution = $triggerData->getEntityData('Contribution'); if (isset($contribution['total_amount'])) { return (double) $contribution['total_amount']; } return (double) 0.0; //undefined birth date }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $contact_id = $triggerData->getContactId(); /* * retrieve count of contributions for donor grouped by extracted YMD from receive_date */ $query = 'SELECT COUNT(DISTINCT CONCAT(EXTRACT(YEAR FROM receive_date), EXTRACT(MONTH FROM receive_date), EXTRACT(DAY FROM receive_date))) AS distinctDates FROM civicrm_contribution WHERE contact_id = %1 AND civicrm_contribution.contribution_status_id = %2'; $params = array(1 => array($contact_id, 'Positive'), 2 => array(CRM_Civirules_Utils::getContributionStatusIdWithName('Completed'), 'Positive')); $periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams); $periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams); if ($periodStartDate) { $query .= " AND DATE(`receive_date`) >= '" . $periodStartDate->format('Y-m-d') . "'"; } if ($periodEndDate) { $query .= " AND DATE(`receive_date`) <= '" . $periodEndDate->format('Y-m-d') . "'"; } $dao = CRM_Core_DAO::executeQuery($query, $params); if ($dao->fetch()) { switch ($this->conditionParams['operator']) { case 1: if ($dao->distinctDates != $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; case 2: if ($dao->distinctDates > $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; case 3: if ($dao->distinctDates >= $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; case 4: if ($dao->distinctDates < $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; case 5: if ($dao->distinctDates <= $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; default: if ($dao->distinctDates == $this->conditionParams['no_of_days']) { $isConditionValid = TRUE; } break; } } return $isConditionValid; }
/** * Returns value of the field * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return mixed * @access protected */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $sourceBirthDate = civicrm_api3('Contact', 'getvalue', array('id' => $triggerData->getContactId(), 'return' => 'birth_date')); if ($sourceBirthDate) { $birthDate = new DateTime($sourceBirthDate); return $birthDate->diff(new DateTime('now'))->y; } return false; //undefined birth date }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $recurring = $triggerData->getEntityData('ContributionRecur'); if ($this->conditionParams['end_date'] == 0 && empty($recurring['end_date'])) { $isConditionValid = TRUE; } if ($this->conditionParams['end_date'] == 1 && !empty($recurring['end_date'])) { $isConditionValid = TRUE; } return $isConditionValid; }
/** * Returns an array with parameters used for processing an action * * @param array $params * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return array $params * @access protected */ protected function alterApiParameters($params, CRM_Civirules_TriggerData_TriggerData $triggerData) { $action_params = $this->getActionParameters(); //this function could be overridden in subclasses to alter parameters to meet certain criteraia $params['target_contact_id'] = $triggerData->getContactId(); $params['activity_type_id'] = $action_params['activity_type_id']; $params['status_id'] = $action_params['status_id']; $params['subject'] = $action_params['subject']; if (!empty($action_params['assignee_contact_id'])) { $params['assignee_contact_id'] = $action_params['assignee_contact_id']; } return $params; }
protected function getBodyParams(CRM_Civirules_TriggerData_TriggerData $triggerData) { $params = $this->getActionParameters(); $bodyParams = explode("&", $params['request_body']); $return = array(); foreach ($bodyParams as $bodyParam) { list($k, $v) = explode("=", $bodyParam); if (!empty($k)) { $return[$k] = trim($this->replaceTokens($v, $triggerData->getContactId())); } } return $return; }
/** * This method returns true or false when an condition is valid or not * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public * @abstract */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $phoneType = $this->conditionParams['phone_type']; $apiParams['contact_id'] = $triggerData->getContactId(); if ($phoneType) { $apiParams['phone_type_id'] = $phoneType; } $count = civicrm_api3('Phone', 'getCount', $apiParams); if ($count) { return true; } return false; }
/** * Returns value of the field * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return mixed * @access protected */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $completed_status_id = CRM_Core_OptionGroup::getValue('contribution_status', 'completed', 'name'); $contact_id = $triggerData->getContactId(); $params[1] = array($completed_status_id, 'Integer'); $params[2] = array($contact_id, 'Integer'); $last_date = CRM_Core_DAO::singleValueQuery("SELECT MAX(`receive_date`) FROM `civicrm_contribution` WHERE `contribution_status_id` = %1 AND `contact_id` = %2", $params); if ($last_date) { $last_date = new DateTime($last_date); return $last_date->diff(new DateTime('now'))->days; } return false; //undefined contribution date }
public static function logError($reason, $original_error, CRM_Civirules_TriggerData_TriggerData $triggerData, $context = array()) { $logger = CRM_Civirules_Utils_LoggerFactory::getLogger(); if (empty($logger)) { return; } $error = "Rule: '{rule_title}' with id {rule_id} failed for contact {contact_id} because of {reason}"; $context['rule_id'] = $triggerData->getTrigger()->getRuleId(); $context['rule_title'] = $triggerData->getTrigger()->getRuleTitle(); $context['original_error'] = $original_error; $context['contact_id'] = $triggerData->getContactId(); $context['reason'] = $reason; $logger->error($error, $context); }
public function __construct($contactId, $entity, $data) { parent::__construct(); $this->entity = $entity; $this->contact_id = $contactId; $this->setEntityData($entity, $data); }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $contribution = $triggerData->getEntityData('Contribution'); /* * retrieve count of completed contributions for donor where recurring_contribution_id is not empty */ $query = 'SELECT COUNT(*) AS recurringContributions FROM civicrm_contribution WHERE contact_id = %1 AND civicrm_contribution.contribution_recur_id > %2 AND contribution_status_id = %3'; $params = array(1 => array($contribution['contact_id'], 'Positive'), 2 => array(0, 'Positive'), 3 => array(CRM_Civirules_Utils::getContributionStatusIdWithName('Completed'), 'String')); $dao = CRM_Core_DAO::executeQuery($query, $params); if ($dao->fetch()) { switch ($this->conditionParams['operator']) { case 1: if ($dao->recurringContributions != $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; case 2: if ($dao->recurringContributions > $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; case 3: if ($dao->recurringContributions >= $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; case 4: if ($dao->recurringContributions < $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; case 5: if ($dao->recurringContributions <= $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; default: if ($dao->recurringContributions == $this->conditionParams['no_of_recurring']) { $isConditionValid = TRUE; } break; } } return $isConditionValid; }
/** * This method returns true or false when an condition is valid or not * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public * @abstract */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = false; $contact_id = $triggerData->getContactId(); switch ($this->conditionParams['operator']) { case 'in one of': $isConditionValid = $this->contactHasOneOfTags($contact_id, $this->conditionParams['tag_ids']); break; case 'in all of': $isConditionValid = $this->contactHasAllTags($contact_id, $this->conditionParams['tag_ids']); break; case 'not in': $isConditionValid = $this->contactHasNotTag($contact_id, $this->conditionParams['tag_ids']); break; } return $isConditionValid; }
/** * This method returns true or false when an condition is valid or not * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool * @access public * @abstract */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = false; $contact_id = $triggerData->getContactId(); switch ($this->conditionParams['operator']) { case 'in one of': $isConditionValid = $this->contactIsMemberOfOneGroup($contact_id, $this->conditionParams['group_ids']); break; case 'in all of': $isConditionValid = $this->contactIsMemberOfAllGroups($contact_id, $this->conditionParams['group_ids']); break; case 'not in': $isConditionValid = $this->contactIsNotMemberOfGroup($contact_id, $this->conditionParams['group_ids']); break; } return $isConditionValid; }
/** * Returns value of the field * * @param object CRM_Civirules_TriggerData_TriggerData $triggerData * @return mixed * @access protected */ protected function getFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) { $completed_status_id = CRM_Core_OptionGroup::getValue('contribution_status', 'completed', 'name'); $contact_id = $triggerData->getContactId(); $sql = "SELECT SUM(`total_amount`)\n FROM `civicrm_contribution`\n WHERE `contribution_status_id` = %1 AND `contact_id` = %2"; $params[1] = array($completed_status_id, 'Integer'); $params[2] = array($contact_id, 'Integer'); $periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams); $periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams); if ($periodStartDate) { $sql .= " AND DATE(`receive_date`) >= '" . $periodStartDate->format('Y-m-d') . "'"; } if ($periodEndDate) { $sql .= " AND DATE(`receive_date`) <= '" . $periodEndDate->format('Y-m-d') . "'"; } $total_amount = (double) CRM_Core_DAO::singleValueQuery($sql, $params); return $total_amount; }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $membership = $triggerData->getEntityData('Membership'); switch ($this->conditionParams['operator']) { case 0: if ($membership['membership_type_id'] == $this->conditionParams['membership_type_id']) { $isConditionValid = TRUE; } break; case 1: if ($membership['membership_type_id'] != $this->conditionParams['membership_type_id']) { $isConditionValid = TRUE; } break; } return $isConditionValid; }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $contribution = $triggerData->getEntityData('Contribution'); switch ($this->conditionParams['operator']) { case 0: if ($contribution['financial_type_id'] == $this->conditionParams['financial_type_id']) { $isConditionValid = TRUE; } break; case 1: if ($contribution['financial_type_id'] != $this->conditionParams['financial_type_id']) { $isConditionValid = TRUE; } break; } return $isConditionValid; }
/** * Method to determine if the condition is valid * * @param CRM_Civirules_TriggerData_TriggerData $triggerData * @return bool */ public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) { $isConditionValid = FALSE; $this->buildWhereClauses($triggerData->getEntityData('Contribution')); if (!empty($this->whereClauses)) { $query = 'SELECT COUNT(*) as countContributions FROM civicrm_contribution WHERE ' . implode(' AND ', $this->whereClauses); $dao = CRM_Core_DAO::executeQuery($query, $this->whereParams); if ($dao->fetch()) { switch ($this->conditionParams['count_operator']) { case 1: if ($dao->countContributions != $this->conditionParams['no_of_contributions']) { $isConditionValid = TRUE; } break; case 2: if ($dao->countContributions > $this->conditionParams['no_of_contributions']) { $isConditionValid = TRUE; } break; case 3: if ($dao->countContributions >= $this->conditionParams['no_of_contributions']) { $isConditionValid = TRUE; } break; case 4: if ($dao->countContributions < $this->conditionParams['no_of_contributions']) { $isConditionValid = TRUE; } break; case 5: if ($dao->countContributions <= $this->conditionParams['no_of_contributions']) { $isConditionValid = TRUE; } break; default: if ($dao->countContributions == $this->conditionParams['no_of_contribution']) { $isConditionValid = TRUE; } break; } } } return $isConditionValid; }