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

refactored so that select fields with multiple options could also be selected

parent 563a84f5
Branches
Tags
No related merge requests found
......@@ -46,6 +46,10 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
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'])) {
return array('multi_value' => 'Compare values is a required field');
}
......@@ -68,6 +72,9 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
$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'))));
......
......@@ -24,6 +24,31 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
}
}
/**
* 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 false;
}
/**
* Returns true when the field is a select option with multiple select
*
* @see getFieldOptions
* @return bool
*/
public function isMultiple() {
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
......@@ -53,6 +78,10 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
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;
}
......@@ -167,6 +196,38 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
}
return false;
break;
case 'contains one of':
$leftArray = $this->convertValueToArray($leftValue);
$rightArray = $this->convertValueToArray($rightValue);
if ($this->containsOneOf($leftArray, $rightArray)) {
return true;
}
return false;
break;
case 'not contains one of':
$leftArray = $this->convertValueToArray($leftValue);
$rightArray = $this->convertValueToArray($rightValue);
if (!$this->containsOneOf($leftArray, $rightArray)) {
return true;
}
return false;
break;
case 'contains all of':
$leftArray = $this->convertValueToArray($leftValue);
$rightArray = $this->convertValueToArray($rightValue);
if ($this->containsAllOf($leftArray, $rightArray)) {
return true;
}
return false;
break;
case 'not contains all of':
$leftArray = $this->convertValueToArray($leftValue);
$rightArray = $this->convertValueToArray($rightValue);
if ($this->notContainsAllOf($leftArray, $rightArray)) {
return true;
}
return false;
break;
default:
return false;
break;
......@@ -174,6 +235,37 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
return false;
}
protected function containsOneOf($leftValues, $rightValues) {
foreach($leftValues as $leftvalue) {
if (in_array($leftvalue, $rightValues)) {
return true;
}
}
return false;
}
protected function containsAllOf($leftValues, $rightValues) {
$foundValues = array();
foreach($leftValues as $leftVaue) {
if (in_array($leftVaue, $rightValues)) {
$foundValues[] = $leftVaue;
}
}
if (count($foundValues) > 0 && count($foundValues) == count($rightValues)) {
return true;
}
return false;
}
protected function notContainsAllOf($leftValues, $rightValues) {
foreach($rightValues as $rightValue) {
if (in_array($rightValue, $leftValues)) {
return false;
}
}
return true;
}
/**
* Converts a string to an array, the delimeter is the CRM_Core_DAO::VALUE_SEPERATOR
*
......@@ -230,6 +322,10 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
'<=' => ts('Is less than or equal to'),
'is one of' => ts('Is one of'),
'is not one of' => ts('Is not one of'),
'contains one of' => ts('Does contain one of'),
'not contains one of' => ts('Does not contain one of'),
'contains all of' => ts('Does contain all of'),
'not contains all of' => ts('Does not contain all of'),
);
}
......
......@@ -73,5 +73,31 @@
});
cj('#entity').trigger('change');
});
function retrieveOptionsForEntityAndField(entity, field) {
var options = new Array();
var multiple = false;
CRM_civirules_conidtion_form_updateOptionValues(options, multiple);
if (field.indexOf('custom_') == 0) {
var custom_field_id = field.replace('custom_', '');
CRM.api3('CustomField', 'getsingle', {'sequential': 1, 'id': custom_field_id}, true)
.done(function(data) {
switch(data.html_type) {
case 'Multi-Select':
case 'AdvMulti-Select':
multiple = true;
CRM_civirules_conidtion_form_updateOptionValues(options, multiple);
break;
}
});
}
CRM.api3(entity, 'getoptions', {'sequential': 1, 'field': field}, true)
.done(function (data) {
if (data.values) {
options = data.values;
}
CRM_civirules_conidtion_form_updateOptionValues(options, multiple);
});
}
</script>
{/literal}
\ No newline at end of file
......@@ -30,4 +30,25 @@
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{include file="CRM/CivirulesConditions/Form/ValueComparisonJs.tpl"}
\ No newline at end of file
{include file="CRM/CivirulesConditions/Form/ValueComparisonJs.tpl"}
{literal}
<script type="text/javascript">
var options = new Array();
{/literal}
{if ($field_options)}
{foreach from=$field_options item=value key=key}
options[options.length] = {'key': key, 'value': value};
{/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);
});
</script>
{/literal}
\ No newline at end of file
......@@ -6,6 +6,10 @@
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('#multi_value_parent').removeClass('hiddenElement');
cj('#value_parent').addClass('hiddenElement');
break;
......@@ -18,96 +22,116 @@
cj('#operator').trigger('change');
});
var CRM_civirules_condition_form_initialOperator;
function retrieveOptionsForEntityAndField(entity, field) {
function CRM_civirules_condition_form_updateOperator (options, multiple) {
if (!CRM_civirules_condition_form_initialOperator) {
CRM_civirules_condition_form_initialOperator = cj('#operator').val();
}
cj('#operator option').removeClass('hiddenElement');
if (options.length) {
cj('#operator option[value=">"').addClass('hiddenElement');
cj('#operator option[value=">="').addClass('hiddenElement');
cj('#operator option[value="<"').addClass('hiddenElement');
cj('#operator option[value="<="').addClass('hiddenElement');
}
if (options.length && multiple) {
cj('#operator option[value="="').addClass('hiddenElement');
cj('#operator option[value="!="').addClass('hiddenElement');
cj('#operator option[value="is one of"').addClass('hiddenElement');
cj('#operator option[value="is not one of"').addClass('hiddenElement');
} else {
cj('#operator option[value="contains one of"').addClass('hiddenElement');
cj('#operator option[value="not contains one of"').addClass('hiddenElement');
cj('#operator option[value="contains all of"').addClass('hiddenElement');
cj('#operator option[value="not contains all of"').addClass('hiddenElement');
}
if (cj('#operator option:selected').hasClass('hiddenElement')) {
if (!cj('#operator option[value="'+CRM_civirules_condition_form_initialOperator+'"]').hasClass('hiddenElement')) {
cj('#operator option[value="'+CRM_civirules_condition_form_initialOperator+'"]').attr('selected', 'selected');
} else {
cj('#operator option:not(.hiddenElement)').first().attr('selected', 'selected');
}
cj('#operator').trigger('change');
}
}
function CRM_civirules_condition_form_resetOptions () {
cj('#multi_value_options').html('');
cj('#value_options').html('');
cj('#multi_value_options').addClass('hiddenElement');
cj('#multi_value_parent .content.textarea').removeClass('hiddenElement');
cj('#value_options').addClass('hiddenElement');
cj('#value').removeClass('hiddenElement');
cj('#operator option').removeClass('hiddenElement');
}
CRM.api3(entity, 'getoptions', {'sequential': 1, 'field': field}, true)
.done(function(data) {
if (data.is_error) {
return;
}
function CRM_civirules_conidtion_form_updateOptionValues(options, multiple) {
CRM_civirules_condition_form_resetOptions();
CRM_civirules_condition_form_updateOperator(options, multiple);
if (options && options.length > 0) {
var select_options = '';
var multi_select_options = '';
cj('#operator option[value=">"').addClass('hiddenElement');
cj('#operator option[value=">="').addClass('hiddenElement');
cj('#operator option[value="<"').addClass('hiddenElement');
cj('#operator option[value="<="').addClass('hiddenElement');
if (cj('#operator option:selected').hasClass('hiddenElement')) {
cj('#operator').val('=');
}
var select_html = '';
var html = '';
var currentOptions = cj('#multi_value').val().match(/[^\r\n]+/g);
var currentSelectedOptions = cj('#multi_value').val().match(/[^\r\n]+/g);
var currentSelectedOption = cj('#value').val();
var selectedOptions = new Array();
var currentSelect = cj('#value').val();
var newValueValue = '';
if (!currentOptions) {
currentOptions = new Array();
var selectedOption = '';
if (!currentSelectedOptions) {
currentSelectedOptions = new Array();
}
cj.each(data.values, function(key, value) {
for(var i=0; i < options.length; i++) {
var selected = '';
var checked = '';
if (currentOptions.indexOf(value.key) >= 0) {
if (currentSelectedOptions.indexOf(options[i].key) >= 0) {
checked = 'checked="checked"';
selectedOptions[selectedOptions.length] = value.key;
selectedOptions[selectedOptions.length] = options[i].key;
}
if (value.key == currentSelect) {
if (value.key == currentSelectedOption) {
selected='selected="selected"';
newValueValue = value.key;
selectedOption = options[i].key;
}
var option = '<input type="checkbox" value="'+value.key+'" '+checked+'>'+value.value+'<br>';
var select_option = '<option value="'+value.key+'" '+selected+'>'+value.value+'</option>';
html = html + option;
select_html = select_html + select_option;
multi_select_options = multi_select_options + '<input type="checkbox" value="'+options[i].key+'" '+checked+'>'+options[i].value+'<br>';
select_options = select_options + '<option value="'+value.key+'" '+selected+'>'+options[i].value+'</option>';
}
cj('#value').val(selectedOption);
cj('#value').addClass('hiddenElement');
cj('#value_options').html(select_options);
cj('#value_options').removeClass('hiddenElement');
cj('#value_options').change(function() {
var value = cj(this).val();
cj('#value').val(value);
});
if (html.length > 0) {
cj('#multi_value').val(selectedOptions.join('\r\n'));
cj('#multi_value_options').html(html);
cj('#multi_value_options').removeClass('hiddenElement');
cj('#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('#multi_value').val(currentOptions.join('\r\n'));
}
} else {
if (index >= 0) {
currentOptions.splice(index, 1);
cj('#multi_value').val(currentOptions.join('\r\n'));
}
cj('#multi_value').val(selectedOptions.join('\r\n'));
cj('#multi_value_parent .content.textarea').addClass('hiddenElement');
cj('#multi_value_options').html(multi_select_options);
cj('#multi_value_options').removeClass('hiddenElement');
cj('#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('#multi_value').val(currentOptions.join('\r\n'));
}
});
cj('#multi_value_parent .content.textarea').addClass('hiddenElement');
} else {
cj('#multi_value_parent .content.textarea').removeClass('hiddenElement');
}
if (select_html.length > 0) {
cj('#value').val(newValueValue);
cj('#value_options').html(select_html);
cj('#value_options').removeClass('hiddenElement');
cj('#value_options').change(function() {
var value = cj(this).val();
cj('#value').val(value);
});
cj('#value').addClass('hiddenElement');
} else {
cj('#value').removeClass('hiddenElement');
}
});
} else {
if (index >= 0) {
currentOptions.splice(index, 1);
cj('#multi_value').val(currentOptions.join('\r\n'));
}
}
});
} else {
cj('#multi_value_parent .content.textarea').removeClass('hiddenElement');
cj('#value').removeClass('hiddenElement');
}
}
</script>
{/literal}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment