From 844ee2f9e517ba045c581abb0ca7f50c73f04776 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Fri, 10 Feb 2023 18:02:25 +0100 Subject: [PATCH] New functionality and fix som coherence in initial object & deleteObject --- htdocs/langs/en_US/errors.lang | 4 + htdocs/langs/fr_FR/errors.lang | 5 +- htdocs/modulebuilder/index.php | 436 +++++++++++++++++++++++++++------ 3 files changed, 374 insertions(+), 71 deletions(-) diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index 54a4b4fb470..5d262f54200 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -305,6 +305,10 @@ ErrorDateOfMovementLowerThanDateOfFileTransmission=The date of the bank transact ErrorTooMuchFileInForm=Too much files in form, the maximum number is %s file(s) ErrorSessionInvalidatedAfterPasswordChange=The session was been invalidated following a change of password, status or dates of validity. Please relogin. ErrorExistingPermission = Permission %s for object %s already exists +ErrorFieldExist=The value for %s already exist +ErrorEqualModule=Module invalid in %s +ErrorFieldValue=Value for %s is incorrect +ErrorCoherenceMenu=%s is required when % equal LEFT # Warnings WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Your PHP parameter upload_max_filesize (%s) is higher than PHP parameter post_max_size (%s). This is not a consistent setup. diff --git a/htdocs/langs/fr_FR/errors.lang b/htdocs/langs/fr_FR/errors.lang index cbf69ff2aa8..eddeadde288 100644 --- a/htdocs/langs/fr_FR/errors.lang +++ b/htdocs/langs/fr_FR/errors.lang @@ -305,7 +305,10 @@ ErrorDateOfMovementLowerThanDateOfFileTransmission=La date de l'opération banca ErrorTooMuchFileInForm=Trop de fichiers dans le formulaire, le nombre maximum est de %s fichier(s) ErrorExistingPermission = La permission %s pour l'objet %s est dejà existante ErrorSessionInvalidatedAfterPasswordChange=La session a été invalidée suite à un changement de mot de passe, d'état ou de dates de validité. Veuillez vous reconnecter. - +ErrorFieldExist=La valeur pour %s existe déja +ErrorEqualModule=Module invalide pour le champ %s +ErrorFieldValue=La valeur du champ %s est incorrecte +ErrorCoherenceMenu = Le champ %s est requis si le champ %s = LEFT # Warnings WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Votre paramètre PHP upload_max_filesize (%s) est supérieur au paramètre PHP post_max_size (%s). Ceci n'est pas une configuration cohérente. diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index e52985e8c50..e4fe622dd1c 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -1356,7 +1356,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { 'type'=>'left', 'titre'=>'List MyObject', 'mainmenu'=>'mymodule', - 'leftmenu'=>'mymodule_myobject', + 'leftmenu'=>'myobject', 'url'=>'/mymodule/myobject_list.php', 'langs'=>'mymodule@mymodule', 'position'=>1100+\$r, @@ -1366,11 +1366,11 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { 'user'=>2, ); \$this->menu[\$r++]=array( - 'fk_menu'=>'fk_mainmenu=mymodule,fk_leftmenu=mymodule_myobject', + 'fk_menu'=>'fk_mainmenu=mymodule,fk_leftmenu=myobject', 'type'=>'left', 'titre'=>'New MyObject', 'mainmenu'=>'mymodule', - 'leftmenu'=>'mymodule_myobject', + 'leftmenu'=>'myobject', 'url'=>'/mymodule/myobject_card.php?action=create', 'langs'=>'mymodule@mymodule', 'position'=>1100+\$r, @@ -1403,7 +1403,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { $menus = $moduleobj->menu; $counter = 0 ; foreach ($menus as $menu) { - if ($menu['leftmenu'] == strtolower($module).'_'.strtolower($objectname)) { + if ($menu['leftmenu'] == strtolower($objectname)) { $counter++; } } @@ -1812,7 +1812,7 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) { $moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php'; foreach ($menus as $menu) { - if ($menu['type'] == 'left' && $menu['leftmenu'] == strtolower($module).'_'.strtolower($objectname)) { + if ($menu['type'] == 'left' && $menu['leftmenu'] == strtolower($objectname)) { $left="\$this->menu[\$r++]=array( 'fk_menu'=>'".$menu['fk_menu']."', 'type'=>'".$menu['type']."', @@ -2436,8 +2436,7 @@ if ($dirins && $action == 'confirm_deletemenu' && GETPOST('menukey', 'int')) { } } if ($menuForObj == 1) { - $extractObjName = explode("_", $menus[$key]['leftmenu']); - dolReplaceInFile($moduledescriptorfile, array('/*LEFTMENU '.strtoupper($extractObjName[1]).'*/'."\n" => '','/*END LEFTMENU '.strtoupper($extractObjName[1]).'*/' => '')); + dolReplaceInFile($moduledescriptorfile, array('/*LEFTMENU '.strtoupper($menus[$key]['leftmenu']).'*/'."\n" => '','/*END LEFTMENU '.strtoupper($menus[$key]['leftmenu']).'*/' => '')); } } @@ -2446,6 +2445,194 @@ if ($dirins && $action == 'confirm_deletemenu' && GETPOST('menukey', 'int')) { exit; } +// Add menu in module without initial object +if ($dirins && $action == 'addmenu' && empty($cancel)) { + // check if module is enabled + if (isModEnabled(strtolower($module))) { + $result = unActivateModule(strtolower($module)); + dolibarr_set_const($db, "MAIN_IHM_PARAMS_REV", (int) $conf->global->MAIN_IHM_PARAMS_REV + 1, 'chaine', 0, '', $conf->entity); + if ($result) { + setEventMessages($result, null, 'errors'); + } + header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=menus&module='.$module); + setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); + } + $error = 0; + $dirins = $listofmodules[strtolower($module)]['moduledescriptorrootpath']; + $destdir = $dirins.'/'.strtolower($module); + $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); + $objects = array(); + foreach ($listofobject as $fileobj) { + if (preg_match('/^api_/', $fileobj['name'])) { + continue; + } + if (preg_match('/^actions_/', $fileobj['name'])) { + continue; + } + + $tmpcontent = file_get_contents($fileobj['fullname']); + $reg = array(); + if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { + $objectnameloop = $reg[1]; + $objects[] = $objectnameloop; + } + } + + // load class and check if right exist + $pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath']; + dol_include_once($pathtofile); + $class = 'mod'.$module; + if (class_exists($class)) { + try { + $moduleobj = new $class($db); + } catch (Exception $e) { + $error++; + dol_print_error($db, $e->getMessage()); + } + } + $menus = $moduleobj->menu; + //verify fields required + if (!GETPOST('type', 'alpha')) { + $error++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Type")), null, 'errors'); + } + if (!GETPOST('titre', 'alpha')) { + $error++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Title")), null, 'errors'); + } + if (!GETPOST('user', 'alpha')) { + $error++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("DetailUser")), null, 'errors'); + } + if (!GETPOST('url', 'alpha')) { + $error++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Url")), null, 'errors'); + } + if (GETPOST('mainmenu') != strtolower($module)) { + $error++; + setEventMessages($langs->trans("ErrorEqualModule", $langs->transnoentities("mainmenu")), null, 'errors'); + } + if (!empty(GETPOST('target'))) { + $targets = array('_blank','_self','_parent','_top',''); + if (!in_array(GETPOST('target'), $targets)) { + $error++; + setEventMessages($langs->trans("ErrorFieldValue", $langs->transnoentities("target")), null, 'errors'); + } + } + if (!empty(GETPOST('perms'))) { + $permssion = array('read','write'); + if (GETPOST('perms') == 1 || GETPOST('perms') == '') { + $perms = 1 ; + } else { + if (!in_array(GETPOST('perms'), $permssion)) { + $error++; + setEventMessages($langs->trans("ErrorFieldValue", $langs->transnoentities("permssion")), null, 'errors'); + } + } + } + + // check if title or url already exist in menus + + foreach ($menus as $menu) { + if (!empty(GETPOST('url')) && GETPOST('url') == $menu['url']) { + $error++; + setEventMessages($langs->trans("ErrorFieldExist", $langs->transnoentities("url")), null, 'errors'); + break; + } + if (strtolower(GETPOST('titre')) == strtolower($menu['titre'])) { + $error++; + setEventMessages($langs->trans("ErrorFieldExist", $langs->transnoentities("titre")), null, 'errors'); + break; + } + } + + + if (GETPOST('type', 'alpha') == 'left' && !empty(GETPOST('lefmenu', 'alpha'))) { + if (!str_contains(GETPOST('leftmenu'), strtolower($module))) { + $error++; + setEventMessages($langs->trans("WarningFieldsMustContains", $langs->transnoentities("leftmenu")), null, 'errors'); + } + } + + if (GETPOST('type', 'alpha') == 'left') { + if (empty(GETPOST('leftmenu') && count($objects) > 0)) { + $error++; + setEventMessages($langs->trans("ErrorCoherenceMenu", $langs->transnoentities("leftmenu"), $langs->transnoentities("type")), null, 'errors'); + } + } + + if (!$error) { + $moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php'; + + $type = GETPOST('type', 'alpha'); + $fk_menu = GETPOST('fk_type', 'alpha'); + $titre = GETPOST('titre', 'alpha'); + $mainmenu = GETPOST('mainmenu', 'alpha'); + $leftmenu = GETPOST('leftmenu', 'alpha'); + $url = GETPOST('url'); + $user = GETPOST('user', 'int'); + (empty(GETPOST('perms')) && GETPOST('type') == 'top') || GETPOST('perms')==1 ? $perms=1 : $perms = 1; + $target = GETPOST('target', 'alpha'); + + + + if ($type == 'top') { + $menuTop = " + \$this->menu[\$r++] = array( + 'fk_menu'=>'".$fk_menu."', + 'type'=>'".strtolower($type)."', + 'titre'=>'".ucfirst($titre)."', + 'prefix' => img_picto('', \$this->picto, 'class=\"paddingright pictofixedwidth valignmiddle\"'), + 'mainmenu'=>'".$mainmenu."', + 'leftmenu'=> '".$leftmenu."', + 'url'=>'".$url."', + 'langs'=>'".strtolower($module)."@".strtolower($module)."', + 'position'=>1000 + \$r, + 'enabled'=>'isModEnabled(\"".strtolower($module)."\")', + 'perms' =>'".$perms."', + 'target'=>'".$target."', + 'user'=>".$user.", + );"; + $addTopMenu = dolReplaceInFile($moduledescriptorfile, array('/* END MODULEBUILDER TOPMENU */' => '/*TOPMENU '.strtolower($titre).'*/'.$menuTop."\n\t\t".'/*END TOPMENU '.strtolower($titre).'*/'."\n\t\t/* END MODULEBUILDER TOPMENU */")); + } + if ($type == 'left') { + $fk_menu = "fk_mainmenu=".strtolower($module).",fk_leftmenu=".strtolower($leftmenu); + $menuLeft= " + \$this->menu[\$r++]=array( + 'fk_menu'=>'".$fk_menu."', + 'type'=>'".$type."', + 'titre'=>'".ucfirst($titre)."', + 'mainmenu'=>'".strtolower($module)."', + 'leftmenu'=>'".strtolower($leftmenu)."', + 'url'=>'".$url."', + 'langs'=>'".strtolower($module)."@".strtolower($module)."', + 'position'=>1100+\$r, + 'enabled'=>'\$conf->".strtolower($module)."->enabled', + 'perms'=>'".$perms."', + 'target'=>'".$target."', + 'user'=>".$user.", + );"; + + $exist = 0; + foreach ($menus as $menu) { + if (strtolower($menu['leftmenu']) == strtolower($leftmenu)) { + $exist++; + } + } + //var_dump($exist);exit; + if ($exist) { + dolReplaceInFile($moduledescriptorfile, array('/*END LEFTMENU '.strtoupper($leftmenu).'*/' => $menuLeft."\n\t\t".'/*END LEFTMENU '.strtoupper($leftmenu).'*/')); + } else { + $addLeftMenu = dolReplaceInFile($moduledescriptorfile, array('/* END MODULEBUILDER LEFTMENU MYOBJECT */' => '/*LEFTMENU '.strtoupper($leftmenu).'*/'."\n".$menuLeft."\n\t\t".'/*END LEFTMENU '.strtoupper($leftmenu).'*/'."\n\t\t".'/* END MODULEBUILDER LEFTMENU MYOBJECT */')); + } + } + + header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=menus&module='.$module); + setEventMessages($langs->trans('MenuAddedSuccesfuly'), null); + exit; + } +} + /* * View @@ -4126,7 +4313,25 @@ if ($module == 'initmodule') { if ($tab == 'menus') { print ''."\n"; $pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath']; + $dirins = $listofmodules[strtolower($module)]['moduledescriptorrootpath']; + $destdir = $dirins.'/'.strtolower($module); + $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); + $objects = array(); + foreach ($listofobject as $fileobj) { + if (preg_match('/^api_/', $fileobj['name'])) { + continue; + } + if (preg_match('/^actions_/', $fileobj['name'])) { + continue; + } + $tmpcontent = file_get_contents($fileobj['fullname']); + $reg = array(); + if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { + $objectnameloop = $reg[1]; + $objects[] = $objectnameloop; + } + } $menus = $moduleobj->menu; if ($action != 'editfile' || empty($file)) { @@ -4155,10 +4360,10 @@ if ($module == 'initmodule') { print ''; print ''; - print_liste_field_titre("#", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'thsticky '); + print_liste_field_titre("#", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'center '); print_liste_field_titre("Position", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder); print_liste_field_titre("LinkToParentMenu", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder); - print_liste_field_titre("Title", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder); + print_liste_field_titre("Title", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'center'); print_liste_field_titre("mainmenu", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder); print_liste_field_titre("leftmenu", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder); print_liste_field_titre("URL", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, '', $langs->transnoentitiesnoconv('DetailUrl')); @@ -4167,76 +4372,167 @@ if ($module == 'initmodule') { print_liste_field_titre("Enabled", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'center ', $langs->trans('DetailEnabled')); print_liste_field_titre("Rights", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, '', $langs->trans('DetailRight')); print_liste_field_titre("Target", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, '', $langs->trans('DetailTarget')); - print_liste_field_titre("MenuForUsers", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'right ', $langs->trans('DetailUser')); + print_liste_field_titre("MenuForUsers", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'center ', $langs->trans('DetailUser')); + print_liste_field_titre("", $_SERVER["PHP_SELF"], '', "", $param, '', $sortfield, $sortorder, 'right ', $langs->trans('')); + print "\n"; + $r = count($menus)+1; + // for adding menu on module + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; if (count($menus)) { $i = 0; foreach ($menus as $menu) { $i++; - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; - - print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; } else { - print $menu['user']; // should not happen - } - print ''; + print ''; + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + print ''; + } print ''; } } else {
'; + print ''; + print ''; + print ''; + print ''; + print '
'; - print $i; - print ''; - print dol_escape_htmltag($menu['type']); - print ''; - print dol_escape_htmltag($menu['fk_menu']); - print ''; - print dol_escape_htmltag($menu['titre']); - print ''; - print dol_escape_htmltag($menu['mainmenu']); - print ''; - print !empty($menu['leftmenu']) ? dol_escape_htmltag($menu['leftmenu']) : ''; - print ''; - print dol_escape_htmltag($menu['url']); - print ''; - print dol_escape_htmltag($menu['langs']); - print ''; - print dol_escape_htmltag($menu['position']); - print ''; - print dol_escape_htmltag($menu['enabled']); - print ''; - print dol_escape_htmltag($menu['perms']); - print ''; - print dol_escape_htmltag($menu['target']); - print ''; - if ($menu['user'] == 2) { - print $langs->trans("AllMenus"); - } elseif ($menu['user'] == 0) { - print $langs->trans('Internal'); - } elseif ($menu['user'] == 1) { - print $langs->trans('External'); + $propFk_menu = !empty($menu['fk_menu']) ? $menu['fk_menu'] : GETPOST('fk_menu'); + $propTitre = !empty($menu['titre']) ? $menu['titre'] : GETPOST('titre'); + $propLeftmenu = !empty($menu['leftmenu']) ? $menu['leftmenu'] : GETPOST('leftmenu'); + $propUrl = !empty($menu['url']) ? $menu['url'] : GETPOST('url', 'alpha'); + $propPerms = !empty($menu['perms']) ? $menu['perms'] : GETPOST('perms'); + $propUser = !empty($menu['user']) ? $menu['user'] : GETPOST('user'); + $propTarget = !empty($menu['target']) ? $menu['target'] : GETPOST('target'); + if ($action == 'editmenu' && GETPOST('menukey', 'int') == ($i-1)) { + print '
'; + print $i; + print ' + + '; + print ''; + print ''; + print '
'; + print ''; + print $i; + print ''; + print dol_escape_htmltag($menu['type']); + print ''; + print dol_escape_htmltag($menu['fk_menu']); + print ''; + print dol_escape_htmltag($menu['titre']); + print ''; + print dol_escape_htmltag($menu['mainmenu']); + print ''; + print dol_escape_htmltag($menu['leftmenu']); + print ''; + print dol_escape_htmltag($menu['url']); + print ''; + print dol_escape_htmltag($menu['langs']); + print ''; + print dol_escape_htmltag($menu['position']); + print ''; + print dol_escape_htmltag($menu['enabled']); + print ''; + print dol_escape_htmltag($menu['perms']); + print ''; + print dol_escape_htmltag($menu['target']); + print ''; + if ($menu['user'] == 2) { + print $langs->trans("AllMenus"); + } elseif ($menu['user'] == 0) { + print $langs->trans('Internal'); + } elseif ($menu['user'] == 1) { + print $langs->trans('External'); + } else { + print $menu['user']; // should not happen + } + print ''; + if ($menu['titre']!= 'Module'.$module.'Name') { + print ''.img_edit().''; + print ''.img_delete().''; + } + print '