From 742890157a1d252fdcece194ecd112c750cbf194 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Mon, 20 Mar 2023 19:43:23 +0100 Subject: [PATCH 1/5] delete table of property from documentation --- htdocs/core/lib/modulebuilder.lib.php | 45 +++++++++++++++++++++++++++ htdocs/modulebuilder/index.php | 6 ++++ 2 files changed, 51 insertions(+) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 26b5437aacc..200d2e98bda 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -620,3 +620,48 @@ function writePropsInAsciiDoc($file, $objectname, $destfile) } return 1; } + +/** + * Search a string and return all lines needed from file + * @param string $file file for searching + * @param string $start start line if exist + * @param string $end end line if exist + * @return string return the content needed + */ +function getFromFile($file, $start, $end) +{ + $i = 1; + $keys = array(); + $lines = file($file); + // Search for start and end lines + foreach ($lines as $i => $line) { + if (strpos($line, $start) !== false) { + // Copy lines until the end on array + while (($line = $lines[++$i]) !== false) { + if (strpos($line, $end) !== false) { + break; + } + $keys[] = $line; + } + break; + } + } + $content = implode("", $keys); + return $content; +} + +/** + * Delete property from documentation if we delete object + * @param string $file file or path + * @param string $objectname name of object wants to deleted + * @return void + */ +function deletePropsFromDoc($file, $objectname) +{ + + $start = "== Table of fields and their properties for object *".ucfirst($objectname)."* : "; + $end = "== end table for object ".ucfirst($objectname); + $string = getFromFile($file, $start, $end); + dolReplaceInFile($file, array($string => '')); + dolReplaceInFile($file, array($start => '', $end => '')); +} diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index c0c29ea2864..82542691f2c 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -1914,6 +1914,12 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) { 'core/modules/mymodule/doc/pdf_standard_myobject.modules.php'=>'core/modules/'.strtolower($module).'/doc/pdf_standard_'.strtolower($objectname).'.modules.php' ); + // delete property if documentation was generated + $file_doc = $dirins.'/'.strtolower($module).'/doc/Documentation.asciidoc'; + if (file_exists($file_doc)) { + deletePropsFromDoc($file_doc, $objectname); + } + //menu for the object selected // load class and check if menu exist for this object $pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath']; From 55667c5ac4b7022d405c38c84733ccea3938052c Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Tue, 21 Mar 2023 12:57:02 +0100 Subject: [PATCH 2/5] update function for delete properties from Doc --- htdocs/core/lib/modulebuilder.lib.php | 36 +++------------------------ 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 200d2e98bda..5b5de104d3d 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -621,35 +621,6 @@ function writePropsInAsciiDoc($file, $objectname, $destfile) return 1; } -/** - * Search a string and return all lines needed from file - * @param string $file file for searching - * @param string $start start line if exist - * @param string $end end line if exist - * @return string return the content needed - */ -function getFromFile($file, $start, $end) -{ - $i = 1; - $keys = array(); - $lines = file($file); - // Search for start and end lines - foreach ($lines as $i => $line) { - if (strpos($line, $start) !== false) { - // Copy lines until the end on array - while (($line = $lines[++$i]) !== false) { - if (strpos($line, $end) !== false) { - break; - } - $keys[] = $line; - } - break; - } - } - $content = implode("", $keys); - return $content; -} - /** * Delete property from documentation if we delete object * @param string $file file or path @@ -661,7 +632,8 @@ function deletePropsFromDoc($file, $objectname) $start = "== Table of fields and their properties for object *".ucfirst($objectname)."* : "; $end = "== end table for object ".ucfirst($objectname); - $string = getFromFile($file, $start, $end); - dolReplaceInFile($file, array($string => '')); - dolReplaceInFile($file, array($start => '', $end => '')); + $str = file_get_contents($file); + $search = '/' . preg_quote($start, '/') . '(.*?)' . preg_quote($end, '/') . '/s'; + $new_contents = preg_replace($search, '', $str); + file_put_contents($file, $new_contents); } From fcbae93328e4e95ea65a6e29f00cad48e9661339 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Tue, 21 Mar 2023 16:00:50 +0100 Subject: [PATCH 3/5] add function writePropsInAsciiDoc when Init object if doc was generated --- htdocs/modulebuilder/index.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 82542691f2c..5fdcb307100 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -1586,6 +1586,13 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { setEventMessages($langs->trans('ErrorFailToCreateFile', $pathoffiletoeditsrc), null, 'errors'); $error++; } + // check if documentation was generate and add table of properties object + $file = $destdir.'/class/'.strtolower($objectname).'.class.php'; + $destfile = $destdir.'/doc/Documentation.asciidoc'; + + if (file_exists($destfile)) { + writePropsInAsciiDoc($file, $objectname, $destfile); + } } if (!$error) { // Edit sql with new properties From 2b14d74cf1d0fed544633ad870bf1d5ecdc5af25 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Fri, 24 Mar 2023 18:42:13 +0100 Subject: [PATCH 4/5] update function deletewrite --- htdocs/core/lib/modulebuilder.lib.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 5b5de104d3d..9d937d67627 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -613,6 +613,7 @@ function writePropsInAsciiDoc($file, $objectname, $destfile) } // end table $table .= "|==="; + $table .= "__ end table for object $objectname"; //write in file $writeInFile = dolReplaceInFile($destfile, array('== DATA SPECIFICATIONS'=> $table)); if ($writeInFile<0) { @@ -631,7 +632,7 @@ function deletePropsFromDoc($file, $objectname) { $start = "== Table of fields and their properties for object *".ucfirst($objectname)."* : "; - $end = "== end table for object ".ucfirst($objectname); + $end = "__ end table for object ".ucfirst($objectname); $str = file_get_contents($file); $search = '/' . preg_quote($start, '/') . '(.*?)' . preg_quote($end, '/') . '/s'; $new_contents = preg_replace($search, '', $str); From 90221407ce7624fe98bf28afdbe001c5886ddad8 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Tue, 2 May 2023 18:02:36 +0200 Subject: [PATCH 5/5] optimize and clean function rewritePerms --- htdocs/core/lib/modulebuilder.lib.php | 51 +++++++++++++++++++++++++-- htdocs/modulebuilder/index.php | 2 +- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 83d06c43c55..4ce4dda1250 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -482,6 +482,16 @@ function deletePerms($file) dolReplaceInFile($file, array($allContent => '')); } +/** + * Compare two value + * @param int|string $a value 1 + * @param int|string $b value 2 + * @return int less 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. +*/ +function compareFirstValue($a, $b) +{ + return strcmp($a[0], $b[0]); +} /** * Rewriting all permissions after any actions * @param string $file filename or path @@ -515,11 +525,46 @@ function reWriteAllPermissions($file, $permissions, $key, $right, $action) $permissions[$i][4] = "\$this->rights[\$r][4] = '".$permissions[$i][4]."'"; $permissions[$i][5] = "\$this->rights[\$r][5] = '".$permissions[$i][5]."';\n\t\t"; } + // for group permissions by object + $perms_grouped = array(); + foreach ($permissions as $perms) { + $object = $perms[4]; + if (!isset($perms_grouped[$object])) { + $perms_grouped[$object] = []; + } + $perms_grouped[$object][] = $perms; + } + //$perms_grouped = array_values($perms_grouped); + $permissions = $perms_grouped; + + + // parcourir les objets + $o=0; + foreach ($permissions as &$object) { + // récupérer la permission de l'objet + $p = 1; + foreach ($object as &$obj) { + if (str_contains($obj[5], 'read')) { + $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 0 + 1)"; + } elseif (str_contains($obj[5], 'write')) { + $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 1 + 1)"; + } elseif (str_contains($obj[5], 'delete')) { + $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 2 + 1)"; + } else { + $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + ".$p." + 1)"; + $p++; + } + } + usort($object, 'compareFirstValue'); + $o++; + } //convert to string foreach ($permissions as $perms) { - $rights[] = implode(";\n\t\t", $perms); - $rights[] = "\$r++;\n\t\t"; + foreach ($perms as $per) { + $rights[] = implode(";\n\t\t", $per); + $rights[] = "\$r++;\n\t\t"; + } } $rights_str = implode("", $rights); // delete all permission from file @@ -527,6 +572,8 @@ function reWriteAllPermissions($file, $permissions, $key, $right, $action) // rewrite all permission again dolReplaceInFile($file, array('/* BEGIN MODULEBUILDER PERMISSIONS */' => '/* BEGIN MODULEBUILDER PERMISSIONS */'."\n\t\t".$rights_str)); return 1; + } else { + return -1; } } diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 20a78a21ab9..2e125a244d9 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -4779,7 +4779,7 @@ if ($module == 'initmodule') { //form for add new right print ''; - print ''; + print ''; print ''; print '