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

added case status changed condition

parent 350d2865
Branches
Tags
No related merge requests found
<?php
return array (
0 =>
array (
'name' => 'Civirules:Condition.Case.StatusChanged',
'entity' => 'CiviRuleCondition',
'params' =>
array (
'version' => 3,
'name' => 'case_status_changed',
'label' => 'Compare old case status to new cases status',
'class_name' => 'CRM_CivirulesConditions_Case_StatusChanged',
'is_active' => 1
),
),
);
\ No newline at end of file
<?php
class CRM_CivirulesConditions_Case_StatusChanged extends CRM_CivirulesConditions_Generic_FieldValueChangeComparison {
/**
* 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 getOriginalFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$field = 'status_id';
$data = $triggerData->getOriginalData();
if (isset($data[$field])) {
return $data[$field];
}
return null;
}
/**
* 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;
}
/**
* Returns an array with required entity names
*
* @return array
* @access public
*/
public function requiredEntities() {
return array('Case');
}
/**
* Returns an array with all possible options for the field, in
* case the field is a select field, e.g. gender, or financial type
* Return false when the field is a select field
*
* This method could be overriden by child classes to return the option
*
* The return is an array with the field option value as key and the option label as value
*
* @return bool
*/
public function getFieldOptions() {
return CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('case_status');
}
/**
* Returns true when the field is a select option with multiple select
*
* @see getFieldOptions
* @return bool
*/
public function isMultiple() {
return true;
}
}
\ No newline at end of file
<?php
class CRM_CivirulesConditions_Form_FieldValueChangeComparison extends CRM_CivirulesConditions_Form_Form {
/**
* Overridden parent method to perform processing before form is build
*
* @access public
*/
public function preProcess() {
parent::preProcess();
if (!$this->conditionClass instanceof CRM_CivirulesConditions_Generic_FieldValueChangeComparison) {
throw new Exception("Not a valid value comparison class");
}
}
/**
* Function to add validation condition rules (overrides parent function)
*
* @access public
*/
public function addRules()
{
$this->addFormRule(array('CRM_CivirulesConditions_Form_ValueComparison', 'validateOperatorAndComparisonValue'));
}
public static function validateOperatorAndComparisonValue($fields) {
$errors = array();
$operator = $fields['operator'];
switch ($operator) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
if (empty($fields['value'])) {
$errors['value'] = ts('Compare value is required');
}
break;
case 'is one of':
case 'is not one of':
case 'contains one of':
case 'not contains one of':
case 'contains all of':
case 'not contains all of':
if (empty($fields['multi_value'])) {
$errors['multi_value'] = ts('Compare values is a required field');
}
break;
}
$original_operator = $fields['original_operator'];
switch ($original_operator) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
if (empty($fields['value'])) {
$errors['original_value'] = ts('Compare value is required');
}
break;
case 'is one of':
case 'is not one of':
case 'contains one of':
case 'not contains one of':
case 'contains all of':
case 'not contains all of':
if (empty($fields['multi_value'])) {
$errors['original_multi_value'] = ts('Compare values is a required field');
}
break;
}
if (count($errors)) {
return $errors;
}
return true;
}
/**
* Overridden parent method to build form
*
* @access public
*/
public function buildQuickForm() {
$this->setFormTitle();
$this->add('hidden', 'rule_condition_id');
$this->add('select', 'original_operator', ts('Operator'), $this->conditionClass->getOperators(), true);
$this->add('text', 'original_value', ts('Compare value'), true);
$this->add('textarea', 'original_multi_value', ts('Compare values'));
$this->add('select', 'operator', ts('Operator'), $this->conditionClass->getOperators(), true);
$this->add('text', 'value', ts('Compare value'), true);
$this->add('textarea', 'multi_value', ts('Compare values'));
$this->assign('field_options', $this->conditionClass->getFieldOptions());
$this->assign('is_field_option_multiple', $this->conditionClass->isMultiple());
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$data = array();
$defaultValues = array();
$defaultValues['rule_condition_id'] = $this->ruleConditionId;
$ruleCondition = new CRM_Civirules_BAO_RuleCondition();
$ruleCondition->id = $this->ruleConditionId;
if ($ruleCondition->find(true)) {
$data = unserialize($ruleCondition->condition_params);
}
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
if (!empty($data['value'])) {
$defaultValues['value'] = $data['value'];
}
if (!empty($data['multi_value'])) {
$defaultValues['multi_value'] = implode("\r\n", $data['multi_value']);
}
if (!empty($data['original_operator'])) {
$defaultValues['original_operator'] = $data['original_operator'];
}
if (!empty($data['original_value'])) {
$defaultValues['original_value'] = $data['original_value'];
}
if (!empty($data['original_multi_value'])) {
$defaultValues['original_multi_value'] = implode("\r\n", $data['original_multi_value']);
}
return $defaultValues;
}
/**
* Overridden parent method to process form data after submission
*
* @throws Exception when rule condition not found
* @access public
*/
public function postProcess() {
$data = unserialize($this->ruleCondition->condition_params);
$data['original_operator'] = $this->_submitValues['original_operator'];
$data['original_value'] = $this->_submitValues['original_value'];
if (isset($this->_submitValues['original_multi_value'])) {
$data['original_multi_value'] = explode("\r\n", $this->_submitValues['original_multi_value']);
}
$data['operator'] = $this->_submitValues['operator'];
$data['value'] = $this->_submitValues['value'];
if (isset($this->_submitValues['multi_value'])) {
$data['multi_value'] = explode("\r\n", $this->_submitValues['multi_value']);
}
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
$session = CRM_Core_Session::singleton();
$session->setStatus('Condition '.$this->condition->label.' parameters updated to CiviRule '
.CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->ruleCondition->rule_id),
'Condition parameters updated', 'success');
$redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id='.$this->ruleCondition->rule_id, TRUE);
CRM_Utils_System::redirect($redirectUrl);
}
}
\ No newline at end of file
<?php
abstract class CRM_CivirulesConditions_Generic_FieldValueChangeComparison extends CRM_CivirulesConditions_Generic_ValueComparison {
/**
* 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
*/
abstract protected function getOriginalFieldValue(CRM_Civirules_TriggerData_TriggerData $triggerData);
/**
* Returns the value for the data comparison
*
* @return mixed
* @access protected
*/
protected function getOriginalComparisonValue() {
switch ($this->getOperator()) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
$key = 'original_value';
break;
case 'is one of':
case 'is not one of':
case 'contains one of':
case 'not contains one of':
case 'contains all of':
case 'not contains all of':
$key = 'original_multi_value';
break;
}
if (!empty($this->conditionParams[$key])) {
return $this->conditionParams[$key];
} else {
return '';
}
}
/**
* Returns the value for the data comparison
*
* @return mixed
* @access protected
*/
protected function getComparisonValue() {
switch ($this->getOperator()) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
$key = 'value';
break;
case 'is one of':
case 'is not one of':
case 'contains one of':
case 'not contains one of':
case 'contains all of':
case 'not contains all of':
$key = 'multi_value';
break;
}
if (!empty($this->conditionParams[$key])) {
return $this->conditionParams[$key];
} else {
return '';
}
}
/**
* Returns an operator for comparison
*
* Valid operators are:
* - equal: =
* - not equal: !=
* - greater than: >
* - lesser than: <
* - greater than or equal: >=
* - lesser than or equal: <=
*
* @return string operator for comparison
* @access protected
*/
protected function getOriginalOperator() {
if (!empty($this->conditionParams['original_operator'])) {
return $this->conditionParams['original_operator'];
} else {
return '';
}
}
/**
* Returns an operator for comparison
*
* Valid operators are:
* - equal: =
* - not equal: !=
* - greater than: >
* - lesser than: <
* - greater than or equal: >=
* - lesser than or equal: <=
*
* @return string operator for comparison
* @access protected
*/
protected function getOperator() {
if (!empty($this->conditionParams['operator'])) {
return $this->conditionParams['operator'];
} else {
return '';
}
}
/**
* Mandatory method to return if the condition is valid
*
* @param object CRM_Civirules_TriggerData_TriggerData $triggerData
* @return bool
* @access public
*/
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) {
//not the right trigger. The trigger data should contain also
if (!$triggerData instanceof CRM_Civirules_TriggerData_Interface_OriginalData) {
return false;
}
$originalValue = $this->getOriginalFieldValue($triggerData);
$originalCompareValue = $this->getOriginalComparisonValue();
$originalComparison = $this->compare($originalValue, $originalCompareValue, $this->getOriginalOperator());
$value = $this->getFieldValue($triggerData);
$compareValue = $this->getComparisonValue();
$newComparison = $this->compare($value, $compareValue, $this->getOperator());
if ($originalComparison && $newComparison) {
return true;
}
return false;
}
public function getExtraDataInputUrl($ruleConditionId) {
return CRM_Utils_System::url('civicrm/civirule/form/condition/datachangedcomparison/', 'rule_condition_id='.$ruleConditionId);
}
/**
* Returns a user friendly text explaining the condition params
* e.g. 'Older than 65'
*
* @return string
* @access public
*/
public function userFriendlyConditionParams() {
$originalComparisonValue = $this->getOriginalComparisonValue();
if (is_array($originalComparisonValue)) {
$originalComparisonValue = implode(", ", $originalComparisonValue);
}
$comparisonValue = $this->getComparisonValue();
if (is_array($comparisonValue)) {
$comparisonValue = implode(", ", $comparisonValue);
}
return
ts('Old value ').
htmlentities(($this->getOriginalOperator())).' '.htmlentities($originalComparisonValue).'&nbsp'.
ts ('and new value ').
htmlentities(($this->getOperator())).' '.htmlentities($comparisonValue);
}
}
\ No newline at end of file
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-value-comparison">
<div class="crm-section">
<div class="label"></div>
<div class="content"><h2>{ts}Original value{/ts}</h2></div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.original_operator.label}</div>
<div class="content">{$form.original_operator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section" id="original_value_parent">
<div class="label">{$form.original_value.label}</div>
<div class="content">
{$form.original_value.html}
<select id="original_value_options" class="hiddenElement">
</select>
</div>
<div class="clear"></div>
</div>
<div class="crm-section" id="original_multi_value_parent">
<div class="label">{$form.original_multi_value.label}</div>
<div class="content textarea">
{$form.original_multi_value.html}
<p class="description">{ts}Seperate each value on a new line{/ts}</p>
</div>
<div id="original_multi_value_options" class="hiddenElement content">
</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label"></div>
<div class="content"><h2>{ts}New value{/ts}</h2></div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.operator.label}</div>
<div class="content">{$form.operator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section" id="value_parent">
<div class="label">{$form.value.label}</div>
<div class="content">
{$form.value.html}
<select id="value_options" class="hiddenElement">
</select>
</div>
<div class="clear"></div>
</div>
<div class="crm-section" id="multi_value_parent">
<div class="label">{$form.multi_value.label}</div>
<div class="content textarea">
{$form.multi_value.html}
<p class="description">{ts}Seperate each value on a new line{/ts}</p>
</div>
<div id="multi_value_options" class="hiddenElement content">
</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{include file="CRM/CivirulesConditions/Form/ValueComparisonJs.tpl"}
{literal}
<script type="text/javascript">
cj(function() {
cj('#original_operator').change(function() {
var val = cj('#original_operator').val();
switch (val) {
case 'is one of':
case 'is not one of':
case 'contains one of':
case 'not contains one of':
case 'contains all of':
case 'not contains all of':
cj('#original_multi_value_parent').removeClass('hiddenElement');
cj('#original_value_parent').addClass('hiddenElement');
break;
default:
cj('#original_multi_value_parent').addClass('hiddenElement');
cj('#original_value_parent').removeClass('hiddenElement');
break;
}
});
cj('#original_operator').trigger('change');
});
var CRM_civirules_condition_form_initialOriginalOperator;
function CRM_civirules_condition_form_updateOriginalOperator (options, multiple) {
if (!CRM_civirules_condition_form_initialOriginalOperator) {
CRM_civirules_condition_form_initialOriginalOperator = cj('#original_operator').val();
}
cj('#original_operator option').removeClass('hiddenElement');
if (options.length) {
cj('#original_operator option[value=">"').addClass('hiddenElement');
cj('#original_operator option[value=">="').addClass('hiddenElement');
cj('#original_operator option[value="<"').addClass('hiddenElement');
cj('#original_operator option[value="<="').addClass('hiddenElement');
}
if (options.length && multiple) {
cj('#original_operator option[value="="').addClass('hiddenElement');
cj('#original_operator option[value="!="').addClass('hiddenElement');
cj('#original_operator option[value="is one of"').addClass('hiddenElement');
cj('#original_operator option[value="is not one of"').addClass('hiddenElement');
} else {
cj('#original_operator option[value="contains one of"').addClass('hiddenElement');
cj('#original_operator option[value="not contains one of"').addClass('hiddenElement');
cj('#original_operator option[value="contains all of"').addClass('hiddenElement');
cj('#original_operator option[value="not contains all of"').addClass('hiddenElement');
}
if (cj('#original_operator option:selected').hasClass('hiddenElement')) {
if (!cj('#original_operator option[value="'+CRM_civirules_condition_form_initialOriginalOperator+'"]').hasClass('hiddenElement')) {
cj('#original_operator option[value="'+CRM_civirules_condition_form_initialOriginalOperator+'"]').attr('selected', 'selected');
} else {
cj('#original_operator option:not(.hiddenElement)').first().attr('selected', 'selected');
}
cj('#original_operator').trigger('change');
}
}
function CRM_civirules_condition_form_resetOriginalOptions () {
cj('#original_multi_value_options').html('');
cj('#original_value_options').html('');
cj('#original_multi_value_options').addClass('hiddenElement');
cj('#original_multi_value_parent .content.textarea').removeClass('hiddenElement');
cj('#original_value_options').addClass('hiddenElement');
cj('#original_value').removeClass('hiddenElement');
}
function CRM_civirules_conidtion_form_updateOriginalOptionValues(options, multiple) {
CRM_civirules_condition_form_resetOriginalOptions();
CRM_civirules_condition_form_updateOriginalOperator(options, multiple);
if (options && options.length > 0) {
var select_options = '';
var multi_select_options = '';
var currentSelectedOptions = cj('#original_multi_value').val().match(/[^\r\n]+/g);
var currentSelectedOption = cj('#original_value').val();
var selectedOptions = new Array();
var selectedOption = '';
if (!currentSelectedOptions) {
currentSelectedOptions = new Array();
}
for(var i=0; i < options.length; i++) {
var selected = '';
var checked = '';
if (currentSelectedOptions.indexOf(options[i].key) >= 0) {
checked = 'checked="checked"';
selectedOptions[selectedOptions.length] = options[i].key;
}
if (options[i].key == currentSelectedOption || (!currentSelectedOption && i == 0)) {
selected='selected="selected"';
selectedOption = options[i].key;
}
multi_select_options = multi_select_options + '<input type="checkbox" value="'+options[i].key+'" '+checked+'>'+options[i].value+'<br>';
select_options = select_options + '<option value="'+options[i].key+'" '+selected+'>'+options[i].value+'</option>';
}
cj('#original_value').val(selectedOption);
cj('#original_value').addClass('hiddenElement');
cj('#original_value_options').html(select_options);
cj('#original_value_options').removeClass('hiddenElement');
cj('#original_value_options').change(function() {
var value = cj(this).val();
cj('#value').val(value);
});
cj('#original_multi_value').val(selectedOptions.join('\r\n'));
cj('#original_multi_value_parent .content.textarea').addClass('hiddenElement');
cj('#original_multi_value_options').html(multi_select_options);
cj('#original_multi_value_options').removeClass('hiddenElement');
cj('#original_multi_value_options input[type="checkbox"]').change(function() {
var currentOptions = cj('#multi_value').val().match(/[^\r\n]+/g);
if (!currentOptions) {
currentOptions = new Array();
}
var value = cj(this).val();
var index = currentOptions.indexOf(value);
if (this.checked) {
if (index < 0) {
currentOptions[currentOptions.length] = value;
cj('#original_multi_value').val(currentOptions.join('\r\n'));
}
} else {
if (index >= 0) {
currentOptions.splice(index, 1);
cj('#original_multi_value').val(currentOptions.join('\r\n'));
}
}
});
} else {
cj('#original_multi_value_parent .content.textarea').removeClass('hiddenElement');
cj('#original_value').removeClass('hiddenElement');
}
}
var options = new Array();
{/literal}
{if ($field_options)}
{foreach from=$field_options item=value key=key}
{literal}options[options.length] = {'key': {/literal}'{$key}', 'value': '{$value}'{literal}};{/literal}
{/foreach}
{/if}
{if ($is_field_option_multiple)}
var multiple = true;
{else}
var multiple = false;
{/if}
{literal}
cj(function() {
CRM_civirules_conidtion_form_updateOptionValues(options, multiple);
CRM_civirules_conidtion_form_updateOriginalOptionValues(options, multiple);
});
</script>
{/literal}
\ No newline at end of file
......@@ -35,6 +35,13 @@
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/datachangedcomparison</path>
<page_callback>CRM_CivirulesConditions_Form_FieldValueChangeComparison</page_callback>
<title>Value comparison</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/fieldvaluecomparison</path>
<page_callback>CRM_CivirulesConditions_Form_FieldValueComparison</page_callback>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment