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