diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index f1a53d32f3a..e44a7245209 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -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. diff --git a/htdocs/core/modules/project/doc/doc_generic_project_odt.modules.php b/htdocs/core/modules/project/doc/doc_generic_project_odt.modules.php index 2f9d72ea3a3..706debb3be3 100644 --- a/htdocs/core/modules/project/doc/doc_generic_project_odt.modules.php +++ b/htdocs/core/modules/project/doc/doc_generic_project_odt.modules.php @@ -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); diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index f79b7b980fb..ba852c68cf3 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -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 diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 1738429e070..aa2358c5f40 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -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 '
'; print '
'; print '
'; + print '
'; print '
'; print ''; print '
'; diff --git a/htdocs/modulebuilder/template/class/api_mymodule.class.php b/htdocs/modulebuilder/template/class/api_mymodule.class.php index 2074351fc21..6ec62448804 100644 --- a/htdocs/modulebuilder/template/class/api_mymodule.class.php +++ b/htdocs/modulebuilder/template/class/api_mymodule.class.php @@ -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; } } diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 564564d2d20..5170570658c 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -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|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|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 diff --git a/htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php b/htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php index 202bed7aa7e..eab91fc4b7e 100644 --- a/htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php +++ b/htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php @@ -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 { diff --git a/htdocs/modulebuilder/template/myobject_card.php b/htdocs/modulebuilder/template/myobject_card.php index 86c7fa3cdfe..05abb7f419f 100644 --- a/htdocs/modulebuilder/template/myobject_card.php +++ b/htdocs/modulebuilder/template/myobject_card.php @@ -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 ''; @@ -523,8 +529,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea print "\n"; } - - + //END MODULEBUILDER LINES // Buttons for actions if ($action != 'presend' && $action != 'editline') {