dolibarr 25.0.0-alpha
modulebuilder.lib.php File Reference

Set of function for modulebuilder management. More...

Go to the source code of this file.

Functions

 dolModuleBuilderSqlIdentifier (string $prefix, string $module, string $table, string $key)
 Build the name of an index or of a foreign key constraint for a table generated by the module builder.
 
 rebuildObjectClass ($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array(), $delfieldentry='')
 Regenerate files .class.php.
 
 rebuildObjectSql ($destdir, $module, $objectname, $newmask, $readdir='', $object=null, $moduletype='external')
 Save data into a memory area shared by all users, all sessions on server.
 
 dolGetListOfObjectClasses ($destdir)
 Get list of existing objects from a directory.
 
 checkExistComment ($file, $number)
 Function to check if comment BEGIN and END exists in modMyModule class.
 
 deletePerms ($file)
 Delete all permissions declared in the permissions section of a module descriptor.
 
 reWriteAllPermissions ($file, $permissions, $key, $right, $objectname, $module, $action)
 Rewriting all permissions after any actions.
 
 parsePropertyString ($string)
 Converts a formatted properties string into an associative array.
 
 writePropsInAsciiDoc ($file, $objectname, $destfile)
 Write all properties of the object in AsciiDoc format.
 
 deletePropsAndPermsFromDoc ($file, $objectname)
 Delete property and permissions from documentation ascii file if we delete an object.
 
 getFromFile ($file, $start, $end, $excludestart='', $includese=0)
 Search a string and return all lines needed from file.
 
 writePermsInAsciiDoc ($file, $destfile)
 Write all permissions of each object in AsciiDoc format.
 
 addObjectsToApiFile ($srcfile, $file, $objects, $modulename)
 Add Object in ModuleApi File.
 
 removeObjectFromApiFile ($file, $objects, $objectname)
 Remove Object variables and methods from API_Module File.
 
 reWriteAllMenus ($file, $menus, $menuWantTo, $key, $action)
 
 updateDictionaryInFile ($module, $file, $dicts)
 Updates a dictionary in a module descriptor file.
 
 createNewDictionnary ($modulename, $file, $namedic, $dictionnaires=null)
 Create a new dictionary table.
 
 writeApiUrlsInDoc ($file_api, $file_doc)
 Generate Urls and add them to documentation module.
 
 countItemsInDirectory ($path, $type=1)
 count directories or files in modulebuilder folder
 
 getModuleBuilderObjectTabs ()
 Return the map of optional tabs that can be generated for a ModuleBuilder object.
 
 getModuleBuilderObjectCardActions ()
 Return the map of optional action buttons that can be generated on the card page of a ModuleBuilder object.
 
 getModuleBuilderDefaultEnabledKeys ($map)
 Return the keys enabled by default in a ModuleBuilder generation map.
 
 getModuleBuilderCardActionBlockPattern ($marker)
 Build the regular expression matching every block of a card action in the object card template.
 
 getModuleBuilderCardActionUncommentPatterns ($marker)
 Build the regular expressions removing the comment fence of a card action generated in 'activate' mode.
 
 getModuleBuilderLinesBlockPattern ()
 Return the regular expression matching a block of object lines code in the templates, used when the user asks not to manage lines on the generated object.
 
 filterEnabledKeys ($requested, $map)
 Filter a list of requested keys against a known map of generable elements.
 
 filterEnabledTabs ($requested, $map)
 Filter a list of requested tab keys against the known optional tabs map.
 
 dolReplaceInFilePreservingModuleBuilderMarkers ($file, $arrayreplacement)
 Apply substitutions to a module descriptor file while preserving the MODULEBUILDER comment markers.
 

Detailed Description

Set of function for modulebuilder management.

Definition in file modulebuilder.lib.php.

Function Documentation

◆ addObjectsToApiFile()

addObjectsToApiFile ( $srcfile,
$file,
$objects,
$modulename )

Add Object in ModuleApi File.

Parameters
string$srcfileSource file to use as example
string$filePath of modified file
string[]$objectsArray of objects in the module
string$modulenameName of module
Returns
int<-1,1> Return 1 if OK, -1 if KO

Definition at line 974 of file modulebuilder.lib.php.

