Fix code generator

This commit is contained in:
Laurent Destailleur 2015-09-08 13:39:28 +02:00
parent 82ad81fc03
commit 10aacb8811
2 changed files with 127 additions and 31 deletions

View File

@ -50,16 +50,16 @@ print "***** $script_file ($version) *****\n";
// -------------------- START OF BUILD_CLASS_FROM_TABLE SCRIPT -------------------- // -------------------- START OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
// Check parameters // Check parameters
if (! isset($argv[1]) || (isset($argv[2]) && ! isset($argv[6]))) if (! isset($argv[1]) || ! isset($argv[2]) || (isset($argv[3]) && ! isset($argv[7])))
{ {
print "Usage: $script_file tablename [server port databasename user pass]\n"; print "Usage: $script_file tablename modulename [server port databasename user pass]\n";
exit; exit;
} }
if (isset($argv[2]) && isset($argv[3]) && isset($argv[4]) && isset($argv[5]) && isset($argv[6])) if (isset($argv[3]) && isset($argv[4]) && isset($argv[5]) && isset($argv[6]) && isset($argv[7]))
{ {
print 'Use specific database ids'."\n"; print 'Use specific database ids'."\n";
$db=getDoliDBInstance('mysqli',$argv[2],$argv[5],$argv[6],$argv[4],$argv[3]); $db=getDoliDBInstance('mysqli',$argv[3],$argv[6],$argv[7],$argv[5],$argv[4]);
} }
if ($db->type != 'mysql' && $db->type != 'mysqli') if ($db->type != 'mysql' && $db->type != 'mysqli')
@ -68,15 +68,18 @@ if ($db->type != 'mysql' && $db->type != 'mysqli')
exit; exit;
} }
$table=$argv[1];
$module=$argv[2];
// Show parameters // Show parameters
print 'Tablename: '.$argv[1]."\n"; print 'Tablename: '.$table."\n";
print 'Modulename: '.$module."\n";
print "Current dir: ".getcwd()."\n"; print "Current dir: ".getcwd()."\n";
print "Database name: ".$db->database_name."\n"; print "Database name: ".$db->database_name."\n";
// Define array with list of properties // Define array with list of properties
$property=array(); $property=array();
$table=$argv[1];
$foundprimary=0; $foundprimary=0;
$resql=$db->DDLDescTable($table); $resql=$db->DDLDescTable($table);
if ($resql) if ($resql)
@ -160,7 +163,7 @@ foreach($property as $key => $prop)
// Define working variables // Define working variables
$table=strtolower($table); $table=strtolower($table);
$tablenoprefix=preg_replace('/'.preg_quote(MAIN_DB_PREFIX).'/i','',$table); $tablenoprefix=preg_replace('/'.preg_quote(MAIN_DB_PREFIX,'/').'/i','',$table);
$classname=preg_replace('/_/','',ucfirst($tablenoprefix)); $classname=preg_replace('/_/','',ucfirst($tablenoprefix));
$classmin=preg_replace('/_/','',strtolower($classname)); $classmin=preg_replace('/_/','',strtolower($classname));
@ -180,11 +183,18 @@ if (! $sourcecontent)
$outfile='out.'.$classmin.'.class.php'; $outfile='out.'.$classmin.'.class.php';
$targetcontent=$sourcecontent; $targetcontent=$sourcecontent;
// Substitute module name
$targetcontent=preg_replace('/dev\/skeletons/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule othermodule1 othermodule2/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule/', $module, $targetcontent);
// Substitute class name // Substitute class name
$targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
$targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element = \''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element = \''.$classmin.'\'', $targetcontent);
$targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element = \''.$tablenoprefix.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element = \''.$tablenoprefix.'\'', $targetcontent);
$targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent); $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
$targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent);
$targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
// Substitute comments // Substitute comments
$targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent); $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
@ -208,8 +218,8 @@ foreach($property as $key => $prop)
$varprop.="\n"; $varprop.="\n";
} }
} }
$targetcontent=preg_replace('/public \$prop1;/', $varprop, $targetcontent); $targetcontent=preg_replace('/'.preg_quote('public $prop1;','/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/public \$prop2;/', '', $targetcontent); $targetcontent=preg_replace('/'.preg_quote('public $prop2;','/').'/', '', $targetcontent);
$targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 1((\s|\n|\r|\t)*)/', '', $targetcontent); $targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 1((\s|\n|\r|\t)*)/', '', $targetcontent);
$targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 2((\s|\n|\r|\t)*)/', '', $targetcontent); $targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 2((\s|\n|\r|\t)*)/', '', $targetcontent);
@ -284,7 +294,7 @@ foreach($property as $key => $prop)
elseif ($prop['istime']) elseif ($prop['istime'])
{ {
$varprop.='\'.(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':"\'".$this->db->idate('; $varprop.='\'.(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':"\'".$this->db->idate(';
$varprop.="\$this->".$prop['field'].""; $varprop.="\$this->".$prop['field'];
$varprop.=").\"'\")"; $varprop.=").\"'\")";
} }
elseif ($prop['ischar']) elseif ($prop['ischar'])
@ -300,14 +310,14 @@ foreach($property as $key => $prop)
elseif ($prop['isint']) elseif ($prop['isint'])
{ {
$varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':'; $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':';
$varprop.="\$this->".$prop['field'].""; $varprop.="\$this->".$prop['field'];
$varprop.=')'; $varprop.=')';
} }
else else
{ {
$varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':"\'".'; $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
$varprop.="\$this->".$prop['field'].""; $varprop.="\$this->".$prop['field'];
$varprop.='"\'")'; $varprop.='."\'")';
} }
if ($i < (count($property)-$no_output_field)) $varprop.=".','"; if ($i < (count($property)-$no_output_field)) $varprop.=".','";
@ -439,6 +449,7 @@ if ($fp)
else $error++; else $error++;
//-------------------------------- //--------------------------------
// Build skeleton_script.php // Build skeleton_script.php
//-------------------------------- //--------------------------------
@ -458,12 +469,19 @@ if (! $sourcecontent)
$outfile='out.'.$classmin.'_script.php'; $outfile='out.'.$classmin.'_script.php';
$targetcontent=$sourcecontent; $targetcontent=$sourcecontent;
// Substitute module name
$targetcontent=preg_replace('/dev\/skeletons/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule othermodule1 othermodule2/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule/', $module, $targetcontent);
// Substitute class name // Substitute class name
$targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
$targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent); $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
$targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
$targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
$targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent); $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
$targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent);
$targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
// Substitute comments // Substitute comments
$targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent); $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
@ -504,13 +522,19 @@ if (! $sourcecontent)
$outfile='out.'.$classmin.'_page.php'; $outfile='out.'.$classmin.'_page.php';
$targetcontent=$sourcecontent; $targetcontent=$sourcecontent;
// Substitute module name
$targetcontent=preg_replace('/dev\/skeletons/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule othermodule1 othermodule2/', $module, $targetcontent);
$targetcontent=preg_replace('/mymodule/', $module, $targetcontent);
// Substitute class name // Substitute class name
$targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
$targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent); $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
$targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
$targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
$targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent); $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
$targetcontent=preg_replace('/skeleton/', $classname, $targetcontent); $targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent);
$targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
// Substitute comments // Substitute comments
$targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent); $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
@ -520,9 +544,79 @@ $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_
// Substitute table name // Substitute table name
$targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent); $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
// Substitute GETPOST search_fieldx
$varprop="\n";
$cleanparam='';
foreach($property as $key => $prop)
{
if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
{
if ($prop['isint']) $varprop.='$search_'.$prop['field']."=GETPOST('search_".$prop['field']."','int');\n";
else $varprop.='$search_'.$prop['field']."=GETPOST('search_".$prop['field']."','alpha');\n";
}
}
$targetcontent=preg_replace('/'.preg_quote('$search_field1=GETPOST("search_field1");','/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/'.preg_quote('$search_field2=GETPOST("search_field2");','/').'/', '', $targetcontent);
// Substitute GETPOST fieldx
$varprop="\n";
$cleanparam='';
foreach($property as $key => $prop)
{
if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
{
if ($prop['isint']) $varprop.="\t\$object->".$prop['field']."=GETPOST('".$prop['field']."','int');\n";
else $varprop.="\t\$object->".$prop['field']."=GETPOST('".$prop['field']."','alpha');\n";
}
}
$targetcontent=preg_replace('/'.preg_quote('$object->prop1=GETPOST("field1");','/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/'.preg_quote('$object->prop2=GETPOST("field2");','/').'/', '', $targetcontent);
// Substitute fetch/select parameters // Substitute fetch/select parameters
$targetcontent=preg_replace('/\$sql \.= " t\.field1,";/', $varpropselect, $targetcontent); $targetcontent=preg_replace('/\$sql\s*\.= " t\.field1,";/', $varpropselect, $targetcontent);
$targetcontent=preg_replace('/\$sql \.= " t\.field2";/', '', $targetcontent); $targetcontent=preg_replace('/\$sql\s*\.= " t\.field2";/', '', $targetcontent);
// Substitute where for search
$varprop="\n";
$cleanparam='';
foreach($property as $key => $prop)
{
if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
{
$varprop.='if ($search_'.$prop['field'].') $sql.= natural_search("'.$prop['field'].'",$search_'.$prop['field'].');'."\n";
}
}
$targetcontent=preg_replace('/'.preg_quote('if ($search_field1) $sql.= natural_search("field1",$search_field1);','/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/'.preg_quote('if ($search_field2) $sql.= natural_search("field2",$search_field2);','/').'/', '', $targetcontent);
// Substitute print_liste_field_titre
$varprop="\n";
$cleanparam='';
foreach($property as $key => $prop)
{
if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
{
$varprop.="print_liste_field_titre(\$langs->trans('".$prop['field']."'),\$_SERVER['PHP_SELF'],'t.".$prop['field']."','',\$param,'',\$sortfield,\$sortorder);\n";
}
}
$targetcontent=preg_replace('/'.preg_quote("print_liste_field_titre(\$langs->trans('field1'),\$_SERVER['PHP_SELF'],'t.field1','',\$param,'',\$sortfield,\$sortorder);",'/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/'.preg_quote("print_liste_field_titre(\$langs->trans('field2'),\$_SERVER['PHP_SELF'],'t.field1','',\$param,'',\$sortfield,\$sortorder);",'/').'/', '', $targetcontent);
// Substitute where for <td>.fieldx.</td>
$varprop="\n";
$cleanparam='';
foreach($property as $key => $prop)
{
if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
{
$varprop.="print '<td>'.\$obj->".$prop['field'].".'</td>';\n";
}
}
$targetcontent=preg_replace('/'.preg_quote("print '<td>'.\$obj->field1.'</td>';",'/').'/', $varprop, $targetcontent);
$targetcontent=preg_replace('/'.preg_quote("print '<td>'.\$obj->field2.'</td>';",'/').'/', '', $targetcontent);
// Build file // Build file
$fp=fopen($outfile,"w"); $fp=fopen($outfile,"w");

View File

@ -44,7 +44,7 @@ if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@inc
if (! $res) die("Include of main fails"); if (! $res) die("Include of main fails");
// Change this following line to use the correct relative path from htdocs // Change this following line to use the correct relative path from htdocs
include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'); include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php');
dol_include_once('/module/class/skeleton_class.class.php'); dol_include_once('/mymodule/class/skeleton_class.class.php');
// Load traductions files requiredby by page // Load traductions files requiredby by page
$langs->load("companies"); $langs->load("companies");
@ -56,6 +56,9 @@ $action = GETPOST('action','alpha');
$backtopage = GETPOST('backtopage'); $backtopage = GETPOST('backtopage');
$myparam = GETPOST('myparam','alpha'); $myparam = GETPOST('myparam','alpha');
$search_field1=GETPOST("search_field1");
$search_field2=GETPOST("search_field2");
// Protection if external user // Protection if external user
if ($user->societe_id > 0) if ($user->societe_id > 0)
{ {
@ -95,7 +98,7 @@ if (empty($reshook))
{ {
if (GETPOST('cancel')) if (GETPOST('cancel'))
{ {
$urltogo=$backtopage?$backtopage:dol_buildpath('/buildingmanagement/list.php',1); $urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/list.php',1);
header("Location: ".$urltogo); header("Location: ".$urltogo);
exit; exit;
} }
@ -227,7 +230,10 @@ jQuery(document).ready(function() {
// Part to show a list // Part to show a list
if ($action == 'list' || (empty($id) && $action != 'create')) if ($action == 'list' || (empty($id) && $action != 'create'))
{ {
$sql = "SELECT"; // Put here content of your page
print load_fiche_titre('PageTitle');
$sql = "SELECT";
$sql.= " t.rowid,"; $sql.= " t.rowid,";
$sql.= " t.field1,"; $sql.= " t.field1,";
$sql.= " t.field2"; $sql.= " t.field2";
@ -238,7 +244,10 @@ if ($action == 'list' || (empty($id) && $action != 'create'))
$reshook=$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook $reshook=$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint; $sql.=$hookmanager->resPrint;
$sql.= " FROM ".MAIN_DB_PREFIX."mytable as t"; $sql.= " FROM ".MAIN_DB_PREFIX."mytable as t";
$sql.= " WHERE field3 = 'xxx'"; $sql.= " WHERE 1 = 1";
if ($search_field1) $sql.= natural_search("field1",$search_field1);
if ($search_field2) $sql.= natural_search("field2",$search_field2);
// Add where from hooks // Add where from hooks
$parameters=array(); $parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
@ -270,12 +279,8 @@ if ($action == 'list' || (empty($id) && $action != 'create'))
// Fields title search // Fields title search
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
print '<td class="liste_titre">'; print '<td class="liste_titre"><input type="text" class="flat" name="search_field1" value="'.$search_field1.'" size="10"></td>';
print '<input type="text" class="flat" name="search_field1" value="'.$search_field1.'" size="10">'; print '<td class="liste_titre"><input type="text" class="flat" name="search_field2" value="'.$search_field2.'" size="10"></td>';
print '</td>';
print '<td class="liste_titre">';
print '<input type="text" class="flat" name="search_field2" value="'.$search_field2.'" size="10">';
print '</td>';
$parameters=array(); $parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook $reshook=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook
print $hookmanager->resPrint; print $hookmanager->resPrint;
@ -295,11 +300,8 @@ if ($action == 'list' || (empty($id) && $action != 'create'))
{ {
// You can use here results // You can use here results
print '<tr>'; print '<tr>';
print '<td>'; print '<td>'.$obj->field1.'</td>';
print $obj->field1; print '<td>'.$obj->field2.'</td>';
print '</td><td>';
print $obj->field2;
print '</td>';
$parameters=array('obj' => $obj); $parameters=array('obj' => $obj);
$reshook=$hookmanager->executeHooks('printFieldListValue',$parameters); // Note that $action and $object may have been modified by hook $reshook=$hookmanager->executeHooks('printFieldListValue',$parameters); // Note that $action and $object may have been modified by hook
print $hookmanager->resPrint; print $hookmanager->resPrint;