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

Specify If the Object Manages Lines for Module Builder #32664

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Specify If the Object Manages Lines for Module Builder
atm-quentin committed Jan 15, 2025
commit 4aad0203d169f61d23bdb9b1f0900a460c824aac
46 changes: 46 additions & 0 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
@@ -761,6 +761,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.
2 changes: 2 additions & 0 deletions htdocs/langs/en_US/modulebuilder.lang
Original file line number Diff line number Diff line change
@@ -167,6 +167,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
26 changes: 25 additions & 1 deletion htdocs/modulebuilder/index.php
Original file line number Diff line number Diff line change
@@ -1628,6 +1628,29 @@ function getLicenceHeader($user, $langs, $now)
$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) {
@@ -2052,7 +2075,7 @@ function getLicenceHeader($user, $langs, $now)
'sql/llx_mymodule_myobject_extrafields.key.sql' => 'sql/llx_'.strtolower($module).'_'.strtolower($objectname).'_extrafields.key.sql',
'scripts/myobject.php' => 'scripts/'.strtolower($objectname).'.php',
'class/myobject.class.php' => 'class/'.strtolower($objectname).'.class.php',
'class/api_myobject.class.php' => 'class/api_'.strtolower($module).'.class.php',
'class/api_myobject.class.php' => strtolower($module),
atm-quentin marked this conversation as resolved.
Show resolved Hide resolved
'core/modules/mymodule/mod_myobject_advanced.php' => 'core/modules/'.strtolower($module).'/mod_'.strtolower($objectname).'_advanced.php',
'core/modules/mymodule/mod_myobject_standard.php' => 'core/modules/'.strtolower($module).'/mod_'.strtolower($objectname).'_standard.php',
'core/modules/mymodule/modules_myobject.php' => 'core/modules/'.strtolower($module).'/modules_'.strtolower($objectname).'.php',
@@ -4058,6 +4081,7 @@ function getLicenceHeader($user, $langs, $now)
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>';
4 changes: 2 additions & 2 deletions htdocs/modulebuilder/template/class/api_mymodule.class.php
Original file line number Diff line number Diff line change
@@ -394,7 +394,7 @@ protected function _cleanObjectDatas($object)

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);
@@ -405,7 +405,7 @@ protected function _cleanObjectDatas($object)
unset($object->lines[$i]->note);
}
}

//END MODULEBUILDER LINES
return $object;
}
}
79 changes: 43 additions & 36 deletions htdocs/modulebuilder/template/class/myobject.class.php
Original file line number Diff line number Diff line change
@@ -191,42 +191,42 @@
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 Field with ID of parent key if this object has a parent
// */
// public $fk_element = 'fk_myobject';
/**
* @var string Name of subtable line
*/
public $table_element_line = 'mymodule_myobjectline';

// /**
// * @var string Name of subtable class that manage subtable lines
// */
// public $class_element_line = 'MyObjectline';
/**
* @var string Field with ID of parent key if this object has a parent
*/
public $fk_element = 'fk_myobject';

// /**
// * @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 string Name of subtable class that manage subtable lines
*/
public $class_element_line = 'MyObjectline';

// /**
// * @var array 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 array List of child tables. To test if we can delete object.
*/
protected $childtables = array('mychildtable' => array('name'=>'MyObject', 'fk_element'=>'fk_myobject'));

Check failure on line 215 in htdocs/modulebuilder/template/class/myobject.class.php

GitHub Actions / phpstan / php-stan (8.2)

PHPDoc type array of property MyObject::$childtables is not covariant with PHPDoc type array<array<string>|string> of overridden property CommonObject::$childtables.

Check failure on line 215 in htdocs/modulebuilder/template/class/myobject.class.php

GitHub Actions / phpstan / php-stan (8.2)

Property MyObject::$childtables type has no value type specified in iterable type array.

// /**
// * @var MyObjectLine[] Array of subtable lines
// */
// public $lines = array();
/**
* @var array 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');

Check failure on line 222 in htdocs/modulebuilder/template/class/myobject.class.php

GitHub Actions / phpstan / php-stan (8.2)

PHPDoc type array of property MyObject::$childtablesoncascade is not covariant with PHPDoc type array<string> of overridden property CommonObject::$childtablesoncascade.

Check failure on line 222 in htdocs/modulebuilder/template/class/myobject.class.php

GitHub Actions / phpstan / php-stan (8.2)

Property MyObject::$childtablesoncascade type has no value type specified in iterable type array.

/**
* @var MyObjectLine[] Array of subtable lines
*/
public $lines = array();