References dol_now(), dol_print_date(), dol_strtolower(), dol_strtoupper(), dol_syslog(), dolChmod(), dolReplaceInFile(), and getFromFile().

◆ checkExistComment()

checkExistComment ( $file,
$number )

Function to check if comment BEGIN and END exists in modMyModule class.

Parameters
string$fileFilename or path
int<0,2>$number 0 = For Menus, 1 = For permissions, 2 = For Dictionaries
Returns
int 1 if OK , -1 if KO

Definition at line 523 of file modulebuilder.lib.php.

◆ countItemsInDirectory()

countItemsInDirectory ( $path,
$type = 1 )

count directories or files in modulebuilder folder

Parameters
string$pathpath of directory
int<1,2>$type type of file 1= file,2=directory
Returns
int|false False on failure (path is not a directory)

Definition at line 1422 of file modulebuilder.lib.php.

References dol_is_dir(), and dol_is_file().

◆ createNewDictionnary()

createNewDictionnary ( $modulename,
$file,
$namedic,
$dictionnaires = null )

Create a new dictionary table.

It generates the necessary SQL code to define the table structure, including columns such as 'rowid', 'code', 'label', 'position', 'use_default', 'active', etc. The table name is constructed based on the provided $namedic parameter.

Parameters
string$modulenameThe lowercase name of the module for which the dictionary table is being created.
string$fileThe file path to the Dolibarr module builder file where the dictionaries are defined.
string$namedicThe name of the dictionary, which will also be used as the base for the table name.
?array{langs:string,tabname:string[],tablib:string[],tabsql:string[],tabsqlsort:string[],tabfield:string[],tabfieldvalue:string[],tabfieldinsert:string[],tabrowid:string[],tabcond:array<string|bool|int>,tabhelp:array<array{code:string,field2:string}>}$dictionnaires An optional array containing pre-existing dictionary data, including tabname, tablib, tabsql, etc.
Returns
int<-1,-1> Return int < 0 if error, return nothing on success

Definition at line 1284 of file modulebuilder.lib.php.

References dol_strtolower(), dol_substr(), dol_ucfirst(), isModEnabled(), and updateDictionaryInFile().

◆ deletePerms()

deletePerms ( $file)

Delete all permissions declared in the permissions section of a module descriptor.

Parameters
string$filefile with path
Returns
int<-1,1> 1 if OK, -1 if KO

Definition at line 567 of file modulebuilder.lib.php.

References dol_syslog(), and PermissionsBlock\fromFile().

◆ deletePropsAndPermsFromDoc()

deletePropsAndPermsFromDoc ( $file,
$objectname )

Delete property and permissions from documentation ascii file if we delete an object.

Parameters
string$filefile or path
string$objectnamename of object wants to deleted
Returns
void

Definition at line 813 of file modulebuilder.lib.php.

References dol_is_file(), and dolChmod().

◆ dolGetListOfObjectClasses()

dolGetListOfObjectClasses ( $destdir)

Get list of existing objects from a directory.

Parameters
string$destdirDirectory
Returns
string[]|int Return integer <=0 if KO, array if OK

Definition at line 490 of file modulebuilder.lib.php.

References dol_dir_list().

◆ dolModuleBuilderSqlIdentifier()

dolModuleBuilderSqlIdentifier ( string $prefix,
string $module,
string $table,
string $key )

Build the name of an index or of a foreign key constraint for a table generated by the module builder.

The identifier must not exceed the 64-character limit of MySQL/MariaDB, and must stay predictable: a migration script has to be able to name an existing index without scanning the database. So it is never hashed. It is returned untouched as long as it fits, and only truncated beyond the limit, by parts: 30 characters for the module, 20 for the table, and the remaining budget for the key.

Parameters
string$prefixPrefix of the identifier ('idx_', 'uk_' or 'llx_')
string$moduleName of the module
string$tableName of the table (without its 'llx_<module>_' prefix)
string$keyName of the field the index or the constraint is built on
Returns
string Identifier, at most 64 characters long

Definition at line 43 of file modulebuilder.lib.php.

References $module, dol_strlen(), dol_strtolower(), and dol_substr().

Referenced by rebuildObjectSql().

◆ dolReplaceInFilePreservingModuleBuilderMarkers()

dolReplaceInFilePreservingModuleBuilderMarkers ( $file,
$arrayreplacement )

