dolibarr 24.0.0-beta
knows-hooks.php
1<?php
2/*
3 * Copyright (C) 2025 Anthony Damhet <a.damhet@progiseize.fr>
4 * Copyright (C) 2025 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
20// Load Dolibarr environment
21require '../../../../main.inc.php';
22
30// Protection if external user
31if ($user->socid > 0) {
33}
34
35// Includes
36require_once DOL_DOCUMENT_ROOT . '/admin/tools/ui/class/documentation.class.php';
37
38// Load documentation translations
39$langs->load('uxdocumentation');
40
41//
42$documentation = new Documentation($db);
43$group = 'UxDolibarrContext';
44$experimentName = 'UxDolibarrContextKnowsHooks';
45
46$js = [
47 '/includes/ace/src/ace.js',
48 '/includes/ace/src/ext-statusbar.js',
49 '/includes/ace/src/ext-language_tools.js'
50];
51$css = [];
52
53// Output html head + body - Param is Title
54$documentation->docHeader($langs->trans($experimentName, $group), $js, $css);
55
56// Set view for menu and breadcrumb
57$documentation->view = [$group, 'UxDolibarrContext', $experimentName];
58
59// Output sidebar
60$documentation->showSidebar(); ?>
61
62<div class="doc-wrapper">
63
64 <?php $documentation->showBreadCrumb(); ?>
65
66 <div class="doc-content-wrapper">
67
68 <h1 class="documentation-title"><?php echo $langs->trans($experimentName); ?></h1>
69
70 <?php $documentation->showSummary(); ?>
71
72 <div class="documentation-section">
73 <h2 id="titlesection-basicusage" class="documentation-title">Introduction</h2>
74
75 <p>
76 Some hooks are not natively triggered by Dolibarr; instead, they rely on external modules. Therefore, we document them here to ensure everyone uses the same method of triggering them, until we provide a standardized native trigger, which does not yet exist.<br/>
77 Please refer to the "How it works" section for further details.
78 </p>
79
80 </div>
81
82 <div class="documentation-section">
83 <h2 id="reloadDocumentLine" class="documentation-title">Hook : reloadDocumentLine</h2>
84 <p>
85 Next, let’s focus on the “reloadDocumentLine” hook. First, it’s important to note that this hook is not triggered automatically by Dolibarr.
86 <br/>Instead, it must be activated via external modules. In the future, we plan to introduce a class directly tied to the object within Dolibarr tools, allowing this hook to be triggered natively. However, although Dolibarr does not currently initiate the trigger itself, it does listen for it.
87 <br/>This is because it uses this trigger to reload certain elements on the lines, particularly the drag-and-drop system for rearranging line items in the document.
88 </p>
89 <p>
90 In the meantime, here is how you can use the event listener.
91 <br/>The process involves two steps: first, you trigger the event, and then you handle it with an event listener.
92 <br/>Below, you will find a code example of how to implement this.
93 </p>
94 <h4>Listen event</h4>
95 <div class="documentation-example">
96 <?php
97 $lines = array(
98 '<script>',
99 'Dolibarr.on(\'reloadDocumentLine\',',
100 ' /** @param {{lineId:number, lineElement:string}} data */',
101 ' function (data) {',
102 ' // Do your stuff',
103 ' }',
104 ');',
105 '</script>',
106 );
107 $documentation->showCode($lines, 'php');
108 ?>
109 </div>
110
111 <h4>Trigger event</h4>
112 <div class="documentation-example">
113 <?php
114 $lines = array(
115 '<script>',
116 ' const rowSelector = \'#row-\' + lineId;',
117 ' const $row = $(rowSelector);',
118 '',
119 ' // newRow is the newly created row element that will replace the existing row',
120 ' $row.replaceWith(newRow);',
121 '',
122 ' // Trigger the hook to dispatch reloaded line event.',
123 ' // This hook will by used to rebuild drag and drop lines order system for example ',
124 ' Dolibarr.executeHook(\'reloadDocumentLine\', {lineId, lineElement});',
125 '',
126 ' // Trigger initNewContent for all common Dom reloaded content. This will reload tooltips system for example ',
127 ' // This will reload tooltips system for example ',
128 ' Dolibarr.initNewContent(rowSelector);',
129 '</script>',
130 );
131 $documentation->showCode($lines, 'php');
132 ?>
133 </div>
134 </div>
135
136 </div>
137</div>
138
139</div>
140<?php
141// Output close body + html
142$documentation->docFooter();
143?>
Class to manage UI documentation.
Class for Hook.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.