Commit deab860b authored by ErikHommel's avatar ErikHommel 🏘
Browse files

add condition on activity date

parent d51551d0
......@@ -359,7 +359,7 @@ class CRM_Civirules_Upgrader extends CRM_Civirules_Upgrader_Base {
*/
public function upgrade_1026() {
// This function is a stub and does not do anything in particulair.
return true;
return TRUE;
}
/**
......@@ -370,12 +370,36 @@ class CRM_Civirules_Upgrader extends CRM_Civirules_Upgrader_Base {
CRM_Civirules_Utils_Upgrader::checkCiviRulesConditions();
CRM_Civirules_Utils_Upgrader::checkCiviRulesActions();
CRM_Civirules_Utils_Upgrader::checkCiviRulesTriggers();
return true;
return TRUE;
}
public function upgrade_2000() {
// Stub function to make sure the schema version jumps to 2000, indicating we are on 2.x version.
return true;
return TRUE;
}
/**
* Upgrade 1028 add activity date condition
*/
public function upgrade_2010() {
$this->ctx->log->info('Applying update 2010 - add Activity Date is .... condition');
$select = "SELECT COUNT(*) FROM civirule_condition WHERE class_name = %1";
$selectParams = array(
1 => array('CRM_CivirulesConditions_Activity_DateComparison', 'String'),
);
$count = CRM_Core_DAO::singleValueQuery($select, $selectParams);
if ($count == 0) {
$insert = "INSERT INTO civirule_condition (name, label, class_name, is_active) VALUES(%1, %2, %3, %4)";
$insertParams = array(
1 => array('activity_date_comparison', 'String'),
2 => array('Activity Date is ....', 'String'),
3 => array('CRM_CivirulesConditions_Activity_Date', 'String'),
4 => array(1, 'Integer'),
);
CRM_Core_DAO::executeQuery($insert, $insertParams);
}
return TRUE;
}
}
......@@ -373,5 +373,23 @@ class CRM_Civirules_Utils {
return $campaignList;
}
/**
* Method to set the operator options
*
* @return array
*/
public static function getActivityDateOperatorOptions() {
return array(
'equals',
'later than',
'later than or equal',
'earlier than',
'earlier than or equal',
'not equal',
'between',
);
}
}
......@@ -59,7 +59,6 @@ abstract class CRM_CivirulesActions_Tag_Tag extends CRM_CivirulesActions_Generic
continue;
}
}
//execute the action
$this->executeApiAction($entity, $action, $params);
}
......
<?php
/**
* Class for CiviRule Condition Activity Date is .....
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 3 May 2018
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
class CRM_CivirulesConditions_Activity_Date extends CRM_Civirules_Condition {
private $_conditionParams = array();
public function getExtraDataInputUrl($ruleConditionId) {
return CRM_Utils_System::url('civicrm/civirule/form/condition/activity/date',
'rule_condition_id='.$ruleConditionId);
}
/**
* Method to set the Rule Condition data
*
* @param array $ruleCondition
* @access public
*/
public function setRuleConditionData($ruleCondition) {
parent::setRuleConditionData($ruleCondition);
$this->_conditionParams = array();
if (!empty($this->ruleCondition['condition_params'])) {
$this->_conditionParams = unserialize($this->ruleCondition['condition_params']);
}
}
/**
* 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) {
$activityData = $triggerData->getEntityData('Activity');
if (isset($activityData['activity_date_time'])) {
$activityDate = new DateTime($activityData['activity_date_time']);
if ($activityDate) {
if ($this->_conditionParams['operator'] == 6) {
$fromDate = new DateTime($this->_conditionParams['activity_from_date']);
$toDate = new DateTime($this->_conditionParams['activity_to_date']);
$fromInterval = date_diff($fromDate, $activityDate);
$toInterval = date_diff($toDate, $activityDate);
if ($fromInterval->days >= 0 && $toInterval->days <= 0) {
return TRUE;
}
}
else {
$compareDate = new DateTime($this->_conditionParams['activity_compare_date']);
$interval = date_diff($compareDate, $activityDate);
switch ($this->_conditionParams['operator']) {
case 0:
if ($interval->days == 0) {
return TRUE;
}
break;
case 1:
if ($interval->days > 0) {
return TRUE;
}
break;
case 2:
if ($interval->days >= 0) {
return TRUE;
}
break;
case 3:
if ($interval->days < 0) {
return TRUE;
}
break;
case 4:
if ($interval->days <= 0) {
return TRUE;
}
break;
case 5:
if ($interval->days != 0) {
return TRUE;
}
break;
}
}
}
}
return FALSE;
}
/**
* Returns a user friendly text explaining the condition params
* e.g. 'Older than 65'
*
* @return string
* @access public
*/
public function userFriendlyConditionParams() {
$operatorOptions = CRM_Civirules_Utils::getActivityDateOperatorOptions();
$friendlyText = ts("Activity Date ") . ts($operatorOptions[$this->_conditionParams['operator']]);
if ($this->_conditionParams['operator'] == 6) {
$fromDate = new DateTime($this->_conditionParams['activity_from_date']);
$toDate = new DateTime($this->_conditionParams['activity_to_date']);
$friendlyText .= ' ' . $fromDate->format('j F Y') . ts(' and ') . $toDate->format('j F Y');
}
else {
$compareDate = new DateTime($this->_conditionParams['activity_compare_date']);
$friendlyText .= ' ' . $compareDate->format('j F Y');
}
return $friendlyText;
}
/**
* This function validates whether this condition works with the selected trigger.
*
* This function could be overriden in child classes to provide additional validation
* whether a condition is possible in the current setup. E.g. we could have a condition
* which works on contribution or on contributionRecur then this function could do
* this kind of validation and return false/true
*
* @param CRM_Civirules_Trigger $trigger
* @param CRM_Civirules_BAO_Rule $rule
* @return bool
*/
public function doesWorkWithTrigger(CRM_Civirules_Trigger $trigger, CRM_Civirules_BAO_Rule $rule) {
return $trigger->doesProvideEntity('Activity');
}
}
\ No newline at end of file
<?php
require_once 'CRM/Core/Form.php';
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_CivirulesConditions_Form_Activity_Date extends CRM_CivirulesConditions_Form_Form {
/**
* Overridden parent method to build the form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$this->add('select', 'operator', ts('Operator'), CRM_Civirules_Utils::getActivityDateOperatorOptions(), TRUE, array('onclick' => "checkOperator()"));
$this->add('datepicker', 'activity_compare_date', ts('Comparison Date'), array('placeholder' => ts('Compare with')),FALSE, array('time' => FALSE));
$this->add('datepicker', 'activity_from_date', ts('From date'), array('placeholder' => ts('From')),FALSE, array('time' => FALSE));
$this->add('datepicker', 'activity_to_date', ts('To date'), array('placeholder' => ts('To')),FALSE, array('time' => FALSE));
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
parent::buildQuickForm();
}
/**
* Function to add validation condition rules (overrides parent function)
*
* @access public
*/
public function addRules() {
$this->addFormRule(array('CRM_CivirulesConditions_Form_Activity_Date', 'validateDates'));
}
/**
* Method to validate if from and to date cover a valid period
*
* @param $fields
* @return array|bool
*/
public static function validateDates($fields) {
// if operator is between
if (isset($fields['operator']) && $fields['operator'] == 6) {
// from and to date can not be empty
if (!isset($fields['activity_from_date']) || !isset($fields['activity_to_date']) || empty($fields['activity_from_date']) || empty($fields['activity_to_date'])) {
$errors['operator'] = ts('From and To Date are required and can not be empty when using Between');
return $errors;
}
// to date can not be earlier than from date
$fromDate = new DateTime($fields['activity_from_date']);
$toDate = new DateTime($fields['activity_to_date']);
if ($toDate < $fromDate) {
$errors['from_date'] = ts('From Date should be earlier than or the same as To Date');
return $errors;
}
}
// if not between, compare date can not be empty
else {
if (!isset($fields['activity_compare_date']) || empty($fields['activity_compare_date'])) {
$errors['activity_compare_date'] = ts('Date to compare with can not be empty');
return $errors;
}
}
return TRUE;
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$defaultValues = parent::setDefaultValues();
$data = unserialize($this->ruleCondition->condition_params);
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
if (!empty($data['activity_compare_date'])) {
$defaultValues['activity_compare_date'] = $data['activity_compare_date'];
}
if (!empty($data['activity_from_date'])) {
$defaultValues['activity_from_date'] = $data['activity_from_date'];
}
if (!empty($data['activity_to_date'])) {
$defaultValues['activity_to_date'] = $data['activity_to_date'];
}
if ($data['operator'] == 6) {
$this->assign('between', 1);
}
else {
$this->assign('between', 0);
}
return $defaultValues;
}
/**
* Overridden parent method to perform data processing once form is submitted
*
* @access public
*/
public function postProcess() {
$data['operator'] = $this->_submitValues['operator'];
if ($this->_submitValues['operator'] == 6) {
$data['activity_compare_date'] = "";
$data['activity_from_date'] = $this->_submitValues['activity_from_date'];
$data['activity_to_date'] = $this->_submitValues['activity_to_date'];
}
else {
$data['activity_compare_date'] = $this->_submitValues['activity_compare_date'];
$data['activity_from_date'] = "";
$data['activity_to_date'] = "";
}
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
parent::postProcess();
}
}
\ No newline at end of file
......@@ -14,8 +14,8 @@
<author>CiviCooP</author>
<email>helpdesk@civicoop.org</email>
</maintainer>
<releaseDate>2018-09-19</releaseDate>
<version>2.2</version>
<releaseDate>2018-10-26</releaseDate>
<version>2.3</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.4</ver>
......@@ -26,7 +26,8 @@
<ver>5.2</ver>
<ver>5.3</ver>
<ver>5.4</ver>
<ver>5.5</ver>
<ver>5.5</ver>
<ver>5.6</ver>
</compatibility>
<comments>This extension is in production</comments>
<civix>
......
......@@ -110,6 +110,8 @@ INSERT INTO civirule_condition (name, label, class_name, is_active)
VALUES('activity_is_future_date', 'Activity Date in the Future', 'CRM_CivirulesConditions_Activity_ActivityIsFuture', 1);
INSERT INTO civirule_condition (name, label, class_name, is_active)
VALUES('activity_is_past_date', 'Activity Date in the Past', 'CRM_CivirulesConditions_Activity_ActivityIsPast', 1);
INSERT INTO civirule_condition (name, label, class_name, is_active)
VALUES('activity_date_comparison', 'Activity Date is .....', 'CRM_CivirulesConditions_Activity_Date', 1);
......
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-activity_date">
<div class="crm-section">
<div class="label">{$form.operator.label}</div>
<div class="content operator">{$form.operator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section sector-section activity-compare-date">
<div class="label">{$form.activity_compare_date.label}</div>
<div class="content activity-date-comparison">{$form.activity_compare_date.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section sector-section activity-from-date">
<div class="label">{$form.activity_from_date.label}</div>
<div class="content activity-date-from">{$form.activity_from_date.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section sector-section activity-to-date">
<div class="label">{$form.activity_to_date.label}</div>
<div class="content activity-date-to">{$form.activity_to_date.html}</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{literal}
<script type="text/javascript">
cj(document).ready(function() {
var selectedOperator = cj('.operator').find(":selected").text();
if (selectedOperator === 'between') {
cj('.activity-compare-date').hide();
}
else {
cj('.activity-from-date').hide();
cj('.activity-to-date').hide();
}
});
function checkOperator() {
var selectedOperator = cj('.operator').find(":selected").text();
if (selectedOperator === 'between') {
cj('.activity-compare-date').hide();
cj('.activity-from-date').show();
cj('.activity-to-date').show();
}
else {
cj('.activity-from-date').hide();
cj('.activity-to-date').hide();
cj('.activity-compare-date').show();
}
}
</script>
{/literal}
\ No newline at end of file
......@@ -7,7 +7,7 @@
</div>
<div class="crm-section sector-section">
<div class="label">
<label for="financial_type-select">{ts}Activity Type(s){/ts}</label>
<label for="activity_type-select">{ts}Activity Type(s){/ts}</label>
</div>
<div class="content crm-select-container" id="activity_type_block">
{$form.activity_type_id.html}
......
......@@ -77,6 +77,13 @@
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/activity/date</path>
<page_callback>CRM_CivirulesConditions_Form_Activity_Date</page_callback>
<title>Activity Date</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/entitytag/tagid</path>
<page_callback>CRM_CivirulesConditions_Form_EntityTag_TagId</page_callback>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment