mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-09 02:58:23 +01:00
Add new dictionary for modulebuilder (#25592)
* product_card problem unexpected tocken
* new update for function generate_doc
* new function for create dictionnary
* add function for create new dictionary
* fix problem
* update function for building correct string
* optimize function and fix incoherences
* optimize function and fix incoherences
* Revert "optimize function and fix incoherences"
This reverts commit ecbd902a9f.
This commit is contained in:
committed by
GitHub
parent
109b54d942
commit
2e8525933c
@@ -1020,3 +1020,116 @@ function reWriteAllMenus($file, $menus, $menuWantTo, $key, $action)
|
||||
return 1;
|
||||
}return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new dictionary table.
|
||||
*
|
||||
* for creating a new dictionary table in Dolibarr. It generates the necessary SQL code to define the table structure,
|
||||
* including columns such as 'rowid', 'code', 'label', 'position', 'use_default', 'active', etc. The table name is constructed based on the provided $namedic parameter.
|
||||
*
|
||||
* @param string $modulename The lowercase name of the module for which the dictionary table is being created.
|
||||
* @param string $file The file path to the Dolibarr module builder file where the dictionaries are defined.
|
||||
* @param string $namedic The name of the dictionary, which will also be used as the base for the table name.
|
||||
* @param array|null $dictionnaires An optional array containing pre-existing dictionary data, including 'tabname', 'tablib', 'tabsql', etc.
|
||||
* @return void
|
||||
*/
|
||||
function createNewDictionnary($modulename, $file, $namedic, $dictionnaires = null)
|
||||
{
|
||||
global $db, $langs;
|
||||
|
||||
if (empty($namedic)) {
|
||||
setEventMessages($langs->trans("ErrorEmptyNameDic"), null, 'errors');
|
||||
return;
|
||||
}
|
||||
if (!file_exists($file)) {
|
||||
return -1;
|
||||
}
|
||||
$modulename = strtolower($modulename);
|
||||
|
||||
if (empty($dictionnaires)) {
|
||||
$dictionnaires = array('tabname' => array(), 'tablib' => array(), 'tabsql' => array(), 'tabsqlsort' => array(), 'tabfield' => array(), 'tabfieldvalue' => array(), 'tabfieldinsert' => array(), 'tabrowid' => array(), 'tabcond' => array(), 'tabhelp' => array());
|
||||
}
|
||||
|
||||
$columns = array(
|
||||
'rowid' => array('type' => 'integer(11)'),
|
||||
'code' => array('type' => 'varchar(255) NOT NULL'),
|
||||
'label' => array('type' => 'varchar(255) NOT NULL'),
|
||||
'position' => array('type' => 'integer(11) NULL'),
|
||||
'use_default' => array('type' => 'varchar(255) DEFAULT 1'),
|
||||
'active' => array('type' => 'integer')
|
||||
);
|
||||
|
||||
|
||||
$primaryKey = 'rowid';
|
||||
foreach ($columns as $key => $value) {
|
||||
if ($key === 'rowid') {
|
||||
$primaryKey = 'rowid';
|
||||
break;
|
||||
}
|
||||
if (!array_key_exists('rowid', $columns)) {
|
||||
$primaryKey = array_key_first($columns);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// check if tablename exist in Database
|
||||
$query = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX.strtolower($namedic) . "'";
|
||||
$checkTable = $db->query($query);
|
||||
if ($checkTable && $db->num_rows($checkTable) > 0) {
|
||||
setEventMessages($langs->trans("ErrorTableExist", $namedic), null, 'errors');
|
||||
return;
|
||||
} else {
|
||||
$_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey, "InnoDB");
|
||||
if ($_results < 0) {
|
||||
dol_print_error($db);
|
||||
$langs->load("errors");
|
||||
setEventMessages($langs->trans("ErrorTableNotFound", $namedic), null, 'errors');
|
||||
}
|
||||
}
|
||||
|
||||
// rewrite dictionnary if
|
||||
$dictionnaires['tabname'][] = $namedic;
|
||||
$dictionnaires['tablib'][] = ucfirst(substr($namedic, 2));
|
||||
$dictionnaires['tabsql'][] = 'SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).' as f';
|
||||
$dictionnaires['tabsqlsort'][] = (array_key_exists('label', $columns) ? 'label ASC' : '');
|
||||
$dictionnaires['tabfield'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
|
||||
$dictionnaires['tabfieldvalue'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
|
||||
$dictionnaires['tabfieldinsert'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
|
||||
$dictionnaires['tabrowid'][] = $primaryKey;
|
||||
$dictionnaires['tabcond'][] = isModEnabled('$modulename');
|
||||
$dictionnaires['tabhelp'][] = (array_key_exists('code', $columns) ? array('code'=>$langs->trans('CodeTooltipHelp'), 'field2' => 'field2tooltip') : '');
|
||||
|
||||
// Build the dictionary string
|
||||
$dicData = "\t\t\$this->dictionaries=array(\n";
|
||||
|
||||
foreach ($dictionnaires as $key => $value) {
|
||||
$dicData .= "\t\t\t'$key'=>";
|
||||
|
||||
if ($key === 'tabcond') {
|
||||
$conditions = array_map(function ($val) use ($modulename) {
|
||||
return ($val === true || $val === false) ? "isModEnabled('$modulename')" : $val;
|
||||
}, $value);
|
||||
$dicData .= "array(" . implode(",", $conditions) . ")";
|
||||
} elseif ($key === 'tabhelp') {
|
||||
$helpItems = array();
|
||||
foreach ($value as $helpValue) {
|
||||
$helpItems[] = "array('code'=>\$langs->trans('".$helpValue['code']."'), 'field2' => 'field2tooltip')";
|
||||
}
|
||||
$dicData .= "array(" . implode(",", $helpItems) . ")";
|
||||
} else {
|
||||
if (is_array($value)) {
|
||||
$dicData .= "array(" . implode(",", array_map(function ($val) {
|
||||
return "'$val'";
|
||||
}, $value)) . ")";
|
||||
} else {
|
||||
$dicData .= "'$value'";
|
||||
}
|
||||
}
|
||||
$dicData .= ",\n";
|
||||
}
|
||||
$dicData .= "\t\t);";
|
||||
$stringDic = getFromFile($file, '/* BEGIN MODULEBUILDER DICTIONARIES */', '/* END MODULEBUILDER DICTIONARIES */');
|
||||
$writeInfile = dolReplaceInFile($file, array($stringDic => $dicData."\n"));
|
||||
if ($writeInfile > 0) {
|
||||
setEventMessages($langs->trans("DictionariesCreated", ucfirst(substr($namedic, 2))), null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user