//END MODULEBUILDER LINES

/**
* Constructor
@@ -311,14 +311,16 @@

// 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();

// get lines so they will be clone
foreach ($this->lines as $line)
$line->fetch_optionals();
//END MODULEBUILDER LINES
// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
@@ -402,12 +404,14 @@
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;
}

//BEGIN MODULEBUILDER LINES
/**
* Load object lines in memory from the database
*
@@ -421,8 +425,7 @@
$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
@@ -525,6 +528,7 @@
//return $this->deleteCommon($user, $notrigger, 1);
}

//BEGIN MODULEBUILDER LINES
/**
* Delete a line of object in database
*
@@ -542,6 +546,7 @@

return $this->deleteLineCommon($user, $idline, $notrigger);
}
//END MODULEBUILDER LINES


/**
@@ -1072,7 +1077,7 @@

return $this->initAsSpecimenCommon();
}

//BEGIN MODULEBUILDER LINES
/**
* Create an array of lines
*
@@ -1089,10 +1094,11 @@
$this->setErrorsFromObject($objectline);
return $result;
} else {
$this->lines = $result;

Check failure on line 1097 in htdocs/modulebuilder/template/class/myobject.class.php

GitHub Actions / phpstan / php-stan (8.2)

Property MyObject::$lines (array<MyObjectLine>) does not accept array<CommonObjectLine>.
return $this->lines;
}
}
//END MODULEBUILDER LINES

/**
* Returns the reference to the following non used object depending on the active numbering module.
@@ -1241,7 +1247,7 @@


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.
*/
@@ -1274,3 +1280,4 @@
$this->isextrafieldmanaged = 0;
}
}
//END MODULEBUILDER LINES
13 changes: 9 additions & 4 deletions htdocs/modulebuilder/template/myobject_card.php
Original file line number Diff line number Diff line change
@@ -216,8 +216,10 @@
// 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';
@@ -364,10 +366,12 @@
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') {
@@ -479,7 +483,7 @@

print dol_get_fiche_end();


//BEGIN MODULEBUILDER LINES
/*
* Lines
*/
@@ -509,6 +513,7 @@
$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') {
@@ -524,6 +529,7 @@
}
}
}
//END MODULEBUILDER LINES

if (!empty($object->lines) || ($object->status == $object::STATUS_DRAFT && $permissiontoadd && $action != 'selectlines' && $action != 'editline')) {
print '</table>';
@@ -532,8 +538,7 @@

print "</form>\n";
}


//END MODULEBUILDER LINES
// Buttons for actions

if ($action != 'presend' && $action != 'editline') {

Unchanged files with check annotations Beta

$prev_progress = $object->lines[$i]->get_prev_progress($object->id);
if ($prev_progress > 0 && !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;

Check warning on line 673 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;situation_percent
} else {
$tvaligne = $sign * $object->lines[$i]->total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent;

Check warning on line 675 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;situation_percent
}
} else {
if (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) {
$localtax1ligne = $object->lines[$i]->total_localtax1;
$localtax2ligne = $object->lines[$i]->total_localtax2;
$localtax1_rate = $object->lines[$i]->localtax1_tx;

Check warning on line 687 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;localtax1_tx
$localtax2_rate = $object->lines[$i]->localtax2_tx;

Check warning on line 688 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;localtax2_tx
$localtax1_type = $object->lines[$i]->localtax1_type;

Check warning on line 689 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;localtax1_type (Did you mean $localtax1_rate)
$localtax2_type = $object->lines[$i]->localtax2_type;

Check warning on line 690 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;localtax2_type (Did you mean $localtax1_type)
$vatrate = (string) $object->lines[$i]->tva_tx;
$this->tva[$vatrate] = 0;
}
$this->tva[$vatrate] += $tvaligne;
$vatcode = $object->lines[$i]->vat_src_code;

Check warning on line 727 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;vat_src_code
if (empty($this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'])) {
$this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'] = 0;
}
}
}
if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {

Check warning on line 764 in htdocs/modulebuilder/template/core/modules/mymodule/doc/pdf_standard_myobject.modules.php

GitHub Actions / phan / Run phan

pdf_standard_myobject.modules.php: PhanUndeclaredProperty: Reference to undeclared property \MyObjectLine-&gt;pagebreak
if ($pagenb == $pageposafter) {
$this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
} else {