* Copyright (C) 2022 Alice Adminson * Copyright (C) 2024 Frédéric France * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file ai/admin/custom_prompt.php * \ingroup ai * \brief Ai other custom page. */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php"; require_once '../lib/ai.lib.php'; $langs->loadLangs(array("admin")); // Parameters $action = GETPOST('action', 'aZ09'); $backtopage = GETPOST('backtopage', 'alpha'); $modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php if (empty($action)) { $action = 'edit'; } $error = 0; $setupnotempty = 0; // Access control if (!$user->admin) { accessforbidden(); } // Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only $useFormSetup = 1; if (!class_exists('FormSetup')) { require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; } $formSetup = new FormSetup($db); // Setup conf AI_PROMPT $item = $formSetup->newItem('AI_CONFIGURATIONS_PROMPT'); $item->defaultFieldValue = ''; $setupnotempty += count($formSetup->items); $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); // List of AI features $arrayofaifeatures = array( 'textgeneration' => array('label' => 'TextGeneration', 'picto'=>'', 'status'=>'development'), 'imagegeneration' => array('label' => 'ImageGeneration', 'picto'=>'', 'status'=>'notused'), 'videogeneration' => array('label' => 'VideoGeneration', 'picto'=>'', 'status'=>'notused'), 'transcription' => array('label' => 'Transcription', 'picto'=>'', 'status'=>'notused'), 'translation' => array('label' => 'Translation', 'picto'=>'', 'status'=>'notused'), 'audiotext' => array('label' => 'AudioText', 'picto'=>'', 'status'=>'notused') ); /* * Actions */ $functioncode = GETPOST('functioncode', 'alpha'); $pre_prompt = GETPOST('prePrompt'); $post_prompt = GETPOST('postPrompt'); // get all configs in const AI $currentConfigurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT'); $currentConfigurations = json_decode($currentConfigurationsJson, true); if ($action == 'update' && GETPOST('cancel')) { $action = 'edit'; } if ($action == 'update' && !GETPOST('cancel')) { $error = 0; if (empty($functioncode)) { $error++; setEventMessages($langs->trans('ErrorInputRequired'), null, 'errors'); } if (!is_array($currentConfigurations)) { $currentConfigurations = []; } if (empty($functioncode) || (empty($pre_prompt) && empty($post_prompt))) { if (isset($currentConfigurations[$functioncode])) { unset($currentConfigurations[$functioncode]); } } else { $currentConfigurations[$functioncode] = [ 'prePrompt' => $pre_prompt, 'postPrompt' => $post_prompt, ]; } $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE); $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity); if (!$error) { if ($result) { header("Location: ".$_SERVER['PHP_SELF']); setEventMessages($langs->trans("SetupSaved"), null, 'mesgs'); exit; } else { setEventMessages($langs->trans("ErrorUpdating"), null, 'errors'); } } $action = 'edit'; } if ($action == 'updatePrompts') { $key = GETPOST('key', 'alpha'); $currentConfigurations[$key] = [ 'prePrompt' => $pre_prompt, 'postPrompt' => $post_prompt, ]; $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE); $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity); if (!$error) { $action = ''; if ($result) { header("Location: ".$_SERVER['PHP_SELF']); setEventMessages($langs->trans("SetupSaved"), null, 'mesgs'); exit; } else { setEventMessages($langs->trans("ErrorUpdating"), null, 'errors'); } } } if ($action == 'confirm_deleteproperty' && GETPOST('confirm') == 'yes') { $key = GETPOST('key', 'alpha'); if (isset($currentConfigurations[$key])) { unset($currentConfigurations[$key]); $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE); $res = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity); if ($res) { header("Location: ".$_SERVER['PHP_SELF']); setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs'); exit; } else { setEventMessages($langs->trans("NoRecordDeleted"), null, 'errors'); } } } /* * View */ $form = new Form($db); $help_url = ''; $title = "AiSetup"; llxHeader('', $langs->trans($title), $help_url); // Subheader $linkback = ''.$langs->trans("BackToModuleList").''; print load_fiche_titre($langs->trans($title), $linkback, 'title_setup'); // Configuration header $head = aiAdminPrepareHead(); print dol_get_fiche_head($head, 'custom', $langs->trans($title), -1, "fa-microchip"); //$newbutton = ''.$langs->trans("New").''; $newbutton = ''; print load_fiche_titre($langs->trans("AIPromptForFeatures"), $newbutton, ''); if ($action == 'deleteproperty') { $formconfirm = $form->formconfirm( $_SERVER["PHP_SELF"].'?key='.urlencode(GETPOST('key', 'alpha')), $langs->trans('Delete'), $langs->trans('ConfirmDeleteSetup', GETPOST('key', 'alpha')), 'confirm_deleteproperty', '', 0, 1 ); print $formconfirm; } if ($action == 'edit') { $out = '
'; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= '
'.$langs->trans('Add').'
'; $out .= ''.$langs->trans("Feature").''; $out .= ''; // Combo list of AI features $out .= ''; $out .= ajax_combobox("functioncode"); $out .= '
'; $out .= 'pre-Prompt'; $out .= ''; $out .= ''; $out .= '
'; $out .= 'Post-prompt'; $out .= ''; $out .= ''; $out .= '
'; $out .= $form->buttonsSaveCancel("Add", ""); $out .= '
'; $out .= '


'; print $out; } if ($action == 'edit' || $action == 'create') { $out = ''; if (!empty($currentConfigurations)) { $out = ''; foreach ($currentConfigurations as $key => $config) { if (!empty($key) && !preg_match('/^[a-z]+$/i', $key)) { // Ignore empty saved setup continue; } $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; } $out .= ''; $out .= '
'.$arrayofaifeatures[$key]['picto'].' '.$langs->trans($arrayofaifeatures[$key]['label']); $out .= ''.img_edit().''; $out .= ''.img_delete().''; $out .= '
'; $out .= 'pre-Prompt'; $out .= ''; $out .= ''; $out .= '
'; $out .= 'Post-prompt'; $out .= ''; $out .= ''; $out .= '
'; $out .= '
'; } $out .= ""; print $out; print '
'; } if (empty($setupnotempty)) { print '
'.$langs->trans("NothingToSetup"); } // Page end print dol_get_fiche_end(); llxFooter(); $db->close();