build_class_from_table.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #!/usr/bin/php
  2. <?php
  3. /* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file dev/skeletons/build_class_from_table.php
  20. * \ingroup core
  21. * \brief Create a complete class file from a table in database
  22. */
  23. $sapi_type = php_sapi_name();
  24. $script_file = basename(__FILE__);
  25. $path=dirname(__FILE__).'/';
  26. // Test if batch mode
  27. if (substr($sapi_type, 0, 3) == 'cgi') {
  28. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  29. exit;
  30. }
  31. // Include Dolibarr environment
  32. require_once($path."../../htdocs/master.inc.php");
  33. // After this $db is a defined handler to database.
  34. // Main
  35. $version='$Revision: 1.34 $';
  36. @set_time_limit(0);
  37. $error=0;
  38. $langs->load("main");
  39. print "***** $script_file ($version) *****\n";
  40. // -------------------- START OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
  41. // Check parameters
  42. if (! isset($argv[1]))
  43. {
  44. print "Usage: $script_file tablename\n";
  45. exit;
  46. }
  47. if ($db->type != 'mysql' && $db->type != 'mysqli')
  48. {
  49. print "Error: This script works with mysql driver only\n";
  50. exit;
  51. }
  52. // Show parameters
  53. print 'Tablename='.$argv[1]."\n";
  54. print "Current dir is ".getcwd()."\n";
  55. //--------------------------------
  56. // Build skeleton_class.class.php
  57. //--------------------------------
  58. $table=$argv[1];
  59. $property=array();
  60. $foundprimary=0;
  61. $resql=$db->DDLDescTable($table);
  62. if ($resql)
  63. {
  64. $i=0;
  65. while($obj=$db->fetch_object($resql))
  66. {
  67. //var_dump($obj);
  68. $i++;
  69. $property[$i]['field']=$obj->Field;
  70. if ($obj->Key == 'PRI')
  71. {
  72. $property[$i]['primary']=1;
  73. $foundprimary=1;
  74. }
  75. else
  76. {
  77. $property[$i]['primary']=1;
  78. }
  79. $property[$i]['type'] =$obj->Type;
  80. $property[$i]['null'] =$obj->Null;
  81. $property[$i]['extra']=$obj->Extra;
  82. if ($property[$i]['type'] == 'date'
  83. || $property[$i]['type'] == 'datetime'
  84. || $property[$i]['type'] == 'timestamp')
  85. {
  86. $property[$i]['istime']=true;
  87. }
  88. else
  89. {
  90. $property[$i]['istime']=false;
  91. }
  92. if (preg_match('/varchar/i',$property[$i]['type'])
  93. || preg_match('/text/i',$property[$i]['type']))
  94. {
  95. $property[$i]['ischar']=true;
  96. }
  97. else
  98. {
  99. $property[$i]['ischar']=false;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. print "Error: Failed to get description for table '".$table."'.\n";
  106. }
  107. //var_dump($property);
  108. // Define working variables
  109. $table=strtolower($table);
  110. $tablenollx=preg_replace('/llx_/i','',$table);
  111. $classname=ucfirst($tablenollx);
  112. $classmin=strtolower($classname);
  113. // Read skeleton_class.class.php file
  114. $skeletonfile='skeleton_class.class.php';
  115. $sourcecontent=file_get_contents($skeletonfile);
  116. if (! $sourcecontent)
  117. {
  118. print "\n";
  119. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  120. print "Try to run script from skeletons directory.\n";
  121. exit;
  122. }
  123. // Define output variables
  124. $outfile='out.'.$classmin.'.class.php';
  125. $targetcontent=$sourcecontent;
  126. // Substitute class name
  127. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  128. $targetcontent=preg_replace('/\$element=\'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  129. $targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  130. $targetcontent=preg_replace('/Skeleton_class/', $classname, $targetcontent);
  131. // Substitute comments
  132. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  133. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  134. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  135. // Substitute table name
  136. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenollx, $targetcontent);
  137. // Substitute declaration parameters
  138. $varprop="\n";
  139. $cleanparam='';
  140. foreach($property as $key => $prop)
  141. {
  142. if ($prop['field'] != 'rowid')
  143. {
  144. $varprop.="\tvar \$".$prop['field'];
  145. if ($prop['istime']) $varprop.="=''";
  146. $varprop.=";";
  147. if ($prop['comment']) $varprop.="\t// ".$prop['extra'];
  148. $varprop.="\n";
  149. }
  150. }
  151. $targetcontent=preg_replace('/var \$prop1;/', $varprop, $targetcontent);
  152. $targetcontent=preg_replace('/var \$prop2;/', '', $targetcontent);
  153. // Substitute clean parameters
  154. $varprop="\n";
  155. $cleanparam='';
  156. foreach($property as $key => $prop)
  157. {
  158. if ($prop['field'] != 'rowid' && ! $prop['istime'])
  159. {
  160. $varprop.="\t\tif (isset(\$this->".$prop['field'].")) \$this->".$prop['field']."=trim(\$this->".$prop['field'].");";
  161. $varprop.="\n";
  162. }
  163. }
  164. $targetcontent=preg_replace('/if \(isset\(\$this->prop1\)\) \$this->prop1=trim\(\$this->prop1\);/', $varprop, $targetcontent);
  165. $targetcontent=preg_replace('/if \(isset\(\$this->prop2\)\) \$this->prop2=trim\(\$this->prop2\);/', '', $targetcontent);
  166. // Substitute insert into parameters
  167. $varprop="\n";
  168. $cleanparam='';
  169. $i=0;
  170. foreach($property as $key => $prop)
  171. {
  172. $i++;
  173. $addfield=1;
  174. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  175. if ($prop['extra'] == 'auto_increment') $addfield=0;
  176. if ($addfield)
  177. {
  178. $varprop.="\t\t\$sql.= \"".$prop['field'];
  179. if ($i < sizeof($property)) $varprop.=",";
  180. $varprop.="\";";
  181. $varprop.="\n";
  182. }
  183. }
  184. $targetcontent=preg_replace('/\$sql\.= " field1,";/', $varprop, $targetcontent);
  185. $targetcontent=preg_replace('/\$sql\.= " field2";/', '', $targetcontent);
  186. // Substitute insert values parameters
  187. $varprop="\n";
  188. $cleanparam='';
  189. $i=0;
  190. foreach($property as $key => $prop)
  191. {
  192. $i++;
  193. $addfield=1;
  194. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  195. if ($prop['extra'] == 'auto_increment') $addfield=0;
  196. if ($addfield)
  197. {
  198. $varprop.="\t\t\$sql.= \" ";
  199. if ($prop['istime'])
  200. {
  201. $varprop.='".(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':$this->db->idate(';
  202. $varprop.="\$this->".$prop['field']."";
  203. $varprop.='))."';
  204. if ($i < sizeof($property)) $varprop.=",";
  205. $varprop.="\";";
  206. }
  207. elseif ($prop['ischar'])
  208. {
  209. $varprop.='".(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
  210. $varprop.='$this->db->escape($this->'.$prop['field'].')';
  211. $varprop.='."\'")."';
  212. if ($i < sizeof($property)) $varprop.=",";
  213. $varprop.='";';
  214. }
  215. else
  216. {
  217. $varprop.='".(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
  218. $varprop.="\$this->".$prop['field']."";
  219. $varprop.='."\'")."';
  220. if ($i < sizeof($property)) $varprop.=",";
  221. $varprop.='";';
  222. }
  223. $varprop.="\n";
  224. }
  225. }
  226. $targetcontent=preg_replace('/\$sql\.= " \'".\$this->prop1\."\',";/', $varprop, $targetcontent);
  227. $targetcontent=preg_replace('/\$sql\.= " \'".\$this->prop2\."\'";/', '', $targetcontent);
  228. // Substitute update values parameters
  229. $varprop="\n";
  230. $cleanparam='';
  231. $i=0;
  232. foreach($property as $key => $prop)
  233. {
  234. $i++;
  235. if ($prop['field'] != 'rowid')
  236. {
  237. $varprop.="\t\t\$sql.= \" ";
  238. $varprop.=$prop['field'].'=';
  239. if ($prop['istime'])
  240. {
  241. // (dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null')
  242. $varprop.='".(dol_strlen($this->'.$prop['field'].')!=0 ? "\'".$this->db->idate(';
  243. $varprop.='$this->'.$prop['field'];
  244. $varprop.=')."\'" : \'null\').';
  245. $varprop.='"';
  246. }
  247. else
  248. {
  249. $varprop.="\".";
  250. // $sql.= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
  251. if ($prop['ischar']) $varprop.='(isset($this->'.$prop['field'].')?"\'".$this->db->escape($this->'.$prop['field'].')."\'":"null")';
  252. // $sql.= " field1=".(isset($this->field1)?$this->field1:"null").",";
  253. else $varprop.='(isset($this->'.$prop['field'].')?$this->'.$prop['field'].':"null")';
  254. $varprop.=".\"";
  255. }
  256. if ($i < sizeof($property)) $varprop.=',';
  257. $varprop.='";';
  258. $varprop.="\n";
  259. }
  260. }
  261. $targetcontent=preg_replace('/\$sql.= " field1=".\(isset\(\$this->field1\)\?"\'".\$this->db->escape\(\$this->field1\)."\'":"null"\).",";/', $varprop, $targetcontent);
  262. $targetcontent=preg_replace('/\$sql.= " field2=".\(isset\(\$this->field2\)\?"\'".\$this->db->escape\(\$this->field2\)."\'":"null"\)."";/', '', $targetcontent);
  263. // Substitute select parameters
  264. $varprop="\n";
  265. $cleanparam='';
  266. $i=0;
  267. foreach($property as $key => $prop)
  268. {
  269. $i++;
  270. if ($prop['field'] != 'rowid')
  271. {
  272. $varprop.="\t\t\$sql.= \" ";
  273. $varprop.="t.".$prop['field'];
  274. if ($i < sizeof($property)) $varprop.=",";
  275. $varprop.="\";";
  276. $varprop.="\n";
  277. }
  278. }
  279. $targetcontent=preg_replace('/\$sql\.= " t\.field1,";/', $varprop, $targetcontent);
  280. $targetcontent=preg_replace('/\$sql\.= " t\.field2";/', '', $targetcontent);
  281. // Substitute select set parameters
  282. $varprop="\n";
  283. $cleanparam='';
  284. $i=0;
  285. foreach($property as $key => $prop)
  286. {
  287. $i++;
  288. if ($prop['field'] != 'rowid')
  289. {
  290. $varprop.="\t\t\t\t\$this->".$prop['field']." = ";
  291. if ($prop['istime']) $varprop.='$this->db->jdate(';
  292. $varprop.='$obj->'.$prop['field'];
  293. if ($prop['istime']) $varprop.=')';
  294. $varprop.=";";
  295. $varprop.="\n";
  296. }
  297. }
  298. $targetcontent=preg_replace('/\$this->prop1 = \$obj->field1;/', $varprop, $targetcontent);
  299. $targetcontent=preg_replace('/\$this->prop2 = \$obj->field2;/', '', $targetcontent);
  300. // Substitute initasspecimen parameters
  301. $varprop="\n";
  302. $cleanparam='';
  303. foreach($property as $key => $prop)
  304. {
  305. if ($prop['field'] != 'rowid')
  306. {
  307. $varprop.="\t\t\$this->".$prop['field']."='';";
  308. $varprop.="\n";
  309. }
  310. }
  311. $targetcontent=preg_replace('/\$this->prop1=\'prop1\';/', $varprop, $targetcontent);
  312. $targetcontent=preg_replace('/\$this->prop2=\'prop2\';/', '', $targetcontent);
  313. // Build file
  314. $fp=fopen($outfile,"w");
  315. if ($fp)
  316. {
  317. fputs($fp, $targetcontent);
  318. fclose($fp);
  319. print "\n";
  320. print "File '".$outfile."' has been built in current directory.\n";
  321. }
  322. else $error++;
  323. //--------------------------------
  324. // Build skeleton_script.php
  325. //--------------------------------
  326. // Read skeleton_script.php file
  327. $skeletonfile='skeleton_script.php';
  328. $sourcecontent=file_get_contents($skeletonfile);
  329. if (! $sourcecontent)
  330. {
  331. print "\n";
  332. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  333. print "Try to run script from skeletons directory.\n";
  334. exit;
  335. }
  336. // Define output variables
  337. $outfile='out.'.$classmin.'_script.php';
  338. $targetcontent=$sourcecontent;
  339. // Substitute class name
  340. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  341. $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
  342. $targetcontent=preg_replace('/\$element=\'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  343. $targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  344. $targetcontent=preg_replace('/Skeleton_class/', $classname, $targetcontent);
  345. // Substitute comments
  346. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  347. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  348. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  349. // Substitute table name
  350. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenollx, $targetcontent);
  351. // Build file
  352. $fp=fopen($outfile,"w");
  353. if ($fp)
  354. {
  355. fputs($fp, $targetcontent);
  356. fclose($fp);
  357. print "File '".$outfile."' has been built in current directory.\n";
  358. }
  359. else $error++;
  360. // -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
  361. print "You must rename files by removing the 'out.' prefix in their name.\n";
  362. return $error;
  363. ?>