Skip to content

Commit

Permalink
Lookup rework (#1257)
Browse files Browse the repository at this point in the history
* get two dropdowns

* get the correct value for the second dropdown, translations

* add lookup logic

* add 2 functions to get the material for lookup

* change frontend for lookup wizard, also put variables in both views because they both need the variables

* translations messages

* remove console.logs

* new logic for targets

* new dropdown for the  target

* values in step 3 too

* properly show the value not the group

* add the qtip in the function wizard

* improve the information messages, provide examples

* add dynamic info from the field in the formula

* function to get the info for the formula box

* add the dynamic path info in twig

* add margin to the buttons at the bottom of the fancy box

* insert function label, also put the tooltip at the bottom

* add little button to show or hide the tooltip

* have the labels for the rule and the field

* change padding of the dropdowns and minwidth to avoid overflow of the labels inside the dropdown

* keep field original
  • Loading branch information
AlexMyddleware authored Feb 5, 2025
1 parent 066a19c commit fa91c99
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 77 deletions.
29 changes: 29 additions & 0 deletions assets/css/animation.css
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,32 @@ h2 { margin-top: 50px;}
cursor: not-allowed;
opacity: 0.6; /* Optional: makes it look disabled */
}


#area_confirm {
margin-top: 35px;
}

.function-wizard-title {
margin-left: 1em;
font-weight: bold;
}

.function-wizard-select {
display: flex;
flex-direction: row;
align-items: center;
}

.function-wizard-title-function-select {
font-weight: bold;
margin-left: 1em;
margin-right: 1em;
margin-bottom: 0;
}

#function-select {
height: auto;
margin-bottom: 0;
padding-right: 0.25rem;
}
126 changes: 124 additions & 2 deletions assets/js/regle.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ $.fn.setCursorPosition = function(pos) {
var newContent =
content.substr(0, position) +
'"' +
$.trim($("select", "#source_info").val()) +
$.trim($("#source_value_select").val()) +
'"' +
content.substr(position);
$("#area_insert").val(newContent);
Expand All @@ -634,7 +634,7 @@ $.fn.setCursorPosition = function(pos) {
var newContent =
content.substr(0, position) +
'"' +
$.trim($("select", "#target_info").val()) +
$.trim($("#target_value_select").val()) +
'"' +
content.substr(position);
$("#area_insert").val(newContent);
Expand Down Expand Up @@ -2365,3 +2365,125 @@ document.addEventListener('DOMContentLoaded', function () {
new bootstrap.Tooltip(tooltipTriggerEl);
});
});

// Function wizard handling
$(document).ready(function() {
const functionSelect = $('#function-select');
const lookupOptions = $('#lookup-options');
const lookupRule = $('#lookup-rule');
const lookupField = $('#lookup-field');
const flagFunctionWizardEnd = $('#flag-function-wizard-end');
let tooltipVisible = true;

// Add tooltip container after the select
$('<div id="function-tooltip" class="tooltip-box" style="padding: 10px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 4px; margin-top: 5px;"></div>')
.insertAfter(flagFunctionWizardEnd);

// Handle tooltip toggle button
$('#toggle-tooltip').on('click', function() {
tooltipVisible = !tooltipVisible;
const tooltipBox = $('#function-tooltip');

if (tooltipVisible) {
$(this).find('i').removeClass('fa-question').addClass('fa-question-circle');
if (functionSelect.val()) { // Only show if a function is selected
tooltipBox.show();
}
} else {
$(this).find('i').removeClass('fa-question-circle').addClass('fa-question');
tooltipBox.hide();
}
});

// Show tooltip when option changes
functionSelect.on('change', function() {
const selectedOption = $(this).find('option:selected');
const tooltip = selectedOption.data('tooltip');
const tooltipBox = $('#function-tooltip');

if (tooltip && tooltipVisible) {
tooltipBox.text(tooltip).show();
} else {
tooltipBox.hide();
}

const selectedFunction = $(this).val();

if (selectedFunction === 'lookup') {
lookupOptions.show();

// Populate rules dropdown
$.ajax({
url: lookupgetrule,
method: 'GET',
success: function(rules) {
lookupRule.empty();
lookupRule.append('<option value="">' + translations.selectRule + '</option>');
rules.forEach(rule => {
lookupRule.append(`<option value="${rule.id}">${rule.name}</option>`);
});
lookupRule.prop('disabled', false);
}
});
} else {
lookupOptions.hide();
insertFunction(selectedFunction);
}
});

// When a rule is selected
lookupRule.on('change', function() {
const selectedRule = $(this).val();


if (selectedRule) {
// Get fields for selected rule
$.ajax({
url: lookupgetfieldroute,
method: 'GET',
success: function(fields) {
lookupField.empty();
lookupField.append('<option value="">' + translations.selectField + '</option>');
// Filter fields to only show those from the selected rule
const filteredFields = fields.filter(field => field.rule_id === selectedRule);
filteredFields.forEach(field => {
lookupField.append(`<option value="${field.id}">${field.name}</option>`);
});
lookupField.prop('disabled', false);
}
});
} else {
lookupField.prop('disabled', true);
}
});

// When a field is selected
lookupField.on('change', function() {
if ($(this).val()) {
// Get the selected field's name (without the rule part in parentheses)
const selectedOption = $(this).find('option:selected');
const fieldName = selectedOption.text().split(' (')[0];
const lookupFormula = `lookup({${fieldName}}, "${lookupRule.val()}"`;
insertFunction(lookupFormula);
}
});

function insertFunction(funcText) {
const areaInsert = $('#area_insert');
const position = areaInsert.getCursorPosition();
const content = areaInsert.val();

// Add parentheses only if not already part of the funcText
const suffix = funcText.endsWith('"') ? ' )' : '( ';

const newContent =
content.substr(0, position) +
funcText +
suffix +
content.substr(position);

areaInsert.val(newContent);
colorationSyntax();
theme(style_template);
}
});
123 changes: 122 additions & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,7 @@ public function ruleStepThree(Request $request)
}
}


