Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop

This commit is contained in:
Laurent Destailleur 2025-11-21 00:07:49 +01:00
commit 6dcaede8fc
9 changed files with 220 additions and 23 deletions

View file

@ -256,7 +256,13 @@ class Documentation
'url' => dol_buildpath($this->baseUrl.'/experimental/experiments/dolibarr-context/index.php', 1),
'icon' => 'fas fa-flask',
'submenu' => array(),
'summary' => array(),
'summary' => array(
'Introduction' => '#titlesection-basicusage',
'ConsoleHelp' => '#titlesection-console-help',
'JSDolibarrhooks' => '#titlesection-hooks',
'ExampleOfCreatingNewContextTool' => '#titlesection-create-tool-example',
'SetEventMessageTool' => '#titlesection-tool-seteventmessage',
),
),
)
);

View file

@ -0,0 +1,29 @@
// This file is purely for IDE autocompletion and developer convenience.
// It is never executed or loaded in Dolibarr itself.
// MOCK DEFINITION: Dolibarr.tools
// This mock helps your code editor understand the structure of Dolibarr.tools
// and provides autocomplete hints, parameter hints, and inline documentation.
// You can safely edit this file to add all standard Dolibarr tools for autocompletion.
var Dolibarr = {
tools: {
/**
* Displays a Dolibarr notification message (success, warning, or error).
* This is the JavaScript equivalent of the PHP setEventMessage tool.
*
* @param {string} msg The message text to display
* @param {string=} type Optional: 'mesgs' (default), 'warnings', or 'errors'
* @param {boolean=} sticky Optional: true if the message should stay until manually closed
*
* Example usage in your IDE:
* Dolibarr.tools.setEventMessage('Operation successful', 'success');
*/
setEventMessage: function(msg, type, sticky) {},
// You can add more standard Dolibarr tools here for IDE autocompletion.
// Example:
// alertUser: function(msg) {},
}
};

View file

