NEW Specify If the Object Manages Lines for Module Builder (#32664)

* Specify If the Object Manages Lines for Module Builder

* Update htdocs/modulebuilder/index.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Update myobject.class.php

* Fix modulebuilder CI issues

* Fix Phan PhanUndeclaredProperty errors on PR #32664

- pdf_standard_myobject: add @phan-suppress-current-line on situation_percent,
  localtax2_tx, localtax1_type, localtax2_type, vat_src_code, pagebreak
  (MyObjectLine template class does not declare these inherited properties)
- doc_generic_project_odt: replace fk_soc with socid (correct PHP property;
  fk_soc is the DB column name, Project::fetch() maps it to $this->socid)

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
VIAL-GOUTEYRON Quentin 2026-05-18 16:13:35 +02:00 committed by GitHub
parent 7b1a93b1c2
commit 110b35cba6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 138 additions and 52 deletions

View file

@ -850,6 +850,52 @@ 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.

View file

@ -683,8 +683,8 @@ class doc_generic_project_odt extends ModelePDFProjects
// Security check
$socid = 0;
if (!empty($object->fk_soc)) {
$socid = $object->fk_soc;
if (!empty($object->socid)) {
$socid = $object->socid;
}
$tasksarray = $taskstatic->getTasksArray(null, null, $object->id, $socid, 0);

View file

@ -170,6 +170,8 @@ DefinePropertiesFromExistingTableDesc=If a table in the database (for 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.
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
NoGenerateLines=I don't want to manage lines on this object
NoGenerateLinesHelp=If you check this, some code will be removed to manage lines of the objects
PermissionDeletedSuccesfuly=Permission has been successfully removed
PermissionUpdatedSuccesfuly=Permission has been successfully updated
PermissionAddedSuccesfuly=Permission has been successfully added

View file

@ -1656,6 +1656,29 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { // Test on
$filetogenerate[] = 'core/modules/mod'.$module.'.class.php';
}
if (! $error && GETPOST('nogeneratelines', 'aZ09')) {
$checkComment = checkExistComment($moduledescriptorfile, 0);
if ($checkComment < 0) {
$warning++;
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), basename($moduledescriptorfile)), null, 'warnings');
} else {
// File path
$TFilePaths = [
$destdir . '/class/' . strtolower($objectname) . '.class.php',
$destdir . '/class/api_' . strtolower($module) . '.class.php',
$destdir . '/' . strtolower($objectname) . '_card.php'
];
// Pattern to remove everything between the tags
$pattern = '/\/\/BEGIN MODULEBUILDER LINES.*?\/\/END MODULEBUILDER LINES\s*/s';
foreach ($TFilePaths as $filePath) {
if (! removePatternFromFile($filePath, $pattern)) {
$error++;
}
}
}
}
if (!$error) {
// Edit PHP files to make replacement
foreach ($filetogenerate as $destfile) {
@ -4150,6 +4173,7 @@ if ($module == 'initmodule') {
print '<input type="checkbox" name="includerefgeneration" id="includerefgeneration" value="includerefgeneration"> <label class="margintoponly" for="includerefgeneration">'.$form->textwithpicto($langs->trans("IncludeRefGeneration"), $langs->trans("IncludeRefGenerationHelp")).'</label><br>';
print '<input type="checkbox" name="includedocgeneration" id="includedocgeneration" value="includedocgeneration"> <label for="includedocgeneration">'.$form->textwithpicto($langs->trans("IncludeDocGeneration"), $langs->trans("IncludeDocGenerationHelp")).'</label><br>';
print '<input type="checkbox" name="generatepermissions" id="generatepermissions" value="generatepermissions"> <label for="generatepermissions">'.$form->textwithpicto($langs->trans("GeneratePermissions"), $langs->trans("GeneratePermissionsHelp")).'</label><br>';
print '<input type="checkbox" name="nogeneratelines" id="nogeneratelines" value="nogeneratelines"> <label for="nogeneratelines">'.$form->textwithpicto($langs->trans("NoGenerateLines"), $langs->trans("NoGenerateLinesHelp")).'</label><br>';
print '<br>';
print '<input type="submit" class="button small" name="create" value="'.dol_escape_htmltag($langs->trans("GenerateCode")).'"'.($dirins ? '' : ' disabled="disabled"').'>';
print '<br>';

View file

@ -416,7 +416,7 @@ class MyModuleApi extends DolibarrApi
unset($object->rowid);
unset($object->canvas);
//BEGIN MODULEBUILDER LINES
// If object has lines, remove $db property
if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
$nboflines = count($object->lines);
@ -427,7 +427,7 @@ class MyModuleApi extends DolibarrApi
unset($object->lines[$i]->note);
}
}
//END MODULEBUILDER LINES
return $object;
}
}

