From 69e9efa361297a13d1bf700b820498200811e309 Mon Sep 17 00:00:00 2001 From: Quentin VIAL--GOUTEYRON Date: Thu, 16 Jan 2025 15:17:28 +0100 Subject: [PATCH] NEW : Select tabs of object for module builder --- htdocs/core/lib/files.lib.php | 44 ++++++++++++++ htdocs/langs/en_US/modulebuilder.lang | 2 + htdocs/modulebuilder/index.php | 60 +++++++++++++++++-- .../template/lib/mymodule_myobject.lib.php | 20 +++++-- 4 files changed, 118 insertions(+), 8 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 16ca60e946e34..6f4f9dbaa7a0c 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -761,6 +761,50 @@ function dolReplaceInFile($srcfile, $arrayreplacement, $destfile = '', $newmask return 1; } +/** + * Removes content from a file that matches a given pattern. + * + * @param string $filePath Path to the file to be processed. + * @param string $pattern Regular expression pattern to identify the content to remove. + * @return bool Returns true if the operation was successful, false otherwise. + */ +function removePatternFromFile(string $filePath, string $pattern): bool +{ + // Check if the file exists + if (! file_exists($filePath)) { + dol_syslog("files.lib.php::removePatternFromFile: File $filePath does not exist", LOG_WARNING); + + return false; + } + + // Read the file content + $content = file_get_contents($filePath); + if ($content === false) { + dol_syslog("files.lib.php::removePatternFromFile: Unable to read the file $filePath", LOG_WARNING); + + return false; + } + + // Remove content matching the pattern + $updatedContent = preg_replace($pattern, '', $content); + if ($updatedContent === null) { + dol_syslog("files.lib.php::removePatternFromFile: Error while processing the file $filePath", LOG_WARNING); + + return false; + } + + // Write the updated content back to the file + $result = file_put_contents($filePath, $updatedContent); + if ($result === false) { + dol_syslog("files.lib.php::removePatternFromFile: Permission denied to overwrite the target file $filePath", LOG_WARNING); + + return false; + } + + dol_syslog("files.lib.php::removePatternFromFile: Content successfully removed in the file $filePath", LOG_INFO); + + return true; +} /** * Copy a file to another file. diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index 5a8117149c7f2..c3912eb15fad9 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -165,6 +165,8 @@ BadValueForType=Bad value for type %s DefinePropertiesFromExistingTable=Define the fields/properties from an existing table DefinePropertiesFromExistingTableDesc=If a table in the database (for the object to create) already exists, you can use it to define the properties of the object. DefinePropertiesFromExistingTableDesc2=Keep empty if the table does not exist yet. The code generator will use different kinds of fields to build an example of table that you can edit later. +SelectTabsForGeneration=Select the tabs you want on your object +SelectTabsForGenerationHelp=This option allows you to choose which tabs will be visible for your object. Use the multiselect to add or remove tabs as needed. Only the selected tabs will be shown, helping you customize the view based on your preferences. GeneratePermissions=I want to manage permissions on this object GeneratePermissionsHelp=If you check this, some code will be added to manage permissions to read, write and delete record of the objects PermissionDeletedSuccesfuly=Permission has been successfully removed diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 86726bb5543f1..de6b88b6804f5 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -148,6 +148,8 @@ $dirsrootforscan[] = DOL_DOCUMENT_ROOT; } +$objectTabsAvailable = ['note'=> $langs->trans('Notes'), 'contact'=> $langs->trans('Contacts'),'document'=> $langs->trans('Documents'),'agenda'=> $langs->trans('Events')]; + // Search modules to edit $textforlistofdirs = ''."\n"; $listofmodules = array(); @@ -1371,12 +1373,16 @@ function getLicenceHeader($user, $langs, $now) $filetogenerate = array(); // For static analysis if (!$error) { // Copy some files + $tabsSelected = GETPOST('tabsAvailable', 'array'); + $availableTabForObject = array( + 'note' => 'myobject_note.php', + 'contact' => 'myobject_contact.php', + 'document' => 'myobject_document.php', + 'agenda' => 'myobject_agenda.php', + ); + $filetogenerate = array( 'myobject_card.php' => strtolower($objectname).'_card.php', - 'myobject_note.php' => strtolower($objectname).'_note.php', - 'myobject_contact.php' => strtolower($objectname).'_contact.php', - 'myobject_document.php' => strtolower($objectname).'_document.php', - 'myobject_agenda.php' => strtolower($objectname).'_agenda.php', 'myobject_list.php' => strtolower($objectname).'_list.php', 'admin/myobject_extrafields.php' => 'admin/'.strtolower($objectname).'_extrafields.php', 'lib/mymodule_myobject.lib.php' => 'lib/'.strtolower($module).'_'.strtolower($objectname).'.lib.php', @@ -1391,6 +1397,13 @@ function getLicenceHeader($user, $langs, $now) 'ajax/myobject.php' => 'ajax/'.strtolower($objectname).'.php', ); + foreach ($availableTabForObject as $keyFile => $file) { + // If the tab is selected, add it to the $filetogenerate array + if (in_array($keyFile, $tabsSelected)) { + $filetogenerate[$file] = strtolower($objectname).'_'.$keyFile.'.php'; + } + } + if (GETPOST('includerefgeneration', 'aZ09')) { dol_mkdir($destdir.'/core/modules/'.strtolower($module)); @@ -1464,6 +1477,36 @@ function getLicenceHeader($user, $langs, $now) } } + if (! $error) { + // Define the available tab delimiters with BEGIN and END patterns for each tab + $availableTabDelimiters = [ + 'contact' => ['BEGIN MODULEBUILDER MYOBJECT TAB CONTACT', 'END MODULEBUILDER MYOBJECT TAB CONTACT'], + 'note' => ['BEGIN MODULEBUILDER MYOBJECT TAB NOTE', 'END MODULEBUILDER MYOBJECT TAB NOTE'], + 'document' => ['BEGIN MODULEBUILDER MYOBJECT TAB DOCUMENT', 'END MODULEBUILDER MYOBJECT TAB DOCUMENT'], + 'agenda' => ['BEGIN MODULEBUILDER MYOBJECT TAB AGENDA', 'END MODULEBUILDER MYOBJECT TAB AGENDA'] + ]; + + // Iterate over each available tab delimiter + foreach ($availableTabDelimiters as $keyTab => $delimiters) { + // Check if the current tab is not in the selected tabs + if (! in_array($keyTab, $tabsSelected)) { + // Create a regular expression pattern to match content between the BEGIN and END delimiters + $patternDelimiters = sprintf( + '/\/\/%s[\s\S]*?\/\/%s/', // Match any content between the BEGIN and END patterns + preg_quote($delimiters[0], '/'), // Escape the BEGIN delimiter for regex + preg_quote($delimiters[1], '/') // Escape the END delimiter for regex + ); + + + // Try to remove the matching content from the specified file + if (! removePatternFromFile($destdir . '/lib/'.strtolower($module).'_'.strtolower($objectname).'.lib.php', $patternDelimiters)) { + // If the operation fails, increment the error counter + $error++; + } + } + } + } + // Replace property section with $stringforproperties if (!$error && $stringforproperties) { //var_dump($stringforproperties);exit; @@ -4052,6 +4095,15 @@ function getLicenceHeader($user, $langs, $now) print $form->textwithpicto('', $langs->trans("DefinePropertiesFromExistingTableDesc").'
'.$langs->trans("DefinePropertiesFromExistingTableDesc2")); print ''; + print '
'; + print ''.$langs->trans("SelectTabsForGeneration").'   '; + print '
'; + + print $form::multiselectarray('tabsAvailable', $objectTabsAvailable, 0, 0, 0, 'minwidth300 quatrevingtpercent widthcentpercentminusx', 0, 0); + print $form->textwithpicto('', $langs->trans("SelectTabsForGenerationHelp")); + + print '
'; + print ''; print '
'; diff --git a/htdocs/modulebuilder/template/lib/mymodule_myobject.lib.php b/htdocs/modulebuilder/template/lib/mymodule_myobject.lib.php index e63dc663ae5d6..7f901faa0a807 100644 --- a/htdocs/modulebuilder/template/lib/mymodule_myobject.lib.php +++ b/htdocs/modulebuilder/template/lib/mymodule_myobject.lib.php @@ -33,10 +33,18 @@ function myobjectPrepareHead($object) $langs->load("mymodule@mymodule"); + //BEGIN MODULEBUILDER MYOBJECT TAB CONTACT $showtabofpagecontact = 1; + //END MODULEBUILDER MYOBJECT TAB CONTACT + //BEGIN MODULEBUILDER MYOBJECT TAB NOTE $showtabofpagenote = 1; + //END MODULEBUILDER MYOBJECT TAB NOTE + //BEGIN MODULEBUILDER MYOBJECT TAB DOCUMENT $showtabofpagedocument = 1; + //END MODULEBUILDER MYOBJECT TAB DOCUMENT + //BEGIN MODULEBUILDER MYOBJECT TAB AGENDA $showtabofpageagenda = 1; + //END MODULEBUILDER MYOBJECT TAB AGENDA $h = 0; $head = array(); @@ -45,14 +53,15 @@ function myobjectPrepareHead($object) $head[$h][1] = $langs->trans("MyObject"); $head[$h][2] = 'card'; $h++; - + //BEGIN MODULEBUILDER MYOBJECT TAB CONTACT if ($showtabofpagecontact) { $head[$h][0] = dol_buildpath("/mymodule/myobject_contact.php", 1).'?id='.$object->id; $head[$h][1] = $langs->trans("Contacts"); $head[$h][2] = 'contact'; $h++; } - + //END MODULEBUILDER MYOBJECT TAB CONTACT + //BEGIN MODULEBUILDER MYOBJECT TAB NOTE if ($showtabofpagenote) { if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) { $nbNote = 0; @@ -71,7 +80,8 @@ function myobjectPrepareHead($object) $h++; } } - + //END MODULEBUILDER MYOBJECT TAB NOTE + //BEGIN MODULEBUILDER MYOBJECT TAB DOCUMENT if ($showtabofpagedocument) { require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php'; @@ -86,13 +96,15 @@ function myobjectPrepareHead($object) $head[$h][2] = 'document'; $h++; } - + //END MODULEBUILDER MYOBJECT TAB DOCUMENT + //BEGIN MODULEBUILDER MYOBJECT TAB AGENDA if ($showtabofpageagenda) { $head[$h][0] = dol_buildpath("/mymodule/myobject_agenda.php", 1).'?id='.$object->id; $head[$h][1] = $langs->trans("Events"); $head[$h][2] = 'agenda'; $h++; } + //END MODULEBUILDER MYOBJECT TAB AGENDA // Show more tabs from modules // Entries must be declared in modules descriptor with line