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

added multi value comparisons

parent 271bddf1
Branches
Tags
No related merge requests found
......@@ -29,8 +29,9 @@ class CRM_CivirulesConditions_FieldValueComparison extends CRM_CivirulesConditio
* @access protected
*/
protected function getComparisonValue() {
if (!empty($this->conditionParams['value'])) {
return $this->normalizeValue($this->conditionParams['value']);
$value = parent::getComparisonValue();
if (!empty($value)) {
return $this->normalizeValue($value);
} else {
return null;
}
......@@ -66,7 +67,11 @@ class CRM_CivirulesConditions_FieldValueComparison extends CRM_CivirulesConditio
* @access public
*/
public function userFriendlyConditionParams() {
return htmlentities($this->conditionParams['entity'].'.'.$this->conditionParams['field'].' '.($this->getOperator())).' '.htmlentities($this->getComparisonValue());
$value = $this->getComparisonValue();
if (is_array($value)) {
$value = implode(", ", $value);
}
return htmlentities($this->conditionParams['entity'].'.'.$this->conditionParams['field'].' '.($this->getOperator())).' '.htmlentities($value);
}
}
\ No newline at end of file
......@@ -6,62 +6,7 @@
* @license AGPL-3.0
*/
class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
protected $ruleConditionId = false;
protected $ruleCondition;
protected $condition;
protected $rule;
protected $event;
/**
* @var CRM_Civirules_Event
*/
protected $eventClass;
/**
* Overridden parent method to perform processing before form is build
*
* @access public
*/
public function preProcess() {
$this->ruleConditionId = CRM_Utils_Request::retrieve('rule_condition_id', 'Integer');
$this->ruleCondition = new CRM_Civirules_BAO_RuleCondition();
$this->ruleCondition->id = $this->ruleConditionId;
$this->condition = new CRM_Civirules_BAO_Condition();
$this->rule = new CRM_Civirules_BAO_Rule();
$this->event = new CRM_Civirules_BAO_Event();
if (!$this->ruleCondition->find(true)) {
throw new Exception('Civirules could not find ruleCondition');
}
$this->condition->id = $this->ruleCondition->condition_id;
if (!$this->condition->find(true)) {
throw new Exception('Civirules could not find condition');
}
$this->rule->id = $this->ruleCondition->rule_id;
if (!$this->rule->find(true)) {
throw new Exception('Civirules could not find rule');
}
$this->event->id = $this->rule->event_id;
if (!$this->event->find(true)) {
throw new Exception('Civirules could not find event');
}
$this->eventClass = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($this->event->class_name, true);
$this->eventClass->setEventId($this->event->id);
parent::preProcess();
}
class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_CivirulesConditions_Form_ValueComparison {
protected function getEntities() {
$return = array();
......@@ -102,27 +47,20 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
* @access public
*/
public function buildQuickForm() {
$this->setFormTitle();
parent::buildQuickForm();
$this->add('hidden', 'rule_condition_id');
$this->add('select', 'entity', ts('Entity'), $this->getEntities(), true);
$this->add('select', 'field', ts('Field'), $this->getFields(), true);
$this->add('select', 'operator', ts('Operator'), array(
'=' => ts('Is equal to'),
'!=' => ts('Is not equal to'),
'>' => ts('Is more than'),
'<' => ts('Is less than'),
'>=' => ts('Is more than or equal to'),
'<=' => ts('Is less than or equal to'),
), true);
$this->add('text', 'value', ts('Compare value'), true);
/*$this->add('select', 'operator', ts('Operator'), $this->conditionClass->getOperators(), true);
$this->add('text', 'value', ts('Compare value'));
$this->add('textarea', 'multi_value', ts('Compare values'));
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
array('type' => 'cancel', 'name' => ts('Cancel'))));*/
}
/**
......@@ -132,6 +70,7 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
*/
public function addRules()
{
parent::addRules();
$this->addFormRule(array('CRM_CivirulesConditions_Form_FieldValueComparison', 'validateEntityAndField'));
}
......@@ -146,6 +85,29 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
return true;
}
/*public static function validateOperatorAndComparisonValue($fields) {
$operator = $fields['operator'];
switch ($operator) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
if (empty($fields['value'])) {
return array('value' => ts('Compare value is required'));
}
break;
case 'is one of':
case 'is not one of':
if (empty($fields['multi_value'])) {
return array('multi_value' => 'Compare values is a required field');
}
break;
}
return true;
}*/
/**
* Overridden parent method to set default values
*
......@@ -154,16 +116,8 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
*/
public function setDefaultValues() {
$data = array();
$defaultValues = parent::setDefaultValues();
$defaultValues['rule_condition_id'] = $this->ruleConditionId;
if (!empty($this->ruleCondition->condition_params)) {
$data = unserialize($this->ruleCondition->condition_params);
}
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
if (!empty($data['value'])) {
$defaultValues['value'] = $data['value'];
}
if (!empty($data['entity'])) {
$defaultValues['entity'] = $data['entity'];
}
......@@ -182,6 +136,7 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_Core_Form {
public function postProcess() {
$data['operator'] = $this->_submitValues['operator'];
$data['value'] = $this->_submitValues['value'];
$data['multi_value'] = explode("\r\n", $this->_submitValues['multi_value']);
$data['entity'] = $this->_submitValues['entity'];
$data['field'] = substr($this->_submitValues['field'], strlen($data['entity'].'_'));
......
......@@ -21,6 +21,39 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
}
}
/**
* 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) {
$operator = $fields['operator'];
switch ($operator) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
if (empty($fields['value'])) {
return array('value' => ts('Compare value is required'));
}
break;
case 'is one of':
case 'is not one of':
if (empty($fields['multi_value'])) {
return array('multi_value' => 'Compare values is a required field');
}
break;
}
return true;
}
/**
* Overridden parent method to build form
*
......@@ -33,6 +66,7 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
$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->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
......@@ -60,6 +94,9 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
if (!empty($data['value'])) {
$defaultValues['value'] = $data['value'];
}
if (!empty($data['multi_value'])) {
$defaultValues['multi_value'] = implode("\r\n", $data['multi_value']);
}
return $defaultValues;
}
......@@ -73,6 +110,7 @@ class CRM_CivirulesConditions_Form_ValueComparison extends CRM_CivirulesConditio
$data = unserialize($this->ruleCondition->condition_params);
$data['operator'] = $this->_submitValues['operator'];
$data['value'] = $this->_submitValues['value'];
$data['multi_value'] = explode("\r\n", $this->_submitValues['multi_value']);
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
......
......@@ -42,8 +42,23 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
* @access protected
*/
protected function getComparisonValue() {
if (!empty($this->conditionParams['value'])) {
return $this->conditionParams['value'];
switch ($this->getOperator()) {
case '=':
case '!=':
case '>':
case '>=':
case '<':
case '<=':
$key = 'value';
break;
case 'is one of':
case 'is not one of':
$key = 'multi_value';
break;
}
if (!empty($this->conditionParams[$key])) {
return $this->conditionParams[$key];
} else {
return '';
}
......@@ -138,6 +153,20 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
return false;
}
break;
case 'is one of':
$rightArray = $this->convertValueToArray($rightValue);
if (in_array($leftValue, $rightArray)) {
return true;
}
return false;
break;
case 'is not one of':
$rightArray = $this->convertValueToArray($rightValue);
if (!in_array($leftValue, $rightArray)) {
return true;
}
return false;
break;
default:
return false;
break;
......@@ -145,6 +174,23 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
return false;
}
/**
* Converts a string to an array, the delimeter is the CRM_Core_DAO::VALUE_SEPERATOR
*
* This function could be overriden by child classes to define their own array
* seperator
*
* @param $value
* @return array
*/
protected function convertValueToArray($value) {
if (is_array($value)) {
return $value;
}
//split on new lines
return explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
}
/**
* Returns a redirect url to extra data input from the user after adding a condition
*
......@@ -182,6 +228,8 @@ abstract class CRM_CivirulesConditions_Generic_ValueComparison extends CRM_Civir
'<' => ts('Is less than'),
'>=' => ts('Is greater than or equal to'),
'<=' => ts('Is less than or equal to'),
'is one of' => ts('Is one of'),
'is not one of' => ts('Is not one of'),
);
}
......
......@@ -20,7 +20,34 @@
<div class="content">{$form.value.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.multi_value.label}</div>
<div class="content">
{$form.multi_value.html}
<p class="description">{ts}Seperate each value on a new line{/ts}</p>
</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
\ No newline at end of file
</div>
{include file="CRM/CivirulesConditions/Form/ValueComparisonJs.tpl"}
{literal}
<script type="text/javascript">
cj(function() {
var all_fields = cj('#field').html();
cj('#entity').change(function() {
var val = cj('#entity').val();
cj('#field option').each(function(index, el) {
if (cj(el).val().indexOf(val+'_') != 0) {
cj(el).remove();
}
});
});
cj('#entity').trigger('change');
});
</script>
{/literal}
\ No newline at end of file
......@@ -10,7 +10,16 @@
<div class="content">{$form.value.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.multi_value.label}</div>
<div class="content">
{$form.multi_value.html}
<p class="description">{ts}Seperate each value on a new line{/ts}</p>
</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
\ No newline at end of file
</div>
{include file="CRM/CivirulesConditions/Form/ValueComparisonJs.tpl"}
\ No newline at end of file
{literal}
<script type="text/javascript">
cj(function() {
cj('#operator').change(function() {
var val = cj('#operator').val();
switch (val) {
case 'is one of':
case 'is not one of':
cj('#multi_value').parent().parent().parent().parent().removeClass('hiddenElement');
cj('#value').parent().parent().addClass('hiddenElement');
break;
default:
cj('#multi_value').parent().parent().parent().parent().addClass('hiddenElement');
cj('#value').parent().parent().removeClass('hiddenElement');
break;
}
});
cj('#operator').trigger('change');
});
</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