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

added ux improvements by showing options to user

parent 4e4e7896
Branches
Tags
No related merge requests found
......@@ -8,7 +8,7 @@
class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_CivirulesConditions_Form_ValueComparison {
protected function getEntities() {
protected function getEntityOptions() {
$return = array();
foreach($this->eventClass->getProvidedEntities() as $entityDef) {
if (!empty($entityDef->daoClass) && class_exists($entityDef->daoClass)) {
......@@ -18,6 +18,16 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_CivirulesCon
return $return;
}
protected function getEntities() {
$return = array();
foreach($this->eventClass->getProvidedEntities() as $entityDef) {
if (!empty($entityDef->daoClass) && class_exists($entityDef->daoClass)) {
$return[$entityDef->entity] = $entityDef->entity;
}
}
return $return;
}
protected function getFields() {
$return = array();
foreach($this->eventClass->getProvidedEntities() as $entityDef) {
......@@ -96,8 +106,10 @@ class CRM_CivirulesConditions_Form_FieldValueComparison extends CRM_CivirulesCon
$this->add('hidden', 'rule_condition_id');
$this->add('select', 'entity', ts('Entity'), $this->getEntities(), true);
$this->add('select', 'entity', ts('Entity'), $this->getEntityOptions(), true);
$this->add('select', 'field', ts('Field'), $this->getFields(), true);
$this->assign('entities', $this->getEntities());
}
/**
......
......@@ -17,14 +17,22 @@
</div>
<div class="crm-section" id="value_parent">
<div class="label">{$form.value.label}</div>
<div class="content">{$form.value.html}</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">
<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>
......@@ -36,9 +44,23 @@
{literal}
<script type="text/javascript">
{/literal}
{foreach from=$entities key=entity_key item=entity}
cj('#entity option[value="{$entity_key}"]').data('civi-entity', '{$entity}');
{/foreach}
{literal}
cj(function() {
var all_fields = cj('#field').html();
cj('#field').change(function() {
var entity = cj('#entity option:selected').data('civi-entity');
var field = cj('#field').val();
var field = field.replace(cj('#entity').val()+'_', "");
retrieveOptionsForEntityAndField(entity, field);
});
cj('#entity').change(function() {
var val = cj('#entity').val();
cj('#field').html(all_fields);
......@@ -47,6 +69,7 @@
cj(el).remove();
}
});
cj('#field').trigger('change');
});
cj('#entity').trigger('change');
});
......
......@@ -7,14 +7,22 @@
</div>
<div class="crm-section" id="value_parent">
<div class="label">{$form.value.label}</div>
<div class="content">{$form.value.html}</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">
<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>
......
......@@ -17,5 +17,97 @@
});
cj('#operator').trigger('change');
});
function retrieveOptionsForEntityAndField(entity, field) {
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;
}
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 selectedOptions = new Array();
var currentSelect = cj('#value').val();
var newValueValue = '';
if (!currentOptions) {
currentOptions = new Array();
}
cj.each(data.values, function(key, value) {
var selected = '';
var checked = '';
if (currentOptions.indexOf(value.key) >= 0) {
checked = 'checked="checked"';
selectedOptions[selectedOptions.length] = value.key;
}
if (value.key == currentSelect) {
selected='selected="selected"';
newValueValue = value.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;
});
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_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');
}
});
}
</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