dolibarr 22.0.5
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 Alice Adminson <aadminson@example.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 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
93 $item = $formSetup->newItem('AI_API_'.strtoupper($ia).'_URL'); // Name of constant must end with _KEY so it is encrypted when saved into database.
94 $item->nameText = $langs->trans("AI_API_URL").' ('.$ialabel.')';
95 $item->defaultFieldValue = '';
96 $item->fieldParams['trClass'] = 'iaservice iaurl '.$ia;
97 $item->cssClass = 'minwidth500 input'.$ia;
98 if ($ia == 'custom') {
99 $item->fieldAttr['placeholder'] = 'https://domainofapi.com/v1/';
100 }
101}
102
103$setupnotempty = + count($formSetup->items);
104
105
106$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
107
108// Access control
109if (!$user->admin) {
111}
112if (!isModEnabled('ai')) {
113 accessforbidden('Module AI not activated.');
114}
115
116
117/*
118 * Actions
119 */
120
121include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
122
123$action = 'edit';
124
125
126/*
127 * View
128 */
129
130$help_url = '';
131$title = "AiSetup";
132
133llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin');
134
135// Subheader
136$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
137
138print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
139
140// Configuration header
141$head = aiAdminPrepareHead();
142print dol_get_fiche_head($head, 'settings', $langs->trans($title), -1, "ai");
143
144
145if ($action == 'edit') {
146 print $formSetup->generateOutput(true);
147} elseif (!empty($formSetup->items)) {
148 print $formSetup->generateOutput();
149 print '<div class="tabsAction">';
150 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'">'.$langs->trans("Modify").'</a>';
151 print '</div>';
152} else {
153 print '<br>'.$langs->trans("NothingToSetup");
154}
155
156
157if (empty($setupnotempty)) {
158 print '<br>'.$langs->trans("NothingToSetup");
159}
160
161print '<script type="text/javascript">
162 jQuery(document).ready(function() {
163 function showHideAIService(aiservice) {
164 console.log("showHideAIService: We select the AI service "+aiservice);
165 jQuery(".iaservice").hide();
166
167 if (aiservice != "-1") {
168 jQuery(".iaservice."+aiservice).show();
169 const arrayofia = {';
170$i = 0;
171foreach ($arrayofai as $key => $airecord) {
172 if ($key == -1) {
173 continue;
174 }
175 if ($i) {
176 print ', ';
177 }
178 $i++;
179 print dol_escape_js($key).': \''.dol_escape_js($airecord['url']).'\'';
180}
181print '};
182 console.log("Check URL for .iaurl."+aiservice+" .input"+aiservice);
183 if (jQuery(".iaurl."+aiservice+" .input"+aiservice).val() == \'\') {
184 console.log("URL is empty, we fill with default value of IA selected");
185 jQuery(".iaurl."+aiservice+" .input"+aiservice).val(arrayofia[aiservice]);
186 }
187 }
188 }
189
190 jQuery("#AI_API_SERVICE").change(function() {
191 var aiservice = $(this).val();
192
193 showHideAIService(aiservice);
194 });
195
196 showHideAIService("'.getDolGlobalString("AI_API_SERVICE").'");
197 });
198</script>';
199
200// Page end
201print dol_get_fiche_end();
202
203
204// The section for test
205
206if (getDolGlobalString("AI_API_SERVICE")) {
207 print '<br>';
208
209 // Section to test
210 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
211 print '<input type="hidden" name="token" value="'.newToken().'">';
212 print '<input type="hidden" name="action" value="add">';
213 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
214
215
216 $key = 'textgenerationother'; // The HTML ID of field to fill
217
218 //if (GETPOST('functioncode') == 'textgenerationemail') {
219
220 print '<br>';
221 //print '<hr>';
222
223 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
224 include_once DOL_DOCUMENT_ROOT."/core/class/html.formai.class.php";
225 $formai = new FormAI($db);
226 $formmail = new FormMail($db);
227
228 $showlinktoai = $key; // 'textgeneration', 'imagegeneration', ...
229 $showlinktoailabel = $langs->trans("AITestText");
230 $showlinktolayout = 0;
231 $htmlname = $key;
232 $formmail->withaiprompt = '';
233
234 // Fill $out
235
236 $out = $langs->trans("Test").': &nbsp; ';
237 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
238 print $out;
239
240 print '<br><textarea id="'.$htmlname.'" placeholder="Lore ipsum..." class="quatrevingtpercent" rows="4"></textarea>'; // The div
241
242 print '<br><br>';
243
244
245 $showlinktoai .= 'html';
246 $htmlname .= 'html';
247 $formmail->withaiprompt = 'html';
248
249 // Fill $out
250 $out = $langs->trans("Test").': &nbsp; ';
251 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
252 print $out;
253
254 print '<br>';
255 $doleditor = new DolEditor($htmlname, '', '', 100, 'dolibarr_details');
256 print $doleditor->Create(1);
257
258
259 print '</form>';
260}
261
262llxFooter();
263$db->close();
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
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.