Skip to content

Commit

Permalink
Merge branch 'develop' into patch-46
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy authored Jul 20, 2024
2 parents 8a9166b + 61d6196 commit 136343b
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 171 deletions.
4 changes: 2 additions & 2 deletions htdocs/admin/mails.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ function change_smtp_auth_method() {

print dol_get_fiche_head(array(), '', '', -1);

// Cree l'objet formulaire mail
// Create form object
include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
$formmail = new FormMail($db);
$formmail->trackid = (($action == 'testhtml') ? "testhtml" : "test");
Expand All @@ -1132,7 +1132,7 @@ function change_smtp_auth_method() {
$formmail->withtopic = (GETPOSTISSET('subject') ? GETPOST('subject') : $langs->trans("Test"));
$formmail->withtopicreadonly = 0;
$formmail->withfile = 2;
$formmail->withlayout = 1;
$formmail->withlayout = 1; // Not MAIN_EMAIL_USE_LAYOUT must be set
$formmail->withaiprompt = ($action == 'testhtml' ? 'html' : 'text');
$formmail->withbody = (GETPOSTISSET('message') ? GETPOST('message', 'restricthtml') : ($action == 'testhtml' ? $langs->transnoentities("PredefinedMailTestHtml") : $langs->transnoentities("PredefinedMailTest")));
$formmail->withbodyreadonly = 0;
Expand Down
96 changes: 36 additions & 60 deletions htdocs/ai/admin/custom_prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@
// Parameters
$action = GETPOST('action', 'aZ09');
$backtopage = GETPOST('backtopage', 'alpha');
$cancel = GETPOST('cancel');
$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php

$functioncode = GETPOST('functioncode', 'alpha');
$pre_prompt = GETPOST('prePrompt');
$post_prompt = GETPOST('postPrompt');
$blacklists = GETPOST('blacklists');
$test = GETPOST('test');

if (empty($action)) {
$action = 'edit';
}
Expand Down Expand Up @@ -83,20 +90,16 @@
* Actions
*/

$functioncode = GETPOST('functioncode', 'alpha');
$pre_prompt = GETPOST('prePrompt');
$post_prompt = GETPOST('postPrompt');
$blacklists = GETPOST('blacklists');
// get all configs in const AI

$currentConfigurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT');
$currentConfigurations = json_decode($currentConfigurationsJson, true);

if ($action == 'update' && GETPOST('cancel')) {
if ($action == 'update' && $cancel) {
$action = 'edit';
}

if ($action == 'update' && !GETPOST('cancel')) {
if ($action == 'update' && !$cancel && !$test) {
$error = 0;
if (empty($functioncode)) {
$error++;
Expand Down Expand Up @@ -135,7 +138,8 @@
$action = 'edit';
}

if ($action == 'updatePrompts') {
// Update entry
if ($action == 'updatePrompts' && !$test) {
$key = GETPOST('key', 'alpha');

$blacklistArray = array_filter(array_map('trim', explode(',', $blacklists)));
Expand All @@ -149,17 +153,21 @@
$newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
$result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
if (!$error) {
$action = '';
$action = 'edit';
if ($result) {
header("Location: ".$_SERVER['PHP_SELF']);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
exit;
} else {
setEventMessages($langs->trans("ErrorUpdating"), null, 'errors');
}
}
}

// Test entry
if ($action == 'updatePrompts' && $test) {
$action = 'edit';
}

// Delete entry
if ($action == 'confirm_deleteproperty' && GETPOST('confirm') == 'yes') {
$key = GETPOST('key', 'alpha');

Expand Down Expand Up @@ -311,12 +319,12 @@
$out .= '<input type="hidden" name="token" value="'.newToken().'">';
$out .= '<input type="hidden" name="key" value="'.$key.'" />';
$out .= '<input type="hidden" name="action" value="updatePrompts">';
$out .= '<input type="hidden" name="page_y" value="">';

$out .= '<table class="noborder centpercent">';
$out .= '<thead>';
$out .= '<tr class="liste_titre">';
$out .= '<td class="titlefield">'.$arrayofaifeatures[$key]['picto'].' '.$langs->trans($arrayofaifeatures[$key]['label']);
$out .= '<a class="viewfielda reposition marginleftonly marginrighttonly showInputBtn" href="#" data-index="'.$key.'" data-state="edit" data-icon-edit="'.dol_escape_htmltag(img_edit()).'" data-icon-cancel="'.dol_escape_htmltag(img_view()).'">'.img_edit().'</a>';
$out .= '<a class="deletefielda reposition marginleftonly right" href="'.$_SERVER["PHP_SELF"].'?action=deleteproperty&token='.newToken().'&key='.urlencode($key).'">'.img_delete().'</a>';
$out .= '</td>';
$out .= '<td></td>';
Expand All @@ -329,7 +337,7 @@
$out .= '<span id="prePrompt" class="spanforparamtooltip">'.$langs->trans("Pre-Prompt").'</span>';
$out .= '</td>';
$out .= '<td>';
$out .= '<textarea class="flat minwidth500" id="prePromptInput_'.$key.'" name="prePrompt" rows="2" disabled>'.$config['prePrompt'].'</textarea>';
$out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput_'.$key.'" name="prePrompt" rows="2">'.$config['prePrompt'].'</textarea>';
$out .= '</td>';
$out .= '</tr>';

Expand All @@ -338,21 +346,34 @@
$out .= '<span id="postPrompt" class="spanforparamtooltip">'.$langs->trans("Post-Prompt").'</span>';
$out .= '</td>';
$out .= '<td>';
$out .= '<textarea class="flat minwidth500" id="postPromptInput_'.$key.'" name="postPrompt" rows="2" disabled>'.$config['postPrompt'].'</textarea>';
$out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput_'.$key.'" name="postPrompt" rows="2">'.$config['postPrompt'].'</textarea>';
$out .= '</td>';
$out .= '</tr>';

$out .= '<tr id="fichetwothirdright-'.$key.'" class="oddeven">';
$out .= '<td>'.$langs->trans("BlackListWords").'</td>';
$out .= '<td>';
$out .= '<textarea class="flat minwidth500" id="blacklist_'.$key.'" name="blacklists" rows="6" disabled>'.(isset($config['blacklists']) ? implode(', ', (array) $config['blacklists']) : '').'</textarea>';
$out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="blacklist_'.$key.'" name="blacklists" rows="3">'.(isset($config['blacklists']) ? implode(', ', (array) $config['blacklists']) : '').'</textarea>';
$out .= '</td>';
$out .= '</tr>';

$out .= '<tr>';
$out .= '<td></td>';
$out .= '<td>';
$out .= '<input type="submit" class="button small submitBtn" name="modify" data-index="'.$key.'" style="display: none;" value="'.dol_escape_htmltag($langs->trans("Modify")).'"/>';
$out .= '<input type="submit" class="button small submitBtn reposition" name="modify" data-index="'.$key.'" value="'.dol_escape_htmltag($langs->trans("Modify")).'"/>';
$out .= ' &nbsp; ';

include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
$showlinktoai = $key; // 'textgeneration', 'imagegeneration', ...
$showlinktoailabel = $langs->trans("ToTest");
$formmail = new FormMail($db);
$htmlname = $key;

// Fill $out
include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';

$out .= '<div id="'.$htmlname.'"></div>';

$out .= '</td>';
$out .= '</tr>';

Expand All @@ -363,51 +384,6 @@
}
}


$out .= "<script>
var configurations = ".$currentConfigurationsJson.";
$(document).ready(function() {
$('#module_select').change(function() {
var selectedModule = $(this).val();
var moduleConfig = configurations[selectedModule];
if (moduleConfig) {
$('#prePromptInput').val(moduleConfig.prePrompt || '');
$('#postPromptInput').val(moduleConfig.postPrompt || '');
$('#blacklistsInput').val(moduleConfig.blacklists ? moduleConfig.blacklists.join(', ') : '');
} else {
$('#prePromptInput').val('');
$('#postPromptInput').val('');
$('#blacklistsInput').val('');
}
});
$('.showInputBtn').click(function() {
event.preventDefault();
var index = $(this).data('index');
var state = $(this).data('state');
if(state === 'edit') {
$('#prePromptInput_'+index).removeAttr('disabled').focus();
$('#postPromptInput_'+index).removeAttr('disabled');
$('#blacklist_'+index).removeAttr('disabled');
$('.submitBtn[data-index=' + index + ']').show();
$(this).html($(this).data('icon-cancel'));
$(this).data('state', 'cancel');
} else {
$('#prePromptInput_'+index).attr('disabled', 'disabled');
$('#postPromptInput_'+index).attr('disabled', 'disabled');
$('#blacklist_'+index).attr('disabled', 'disabled');
$('.submitBtn[data-index=' + index + ']').hide();
$(this).html($(this).data('icon-edit'));
$(this).data('state', 'edit');
}
});
});
</script>";

print $out;

print '<br>';
Expand Down
2 changes: 2 additions & 0 deletions htdocs/ai/admin/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@
$item->nameText = $langs->trans("AI_API_KEY").' ('.$ialabel.')';
$item->defaultFieldValue = '';
$item->fieldParams['hideGenerateButton'] = 1;
$item->fieldParams['trClass'] = $ia;
$item->cssClass = 'minwidth500 text-security';

$item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_URL'); // Name of constant must end with _KEY so it is encrypted when saved into database.
$item->nameText = $langs->trans("AI_API_URL").' ('.$ialabel.')';
$item->defaultFieldValue = '';
$item->fieldParams['trClass'] = $ia;
$item->cssClass = 'minwidth500';
}

Expand Down
19 changes: 17 additions & 2 deletions htdocs/ai/ajax/generate_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@

require_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php';

if (!isModEnabled('ai')) {
accessforbidden('Module AI not enabled');
}


/*
* View
Expand All @@ -63,7 +67,7 @@
$ai = new Ai($db);

// Get parameters
$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgenerationemail', 'textgenerationwebpage', ...
$function = empty($jsonData['function']) ? 'textgeneration' : $jsonData['function']; // Default value. Can also be 'textgeneration', 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', 'videogeneration', ...
$instructions = dol_string_nohtmltag($jsonData['instructions'], 1, 'UTF-8');
$format = empty($jsonData['format']) ? '' : $jsonData['format'];

Expand All @@ -80,5 +84,16 @@
print "Error returned by API call: " . $generatedContent['message'];
}
} else {
print $generatedContent;
if ($function == 'textgenerationemail' || $function == 'textgenerationwebpage') {
print dolPrintHTML($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
} elseif ($function == 'imagegeneration') {
// TODO
} elseif ($function == 'videogeneration') {
// TODO
} elseif ($function == 'audiogeneration') {
// TODO
} else {
// Default case 'textgeneration'
print dolPrintText($generatedContent); // Note that common HTML tags are NOT escaped (but a sanitization is done)
}
}
Loading

0 comments on commit 136343b

Please sign in to comment.