Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW : Select tabs of object for module builder #32685

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/modulebuilder.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 56 additions & 4 deletions htdocs/modulebuilder/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- Directory scanned -->'."\n";
$listofmodules = array();
Expand Down Expand Up @@ -1371,12 +1373,16 @@
$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',
Expand All @@ -1391,6 +1397,13 @@
'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));

Expand Down Expand Up @@ -1464,6 +1477,36 @@
}
}

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;
Expand Down Expand Up @@ -4052,6 +4095,15 @@
print $form->textwithpicto('', $langs->trans("DefinePropertiesFromExistingTableDesc").'<br>'.$langs->trans("DefinePropertiesFromExistingTableDesc2"));
print '</div></div>';

print '<div class="tagtr"><div class="tagtd">';
print '<span class="opacitymedium">'.$langs->trans("SelectTabsForGeneration").'</span> &nbsp; ';
print '</div><div class="tagtd">';

print $form::multiselectarray('tabsAvailable', $objectTabsAvailable, 0, 0, 0, 'minwidth300 quatrevingtpercent widthcentpercentminusx', 0, 0);

Check warning on line 4102 in htdocs/modulebuilder/index.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

index.php: PhanTypeMismatchArgumentProbablyReal: Argument 3 ($selected) is 0 of type 0 but \Form::multiselectarray() takes array|string[] (no real type) defined at htdocs/core/class/html.form.class.php:9290 (the inferred real argument type has nothing in common with the parameter's phpdoc type)
print $form->textwithpicto('', $langs->trans("SelectTabsForGenerationHelp"));

print '</div></div>';

print '</div>';

print '<br>';
Expand Down
20 changes: 16 additions & 4 deletions htdocs/modulebuilder/template/lib/mymodule_myobject.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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';
Expand All @@ -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
Expand Down
Loading