2024-01-16 15:52:52 +01:00
< ? php
2024-07-08 22:10:25 +02:00
/* Copyright ( C ) 2004 - 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2022 Alice Adminson < aadminson @ example . com >
2024-03-12 16:16:29 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-04-06 17:38:39 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2024-07-08 22:10:25 +02:00
* Coryright ( C ) 2024 Alexandre Spangaro < alexandre @ inovea - conseil . com >
2024-01-16 15:52:52 +01:00
*
* 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 < https :// www . gnu . org / licenses />.
*/
/**
2024-04-06 17:38:39 +02:00
* \file htdocs / ai / admin / setup . php
2024-01-16 15:52:52 +01:00
* \ingroup ai
* \brief Ai setup page .
*/
// Load Dolibarr environment
require '../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . " /core/lib/admin.lib.php " ;
require_once '../lib/ai.lib.php' ;
2024-11-04 23:53:20 +01:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
2024-02-14 01:42:38 +01:00
$langs -> loadLangs ( array ( " admin " ));
2024-01-16 15:52:52 +01:00
// Parameters
$action = GETPOST ( 'action' , 'aZ09' );
$backtopage = GETPOST ( 'backtopage' , 'alpha' );
$modulepart = GETPOST ( 'modulepart' , 'aZ09' ); // Used by actions_setmoduleoptions.inc.php
2024-02-10 10:38:29 +01:00
if ( empty ( $action )) {
$action = 'edit' ;
}
2024-01-16 15:52:52 +01:00
$value = GETPOST ( 'value' , 'alpha' );
$label = GETPOST ( 'label' , 'alpha' );
$scandir = GETPOST ( 'scan_dir' , 'alpha' );
$type = 'myobject' ;
$error = 0 ;
$setupnotempty = 0 ;
2024-01-27 02:22:25 +01:00
2024-01-16 15:52:52 +01:00
// 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 );
2024-03-06 00:46:04 +01:00
// List all available IA
2024-04-24 13:09:43 +02:00
$arrayofia = array (
'chatgpt' => 'ChatGPT' ,
2024-06-06 00:32:43 +02:00
'groq' => 'Groq' ,
2024-07-16 18:08:26 +02:00
'custom' => 'Custom'
2024-04-24 13:09:43 +02:00
//'gemini' => 'Gemini'
);
2024-01-16 15:52:52 +01:00
2024-04-24 13:07:47 +02:00
$item = $formSetup -> newItem ( 'AI_API_SERVICE' ); // Name of constant must end with _KEY so it is encrypted when saved into database.
$item -> setAsSelect ( $arrayofia );
2024-06-05 23:49:09 +02:00
$item -> cssClass = 'minwidth150' ;
2024-04-24 13:07:47 +02:00
2024-04-24 11:26:08 +02:00
foreach ( $arrayofia as $ia => $ialabel ) {
2024-01-27 02:22:25 +01:00
// Setup conf AI_PUBLIC_INTERFACE_TOPIC
2024-03-06 00:46:04 +01:00
/* $item = $formSetup -> newItem ( 'AI_API_' . strtoupper ( $ia ) . '_ENDPOINT' ); // Name of constant must end with _KEY so it is encrypted when saved into database.
2024-01-27 02:22:25 +01:00
$item -> defaultFieldValue = '' ;
2024-03-06 00:46:04 +01:00
$item -> cssClass = 'minwidth500' ; */
2024-01-16 15:52:52 +01:00
2024-06-06 00:32:43 +02:00
$item = $formSetup -> newItem ( 'AI_API_' . strtoupper ( $ia ) . '_KEY' ) -> setAsSecureKey (); // Name of constant must end with _KEY so it is encrypted when saved into database.
2024-04-24 11:26:08 +02:00
$item -> nameText = $langs -> trans ( " AI_API_KEY " ) . ' (' . $ialabel . ')' ;
2024-03-06 00:46:04 +01:00
$item -> defaultFieldValue = '' ;
2024-06-06 00:32:43 +02:00
$item -> fieldParams [ 'hideGenerateButton' ] = 1 ;
2024-07-19 12:06:37 +02:00
$item -> fieldParams [ 'trClass' ] = $ia ;
2024-06-06 00:32:43 +02:00
$item -> cssClass = 'minwidth500 text-security' ;
2024-07-16 18:08:26 +02:00
$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 = '' ;
2024-07-19 12:06:37 +02:00
$item -> fieldParams [ 'trClass' ] = $ia ;
2024-07-16 18:08:26 +02:00
$item -> cssClass = 'minwidth500' ;
2024-03-06 00:46:04 +01:00
}
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
$setupnotempty = + count ( $formSetup -> items );
2024-01-16 15:52:52 +01:00
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2024-07-16 18:08:26 +02:00
// Access control
if ( ! $user -> admin ) {
accessforbidden ();
}
if ( ! isModEnabled ( 'ai' )) {
accessforbidden ( 'Module AI not activated.' );
}
2024-01-16 15:52:52 +01:00
/*
* Actions
*/
include DOL_DOCUMENT_ROOT . '/core/actions_setmoduleoptions.inc.php' ;
2024-02-10 10:38:29 +01:00
$action = 'edit' ;
2024-01-16 15:52:52 +01:00
/*
* View
*/
2024-03-12 16:16:29 +01:00
$form = new Form ( $db );
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
$help_url = '' ;
$title = " AiSetup " ;
2024-01-16 15:52:52 +01:00
2024-07-08 22:10:25 +02:00
llxHeader ( '' , $langs -> trans ( $title ), $help_url , '' , 0 , 0 , '' , '' , '' , 'mod-ai page-admin' );
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
// Subheader
$linkback = '<a href="' . ( $backtopage ? $backtopage : DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1' ) . '">' . $langs -> trans ( " BackToModuleList " ) . '</a>' ;
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
print load_fiche_titre ( $langs -> trans ( $title ), $linkback , 'title_setup' );
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
// Configuration header
$head = aiAdminPrepareHead ();
print dol_get_fiche_head ( $head , 'settings' , $langs -> trans ( $title ), - 1 , " fa-microchip " );
2024-01-16 15:52:52 +01:00
if ( $action == 'edit' ) {
2024-03-12 16:16:29 +01:00
print $formSetup -> generateOutput ( true );
print '<br>' ;
2024-01-16 15:52:52 +01:00
} elseif ( ! empty ( $formSetup -> items )) {
print $formSetup -> generateOutput ();
print '<div class="tabsAction">' ;
print '<a class="butAction" href="' . $_SERVER [ " PHP_SELF " ] . '?action=edit&token=' . newToken () . '">' . $langs -> trans ( " Modify " ) . '</a>' ;
print '</div>' ;
} else {
2024-03-12 16:16:29 +01:00
print '<br>' . $langs -> trans ( " NothingToSetup " );
2024-01-16 15:52:52 +01:00
}
if ( empty ( $setupnotempty )) {
2024-03-12 16:16:29 +01:00
print '<br>' . $langs -> trans ( " NothingToSetup " );
2024-01-16 15:52:52 +01:00
}
2024-03-12 16:16:29 +01:00
// Page end
print dol_get_fiche_end ();
2024-01-16 15:52:52 +01:00
2024-03-12 16:16:29 +01:00
llxFooter ();
$db -> close ();