From 10aacb88111fce9ba58bf8aae98e08daa4c7978e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 8 Sep 2015 13:39:28 +0200 Subject: [PATCH] Fix code generator --- dev/skeletons/build_class_from_table.php | 126 ++++++++++++++++++++--- dev/skeletons/skeleton_page.php | 32 +++--- 2 files changed, 127 insertions(+), 31 deletions(-) diff --git a/dev/skeletons/build_class_from_table.php b/dev/skeletons/build_class_from_table.php index 6dac857ac11..f4f25c84391 100755 --- a/dev/skeletons/build_class_from_table.php +++ b/dev/skeletons/build_class_from_table.php @@ -50,16 +50,16 @@ print "***** $script_file ($version) *****\n"; // -------------------- START OF BUILD_CLASS_FROM_TABLE SCRIPT -------------------- // 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; } -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"; - $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') @@ -68,15 +68,18 @@ if ($db->type != 'mysql' && $db->type != 'mysqli') exit; } +$table=$argv[1]; +$module=$argv[2]; + // Show parameters -print 'Tablename: '.$argv[1]."\n"; +print 'Tablename: '.$table."\n"; +print 'Modulename: '.$module."\n"; print "Current dir: ".getcwd()."\n"; print "Database name: ".$db->database_name."\n"; // Define array with list of properties $property=array(); -$table=$argv[1]; $foundprimary=0; $resql=$db->DDLDescTable($table); if ($resql) @@ -160,7 +163,7 @@ foreach($property as $key => $prop) // Define working variables $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)); $classmin=preg_replace('/_/','',strtolower($classname)); @@ -180,11 +183,18 @@ if (! $sourcecontent) $outfile='out.'.$classmin.'.class.php'; $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 $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element = \''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element = \''.$tablenoprefix.'\'', $targetcontent); $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent); +$targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent); +$targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent); // Substitute comments $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"; } } -$targetcontent=preg_replace('/public \$prop1;/', $varprop, $targetcontent); -$targetcontent=preg_replace('/public \$prop2;/', '', $targetcontent); +$targetcontent=preg_replace('/'.preg_quote('public $prop1;','/').'/', $varprop, $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 2((\s|\n|\r|\t)*)/', '', $targetcontent); @@ -284,7 +294,7 @@ foreach($property as $key => $prop) elseif ($prop['istime']) { $varprop.='\'.(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':"\'".$this->db->idate('; - $varprop.="\$this->".$prop['field'].""; + $varprop.="\$this->".$prop['field']; $varprop.=").\"'\")"; } elseif ($prop['ischar']) @@ -300,14 +310,14 @@ foreach($property as $key => $prop) elseif ($prop['isint']) { $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':'; - $varprop.="\$this->".$prop['field'].""; + $varprop.="\$this->".$prop['field']; $varprop.=')'; } else { $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':"\'".'; - $varprop.="\$this->".$prop['field'].""; - $varprop.='"\'")'; + $varprop.="\$this->".$prop['field']; + $varprop.='."\'")'; } if ($i < (count($property)-$no_output_field)) $varprop.=".','"; @@ -439,6 +449,7 @@ if ($fp) else $error++; + //-------------------------------- // Build skeleton_script.php //-------------------------------- @@ -458,12 +469,19 @@ if (! $sourcecontent) $outfile='out.'.$classmin.'_script.php'; $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 $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent); +$targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent); +$targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent); // Substitute comments $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'; $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 $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent); $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent); $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent); $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $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 $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 $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 -$targetcontent=preg_replace('/\$sql \.= " t\.field1,";/', $varpropselect, $targetcontent); -$targetcontent=preg_replace('/\$sql \.= " t\.field2";/', '', $targetcontent); +$targetcontent=preg_replace('/\$sql\s*\.= " t\.field1,";/', $varpropselect, $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 .fieldx. +$varprop="\n"; +$cleanparam=''; +foreach($property as $key => $prop) +{ + if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime']) + { + $varprop.="print ''.\$obj->".$prop['field'].".'';\n"; + } +} +$targetcontent=preg_replace('/'.preg_quote("print ''.\$obj->field1.'';",'/').'/', $varprop, $targetcontent); +$targetcontent=preg_replace('/'.preg_quote("print ''.\$obj->field2.'';",'/').'/', '', $targetcontent); + + // Build file $fp=fopen($outfile,"w"); diff --git a/dev/skeletons/skeleton_page.php b/dev/skeletons/skeleton_page.php index a74088b3702..2c4fde30ca5 100644 --- a/dev/skeletons/skeleton_page.php +++ b/dev/skeletons/skeleton_page.php @@ -44,7 +44,7 @@ if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@inc if (! $res) die("Include of main fails"); // Change this following line to use the correct relative path from htdocs 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 $langs->load("companies"); @@ -56,6 +56,9 @@ $action = GETPOST('action','alpha'); $backtopage = GETPOST('backtopage'); $myparam = GETPOST('myparam','alpha'); +$search_field1=GETPOST("search_field1"); +$search_field2=GETPOST("search_field2"); + // Protection if external user if ($user->societe_id > 0) { @@ -95,7 +98,7 @@ if (empty($reshook)) { if (GETPOST('cancel')) { - $urltogo=$backtopage?$backtopage:dol_buildpath('/buildingmanagement/list.php',1); + $urltogo=$backtopage?$backtopage:dol_buildpath('/mymodule/list.php',1); header("Location: ".$urltogo); exit; } @@ -227,7 +230,10 @@ jQuery(document).ready(function() { // Part to show a list 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.field1,"; $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 $sql.=$hookmanager->resPrint; $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 $parameters=array(); $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 print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; + print ''; + print ''; $parameters=array(); $reshook=$hookmanager->executeHooks('printFieldListOption',$parameters); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint; @@ -295,11 +300,8 @@ if ($action == 'list' || (empty($id) && $action != 'create')) { // You can use here results print ''; - print ''; - print $obj->field1; - print ''; - print $obj->field2; - print ''; + print ''.$obj->field1.''; + print ''.$obj->field2.''; $parameters=array('obj' => $obj); $reshook=$hookmanager->executeHooks('printFieldListValue',$parameters); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint;