2
0
forked from Wavyzz/dolibarr
This commit is contained in:
Laurent Destailleur
2022-07-15 15:35:45 +02:00
parent 504383c4a2
commit 1d0ba05e90
3 changed files with 50 additions and 25 deletions

View File

@@ -30,7 +30,7 @@
* @param string $objectname Name of object
* @param string $newmask New mask
* @param string $readdir Directory source (use $destdir when not defined)
* @param string $addfieldentry Array of the field entry to add array('key'=>,'type'=>,''label'=>,'visible'=>,'enabled'=>,'position'=>,'notnull'=>','index'=>,'searchall'=>,'comment'=>,'help'=>,'isameasure')
* @param string $addfieldentry Array of 1 field entry to add array('key'=>,'type'=>,''label'=>,'visible'=>,'enabled'=>,'position'=>,'notnull'=>','index'=>,'searchall'=>,'comment'=>,'help'=>,'isameasure')
* @param string $delfieldentry Id of field to remove
* @return int|object <=0 if KO, Object if OK
* @see rebuildObjectSql()
@@ -40,7 +40,7 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
global $db, $langs;
if (empty($objectname)) {
return -1;
return -6;
}
if (empty($readdir)) {
$readdir = $destdir;
@@ -48,9 +48,11 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
if (!empty($addfieldentry['arrayofkeyval']) && !is_array($addfieldentry['arrayofkeyval'])) {
dol_print_error('', 'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
return -1;
return -7;
}
$error = 0;
// Check parameters
if (is_array($addfieldentry) && count($addfieldentry) > 0) {
if (empty($addfieldentry['name'])) {
@@ -62,8 +64,8 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
return -2;
}
if (!preg_match('/^(integer|price|sellist|varchar|double|text|html|duration)/', $addfieldentry['type'])
&& !preg_match('/^(boolean|real|date|datetime|timestamp)$/', $addfieldentry['type'])) {
setEventMessages($langs->trans('BadValueForType', $objectname), null, 'errors');
&& !preg_match('/^(boolean|smallint|real|date|datetime|timestamp|phone|mail|url|ip|password)$/', $addfieldentry['type'])) {
setEventMessages($langs->trans('BadValueForType', $addfieldentry['type']), null, 'errors');
return -2;
}
}
@@ -217,10 +219,14 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
dol_mkdir(dirname($pathoffiletoedittarget));
//file_put_contents($pathoffiletoedittmp, $contentclass);
file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
$result = file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
if ($result) {
@chmod($pathoffiletoedittarget, octdec($newmask));
} else {
$error++;
}
return $object;
return $error ? -1 : $object;
} catch (Exception $e) {
print $e->getMessage();
return -5;
@@ -257,12 +263,32 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '
// Edit .sql file
if ($moduletype == 'internal') {
$pathoffiletoeditsrc = $readdir.'/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
$pathoffiletoedittarget = $destdir.'/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql'.($readdir != $destdir ? '.new' : '');
} else {
$pathoffiletoeditsrc = $readdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
$pathoffiletoedittarget = $destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql'.($readdir != $destdir ? '.new' : '');
$pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'-'.strtolower($module).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'-'.strtolower($module).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'.sql';
}
}
}
} else {
$pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'-'.strtolower($module).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'-'.strtolower($module).'.sql';
if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
$pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'.sql';
}
}
}
}
// Complete path to be full path
$pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ? '.new' : '');
$pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
if (!dol_is_file($pathoffiletoeditsrc)) {
$langs->load("errors");
@@ -340,16 +366,13 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '
@chmod($pathoffiletoedittarget, octdec($newmask));
} else {
$error++;
setEventMessages($langs->trans("ErrorFailToCreateFile", $pathoffiletoedittarget), null, 'errors');
}
// Edit .key.sql file
if ($moduletype == 'internal') {
$pathoffiletoeditsrc = $readdir.'/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql';
$pathoffiletoedittarget = $destdir.'/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql'.($readdir != $destdir ? '.new' : '');
} else {
$pathoffiletoeditsrc = $destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql';
$pathoffiletoedittarget = $destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql'.($readdir != $destdir ? '.new' : '');
}
$pathoffiletoeditsrc = preg_replace('/\.sql$/', '.key.sql', $pathoffiletoeditsrc);
$pathoffiletoedittarget = preg_replace('/\.sql$/', '.key.sql', $pathoffiletoedittarget);
$pathoffiletoedittarget = preg_replace('/\.sql.new$/', '.key.sql.new', $pathoffiletoedittarget);
$contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
@@ -378,10 +401,11 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '
dol_mkdir(dirname($pathoffiletoedittarget));
$result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
if ($result) {
if ($result2) {
@chmod($pathoffiletoedittarget, octdec($newmask));
} else {
$error++;
setEventMessages($langs->trans("ErrorFailToCreateFile", $pathoffiletoedittarget), null, 'errors');
}
return $error ? -1 : 1;

View File

@@ -155,3 +155,4 @@ LinkToParentMenu=Parent menu (fk_xxxxmenu)
ListOfTabsEntries=List of tab entries
TabsDefDesc=Define here the tabs provided by your module
TabsDefDescTooltip=The tabs provided by your module/application are defined into the array <strong>$this->tabs</strong> into the module descriptor file. You can edit manually this file or use the embedded editor.
BadValueForType=Bad value for type %s

View File

@@ -1518,9 +1518,10 @@ if ($dirins && $action == 'addproperty' && empty($cancel) && !empty($module) &&
$error++;
}*/
$moduletype = $listofmodules[strtolower($module)]['moduletype'];
// Edit the class file to write properties
if (!$error) {
$moduletype = 'external';
$object = rebuildObjectClass($destdir, $module, $objectname, $newmask, $srcdir, $addfieldentry, $moduletype);
if (is_numeric($object) && $object <= 0) {
@@ -1530,21 +1531,20 @@ if ($dirins && $action == 'addproperty' && empty($cancel) && !empty($module) &&
// Edit sql with new properties
if (!$error) {
$moduletype = 'external';
$result = rebuildObjectSql($destdir, $module, $objectname, $newmask, $srcdir, $object, $moduletype);
if ($result <= 0) {
$error++;
}
}
if (!$error) {
clearstatcache(true);
setEventMessages($langs->trans('FilesForObjectUpdated', $objectname), null);
setEventMessages($langs->trans('WarningDatabaseIsNotUpdated'), null);
clearstatcache(true);
// Make a redirect to reload all data
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=objects&module='.$module.($forceddirread ? '@'.$dirread : '').'&tabobj='.$objectname.'&nocache='.time());