// -----[ TARGET ]-----
if ($this->sessionService->isParamRuleTargetFieldsExist($ruleKey)) {
foreach ($this->sessionService->getParamRuleTargetFields($ruleKey) as $field => $fields_tab) {
Expand Down Expand Up @@ -2154,6 +2155,46 @@ public function ruleStepThree(Request $request)
$result['lst_errorMissing'] = ToolsManager::composeListHtml($result['lst_errorMissing'], '', '1');
$result['lst_errorEmpty'] = ToolsManager::composeListHtml($result['lst_errorEmpty'], '', '0');

// Modify this section where $html_list_source is built
$source_groups = [];
$source_values = [];
if (isset($formule_list['source'])) {
foreach ($formule_list['source'] as $field => $fields_tab) {
// Store group names
$source_groups[$field] = $field;

// Store values for each group
$source_values[$field] = [];
foreach ($fields_tab['option'] as $value => $label) {
$source_values[$field][$value] = $label;
}
}
}

// Pass these to the template instead of $html_list_source
$result['source_groups'] = $source_groups;
$result['source_values'] = $source_values;

// Do the same for target
$target_groups = [];
$target_values = [];
if (isset($formule_list['target'])) {
foreach ($formule_list['target'] as $field => $fields_tab) {
// Store group names
$target_groups[$field] = $field;

// Store values for each group
$target_values[$field] = [];
foreach ($fields_tab['option'] as $value => $label) {
$target_values[$field][$value] = $label;
}
}
}

// Pass target data to template
$result['target_groups'] = $target_groups;
$result['target_values'] = $target_values;

return $this->render('Rule/create/step3.html.twig', $result);

// ----------------
Expand Down Expand Up @@ -2587,7 +2628,6 @@ public function ruleValidation(Request $request): JsonResponse

// --------------------------------------------------------------------------------------------------
// Order all rules
error_log(print_r($request->request->all(), true));
$this->jobManager->orderRules();

// --------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -3182,4 +3222,85 @@ public function updateComment(RuleField $ruleField, Request $request, EntityMana

return new Response('Update successful', Response::HTTP_OK);
}

/**
* @Route("/get-rules-for-lookup", name="get_rules_for_lookup", methods={"GET"})
*/
public function getRulesForLookup(): JsonResponse
{
$rules = $this->entityManager->getRepository(Rule::class)
->findBy(['deleted' => 0]);

$ruleData = array_map(function($rule) {
return [
'id' => $rule->getId(),
'name' => $rule->getName()
];
}, $rules);


return new JsonResponse($ruleData);
}

/**
* @Route("/get-fields-for-rule", name="rule_get_fields_for_rule", methods={"GET"})
*/
public function getFieldsForRule(): JsonResponse
{
$fields = $this->entityManager->getRepository(RuleField::class)->findAll();

$fieldData = array_map(function($field) {
return [
'id' => $field->getId(),
'name' => $field->getTarget(),
'rule' => $field->getRule()->getName(),
'rule_id' => $field->getRule()->getId()
];
}, $fields);

return new JsonResponse($fieldData);
}

/**
* Returns field information as JSON
* @Route("/api/field-info/{type}/{field}/", name="api_field_info", methods={"GET"})
*/
public function getFieldInfo(Request $request, $field, $type): JsonResponse
{
$session = $request->getSession();
$myddlewareSession = $session->get('myddlewareSession');
// We always add data again in session because these data are removed after the call of the get
$session->set('myddlewareSession', $myddlewareSession);

$fieldInfo = ['field' => '', 'name' => ''];

if (isset($field) && !empty($field) && isset($myddlewareSession['param']['rule']) && 'my_value' != $field) {
if (isset($myddlewareSession['param']['rule'][0][$type]['fields'][$field])) {
$fieldInfo = [
'field' => $myddlewareSession['param']['rule'][0][$type]['fields'][$field],
'name' => htmlentities(trim($field))
];
// SuiteCRM connector uses this structure instead
} elseif (isset($myddlewareSession['param']['rule']['key'])) {
$ruleKey = $myddlewareSession['param']['rule']['key'];
$fieldInfo = [
'field' => $myddlewareSession['param']['rule'][$ruleKey][$type]['fields'][$field],
'name' => htmlentities(trim($field))
];
} else {
// Possibilité de Mutlimodules
foreach ($myddlewareSession['param']['rule'][0][$type]['fields'] as $subModule) {
if (isset($subModule[$field])) {
$fieldInfo = [
'field' => $subModule[$field],
'name' => htmlentities(trim($field))
];
break;
}
}
}
}

return new JsonResponse($fieldInfo);
}
}
Loading

0 comments on commit fa91c99

Please sign in to comment.