dolibarr 21.0.0-beta
webhook.php
1<?php
2/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
29require_once DOL_DOCUMENT_ROOT.'/webhook/lib/webhook.lib.php';
30
39// Translations
40$langs->loadLangs(array("admin", "webhook"));
41
42// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
43$hookmanager->initHooks(array('webhooksetup', 'globalsetup'));
44
45// Access control
46if (!$user->admin) {
48}
49
50// Parameters
51$action = GETPOST('action', 'aZ09');
52$backtopage = GETPOST('backtopage', 'alpha');
53$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
54
55$value = GETPOST('value', 'alpha');
56$label = GETPOST('label', 'alpha');
57$scandir = GETPOST('scan_dir', 'alpha');
58$type = 'myobject';
59
60
61$error = 0;
62$setupnotempty = 0;
63
64// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
65$useFormSetup = 1;
66
67if (!class_exists('FormSetup')) {
68 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
69}
70
71$formSetup = new FormSetup($db);
72
73
74$setupnotempty = count($formSetup->items);
75
76
77$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
78
79
80/*
81 * Actions
82 */
83
84include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
85
86if ($action == 'updateMask') {
87 $maskconst = GETPOST('maskconst', 'aZ09');
88 $maskvalue = GETPOST('maskvalue', 'alpha');
89
90 if ($maskconst && preg_match('/_MASK$/', $maskconst)) {
91 $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
92 if (!($res > 0)) {
93 $error++;
94 }
95 }
96
97 if (!$error) {
98 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
99 } else {
100 setEventMessages($langs->trans("Error"), null, 'errors');
101 }
102} elseif ($action == 'setmod') {
103 // TODO Check if numbering module chosen can be activated by calling method canBeActivated
104 $tmpobjectkey = GETPOST('object', 'aZ09');
105 if (!empty($tmpobjectkey)) {
106 $constforval = 'WEBHOOK_'.strtoupper($tmpobjectkey)."_ADDON";
107 dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity);
108 }
109} elseif ($action == 'set') {
110 // Activate a model
111 $ret = addDocumentModel($value, $type, $label, $scandir);
112} elseif ($action == 'del') {
113 $ret = delDocumentModel($value, $type);
114 if ($ret > 0) {
115 $tmpobjectkey = GETPOST('object', 'aZ09');
116 if (!empty($tmpobjectkey)) {
117 $constforval = 'WEBHOOK_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
118 if (getDolGlobalString($constforval) == "$value") {
119 dolibarr_del_const($db, $constforval, $conf->entity);
120 }
121 }
122 }
123} elseif ($action == 'setdoc') {
124 // Set or unset default model
125 $tmpobjectkey = GETPOST('object', 'aZ09');
126 if (!empty($tmpobjectkey)) {
127 $constforval = 'WEBHOOK_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
128 if (dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity)) {
129 // The constant that was read before the new set
130 // We therefore requires a variable to have a coherent view
131 $conf->global->$constforval = $value;
132 }
133
134 // We disable/enable the document template (into llx_document_model table)
135 $ret = delDocumentModel($value, $type);
136 if ($ret > 0) {
137 $ret = addDocumentModel($value, $type, $label, $scandir);
138 }
139 }
140} elseif ($action == 'unsetdoc') {
141 $tmpobjectkey = GETPOST('object', 'aZ09');
142 if (!empty($tmpobjectkey)) {
143 $constforval = 'WEBHOOK_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
144 dolibarr_del_const($db, $constforval, $conf->entity);
145 }
146}
147
148
149
150/*
151 * View
152 */
153
154$form = new Form($db);
155
156$help_url = '';
157$page_name = "WebhookSetup";
158
159llxHeader('', $langs->trans($page_name), $help_url, '', 0, 0, '', '', '', 'mod-admin page-webhook');
160
161// Subheader
162$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
163
164print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
165
166// Configuration header
168print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "webhook");
169
170print '<br>';
171
172// Setup page goes here
173print '<span class="opacitymedium">'.$langs->trans("WebhookSetupPage", $langs->transnoentitiesnoconv("Targets")).'...</span><br><br>';
174
175
176if ($action == 'edit') {
177 if ($useFormSetup && (float) DOL_VERSION >= 15) { // @phpstan-ignore-line
178 print $formSetup->generateOutput(true);
179 } else {
180 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
181 print '<input type="hidden" name="token" value="'.newToken().'">';
182 print '<input type="hidden" name="action" value="update">';
183
184 print '<table class="noborder centpercent">';
185 print '<tr class="liste_titre"><td class="titlefield">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
186
187 // @phan-suppress-next-line PhanEmptyForeach
188 foreach ($arrayofparameters as $constname => $val) {
189 if ($val['enabled'] == 1) {
190 $setupnotempty++;
191 print '<tr class="oddeven"><td>';
192 $tooltiphelp = (($langs->trans($constname . 'Tooltip') != $constname . 'Tooltip') ? $langs->trans($constname . 'Tooltip') : '');
193 print '<span id="helplink'.$constname.'" class="spanforparamtooltip">'.$form->textwithpicto($langs->trans($constname), $tooltiphelp, 1, 'info', '', 0, 3, 'tootips'.$constname).'</span>';
194 print '</td><td>';
195
196 if ($val['type'] == 'textarea') {
197 print '<textarea class="flat" name="'.$constname.'" id="'.$constname.'" cols="50" rows="5" wrap="soft">' . "\n";
198 print getDolGlobalString($constname);
199 print "</textarea>\n";
200 } elseif ($val['type'] == 'html') {
201 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
202 $doleditor = new DolEditor($constname, getDolGlobalString($constname), '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
203 $doleditor->Create();
204 } elseif ($val['type'] == 'yesno') {
205 print $form->selectyesno($constname, getDolGlobalString($constname), 1);
206 } elseif (preg_match('/emailtemplate:/', $val['type'])) {
207 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
208 $formmail = new FormMail($db);
209
210 $tmp = explode(':', $val['type']);
211 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
212 //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, '');
213 $arrayofmessagename = array();
214 if (is_array($formmail->lines_model)) {
215 foreach ($formmail->lines_model as $modelmail) {
216 //var_dump($modelmail);
217 $moreonlabel = '';
218 if (!empty($arrayofmessagename[$modelmail->label])) {
219 $moreonlabel = ' <span class="opacitymedium">(' . $langs->trans("SeveralLangugeVariatFound") . ')</span>';
220 }
221 // The 'label' is the key that is unique if we exclude the language
222 $arrayofmessagename[$modelmail->id] = $langs->trans(preg_replace('/\‍(|\‍)/', '', $modelmail->label)) . $moreonlabel;
223 }
224 }
225 print $form->selectarray($constname, $arrayofmessagename, getDolGlobalString($constname), 'None', 0, 0, '', 0, 0, 0, '', '', 1);
226 } elseif (preg_match('/category:/', $val['type'])) {
227 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
228 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
229 $formother = new FormOther($db);
230
231 $tmp = explode(':', $val['type']);
232 print img_picto('', 'category', 'class="pictofixedwidth"');
233 print $formother->select_categories($tmp[1], getDolGlobalString($constname), $constname, 0, $langs->trans('CustomersProspectsCategoriesShort'));
234 } elseif (preg_match('/thirdparty_type/', $val['type'])) {
235 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
236 $formcompany = new FormCompany($db);
237 print $formcompany->selectProspectCustomerType(getDolGlobalString($constname), $constname);
238 } elseif ($val['type'] == 'securekey') {
239 print '<input required="required" type="text" class="flat" id="'.$constname.'" name="'.$constname.'" value="'.(GETPOST($constname, 'alpha') ? GETPOST($constname, 'alpha') : getDolGlobalString($constname)).'" size="40">';
240 if (!empty($conf->use_javascript_ajax)) {
241 print '&nbsp;'.img_picto($langs->trans('Generate'), 'refresh', 'id="generate_token'.$constname.'" class="linkobject"');
242 }
243
244 // Add button to autosuggest a key
245 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
246 print dolJSToSetRandomPassword($constname, 'generate_token'.$constname);
247 } elseif ($val['type'] == 'product') {
248 if (isModEnabled("product") || isModEnabled("service")) {
249 $selected = getDolGlobalString($constname);
250 $form->select_produits($selected, $constname, '', 0);
251 }
252 } else {
253 print '<input name="'.$constname.'" class="flat '.(empty($val['css']) ? 'minwidth200' : $val['css']).'" value="'.getDolGlobalString($constname).'">';
254 }
255 print '</td></tr>';
256 }
257 }
258 print '</table>';
259
260 print '<br><div class="center">';
261 print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
262 print '</div>';
263
264 print '</form>';
265 }
266
267 print '<br>';
268} else {
269 if ($useFormSetup && (float) DOL_VERSION >= 15) { // @phpstan-ignore-line
270 if (!empty($formSetup->items)) {
271 print $formSetup->generateOutput();
272 }
273 } else {
274 if (!empty($arrayofparameters)) {
275 print '<table class="noborder centpercent">';
276 print '<tr class="liste_titre"><td class="titlefield">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
277
278 foreach ($arrayofparameters as $constname => $val) {
279 if ($val['enabled'] == 1) {
280 $setupnotempty++;
281 print '<tr class="oddeven"><td>';
282 $tooltiphelp = (($langs->trans($constname . 'Tooltip') != $constname . 'Tooltip') ? $langs->trans($constname . 'Tooltip') : '');
283 print $form->textwithpicto($langs->trans($constname), $tooltiphelp);
284 print '</td><td>';
285
286 if ($val['type'] == 'textarea') {
287 print dol_nl2br(getDolGlobalString($constname));
288 } elseif ($val['type'] == 'html') {
289 print getDolGlobalString($constname);
290 } elseif ($val['type'] == 'yesno') {
291 print ajax_constantonoff($constname);
292 } elseif (preg_match('/emailtemplate:/', $val['type'])) {
293 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
294 $formmail = new FormMail($db);
295
296 $tmp = explode(':', $val['type']);
297
298 $template = $formmail->getEMailTemplate($db, $tmp[1], $user, $langs, getDolGlobalString($constname));
299 if ($template < 0) {
300 setEventMessages(null, $formmail->errors, 'errors');
301 }
302 print $langs->trans($template->label);
303 } elseif (preg_match('/category:/', $val['type'])) {
304 $c = new Categorie($db);
305 $result = $c->fetch(getDolGlobalString($constname));
306 if ($result < 0) {
307 setEventMessages(null, $c->errors, 'errors');
308 } elseif ($result > 0) {
309 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
310 $toprint = array();
311 foreach ($ways as $way) {
312 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
313 }
314 print '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
315 }
316 } elseif (preg_match('/thirdparty_type/', $val['type'])) {
317 if (getDolGlobalInt($constname) == 2) {
318 print $langs->trans("Prospect");
319 } elseif (getDolGlobalInt($constname) == 3) {
320 print $langs->trans("ProspectCustomer");
321 } elseif (getDolGlobalInt($constname) == 1) {
322 print $langs->trans("Customer");
323 } elseif (getDolGlobalInt($constname) == 0) {
324 print $langs->trans("NorProspectNorCustomer");
325 }
326 } elseif ($val['type'] == 'product') {
327 $product = new Product($db);
328 $resprod = $product->fetch(getDolGlobalInt($constname));
329 if ($resprod > 0) {
330 print $product->ref;
331 } elseif ($resprod < 0) {
332 setEventMessages(null, $object->errors, "errors");
333 }
334 } else {
335 print getDolGlobalString($constname);
336 }
337 print '</td></tr>';
338 }
339 }
340
341 print '</table>';
342 }
343 }
344
345 if ($setupnotempty) {
346 print '<div class="tabsAction">';
347 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'">'.$langs->trans("Modify").'</a>';
348 print '</div>';
349 } else {
350 //print '<br>'.$langs->trans("NothingToSetup");
351 }
352}
353
354/*
355if (empty($setupnotempty)) {
356 print '<br>'.$langs->trans("NothingToSetup");
357}
358*/
359
360// Page end
361print dol_get_fiche_end();
362
363llxFooter();
364$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
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).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
delDocumentModel($name, $type)
Delete document model used by doc generator.
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:71
Class to manage categories.
Class to manage a WYSIWYG editor.
Class to build HTML component for third parties management Only common components are here.
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...
Class permettant la generation de composants html autre Only common components are here.
This class help you create setup render.
Class to manage products or services.
llxFooter()
Footer empty.
Definition document.php:107
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_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
webhookAdminPrepareHead()
Prepare admin pages header.