Apply substitutions to a module descriptor file while preserving the MODULEBUILDER comment markers.

Markers such as "BEGIN MODULEBUILDER LEFTMENU MYOBJECT" must keep their MYOBJECT/MYMODULE placeholder so that generating subsequent objects can still locate them (see checkExistComment()). A blanket substitution would rewrite them to the first object name and break the generation of further objects.

Parameters
string$filePath to the module descriptor file
array<string,string>$arrayreplacement Substitution map (search => replace), applied as literal strings
Returns
int 1 on success, -1 on read/write error

Definition at line 1591 of file modulebuilder.lib.php.

◆ filterEnabledKeys()

filterEnabledKeys ( $requested,
$map )

Filter a list of requested keys against a known map of generable elements.

Protects against injection of unknown keys, removes duplicates, normalizes order.

Parameters
string[] | string$requestedRaw keys requested by the user (e.g. from GETPOST array)
array<string,array<string,mixed>>$map Map of known elements, keyed by element key
Returns
string[] Sanitized list of valid keys, in map order

Definition at line 1554 of file modulebuilder.lib.php.

Referenced by filterEnabledTabs().

◆ filterEnabledTabs()

filterEnabledTabs ( $requested,
$map )

Filter a list of requested tab keys against the known optional tabs map.

Protects against injection of unknown keys, removes duplicates, normalizes order.

Parameters
string[] | string$requestedRaw tab keys requested by the user (e.g. from GETPOST array)
array<string,array{file:string,var:string,marker:string,label:string}>$map Map from getModuleBuilderObjectTabs()
Returns
string[] Sanitized list of valid tab keys, in map order

Definition at line 1576 of file modulebuilder.lib.php.

References filterEnabledKeys().

◆ getFromFile()

getFromFile ( $file,
$start,
$end,
$excludestart = '',
$includese = 0 )

Search a string and return all lines needed from file.

Does not include line $start nor $end

Parameters
string$filefile for searching
string$startstart line if it exists
string$endend line if it exists
string$excludestartIgnore if start line is $excludestart
int<0,1>$includese Include start and end line
Returns
string Return the lines between first line with $start and $end. "" if not found.

Definition at line 847 of file modulebuilder.lib.php.

References dol_osencode().

Referenced by addObjectsToApiFile(), removeObjectFromApiFile(), reWriteAllMenus(), updateDictionaryInFile(), writeApiUrlsInDoc(), and writePermsInAsciiDoc().

◆ getModuleBuilderCardActionBlockPattern()

getModuleBuilderCardActionBlockPattern ( $marker)

Build the regular expression matching every block of a card action in the object card template.

A marker can anchor several non-contiguous blocks: the pattern is non greedy and is meant to be used with removePatternFromFile(), which replaces all the occurrences at once.

Parameters
string$markerMarker of the action, from getModuleBuilderObjectCardActions()
Returns
string Regular expression matching the whole block, delimiters included

Definition at line 1509 of file modulebuilder.lib.php.

◆ getModuleBuilderCardActionUncommentPatterns()

getModuleBuilderCardActionUncommentPatterns ( $marker)

Build the regular expressions removing the comment fence of a card action generated in 'activate' mode.

Removing both sentinel lines turns the commented block into live code.

Parameters
string$markerMarker of the action, from getModuleBuilderObjectCardActions()
Returns
string[] Regular expressions, to apply in order

Definition at line 1523 of file modulebuilder.lib.php.

◆ getModuleBuilderDefaultEnabledKeys()

getModuleBuilderDefaultEnabledKeys ( $map)

Return the keys enabled by default in a ModuleBuilder generation map.

An entry without a 'default' index is considered enabled.

Parameters
array<string,array<string,mixed>>$map Map of known elements, keyed by element key
Returns
string[] Keys enabled by default, in map order

Definition at line 1490 of file modulebuilder.lib.php.

◆ getModuleBuilderLinesBlockPattern()

getModuleBuilderLinesBlockPattern ( )

Return the regular expression matching a block of object lines code in the templates, used when the user asks not to manage lines on the generated object.

The pattern is non greedy, so the BEGIN/END markers of a file must be balanced and never nested: a nested pair makes the outer BEGIN match the inner END and cuts an unbalanced fragment.

