diff --git a/CRM/CivirulesConditions/Contribution/LastContribution.mgd.php b/CRM/CivirulesConditions/Contribution/LastContribution.mgd.php new file mode 100644 index 0000000000000000000000000000000000000000..da1c5d6c86e5d7d2ad266e6ac730e719f1d0c8e8 --- /dev/null +++ b/CRM/CivirulesConditions/Contribution/LastContribution.mgd.php @@ -0,0 +1,17 @@ +<?php + +return array ( + 0 => + array ( + 'name' => 'Civirules:Condition.LastContribution', + 'entity' => 'CiviRuleCondition', + 'params' => + array ( + 'version' => 3, + 'name' => 'last_contribution_of_contact', + 'label' => 'Last Contribution of a Contact', + 'class_name' => 'CRM_CivirulesConditions_Contribution_LastContribution', + 'is_active' => 1 + ), + ), +); \ No newline at end of file diff --git a/CRM/CivirulesConditions/Contribution/LastContribution.php b/CRM/CivirulesConditions/Contribution/LastContribution.php new file mode 100644 index 0000000000000000000000000000000000000000..65920d49b5e22332bdad4086e6e3ce83f017d4d1 --- /dev/null +++ b/CRM/CivirulesConditions/Contribution/LastContribution.php @@ -0,0 +1,93 @@ +<?php +/** + * Class for CiviRules condition last contribution xxx days ago + * + * @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org> + * @license AGPL-3.0 + */ + +class CRM_CivirulesConditions_Contribution_LastContribution extends CRM_CivirulesConditions_Generic_ValueComparison { + + /** + * Returns value of the field + * + * @param object CRM_Civirules_EventData_EventData $eventData + * @return mixed + * @access protected + */ + protected function getFieldValue(CRM_Civirules_EventData_EventData $eventData) { + $completed_status_id = CRM_Core_OptionGroup::getValue('contribution_status', 'completed', 'name'); + $contact_id = $eventData->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 + } + + /** + * Returns a user friendly text explaining the condition params + * e.g. 'Older than 65' + * + * @return string + * @access public + */ + public function userFriendlyConditionParams() { + switch ($this->getOperator()) { + case '=': + $label = 'Last contribution is %1 days ago'; + break; + case '>': + $label = 'Last contribution is more than %1 days ago'; + break; + case '<': + $label = 'Last contribution is less than %1 days ago'; + break; + case '>=': + $label = 'Last contribution is more than %1 days ago or is %1 days ago'; + break; + case '<=': + $label = 'Last contribution is less than %1 days ago or is %1 days ago'; + break; + case '!=': + $label = 'Last contribution is not %1 days ago'; + break; + default: + return ''; + break; + } + return ts($label, array(1 => $this->getComparisonValue())); + } + + /** + * Returns an array with possible operators + * + * @return array + */ + public function getOperators() { + return array( + '=' => ts('Last contribution is n days ago'), + '!=' => ts('Last contribution is not n days ago'), + '>' => ts('Last contribution is more than n days ago'), + '<' => ts('Last contribution is less than n days ago'), + '>=' => ts('Last contribution is more than n days ago or is n days ago'), + '<=' => ts('Last contribution is less than n days ago or is n days ago'), + ); + } + + /** + * Returns an array with required entity names + * + * @return array + * @access public + */ + public function requiredEntities() { + return array(); + } + +} \ No newline at end of file diff --git a/CRM/CivirulesConditions/Form/Form.php b/CRM/CivirulesConditions/Form/Form.php index 297d7a59995f561a4d1ffd9ffd8b18234d950d8b..87e303b11111fc411fff2aed5359cdc8450155d1 100644 --- a/CRM/CivirulesConditions/Form/Form.php +++ b/CRM/CivirulesConditions/Form/Form.php @@ -18,6 +18,11 @@ class CRM_CivirulesConditions_Form_Form extends CRM_Core_Form */ protected $eventClass; + /** + * @var CRM_Civirules_Condition + */ + protected $conditionClass; + /** * Overridden parent method to perform processing before form is build * @@ -29,6 +34,8 @@ class CRM_CivirulesConditions_Form_Form extends CRM_Core_Form $this->ruleCondition = new CRM_Civirules_BAO_RuleCondition(); $this->ruleCondition->id = $this->ruleConditionId; + $ruleConditionData = array(); + CRM_Core_DAO::storeValues($this->ruleCondition, $ruleConditionData); $this->condition = new CRM_Civirules_BAO_Condition(); $this->rule = new CRM_Civirules_BAO_Rule(); @@ -53,8 +60,14 @@ class CRM_CivirulesConditions_Form_Form extends CRM_Core_Form throw new Exception('Civirules could not find event'); } - $this->eventClass = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($this->event->class_name, true); + $this->conditionClass = CRM_Civirules_BAO_Condition::getConditionObjectById($this->condition->id, false); + if ($this->conditionClass) { + $this->conditionClass->setRuleConditionData($ruleConditionData); + } + + $this->eventClass = CRM_Civirules_BAO_Event::getEventObjectByEventId($this->event->id, true); $this->eventClass->setEventId($this->event->id); + $this->eventClass->setEventParams($this->rule->event_params); parent::preProcess(); diff --git a/CRM/CivirulesConditions/Form/ValueComparison.php b/CRM/CivirulesConditions/Form/ValueComparison.php index 393d72a43da72d600be2a8957c4155cba2664c4a..2ce5939d7a29149c58732c3ed94182a2138d8ad8 100644 --- a/CRM/CivirulesConditions/Form/ValueComparison.php +++ b/CRM/CivirulesConditions/Form/ValueComparison.php @@ -6,9 +6,7 @@ * @license AGPL-3.0 */ -class CRM_CivirulesConditions_Form_ValueComparison extends CRM_Core_Form { - - protected $ruleConditionId = false; +class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditions_Form_Form { /** * Overridden parent method to perform processing before form is build @@ -16,8 +14,11 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_Core_Form { * @access public */ public function preProcess() { - $this->ruleConditionId = CRM_Utils_Request::retrieve('rule_condition_id', 'Integer'); parent::preProcess(); + + if (!$this->conditionClass instanceof CRM_CivirulesConditions_Generic_ValueComparison) { + throw new Exception("Not a valid value comparison class"); + } } /** @@ -30,14 +31,7 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_Core_Form { $this->add('hidden', 'rule_condition_id'); - $this->add('select', 'operator', ts('Operator'), array( - '=' => ts('Is equal to'), - '!=' => ts('Is not equal to'), - '>' => ts('Is greater than'), - '<' => ts('Is less than'), - '>=' => ts('Is greater than or equal to'), - '<=' => ts('Is less than or equal to'), - ), true); + $this->add('select', 'operator', ts('Operator'), $this->conditionClass->getOperators(), true); $this->add('text', 'value', ts('Compare value'), true); $this->addButtons(array( diff --git a/CRM/CivirulesConditions/Generic/ValueComparison.php b/CRM/CivirulesConditions/Generic/ValueComparison.php index e635b11c02346e4b52e24e4722ff273332fcfac8..680665dc0ec05bc595c298ff4e7ad2794dc3a3f6 100644 --- a/CRM/CivirulesConditions/Generic/ValueComparison.php +++ b/CRM/CivirulesConditions/Generic/ValueComparison.php @@ -169,4 +169,20 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir return htmlentities(($this->getOperator())).' '.htmlentities($this->getComparisonValue()); } + /** + * Returns an array with possible operators + * + * @return array + */ + public function getOperators() { + return array( + '=' => ts('Is equal to'), + '!=' => ts('Is not equal to'), + '>' => ts('Is greater than'), + '<' => ts('Is less than'), + '>=' => ts('Is greater than or equal to'), + '<=' => ts('Is less than or equal to'), + ); + } + } \ No newline at end of file