View file

@ -205,43 +205,42 @@ class MyObject extends CommonObject
public $import_key;
// END MODULEBUILDER PROPERTIES
//BEGIN MODULEBUILDER LINES
// If this object has a subtable with lines
// /**
// * @var string Name of subtable line
// */
// public $table_element_line = 'mymodule_myobjectline';
/**
* @var string Name of subtable line
*/
public $table_element_line = 'mymodule_myobjectline';
// /**
// * @var string Field name with ID of parent key if this object has a parent, Or Field name of in child tables to link to this record.
// */
// public $fk_element = 'fk_myobject';
/**
* @var string Field with ID of parent key if this object has a parent
*/
public $fk_element = 'fk_myobject';
// /**
// * @var string Name of subtable class that manage subtable lines
// */
// public $class_element_line = 'MyObjectline';
/**
* @var string Name of subtable class that manage subtable lines
*/
public $class_element_line = 'MyObjectline';
// /**
// * @var array List of child tables. To test if we can delete object.
// */
// protected $childtables = array('mychildtable' => array('name'=>'MyObject', 'fk_element'=>'fk_myobject'));
/**
* @var array<array<string>|string> List of child tables. To test if we can delete object.
*/
protected $childtables = array('mychildtable' => array('name'=>'MyObject', 'fk_element'=>'fk_myobject'));
// /**
// * @var array List of child tables. To know object to delete on cascade.
// * If name matches '@ClassName:FilePathClass:ParentFkFieldName' (the recommended mode) it will
// * call method ClassName->deleteByParentField(parentId, 'ParentFkFieldName') to fetch and delete child object.
// * Using an array like childtables should not be implemented because a child may have other child, so we must only use the method that call deleteByParentField().
// */
// protected $childtablesoncascade = array('mymodule_myobjectdet');
// /**
// * @var MyObjectLine[] Array of subtable lines
// */
// public $lines = array();
/**
* @var string[] List of child tables. To know object to delete on cascade.
* If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
* call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
*/
protected $childtablesoncascade = array('mymodule_myobjectdet');
/**
* @var MyObjectLine[] Array of subtable lines
*/
public $lines = array();
//END MODULEBUILDER LINES
/**
* Constructor
@ -327,14 +326,15 @@ class MyObject extends CommonObject
// Load source object
$result = $object->fetchCommon($fromid);
//BEGIN MODULEBUILDER LINES
if ($result > 0 && !empty($object->table_element_line)) {
$object->fetchLines();
}
// get lines so they will be clone
//foreach($this->lines as $line)
// $line->fetch_optionals();
foreach ($this->lines as $line)
$line->fetch_optionals();
//END MODULEBUILDER LINES
// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
@ -418,9 +418,11 @@ class MyObject extends CommonObject
public function fetch($id, $ref = null, $noextrafields = 0, $nolines = 0)
{
$result = $this->fetchCommon($id, $ref, '', $noextrafields);
//BEGIN MODULEBUILDER LINES
if ($result > 0 && !empty($this->table_element_line) && empty($nolines)) {
$this->fetchLines($noextrafields);
}
//END MODULEBUILDER LINES
return $result;
}
@ -437,8 +439,7 @@ class MyObject extends CommonObject
$result = $this->fetchLinesCommon('', $noextrafields);
return $result;
}
//END MODULEBUILDER LINES
/**
* Load list of objects in memory from the database.
* Using a fetchAll() with limit = 0 is a very bad practice. Instead try to forge yourself an optimized SQL request with
@ -545,6 +546,7 @@ class MyObject extends CommonObject
//return $this->deleteCommon($user, $notrigger, 1);
}
//BEGIN MODULEBUILDER LINES
/**
* Delete a line of object in database
*
@ -562,6 +564,7 @@ class MyObject extends CommonObject
return $this->deleteLineCommon($user, $idline, $notrigger);
}
//END MODULEBUILDER LINES
/**
@ -1107,10 +1110,11 @@ class MyObject extends CommonObject
return $this->initAsSpecimenCommon();
}
//BEGIN MODULEBUILDER LINES
/**
* Create an array of lines
*
* @return CommonObjectLine[]|int array of lines if OK, <0 if KO
* @return array<CommonObjectLine>|int array of lines if OK, <0 if KO
*/
public function getLinesArray()
{
@ -1123,10 +1127,12 @@ class MyObject extends CommonObject
$this->setErrorsFromObject($objectline);
return $result;
} else {
/** @phpstan-ignore-next-line */
$this->lines = $result;
return $this->lines;
}
}
//END MODULEBUILDER LINES
/**
* Returns the reference to the following non used object depending on the active numbering module.
@ -1273,7 +1279,7 @@ class MyObject extends CommonObject
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
//BEGIN MODULEBUILDER LINES
/**
* Class MyObjectLine. You can also remove this and generate a CRUD class for lines objects.
*/
@ -1316,3 +1322,4 @@ class MyObjectLine extends CommonObjectLine
$this->db = $db;
}
}
//END MODULEBUILDER LINES