@ -16,6 +16,9 @@
* Dolibarr Global Context (UMD)
* Provides a secure global object window.Dolibarr
* with non-replaceable tools, events and debug mode.
*
* See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.
*
*/
(function (root, factory) {
// Support AMD
@ -61,6 +64,8 @@
* @param {string} name Name of the tool
* @param {*} value Function, class or object
* @param {boolean} overwrite Explicitly allow overwriting an existing tool
*
* See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.
*/
defineTool(name, value, overwrite = false, triggerHook = true) {
// Prevent silent overrides unless "overwrite" is true
@ -76,7 +81,7 @@
enumerable: true,
});
this.log(`Tool defined: ${name}, triggerHook: ${triggerHook}`);
this.log(`Tool defined: ${name}, triggerHook: ${triggerHook}, overwrite: ${overwrite} `);
if(triggerHook) {
Dolibarr.executeHook('defineTool', { toolName: name, overwrite: overwrite });
}
@ -124,7 +129,7 @@
// Dispatch on internal EventTarget
_events.dispatchEvent(ev);
// Dispatch globally on document so document.addEventListener('DolibarrHook:' + hookName) can catch it
// Dispatch globally on document so document.addEventListener('Dolibarr:' + hookName) can catch it
if (typeof document !== "undefined") {
document.dispatchEvent(new CustomEvent('Dolibarr:' + hookName, { detail: data }));
}
@ -192,7 +197,7 @@
Dolibarr.tools.showConsoleHelp();
// Trigger DolibarrContext:init as DOM ready
// Trigger Dolibarr:Ready as DOM ready
(function triggerContextInit() {
const initHook = () => {
Dolibarr.executeHook('Ready', { context: Dolibarr });

View file

@ -0,0 +1,34 @@
document.addEventListener('Dolibarr:Ready', function(e) {
// this tool allow overwrite because of DISABLE_JQUERY_JNOTIFY conf
/**
* status : 'mesgs' by default, 'warnings', 'errors'
*/
Dolibarr.defineTool('setEventMessage', (msg, status = 'mesgs', sticky = false) =>{
// Normalize status to match jNotify expected values
const normalizeStatus = (s) => {
s = (s || '').toLowerCase();
if (s === 'error' || s === 'errors') return 'error';
if (s === 'warning' || s === 'warnings') return 'warning';
return '';
};
let jnotifyConf = {
delay: 1500 // the default time to show each notification (in milliseconds)
, type : normalizeStatus(status)
, sticky: sticky // determines if the message should be considered "sticky" (user must manually close notification)
, closeLabel: "×" // the HTML to use for the "Close" link
, showClose: true // determines if the "Close" link should be shown if notification is also sticky
, fadeSpeed: 150 // the speed to fade messages out (in milliseconds)
, slideSpeed: 250 // the speed used to slide messages out (in milliseconds)
}
if(msg.length > 0){
$.jnotify(msg, jnotifyConf);
}
else{
Dolibarr.log('setEventMessage : Message is empty');
}
}, true);
});

View file

@ -49,6 +49,7 @@ $js = [
'/includes/ace/src/ext-statusbar.js',
'/includes/ace/src/ext-language_tools.js',
$experimentAssetsPath . '/dolibarr-context.umd.js',
$experimentAssetsPath . '/dolibarr-tool.seteventmessage.js',
];
$css = [];
@ -72,7 +73,7 @@ $documentation->showSidebar(); ?>
<?php $documentation->showSummary(); ?>
<div class="documentation-section">
<h2 class="documentation-title">Introduction</h2>
<h2 id="titlesection-basicusage" class="documentation-title">Introduction</h2>
<p>
DolibarrContext is a secure global JavaScript context for Dolibarr.
@ -108,7 +109,7 @@ $documentation->showSidebar(); ?>
</div>
<div class="documentation-section">
<h2 class="documentation-title">Console help</h2>
<h2 id="titlesection-console-help" class="documentation-title">Console help</h2>
<p>
Open your browser console with <code>F12</code> to view the available commands.<br/>
@ -117,7 +118,7 @@ $documentation->showSidebar(); ?>
</div>
<div class="documentation-section">
<h2 class="documentation-title">JS Dolibarr hooks</h2>
<h2 id="titlesection-hooks" class="documentation-title">JS Dolibarr hooks</h2>
<h3>Event listener : the Dolibarr ready like</h3>
<div class="documentation-example">
@ -139,7 +140,7 @@ $documentation->showSidebar(); ?>
</div>
<h3>Example of code usage</h3>
<div class="documentation-example">
<div id="titlesection-create-tool-example" class="documentation-example">
<?php
$lines = array(
'<script>',
@ -191,12 +192,13 @@ $documentation->showSidebar(); ?>
</div>
<div class="documentation-section">
<h2 class="documentation-title">Example of creating a new context tool</h2>
<h2 id="titlesection-tool-seteventmessage" class="documentation-title">Example of creating a new context tool</h2>
<h3>Defining Tools</h3>
<p>
You can define reusable and protected tools in the Dolibarr context using <code>Dolibarr.defineTool</code>:
You can define reusable and protected tools in the Dolibarr context using <code>Dolibarr.defineTool</code>.
</p>
<p>See also <code>dolibarr-context.mock.js</code> for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.</p>
<div class="documentation-example">
<?php
@ -254,8 +256,85 @@ $documentation->showSidebar(); ?>
</div>
<div class="documentation-section">
<h2 class="documentation-title">Set event message tool</h2>
<p>
Instead of calling JNotify directly in your code, use Dolibarr’s setEventMessage tool.
Dolibarr provides the configuration option DISABLE_JQUERY_JNOTIFY, which disables the jQuery JNotify system, usually because another notification library will be used instead.
</p>
<p>
If you rely on Dolibarr.tools.setEventMessage(), your code remains compatible even if the underlying notification system changes.
The setEventMessage tool can be replaced internally without requiring any changes in your modules or custom scripts.
</p>
<p>
This means all developers can write features without worrying about frontend compatibility or future library replacements. Enjoy!
</p>
<div class="documentation-example">
<?php
$lines = array(
'<script nonce="<?php print getNonce() ?>">',
' document.addEventListener(\'Dolibarr:Ready\', function(e) {',
'',
' document.getElementById(\'setEventMessage-success\').addEventListener(\'click\', function(e) {',
' Dolibarr.tools.setEventMessage(\'Success Test\');',
' });',
'',
' document.getElementById(\'setEventMessage-error\').addEventListener(\'click\', function(e) {',
' Dolibarr.tools.setEventMessage(\'Error Test\', \'errors\');',
' });',
'',
' document.getElementById(\'setEventMessage-error-sticky\').addEventListener(\'click\', function(e) {',
' Dolibarr.tools.setEventMessage(\'Error Test\', \'errors\', true);',
' });',
'',
' document.getElementById(\'setEventMessage-warning\').addEventListener(\'click\', function(e) {',
' Dolibarr.tools.setEventMessage(\'Warning Test\', \'warnings\');',
' });',
'',
' });',
'</script>',
);
echo $documentation->showCode($lines, 'php'); ?>
<script nonce="<?php print getNonce() ?>" >
document.addEventListener('Dolibarr:Ready', function(e) {
document.getElementById('setEventMessage-success').addEventListener('click', function(e) {
Dolibarr.tools.setEventMessage('Success Test')
});
document.getElementById('setEventMessage-error').addEventListener('click', function(e) {
Dolibarr.tools.setEventMessage('Error Test', 'errors');
});
document.getElementById('setEventMessage-error-sticky').addEventListener('click', function(e) {
Dolibarr.tools.setEventMessage('Error Test', 'errors', true);
});
document.getElementById('setEventMessage-warning').addEventListener('click', function(e) {
Dolibarr.tools.setEventMessage('Warning Test', 'warnings');
});
});
</script>
<button id="setEventMessage-success" class="button">Alert success</button>
<button id="setEventMessage-error" class="button">Alert error</button>
<button id="setEventMessage-error-sticky" class="button">Alert error sticky</button>
<button id="setEventMessage-warning" class="button">Alert warning</button>
</div>
</div>
</div>
</div>
</div>
<?php
// Output close body + html

View file

@ -1001,16 +1001,22 @@ print '<input type="hidden" name="page_y" value="">';
print '<input type="hidden" name="mode" value="'.$mode.'">';
$newcardbutton = '';
$newcardbutton .= dolGetButtonTitle($langs->trans('ViewList'), '', 'fa fa-bars imgforviewmode', $_SERVER["PHP_SELF"].'?mode=common'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ((empty($mode) || $mode == 'common') ? 2 : 1), array('morecss' => 'reposition'));
$newcardbutton .= dolGetButtonTitle($langs->trans('ViewKanban'), '', 'fa fa-th-list imgforviewmode', $_SERVER["PHP_SELF"].'?mode=kanban'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ($mode == 'kanban' ? 2 : 1), array('morecss' => 'reposition'));
$newcardbutton .= dolGetButtonTitleSeparator();
if ($contextpage != 'poslist') {
$newcardbutton .= dolGetButtonTitle($langs->trans('NewContactAddress'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/contact/card.php?action=create', '', $permissiontoadd);
} elseif ($user->hasRight('societe', 'contact', 'creer')) {
$url = DOL_URL_ROOT . '/contact/card.php?action=create&type=t&contextpage=poslist&optioncss=print&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?token=' . newToken() . 'type=t&contextpage=poslist&nomassaction=1&optioncss=print&place='.$place);
$label = 'MenuNewCustomer';
$newcardbutton .= dolGetButtonTitle($langs->trans($label), '', 'fa fa-plus-circle', $url);
$newcardbutton = '';
$parameters = array();
$reshook = $hookmanager->executeHooks('printNewCardButton', $parameters, $object);
if (empty($reshook)) {
$newcardbutton .= dolGetButtonTitle($langs->trans('ViewList'), '', 'fa fa-bars imgforviewmode', $_SERVER["PHP_SELF"].'?mode=common'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ((empty($mode) || $mode == 'common') ? 2 : 1), array('morecss' => 'reposition'));
$newcardbutton .= dolGetButtonTitle($langs->trans('ViewKanban'), '', 'fa fa-th-list imgforviewmode', $_SERVER["PHP_SELF"].'?mode=kanban'.preg_replace('/(&|\?)*mode=[^&]+/', '', $param), '', ($mode == 'kanban' ? 2 : 1), array('morecss' => 'reposition'));
$newcardbutton .= dolGetButtonTitleSeparator();
if ($contextpage != 'poslist') {
$newcardbutton .= dolGetButtonTitle($langs->trans('NewContactAddress'), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/contact/card.php?action=create', '', $permissiontoadd);
} elseif ($user->hasRight('societe', 'contact', 'creer')) {
$url = DOL_URL_ROOT . '/contact/card.php?action=create&type=t&contextpage=poslist&optioncss=print&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?token=' . newToken() . 'type=t&contextpage=poslist&nomassaction=1&optioncss=print&place='.$place);
$label = 'MenuNewCustomer';
$newcardbutton .= dolGetButtonTitle($langs->trans($label), '', 'fa fa-plus-circle', $url);
}
} else {
$newcardbutton = $hookmanager->resPrint;
}
print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, (string) $massactionbutton, $num, $nbtotalofrecords, 'address', 0, $newcardbutton, '', $limit, 0, 0, 1);

View file

@ -474,7 +474,7 @@ class modStock extends DolibarrModules
$this->import_label[$r] = "Warehouses"; // Translation key
$this->import_icon[$r] = "warehouse";
$this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
$this->import_tables_array[$r] = array('e' => MAIN_DB_PREFIX.'entrepot');
$this->import_tables_array[$r] = array('e' => MAIN_DB_PREFIX.'entrepot', 'extra' => MAIN_DB_PREFIX.'entrepot_extrafields');
$this->import_tables_creator_array[$r] = array('e' => 'fk_user_author');
$this->import_fields_array[$r] = array('e.ref' => "LocationSummary*",
'e.description' => "DescWareHouse",
@ -495,7 +495,22 @@ class modStock extends DolibarrModules
'e.fk_parent' => array('rule' => 'fetchidfromref', 'classfile' => '/product/stock/class/entrepot.class.php', 'class' => 'Entrepot', 'method' => 'fetch', 'element' => 'ref')
);
$this->import_regex_array[$r] = array('e.statut' => '^[0|1]');
$this->import_examplevalues_array[$r] = array('e.ref' => "ALM001",
// Add extra fields
$import_extrafield_sample = array();
$sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'entrepot' AND entity IN (0, ".$conf->entity.")";
$resql = $this->db->query($sql);
if ($resql) { // This can fail when class is used on old database (during migration for example)
while ($obj = $this->db->fetch_object($resql)) {
$fieldname = 'extra.'.$obj->name;
$fieldlabel = ucfirst($obj->label);
$this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
$import_extrafield_sample[$fieldname] = $fieldlabel;
}
}
// End add extra fields
$this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'entrepot'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
$this->import_examplevalues_array[$r] = array_merge(array('e.ref' => "ALM001",
'e.description' => "Central Warehouse",
'e.lieu' => "Central",
'e.address' => "Route 66",
@ -506,7 +521,7 @@ class modStock extends DolibarrModules
'e.fax' => '(+33)(0)123456790',
'e.statut' => '1',
'e.fk_parent' => 'id or ref of warehouse'
);
), $import_extrafield_sample);
$this->import_updatekeys_array[$r] = array('p.ref' => 'Ref');
// Import stocks

View file

@ -180,3 +180,9 @@ DocTitleIconUsageMethod3=Use your image. You must set the 4th parameter to 1.
DocTitleMoreContentDescription=You can further customize the headings by adding an ID or CSS class(es), or adding content. See the examples below.
DocTitleWithFilters=Display title with filters, buttons or navigation
DocTitleWithFiltersDescription=If you want to display a title with many navigation options like on list pages, you can use the print_barre_liste() function which is a bit more complex. See code below or core/lib/functions.lib.php file to see how it works.
ConsoleHelp = Console help
JSDolibarrhooks = JS Dolibarr hooks
ExampleOfCreatingNewContextTool = Example of creating a new context tool
SetEventMessageTool = Set event message tool

View file

@ -307,6 +307,17 @@ class Thirdparties extends DolibarrApi
if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
throw new RestException(403);
}
// External api user does not know internal country ID
if (!isset($request_data['country_id']) && isset($request_data['country_code'])) {
$field = strlen($request_data['country_code']) > 2 ? 'code_iso' : 'code';
$id = dol_getIdFromCode($this->db, $request_data['country_code'], "c_country", $field, "rowid");
if ($id < 0) {
throw new RestException(404, 'Country code not found in database: ' . $this->db->error);
}
$request_data['country_id'] = $id;
}
// Check mandatory fields
$result = $this->_validate($request_data);
@ -316,6 +327,12 @@ class Thirdparties extends DolibarrApi
$this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
continue;
}
if ($field == 'array_options' && is_array($value)) {
foreach ($value as $index => $val) {
$this->company->array_options[$index] = $this->_checkValForAPI('extrafields', $val, $this->company);
}
continue;
}
$this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
}