diff --git a/htdocs/admin/tools/ui/class/documentation.class.php b/htdocs/admin/tools/ui/class/documentation.class.php index a64bd36b3ca..6d451f43084 100644 --- a/htdocs/admin/tools/ui/class/documentation.class.php +++ b/htdocs/admin/tools/ui/class/documentation.class.php @@ -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', + ), ), ) ); diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js new file mode 100644 index 00000000000..8d5d7649d56 --- /dev/null +++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.mock.js @@ -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) {}, + } +}; diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js index ac2f38b621b..fe4ed7c9292 100644 --- a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js +++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-context.umd.js @@ -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 }); diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js new file mode 100644 index 00000000000..2d6d58e2d43 --- /dev/null +++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/assets/dolibarr-tool.seteventmessage.js @@ -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); +}); diff --git a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php index 39bd757cf5c..6bc367f311a 100644 --- a/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php +++ b/htdocs/admin/tools/ui/experimental/experiments/dolibarr-context/index.php @@ -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(); ?> showSummary(); ?>
-

Introduction

+

Introduction

DolibarrContext is a secure global JavaScript context for Dolibarr. @@ -108,7 +109,7 @@ $documentation->showSidebar(); ?>

-

Console help

+

Console help

Open your browser console with F12 to view the available commands.
@@ -117,7 +118,7 @@ $documentation->showSidebar(); ?>

-

JS Dolibarr hooks

+

JS Dolibarr hooks

Event listener : the Dolibarr ready like

@@ -139,7 +140,7 @@ $documentation->showSidebar(); ?>

Example of code usage

-
+
', @@ -191,12 +192,13 @@ $documentation->showSidebar(); ?>
-

Example of creating a new context tool

+

Example of creating a new context tool

Defining Tools

- You can define reusable and protected tools in the Dolibarr context using Dolibarr.defineTool: + You can define reusable and protected tools in the Dolibarr context using Dolibarr.defineTool.

+

See also dolibarr-context.mock.js for defining all standard Dolibarr tools and creating mock implementations to improve code completion and editor support.

showSidebar(); ?>
+
+

Set event message tool

+ +

+ 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. +

+ +

+ 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. +

+

+ This means all developers can write features without worrying about frontend compatibility or future library replacements. Enjoy! + +

+ +
+ ">', + ' 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\');', + ' });', + '', + ' });', + '', + ); + echo $documentation->showCode($lines, 'php'); ?> + + + + + +
+ +
+
+ + + +
+
'; print ''; -$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); diff --git a/htdocs/core/modules/modStock.class.php b/htdocs/core/modules/modStock.class.php index 34365f2d71f..1b9f280a125 100644 --- a/htdocs/core/modules/modStock.class.php +++ b/htdocs/core/modules/modStock.class.php @@ -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 diff --git a/htdocs/langs/en_US/uxdocumentation.lang b/htdocs/langs/en_US/uxdocumentation.lang index 329af207651..47b5b4df282 100644 --- a/htdocs/langs/en_US/uxdocumentation.lang +++ b/htdocs/langs/en_US/uxdocumentation.lang @@ -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 diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index e20c6fb1705..4e83728ff7d 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -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); }