View file

@ -37,6 +37,7 @@
dol_include_once('/mymodule/core/modules/mymodule/modules_myobject.php');
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
@ -668,11 +669,11 @@ class pdf_standard_myobject extends ModelePDFMyObject
$sign = 1;
// Collection of totals by value of VAT in $this->tva["taux"]=total_tva
$prev_progress = $object->lines[$i]->get_prev_progress($object->id);
if ($prev_progress > 0 && $object->lines instanceof CommonInvoiceLine && !empty($object->lines[$i]->situation_percent)) { // Compute progress from previous situation
if ($prev_progress > 0 && $object->lines[$i] instanceof CommonInvoiceLine && !empty($object->lines[$i]->situation_percent)) { // Compute progress from previous situation
if (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) {
$tvaligne = $sign * $object->lines[$i]->multicurrency_total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent;
$tvaligne = $sign * $object->lines[$i]->multicurrency_total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent; // @phan-suppress-current-line PhanUndeclaredProperty
} else {
$tvaligne = $sign * $object->lines[$i]->total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent;
$tvaligne = $sign * $object->lines[$i]->total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent; // @phan-suppress-current-line PhanUndeclaredProperty
}
} else {
if (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) {
@ -684,10 +685,11 @@ class pdf_standard_myobject extends ModelePDFMyObject
$localtax1ligne = $object->lines[$i]->total_localtax1;
$localtax2ligne = $object->lines[$i]->total_localtax2;
// @phan-suppress-next-line PhanUndeclaredProperty
$localtax1_rate = $object->lines[$i]->localtax1_tx;
$localtax2_rate = $object->lines[$i]->localtax2_tx;
$localtax1_type = $object->lines[$i]->localtax1_type;
$localtax2_type = $object->lines[$i]->localtax2_type;
$localtax2_rate = $object->lines[$i]->localtax2_tx; // @phan-suppress-current-line PhanUndeclaredProperty
$localtax1_type = $object->lines[$i]->localtax1_type; // @phan-suppress-current-line PhanUndeclaredProperty
$localtax2_type = $object->lines[$i]->localtax2_type; // @phan-suppress-current-line PhanUndeclaredProperty
$vatrate = (string) $object->lines[$i]->tva_tx;
@ -724,7 +726,7 @@ class pdf_standard_myobject extends ModelePDFMyObject
$this->tva[$vatrate] = 0;
}
$this->tva[$vatrate] += $tvaligne;
$vatcode = $object->lines[$i]->vat_src_code;
$vatcode = $object->lines[$i]->vat_src_code; // @phan-suppress-current-line PhanUndeclaredProperty
if (empty($this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'])) {
$this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'] = 0;
}
@ -761,7 +763,7 @@ class pdf_standard_myobject extends ModelePDFMyObject
}
}
if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) { // @phan-suppress-current-line PhanUndeclaredProperty
if ($pagenb == $pageposafter) {
$this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
} else {

View file

@ -211,8 +211,10 @@ if (empty($reshook)) {
// Actions when printing a doc from card
include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
//BEGIN MODULEBUILDER LINES
// Action to move up and down lines of object
//include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
//END MODULEBUILDER LINES
// Action to build doc
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
@ -352,10 +354,12 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if ($action == 'delete' || ($conf->use_javascript_ajax && empty($conf->dol_use_jmobile))) {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteMyObject'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 'action-delete');
}
//BEGIN MODULEBUILDER LINES
// Confirmation to delete line
if ($action == 'deleteline') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
}
//END MODULEBUILDER LINES
// Clone confirmation
if ($action == 'clone') {
@ -470,7 +474,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print dol_get_fiche_end();
//BEGIN MODULEBUILDER LINES
/*
* Lines
*/
@ -500,6 +504,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$object->printObjectLines($action, $mysoc, null, GETPOSTINT('lineid'), 1);
}
//BEGIN MODULEBUILDER LINES
// Form to add new line
if ($object->status == 0 && $permissiontoadd && $action != 'selectlines') {
if ($action != 'editline') {
@ -515,6 +520,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
}
}
}
//END MODULEBUILDER LINES
if (!empty($object->lines) || ($object->status == $object::STATUS_DRAFT && $permissiontoadd && $action != 'selectlines' && $action != 'editline')) {
print '</table>';
@ -523,8 +529,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print "</form>\n";
}
//END MODULEBUILDER LINES
// Buttons for actions
if ($action != 'presend' && $action != 'editline') {