dolibarr 22.0.5
custom_prompt.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
4 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
5 * Coryright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
30require_once DOL_DOCUMENT_ROOT."/ai/lib/ai.lib.php";
31require_once DOL_DOCUMENT_ROOT."/core/class/html.formai.class.php";
32
41$langs->loadLangs(array("admin", "website", "other"));
42
43$arrayofaifeatures = getListOfAIFeatures();
44$arrayofai = getListOfAIServices();
45
46// Parameters
47$action = GETPOST('action', 'aZ09');
48$backtopage = GETPOST('backtopage', 'alpha');
49$cancel = GETPOST('cancel');
50$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
51
52$functioncode = GETPOST('functioncode', 'alpha');
53$pre_prompt = GETPOST('prePrompt');
54$post_prompt = GETPOST('postPrompt');
55$blacklists = GETPOST('blacklists');
56$test = GETPOST('test');
57$key = (string) GETPOST('key', 'alpha');
58
59if (empty($action)) {
60 $action = 'edit';
61}
62
63$error = 0;
64$setupnotempty = 0;
65
66// Access control
67if (!$user->admin) {
69}
70
71
72// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
73$useFormSetup = 1;
74
75if (!class_exists('FormSetup')) {
76 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
77}
78
79$formSetup = new FormSetup($db);
80$aiservice = getDolGlobalString('AI_API_SERVICE', 'chatgpt');
81
82// Setup conf for AI model
83$formSetup->formHiddenInputs['action'] = "updatefeaturemodel";
84foreach ($arrayofaifeatures as $featurekey => $feature) {
85 $newkey = $featurekey;
86 if (preg_match('/^text/', $featurekey)) {
87 $newkey = 'textgeneration';
88 }
89 $item = $formSetup->newItem('AI_API_'.strtoupper($aiservice).'_MODEL_'.$feature["function"]); // Name of constant must end with _KEY so it is encrypted when saved into database.
90 if ($arrayofai[$aiservice][$newkey] != 'na') {
91 $item->nameText = $langs->trans("AI_API_MODEL_".$feature["function"]).' <span class="opacitymedium">('.$langs->trans("Default").' = '.$arrayofai[$aiservice][$newkey].')</span>';
92 } else {
93 $item->nameText = $langs->trans("AI_API_MODEL_".$feature["function"]).' <span class="opacitymedium">('.$langs->trans("None").')</span>';
94 }
95 $item->cssClass = 'minwidth500 input';
96}
97
98$setupnotempty += count($formSetup->items);
99
100$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
101
102
103/*
104 * Actions
105 */
106
107// get all configs in const AI
108
109$currentConfigurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT');
110$currentConfigurations = json_decode($currentConfigurationsJson, true);
111
112if ($action == 'updatefeaturemodel' && !empty($user->admin)) {
113 $formSetup->saveConfFromPost();
114 $action = 'edit';
115}
116
117if ($action == 'update' && $cancel) {
118 $action = 'edit';
119}
120
121if ($action == 'update' && !$cancel && !$test) {
122 $error = 0;
123 if (empty($functioncode)) {
124 $error++;
125 setEventMessages($langs->trans('ErrorInputRequired'), null, 'errors');
126 }
127 if (!is_array($currentConfigurations)) {
128 $currentConfigurations = [];
129 }
130
131 $blacklistArray = array_filter(array_map('trim', explode(',', $blacklists)));
132
133 if (empty($functioncode) || (empty($pre_prompt) && empty($post_prompt) && empty($blacklists))) {
134 if (isset($currentConfigurations[$functioncode])) {
135 unset($currentConfigurations[$functioncode]);
136 }
137 } else {
138 $currentConfigurations[$functioncode] = [
139 'prePrompt' => $pre_prompt,
140 'postPrompt' => $post_prompt,
141 'blacklists' => $blacklistArray,
142 ];
143 }
144
145 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
146 $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
147 if (!$error) {
148 if ($result) {
149 header("Location: ".$_SERVER['PHP_SELF']);
150 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
151 exit;
152 } else {
153 setEventMessages($langs->trans("ErrorUpdating"), null, 'errors');
154 }
155 }
156
157 $action = 'edit';
158}
159
160// Update entry
161if ($action == 'updatePrompts' && !$test) {
162 $blacklistArray = array_filter(array_map('trim', explode(',', $blacklists)));
163
164 $currentConfigurations[$key] = [
165 'prePrompt' => $pre_prompt,
166 'postPrompt' => $post_prompt,
167 'blacklists' => $blacklistArray,
168 ];
169
170 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
171 $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
172 if (!$error) {
173 $action = 'edit';
174 if ($result) {
175 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
176 } else {
177 setEventMessages($langs->trans("ErrorUpdating"), null, 'errors');
178 }
179 }
180}
181
182// Test entry
183if ($action == 'updatePrompts' && $test) {
184 $action = 'edit';
185}
186
187// Delete entry
188if ($action == 'confirm_deleteproperty' && GETPOST('confirm') == 'yes') {
189 if (isset($currentConfigurations[$key])) {
190 unset($currentConfigurations[$key]);
191
192 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
193 $res = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
194 if ($res) {
195 header("Location: ".$_SERVER['PHP_SELF']);
196 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
197 exit;
198 } else {
199 setEventMessages($langs->trans("NoRecordDeleted"), null, 'errors');
200 }
201 }
202}
203
204
205/*
206 * View
207 */
208
209$form = new Form($db);
210$formai = new FormAI($db);
211
212$help_url = '';
213$title = "AiSetup";
214
215llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin_custom_prompt');
216
217// Subheader
218$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
219
220print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
221
222// Configuration header
223$head = aiAdminPrepareHead();
224print dol_get_fiche_head($head, 'custom', $langs->trans($title), -1, "ai");
225
226$newcardbutton = dolGetButtonTitle($langs->trans('NewCustomPrompt'), '', 'fa fa-plus-circle', $_SERVER["PHP_SELF"].'?action=create', '', 1);
227/*
228$newbutton = '<a href="'.$_SERVER["PHP_SELF"].'?action=create" title="'.$langs->trans("NewCustomPrompt").'">';
229$newbutton .= img_picto('', 'add');
230$newbutton .= '</a>';
231*/
232
233print load_fiche_titre($langs->trans("AIPromptForFeatures", $arrayofai[$aiservice]['label']), $newcardbutton, '');
234
235
236if ($action == 'deleteproperty') {
237 $formconfirm = $form->formconfirm(
238 $_SERVER["PHP_SELF"].'?key='.urlencode(GETPOST('key', 'alpha')),
239 $langs->trans('Delete'),
240 $langs->trans('ConfirmDeleteSetup', GETPOST('key', 'alpha')),
241 'confirm_deleteproperty',
242 '',
243 0,
244 1
245 );
246 print $formconfirm;
247}
248
249print '<br>';
250
251if ($action == 'create') {
252 $out = '<div class="addcustomprompt">';
253
254 $out .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
255 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
256 $out .= '<input type="hidden" name="action" value="update">';
257
258
259 $out .= '<table class="noborder centpercent">';
260 $out .= '<thead>';
261 $out .= '<tr class="liste_titre">';
262 $out .= '<td>'.$langs->trans('NewCustomPrompt').'</td>';
263 $out .= '<td></td>';
264 $out .= '</tr>';
265 $out .= '</thead>';
266
267 $out .= '<tbody>';
268 $out .= '<tr class="oddeven">';
269 $out .= '<td class="col-setup-title titlefield">';
270 $out .= '<span id="module" class="spanforparamtooltip">'.$langs->trans("Feature").'</span>';
271 $out .= '</td>';
272 $out .= '<td>';
273 // Combo list of AI features
274 $out .= '<select name="functioncode" id="functioncode" class="flat minwidth500">';
275 $out .= '<option>&nbsp;</option>';
276 foreach ($arrayofaifeatures as $featurekey => $feature) {
277 $labelhtml = $langs->trans($arrayofaifeatures[$featurekey]['label']).($arrayofaifeatures[$featurekey]['status'] == 'notused' ? ' <span class="opacitymedium">('.$langs->trans("NotYetAvailable").')</span>' : "");
278 $labeltext = $langs->trans($arrayofaifeatures[$featurekey]['label']);
279 $out .= '<option value="'.dol_escape_js($featurekey).'" data-html="'.dol_escape_htmltag($labelhtml).'">'.dol_escape_htmltag($labeltext).'</option>';
280 }
281 $out .= '</select>';
282 $out .= ajax_combobox("functioncode");
283 $out .= '<script type="text/javascript">
284 jQuery(document).ready(function() {
285 jQuery("#functioncode").on("change", function() {
286 console.log("We change value of ai function");
287 var changedValue = $(this).val();
288 console.log(changedValue);
289 var arrayplaceholder = {';
290 foreach ($arrayofaifeatures as $featurekey => $feature) {
291 $out .= dol_escape_js($featurekey).': \''.dol_escape_js(empty($feature['placeholder']) ? '' : $feature['placeholder']).'\',';
292 }
293 $out .= '}
294 jQuery("#prePromptInput'.dol_escape_js($key).'").val(arrayplaceholder[changedValue]);
295 });
296 });
297 </script>
298 ';
299
300 $out .= '</td>';
301 $out .= '</tr>';
302
303 $out .= '<tr class="oddeven">';
304 $out .= '<td class="col-setup-title">';
305 $out .= '<span id="prePrompt" class="spanforparamtooltip">';
306 $out .= $form->textwithpicto($langs->trans("Pre-Prompt"), $langs->trans("Pre-PromptHelp"));
307 $out .= '</span>';
308 $out .= '</td>';
309 $out .= '<td>';
310 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput'.$key.'" name="prePrompt" rows="2"></textarea>';
311 $out .= '</td>';
312 $out .= '</tr>';
313 $out .= '<tr class="oddeven">';
314 $out .= '<td class="col-setup-title">';
315 $out .= '<span id="postPrompt" class="spanforparamtooltip">';
316 $out .= $form->textwithpicto($langs->trans("Post-Prompt"), $langs->trans("Post-PromptHelp"));
317 $out .= '</span>';
318 $out .= '</td>';
319 $out .= '<td>';
320 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput" name="postPrompt" rows="2"></textarea>';
321 $out .= '</td>';
322 $out .= '</tr>';
323 $out .= '<tr class="oddeven">';
324 $out .= '<td class="col-setup-title">';
325 $out .= '<span id="blacklists" class="spanforparamtooltip">';
326 $out .= $form->textwithpicto($langs->trans("BlackListWords"), $langs->trans("BlackListWordsAIHelp").'.<br>'.$langs->trans("BlackListWordsHelp"));
327 $out .= '</span>';
328 $out .= '</td>';
329 $out .= '<td>';
330 $out .= '<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklistsInput" name="blacklists">';
331 $out .= '</td>';
332 $out .= '</tr>';
333 $out .= '</tbody>';
334 $out .= '</table>';
335
336 $out .= $form->buttonsSaveCancel("Add", "");
337 $out .= '</form>';
338
339 $out .= '<br><br><br>';
340 $out .= '</div>';
341
342 print $out;
343}
344
345
346if ($action == 'edit' || $action == 'create' || $action == 'deleteproperty') {
347 $out = '';
348
349 if (!empty($currentConfigurations)) {
350 foreach ($currentConfigurations as $confkey => $config) {
351 if (!empty($confkey) && !preg_match('/^[a-z]+$/i', $confkey)) { // Ignore empty saved setup
352 continue;
353 }
354
355 $out .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
356 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
357 $out .= '<input type="hidden" name="key" value="'.$confkey.'" />';
358 $out .= '<input type="hidden" name="action" value="updatePrompts">';
359 $out .= '<input type="hidden" name="page_y" value="">';
360
361 $out .= '<table class="noborder centpercent">';
362 $out .= '<thead>';
363 $out .= '<tr class="liste_titre">';
364 $out .= '<td class="titlefield">'.$arrayofaifeatures[$confkey]['picto'].' '.$langs->trans($arrayofaifeatures[$confkey]['label']);
365 $out .= '<a class="deletefielda reposition marginleftonly right" href="'.$_SERVER["PHP_SELF"].'?action=deleteproperty&token='.newToken().'&key='.urlencode($confkey).'">'.img_delete().'</a>';
366 $out .= '</td>';
367 $out .= '<td></td>';
368 $out .= '</tr>';
369 $out .= '</thead>';
370 $out .= '<tbody>';
371
372 $out .= '<tr class="oddeven">';
373 $out .= '<td class="col-setup-title">';
374 $out .= '<span id="prePrompt" class="spanforparamtooltip">'.$langs->trans("Pre-Prompt").'</span>';
375 $out .= '</td>';
376 $out .= '<td>';
377 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput_'.$confkey.'" name="prePrompt" rows="2">'.$config['prePrompt'].'</textarea>';
378 $out .= '</td>';
379 $out .= '</tr>';
380
381 $out .= '<tr class="oddeven">';
382 $out .= '<td class="col-setup-title">';
383 $out .= '<span id="postPrompt" class="spanforparamtooltip">'.$langs->trans("Post-Prompt").'</span>';
384 $out .= '</td>';
385 $out .= '<td>';
386 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput_'.$confkey.'" name="postPrompt" rows="2">'.$config['postPrompt'].'</textarea>';
387 $out .= '</td>';
388 $out .= '</tr>';
389
390 $out .= '<tr id="fichetwothirdright-'.$confkey.'" class="oddeven">';
391 $out .= '<td>'.$form->textwithpicto($langs->trans("BlackListWords"), $langs->trans("BlackListWordsHelp")).'</td>';
392 $out .= '<td>';
393 $out .= '<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklist_'.$confkey.'" name="blacklists" value="'.(isset($config['blacklists']) ? implode(', ', (array) $config['blacklists']) : '').'">';
394 $out .= '</td>';
395 $out .= '</tr>';
396
397 $out .= '<tr>';
398 $out .= '<td>'.$langs->trans("Test").'</td>';
399 $out .= '<td>';
400
401 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
402 $formmail = new FormMail($db);
403 $formmail->withaiprompt = 'html'; // set format
404
405 $showlinktoai = $confkey; // 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', ...
406 $showlinktoailabel = $langs->trans("ToTest");
407 $htmlname = $confkey;
408 $onlyenhancements = $confkey;
409 $showlinktolayout = 0;
410
411 // Fill $out
412 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
413
414 $out .= '<div id="'.$htmlname.'"></div>';
415
416 $out .= '</td>';
417 $out .= '</tr>';
418
419 $out .= '</tbody>';
420 $out .= '</table>';
421
422 $out .= '<center><input type="submit" class="button small submitBtn reposition" name="modify" data-index="'.$confkey.'" value="'.dol_escape_htmltag($langs->trans("Save")).'"/></center>';
423
424 $out .= '</form>';
425
426 $out .= '<br><br>';
427 }
428 }
429
430 print $out;
431
432 print '<br>';
433}
434
435
436if ($action == 'edit' || $action == 'create' || $action == 'deleteproperty') {
437 print load_fiche_titre($langs->trans("AIModelForFeature", $arrayofai[$aiservice]['label']), '', '');
438 print $formSetup->generateOutput(true);
439}
440
441
442if (empty($setupnotempty)) {
443 print '<br>'.$langs->trans("NothingToSetup");
444}
445
446
447// Page end
448print dol_get_fiche_end();
449
450llxFooter();
451$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
getListOfAIFeatures()
Prepare admin pages header.
Definition ai.lib.php:34
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:163
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:64
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:475
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
Class to manage generation of HTML components Only common components must be here.
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
This class help you create setup render.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dolGetButtonTitle($label, $helpText='', $iconClass='fa fa-file', $url='', $id='', $status=1, $params=array())
Function dolGetButtonTitle : this kind of buttons are used in title in list.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.