Returns
string Regular expression, usable with removePatternFromFile()

Definition at line 1541 of file modulebuilder.lib.php.

◆ getModuleBuilderObjectCardActions()

getModuleBuilderObjectCardActions ( )

Return the map of optional action buttons that can be generated on the card page of a ModuleBuilder object.

The "Modify" button is not listed on purpose: a generated card without it could not be edited any more.

'mode' drives the generation: 'remove' means the block is present in the template and is dropped when the action is not selected, 'activate' means the block ships commented out in the template and is uncommented when the action is selected.

Returns
array<string,array{marker:string,label:string,default:int,mode:string}> Map: action key => metadata

Definition at line 1471 of file modulebuilder.lib.php.

◆ getModuleBuilderObjectTabs()

getModuleBuilderObjectTabs ( )

Return the map of optional tabs that can be generated for a ModuleBuilder object.

The CARD tab is always generated and is therefore not listed here. HISTORY is an alias of AGENDA (object event history is the agenda tab in Dolibarr).

Returns
array<string,array{file:string,var:string,marker:string,label:string}> Map: tab key => metadata

Definition at line 1450 of file modulebuilder.lib.php.

◆ parsePropertyString()

parsePropertyString ( $string)

Converts a formatted properties string into an associative array.

Parameters
string$stringThe formatted properties string.
Returns
array<string,bool|int|float|string|mixed[]> The resulting associative array.

Definition at line 669 of file modulebuilder.lib.php.

References dol_substr(), and parsePropertyString().

Referenced by parsePropertyString(), and writePropsInAsciiDoc().

◆ rebuildObjectClass()

rebuildObjectClass ( $destdir,
$module,
$objectname,
$newmask,
$readdir = '',
$addfieldentry = array(),
$delfieldentry = '' )

Regenerate files .class.php.

Parameters
string$destdirDirectory
string$moduleModule name
string$objectnameName of object
string$newmaskNew mask
string$readdirDirectory source (use $destdir when not defined)
array{}|array{name:string,key:string,type:string,label:string,picot?:string,enabled:int<0,1>,notnull:int<0,1>,position:int,visible:int,noteditable?:int<0,1>,alwayseditable?:int<0,1>,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int<0,1>,css?:string,cssview?:string,csslist?:string,help?:string,showoncombobox?:int<0,1>,disabled?:int<0,1>,autofocusoncreate?:int<0,1>,arrayofkeyval?:array<string,string>,validate?:int<0,1>,comment?:string}$addfieldentry Array of 1 field entry to add
string$delfieldentryId of field to remove
Returns
int<-7,-1>|CommonObject Return integer <=0 if KO, Object if OK
See also
rebuildObjectSql()

Definition at line 76 of file modulebuilder.lib.php.

References $object, dol_copy(), dol_escape_php(), dol_is_file(), dol_mkdir(), dol_osencode(), dol_sort_array(), dol_strtolower(), and dolChmod().

◆ rebuildObjectSql()

rebuildObjectSql ( $destdir,
$module,
$objectname,
$newmask,
$readdir = '',
$object = null,
$moduletype = 'external' )

Save data into a memory area shared by all users, all sessions on server.

Parameters
string$destdirDirectory
string$moduleModule name
string$objectnameName of object
string$newmaskNew mask
string$readdirDirectory source (use $destdir when not defined)
Object$objectIf object was already loaded/known, it is pass to avoid another include and new.
string$moduletype'external' or 'internal'
Returns
int Return integer <=0 if KO, >0 if OK
See also
rebuildObjectClass()

Definition at line 304 of file modulebuilder.lib.php.

References $module, $object, dol_copy(), dol_is_file(), dol_mkdir(), dol_osencode(), dol_strtolower(), dolChmod(), and dolModuleBuilderSqlIdentifier().

◆ removeObjectFromApiFile()

removeObjectFromApiFile ( $file,
$objects,
$objectname )

Remove Object variables and methods from API_Module File.

Parameters
string$fileFile api module
string[]$objectsArray of objects in the module
string$objectnameName of object want to remove
Returns
int<-1,1> 1 if OK, -1 if KO

Definition at line 1055 of file modulebuilder.lib.php.

References dolChmod(), dolReplaceInFile(), and getFromFile().

◆ reWriteAllMenus()

