Skip to content
Snippets Groups Projects
Commit 0de24cf6 authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Enhance generators

parent 87efb2cb
No related branches found
No related tags found
No related merge requests found
...@@ -58,7 +58,7 @@ if (! isset($argv[1])) ...@@ -58,7 +58,7 @@ if (! isset($argv[1]))
if ($db->type != 'mysql' && $db->type != 'mysqli') if ($db->type != 'mysql' && $db->type != 'mysqli')
{ {
print "Error: This script works with mysql driver only\n"; print "Error: This script works with mysql or mysqli driver only\n";
exit; exit;
} }
...@@ -67,15 +67,10 @@ print 'Tablename='.$argv[1]."\n"; ...@@ -67,15 +67,10 @@ print 'Tablename='.$argv[1]."\n";
print "Current dir is ".getcwd()."\n"; print "Current dir is ".getcwd()."\n";
// Define array with list of properties
//--------------------------------
// Build skeleton_class.class.php
//--------------------------------
$table=$argv[1];
$property=array(); $property=array();
$table=$argv[1];
$foundprimary=0; $foundprimary=0;
$resql=$db->DDLDescTable($table); $resql=$db->DDLDescTable($table);
if ($resql) if ($resql)
{ {
...@@ -121,9 +116,32 @@ if ($resql) ...@@ -121,9 +116,32 @@ if ($resql)
else else
{ {
print "Error: Failed to get description for table '".$table."'.\n"; print "Error: Failed to get description for table '".$table."'.\n";
return false;
} }
//var_dump($property); //var_dump($property);
// Define substitute select parameters
$varpropselect="\n";
$cleanparam='';
$i=0;
foreach($property as $key => $prop)
{
$i++;
if ($prop['field'] != 'rowid')
{
$varpropselect.="\t\t\$sql.= \" ";
$varpropselect.="t.".$prop['field'];
if ($i < count($property)) $varpropselect.=",";
$varpropselect.="\";";
$varpropselect.="\n";
}
}
//--------------------------------
// Build skeleton_class.class.php
//--------------------------------
// Define working variables // Define working variables
$table=strtolower($table); $table=strtolower($table);
...@@ -296,22 +314,7 @@ $targetcontent=preg_replace('/\$sql.= " field1=".\(isset\(\$this->field1\)\?"\'" ...@@ -296,22 +314,7 @@ $targetcontent=preg_replace('/\$sql.= " field1=".\(isset\(\$this->field1\)\?"\'"
$targetcontent=preg_replace('/\$sql.= " field2=".\(isset\(\$this->field2\)\?"\'".\$this->db->escape\(\$this->field2\)."\'":"null"\)."";/', '', $targetcontent); $targetcontent=preg_replace('/\$sql.= " field2=".\(isset\(\$this->field2\)\?"\'".\$this->db->escape\(\$this->field2\)."\'":"null"\)."";/', '', $targetcontent);
// Substitute select parameters // Substitute select parameters
$varprop="\n"; $targetcontent=preg_replace('/\$sql\.= " t\.field1,";/', $varpropselect, $targetcontent);
$cleanparam='';
$i=0;
foreach($property as $key => $prop)
{
$i++;
if ($prop['field'] != 'rowid')
{
$varprop.="\t\t\$sql.= \" ";
$varprop.="t.".$prop['field'];
if ($i < count($property)) $varprop.=",";
$varprop.="\";";
$varprop.="\n";
}
}
$targetcontent=preg_replace('/\$sql\.= " t\.field1,";/', $varprop, $targetcontent);
$targetcontent=preg_replace('/\$sql\.= " t\.field2";/', '', $targetcontent); $targetcontent=preg_replace('/\$sql\.= " t\.field2";/', '', $targetcontent);
// Substitute select set parameters // Substitute select set parameters
...@@ -405,6 +408,54 @@ if ($fp) ...@@ -405,6 +408,54 @@ if ($fp)
} }
else $error++; else $error++;
//--------------------------------
// Build skeleton_page.php
//--------------------------------
// Read skeleton_page.php file
$skeletonfile=$path.'skeleton_page.php';
$sourcecontent=file_get_contents($skeletonfile);
if (! $sourcecontent)
{
print "\n";
print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
print "Try to run script from skeletons directory.\n";
exit;
}
// Define output variables
$outfile='out.'.$classmin.'_page.php';
$targetcontent=$sourcecontent;
// 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);
// 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('/\s*\/\/\.\.\./', '', $targetcontent);
$targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
// Substitute table name
$targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
// Build file
$fp=fopen($outfile,"w");
if ($fp)
{
fputs($fp, $targetcontent);
fclose($fp);
print "File '".$outfile."' has been built in current directory.\n";
}
else $error++;
// -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT -------------------- // -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
print "You can now rename generated files by removing the 'out.' prefix in their name and store them in a directory of your choice.\n"; print "You can now rename generated files by removing the 'out.' prefix in their name and store them in a directory of your choice.\n";
......
...@@ -43,8 +43,8 @@ if (! $res && file_exists("../../../dolibarr/htdocs/main.inc.php")) $res=@includ ...@@ -43,8 +43,8 @@ if (! $res && file_exists("../../../dolibarr/htdocs/main.inc.php")) $res=@includ
if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@include("../../../../dolibarr/htdocs/main.inc.php"); // Used on dev env only if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@include("../../../../dolibarr/htdocs/main.inc.php"); // Used on dev env only
if (! $res && file_exists("../../../../../dolibarr/htdocs/main.inc.php")) $res=@include("../../../../../dolibarr/htdocs/main.inc.php"); // Used on dev env only if (! $res && file_exists("../../../../../dolibarr/htdocs/main.inc.php")) $res=@include("../../../../../dolibarr/htdocs/main.inc.php"); // Used on dev env only
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 (do not remove DOL_DOCUMENT_ROOT) // Change this following line to use the correct relative path from htdocs
require_once(DOL_DOCUMENT_ROOT."/skeleton/skeleton_class.class.php"); dol_include_once("/module/class/skeleton_class.class.php");
// Load traductions files requiredby by page // Load traductions files requiredby by page
$langs->load("companies"); $langs->load("companies");
...@@ -118,11 +118,57 @@ jQuery(document).ready(function() { ...@@ -118,11 +118,57 @@ jQuery(document).ready(function() {
</script>'; </script>';
// Example 2 : Adding links to objects
// Example 2 : Adding jquery code
$somethingshown=$myobject->showLinkedObjectBlock(); $somethingshown=$myobject->showLinkedObjectBlock();
// Example 3 : List of data
if ($action == 'list')
{
$sql = "SELECT";
$sql.= " t.field1,";
$sql.= " t.field2";
$sql.= " FROM ".MAIN_DB_PREFIX."skeleton as t";
$sql.= " WHERE field3 = 'xxx'";
$sql.= " ORDER BY field1 ASC";
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans('field1'),$_SERVER['PHP_SELF'],'t.field1','',$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans('field2'),$_SERVER['PHP_SELF'],'t.field2','',$param,'',$sortfield,$sortorder);
print '</tr>';
dol_syslog($script_file." sql=".$sql, LOG_DEBUG);
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);
if ($obj)
{
// You can use here results
print '<tr><td>';
print $obj->field1;
print $obj->field2;
print '</td></tr>';
}
$i++;
}
}
}
else
{
$error++;
dol_print_error($db);
}
}
// End of page // End of page
llxFooter(); llxFooter();
$db->close(); $db->close();
......
...@@ -112,7 +112,7 @@ else print "Object with id ".$myobject->id." deleted\n"; ...@@ -112,7 +112,7 @@ else print "Object with id ".$myobject->id." deleted\n";
// An example of a direct SQL read without using the fetch method // An example of a direct SQL read without using the fetch method
/* /*
$sql = "SELECT field1, field2"; $sql = "SELECT field1, field2";
$sql.= " FROM ".MAIN_DB_PREFIX."c_pays"; $sql.= " FROM ".MAIN_DB_PREFIX."skeleton";
$sql.= " WHERE field3 = 'xxx'"; $sql.= " WHERE field3 = 'xxx'";
$sql.= " ORDER BY field1 ASC"; $sql.= " ORDER BY field1 ASC";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment