dolibarr 23.0.3
setup.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 Lamrani Abdel
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 * Coryright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
31require_once DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php";
32require_once DOL_DOCUMENT_ROOT."/ai/lib/ai.lib.php";
33
42$langs->loadLangs(array("admin", "website", "other"));
43
44
45// Parameters
46$action = GETPOST('action', 'aZ09');
47$backtopage = GETPOST('backtopage', 'alpha');
48$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
49
50if (empty($action)) {
51 $action = 'edit';
52}
53
54$content = GETPOST('content');
55
56$error = 0;
57$setupnotempty = 0;
58
59
60// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
61$useFormSetup = 1;
62
63if (!class_exists('FormSetup')) {
64 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
65}
66
67$formSetup = new FormSetup($db);
68
69// List all available IA
70$arrayofai = getListOfAIServices();
71
72// List all available features
73$arrayofaifeatures = getListOfAIFeatures();
74
75$item = $formSetup->newItem('AI_API_SERVICE'); // Name of constant must end with _KEY so it is encrypted when saved into database.
76$item->setAsSelect($arrayofai);
77$item->cssClass = 'minwidth150';
78
79foreach ($arrayofai as $ia => $iarecord) {
80 $ialabel = $iarecord['label'];
81 // Setup conf AI_PUBLIC_INTERFACE_TOPIC
82 /*$item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_ENDPOINT'); // Name of constant must end with _KEY so it is encrypted when saved into database.
83 $item->defaultFieldValue = '';
84 $item->cssClass = 'minwidth500';*/
85
86 $item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_KEY')->setAsSecureKey(); // Name of constant must end with _KEY so it is encrypted when saved into database.
87 $item->nameText = $langs->trans("AI_API_KEY").' ('.$ialabel.')';
88 $item->defaultFieldValue = '';
89 $item->fieldParams['hideGenerateButton'] = 1;
90 $item->fieldParams['trClass'] = 'iaservice '.$ia;
91 $item->cssClass = 'minwidth500 text-security input'.$ia;
92 $item->helpText = '<span class="helptoshow">HelpToShow</span>';
93
94 $item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_URL'); // Name of constant must end with _KEY so it is encrypted when saved into database.
95 $item->nameText = $langs->trans("AI_API_URL").' ('.$ialabel.')';
96 $item->defaultFieldValue = '';
97 $item->fieldParams['trClass'] = 'iaservice iaurl '.$ia;
98 $item->cssClass = 'minwidth500 input'.$ia;
99 if ($ia == 'custom') {
100 $item->fieldAttr['placeholder'] = 'https://domainofapi.com/v1/';
101 }
102}
103
104$setupnotempty = + count($formSetup->items);
105
106
107$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
108
109// Access control
110if (!$user->admin) {
112}
113if (!isModEnabled('ai')) {
114 accessforbidden('Module AI not activated.');
115}
116
117
118/*
119 * Actions
120 */
121
122include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
123
124$action = 'edit';
125
126
127/*
128 * View
129 */
130
131$help_url = '';
132$title = "AiSetup";
133
134llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin');
135
136// Subheader
137$linkback = '<a href="'.($backtopage ? $backtopage : dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1])).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
138
139print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
140
141// Configuration header
142$head = aiAdminPrepareHead();
143print dol_get_fiche_head($head, 'settings', $langs->trans($title), -1, "ai");
144
145
146if ($action == 'edit') {
147 print $formSetup->generateOutput(true);
148} elseif (!empty($formSetup->items)) {
149 print $formSetup->generateOutput();
150 print '<div class="tabsAction">';
151 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'">'.$langs->trans("Modify").'</a>';
152 print '</div>';
153} else {
154 print '<br>'.$langs->trans("NothingToSetup");
155}
156
157
158if (empty($setupnotempty)) {
159 print '<br>'.$langs->trans("NothingToSetup");
160}
161
162print '<script type="text/javascript">
163 jQuery(document).ready(function() {
164 function showHideAIService(aiservice) {
165 console.log("showHideAIService: We select the AI service "+aiservice);
166 jQuery(".iaservice").hide();
167
168 if (aiservice != "-1") {
169 jQuery(".iaservice."+aiservice).show();
170 const arrayofia = {';
171$i = 0;
172foreach ($arrayofai as $key => $airecord) {
173 if ($key == -1) {
174 continue;
175 }
176 if ($i) {
177 print ', ';
178 }
179 $i++;
180 print dol_escape_js($key).': \''.dol_escape_js($airecord['url']).'\'';
181}
182print '};
183 const arrayofextlink = {';
184$i = 0;
185foreach ($arrayofai as $key => $airecord) {
186 if ($key == -1) {
187 continue;
188 }
189 if ($i) {
190 print ', ';
191 }
192 $i++;
193 print dol_escape_js($key).': \''.dol_escape_js($airecord['setup']).'\'';
194}
195print '};
196 console.log("Check URL for .iaurl."+aiservice+" .input"+aiservice);
197 if (jQuery(".iaurl."+aiservice+" .input"+aiservice).val() == \'\') {
198 console.log("URL is empty, we fill with default value of IA selected");
199 jQuery(".iaurl."+aiservice+" .input"+aiservice).val(arrayofia[aiservice]);
200 }
201 jQuery(".helptoshow").text(arrayofextlink[aiservice]);
202 }
203 }
204
205 jQuery("#AI_API_SERVICE").change(function() {
206 var aiservice = $(this).val();
207
208 showHideAIService(aiservice);
209
210 jQuery(".sectiontest").hide(); /* Hide test section, will appear after the save */
211 });
212
213 showHideAIService("'.getDolGlobalString("AI_API_SERVICE").'");
214 });
215</script>';
216
217// Page end
218print dol_get_fiche_end();
219
220
221// The section for test
222
223if (getDolGlobalString("AI_API_SERVICE")) {
224 print '<br><div class="sectiontest">';
225
226 // Section to test
227 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
228 print '<input type="hidden" name="token" value="'.newToken().'">';
229 print '<input type="hidden" name="action" value="add">';
230 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
231
232
233 $key = 'textgenerationother'; // The HTML ID of field to fill
234
235 //if (GETPOST('functioncode') == 'textgenerationemail') {
236
237 print '<br>';
238 //print '<hr>';
239
240 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
241 include_once DOL_DOCUMENT_ROOT."/core/class/html.formai.class.php";
242 $formai = new FormAI($db);
243 $formmail = new FormMail($db);
244
245 $showlinktoai = $key; // 'textgeneration', 'imagegeneration', ...
246 $showlinktoailabel = $langs->trans("AITestText");
247 $showlinktolayout = 0;
248 $htmlname = $key;
249 $formmail->withaiprompt = '';
250
251 // Fill $out
252
253 $out = $langs->trans("Test").': &nbsp; ';
254 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
255 print $out;
256
257 print '<br><textarea id="'.$htmlname.'" placeholder="Lore ipsum..." class="quatrevingtpercent" rows="4"></textarea>'; // The div
258
259 print '<br><br>';
260
261
262 $showlinktoai .= 'html';
263 $htmlname .= 'html';
264 $formmail->withaiprompt = 'html';
265
266 // Fill $out
267 $out = $langs->trans("Test").': &nbsp; ';
268 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
269 print $out;
270
271 print '<br>';
272 $doleditor = new DolEditor($htmlname, '', '', 100, 'dolibarr_details');
273 print $doleditor->Create(1);
274
275 print '</form>';
276
277 print '</div>';
278}
279
280llxFooter();
281$db->close();
getListOfAIFeatures()
Prepare admin pages header.
Definition ai.lib.php:34
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:167
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:64
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 to manage a WYSIWYG editor.
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
This class help you create setup render.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dolBuildUrl($url, $params=[], $addtoken=false)
Return path of url.
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.
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.