reWriteAllMenus ( $file,
$menus,
$menuWantTo,
$key,
$action )
Parameters
string$filepath of filename
array<int,array{commentgroup?:string,fk_menu:string,type:string,titre:string,mainmenu:string,leftmenu:string,url:string,langs:string,position:int|string,enabled:int|string,perms:string,target:string,user:int}>$menus all menus for module
null|string|array{commentgroup?:string,fk_menu:string,type:string,titre:string,mainmenu:string,leftmenu:string,url:string,langs:string,position:int|string,enabled:int|string,perms:string,target:string,user:int}$menuWantTo menu get for do actions
?int$keykey for the concerned menu
int<-1,2>$action for specify what action (0 = delete perm, 1 = add perm, 2 = update perm, -1 = when we delete object)
Returns
int<-1,1> 1 if OK, -1 if KO

Definition at line 1106 of file modulebuilder.lib.php.

References dol_strtolower(), dol_strtoupper(), dolReplaceInFile(), and getFromFile().

◆ reWriteAllPermissions()

reWriteAllPermissions ( $file,
$permissions,
$key,
$right,
$objectname,
$module,
$action )

Rewriting all permissions after any actions.

Kept for backward compatibility: this is a public core function that external modules may call. The signature and the 1/-1 contract are unchanged, the body delegates to DescriptorRightsSyncService.

Parameters
string$filefilename or path
array<int,string[]>$permissions permissions existing in file
?int$keykey for permission needed
?array<int,string>$right $right to update or add
string$objectnamename of object
string$modulename of module
int<-2,2>$action 0 for delete, 1 for add, 2 for update, -1 when delete object completely, -2 for generate rights after add
Returns
int<-1,1> 1 if OK,-1 if KO

Definition at line 597 of file modulebuilder.lib.php.

References $module, dol_syslog(), RightsSyncCommand\forObjectCreation(), RightsSyncCommand\forObjectDeletion(), RightsSyncCommand\forRightAddition(), RightsSyncCommand\forRightDeletion(), and RightsSyncCommand\forRightUpdate().

◆ updateDictionaryInFile()

updateDictionaryInFile ( $module,
$file,
$dicts )

Updates a dictionary in a module descriptor file.

Parameters
string$moduleThe name of the module.
string$fileThe path to the module descriptor file.
array{langs:string,tabname:string[],tablib:string[],tabsql:string[],tabsqlsort:string[],tabfield:string[],tabfieldvalue:string[],tabfieldinsert:string[],tabrowid:string[],tabcond:array<string|bool|int>,tabhelp:array<array{code:string,field2:string}>}$dicts The dictionary data to be updated.
Returns
int Returns the number of replacements made in the file.
Parameters
bool | string | int$val
Returns
string|int
Parameters
string$val
Returns
string

Definition at line 1211 of file modulebuilder.lib.php.

References $module, dol_strtolower(), dolReplaceInFile(), and getFromFile().

Referenced by createNewDictionnary().

◆ writeApiUrlsInDoc()

writeApiUrlsInDoc ( $file_api,
$file_doc )

Generate Urls and add them to documentation module.

Parameters
string$file_apifilename or path of api
string$file_docfilename or path of documentation
Returns
int<-1,1> -1 if KO, 1 if OK, 0 if nothing change

Definition at line 1365 of file modulebuilder.lib.php.

References dol_is_file(), dol_strlen(), dol_substr(), dolReplaceInFile(), and getFromFile().

◆ writePermsInAsciiDoc()

writePermsInAsciiDoc ( $file,
$destfile )

Write all permissions of each object in AsciiDoc format.

Parameters
string$filepath of the class
string$destfilefile where write table of permissions
Returns
int<-1,1> 1 if OK, -1 if KO

Definition at line 887 of file modulebuilder.lib.php.

References dol_substr(), dolReplaceInFile(), and getFromFile().

◆ writePropsInAsciiDoc()

writePropsInAsciiDoc ( $file,
$objectname,
$destfile )

Write all properties of the object in AsciiDoc format.

Parameters
string$filepath of the class
string$objectnamename of the objectClass
string$destfilefile where write table of properties
Returns
int 1 if OK, -1 if KO

Definition at line 715 of file modulebuilder.lib.php.

References dolReplaceInFile(), and parsePropertyString().