Form = $form; $intent_text = $intent->intent_text; $targetField = $intent->target_field; $fields = $intent->fields; if(!in_array($targetField, $fields)) $fields[] = $targetField; $conditions = $this->GetArrayDescription($targetField, 'conditions', []); $fieldTex = ''; $descriptor = new AIFieldDescriptor(); foreach($fields as $currentField) { $fieldParts = $this->AIFieldUtilities->GetIdParts($currentField); $field = $form->GetFieldById($fieldParts[0], true, false, 'FieldTemplate'); $fieldDescription = $descriptor->GetField($field->Options->Type); $fieldTex .= '- ID: "' . $currentField . '", FieldType: "' . $fieldDescription["datatype"] . '"' . "\r\n"; } $prompt = file_get_contents(AllInOneForms()->GetLoader()->DIR . '/Managers/AIManagers/AIGenerators/AIContextualFormDesigner/ContextActions/GenerateConditionContextAction.txt'); $prompt = str_replace('{{fields}}', $fieldTex, $prompt); $specialCalculationInstructions = ''; if($this->ThereIsAFieldOfType($fields, ['datepicker'])) { $specialCalculationInstructions = "- Use the function RNAddDays(field,numberOfDays) to add or subtract days to a date field. For example RNAddDays([Field 11],11) will get you the date of the field 11 plus 11 days.\r\n"; } $prompt = str_replace('{{SpecialCalculationsInstructions}}', $specialCalculationInstructions, $prompt); $prompt = [ $this->CreateMessage('system', $prompt), $this->CreateMessage('user', $intent_text) ]; $response = $this->GetCaller()->Chat($prompt); $data = json_decode($response); if($data == null) return null; $conditionOptions = $this->Sanitize($targetField, $data); return [ 'ActionType' => 'CreateCondition', "IntentText" => $intent_text, 'Data' => [ 'ConditionOptions' => $conditionOptions, 'TargetField' => $targetField, ] ]; } private function IsValid($data) { } private function Sanitize($targetField, $data) { $type = Sanitizer::GetStringValueFromPath($data, ['ConditionType']); $conditions = Sanitizer::GetValueFromPath($data, ['Condition']); $rootFormula = trim(Sanitizer::GetStringValueFromPath($data, ['RootFormula'], '')); if($type == null) throw new FriendlyException('The type of condition does not exist'); if($rootFormula == '' && ($conditions == null || !is_array($conditions) || count($conditions) == 0)) throw new FriendlyException('No conditions found'); $options = new stdClass(); switch ($type) { case 'Hide': $type = 'ShowHide'; $options->ShowWhenTrue = false; break; case 'Required': $type = 'Required'; break; case 'Validation': $options->InvalidMessage = Sanitizer::GetStringValueFromPath($data, ['ErrorMessage'], 'Invalid Field'); $type = 'Validation'; break; default: throw new FriendlyException('The type of condition does not exist'); } $options->Type = $type; $options->ConditionGroups = []; if($rootFormula != '') { $options->RootFormula = $rootFormula; $conditions = []; } foreach($conditions as $conditionGroup) { if(!is_array($conditionGroup) || count($conditionGroup) == 0) throw new FriendlyException('The condition is not valid'); $optionConditionGroup = new stdClass(); $optionConditionGroup->ConditionLines = []; $options->ConditionGroups[] = $optionConditionGroup; foreach($conditionGroup as $conditionLine) { $fieldId = Sanitizer::GetStringValueFromPath($conditionLine, ['Field']); $comparisonType = Sanitizer::GetStringValueFromPath($conditionLine, ['ComparisonType']); $value = Sanitizer::GetValueFromPath($conditionLine, ['Value']); $field = $this->Form->GetFieldById($this->AIFieldUtilities->GetIdParts($fieldId)[0], true, false, 'FieldTemplate'); if($field == null) throw new FriendlyException('One of the fields used in a condition was not found'); $fieldDescription = $this->FieldDescription->GetField($field->Options->Type); $subType = 'Standard'; if (in_array($field->Options->Type, ['checkbox', 'radio', 'dropdown', 'buttonselection', 'imagepicker'])) { $subType = 'MultipleValues'; } if (in_array($field->Options->Type, ['googlemaps'])) { $subType = 'GoogleMaps'; } if (in_array($field->Options->Type, ['slider', 'numeric'])) { $subType = 'Numeric'; } if (in_array($field->Options->Type, ['datepicker', 'daterange'])) { $subType = 'Date'; } if (in_array($field->Options->Type, ['list'])) { $subType = 'List'; } if (in_array($field->Options->Type, ['switch'])) { $subType = 'Checkbox'; } if (in_array($field->Options->Type, ['actionbutton'])) { $subType = 'Button'; } if($subType == 'MultipleValues') { if(!is_array($value)) $value = [$value]; $value = array_map(function ($item) { return intval($item); }, $value); } $fieldType = $fieldDescription['datatype']; $supportedComparisons = [ 'composed' => [ 'EqualTo', 'NotEqualTo', 'Contains', 'NotContains', 'IsEmpty', 'IsNotEmpty' ], 'text' => [ 'EqualTo', 'NotEqualTo', 'Contains', 'NotContains', 'IsEmpty', 'IsNotEmpty' ], 'number' => [ 'EqualTo', 'NotEqualTo', 'IsEmpty', 'IsNotEmpty', 'GreaterThan', 'LessThan', 'GreaterOrEqualThan', 'LessOrEqualThan' ], 'date' => [ 'EqualTo', 'NotEqualTo', 'IsEmpty', 'IsNotEmpty', 'GreaterThan', 'LessThan', 'GreaterOrEqualThan', 'LessOrEqualThan' ], 'multiple_options' => [ 'ContainsOption', 'NotContainsOption', 'IsEmpty', 'IsNotEmpty' ], 'boolean' => [ 'IsChecked', 'IsNotChecked' ] ]; if (!array_key_exists($fieldType, $supportedComparisons) || !in_array($comparisonType, $supportedComparisons[$fieldType])) { throw new FriendlyException('The comparison type is not valid'); } if($comparisonType == 'EqualTo') $comparisonType = 'Equal'; if($comparisonType == 'NotEqualTo') $comparisonType = 'NotEqual'; if($comparisonType == 'ContainsOption') $comparisonType = 'Contains'; if($comparisonType == 'NotContainsOption') $comparisonType = 'NotContains'; $optioncConditionLine = new stdClass(); $optioncConditionLine->Type = 'Field'; $optioncConditionLine->SubType = $subType; $optioncConditionLine->FieldId = $fieldId; $optioncConditionLine->Comparison = $comparisonType; if(isset($conditionLine->Formula)) { $optioncConditionLine->Value = ''; $optioncConditionLine->Formula = $conditionLine->Formula; } else $optioncConditionLine->Value = $value; $optionConditionGroup->ConditionLines[] = $optioncConditionLine; } } return $options; } }