PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/skeletons/build_class_from_table.php

http://github.com/Dolibarr/dolibarr
PHP | 463 lines | 382 code | 33 blank | 48 comment | 47 complexity | 77b71a423526d089fb5b53c6e3736ebc MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  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='3.2';
  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 or mysqli driver only\n";
  50. exit;
  51. }
  52. // Show parameters
  53. print 'Tablename='.$argv[1]."\n";
  54. print "Current dir is ".getcwd()."\n";
  55. // Define array with list of properties
  56. $property=array();
  57. $table=$argv[1];
  58. $foundprimary=0;
  59. $resql=$db->DDLDescTable($table);
  60. if ($resql)
  61. {
  62. $i=0;
  63. while($obj=$db->fetch_object($resql))
  64. {
  65. //var_dump($obj);
  66. $i++;
  67. $property[$i]['field']=$obj->Field;
  68. if ($obj->Key == 'PRI')
  69. {
  70. $property[$i]['primary']=1;
  71. $foundprimary=1;
  72. }
  73. else
  74. {
  75. $property[$i]['primary']=1;
  76. }
  77. $property[$i]['type'] =$obj->Type;
  78. $property[$i]['null'] =$obj->Null;
  79. $property[$i]['extra']=$obj->Extra;
  80. if ($property[$i]['type'] == 'date'
  81. || $property[$i]['type'] == 'datetime'
  82. || $property[$i]['type'] == 'timestamp')
  83. {
  84. $property[$i]['istime']=true;
  85. }
  86. else
  87. {
  88. $property[$i]['istime']=false;
  89. }
  90. if (preg_match('/varchar/i',$property[$i]['type'])
  91. || preg_match('/text/i',$property[$i]['type']))
  92. {
  93. $property[$i]['ischar']=true;
  94. }
  95. else
  96. {
  97. $property[$i]['ischar']=false;
  98. }
  99. }
  100. }
  101. else
  102. {
  103. print "Error: Failed to get description for table '".$table."'.\n";
  104. return false;
  105. }
  106. //var_dump($property);
  107. // Define substitute select parameters
  108. $varpropselect="\n";
  109. $cleanparam='';
  110. $i=0;
  111. foreach($property as $key => $prop)
  112. {
  113. $i++;
  114. if ($prop['field'] != 'rowid')
  115. {
  116. $varpropselect.="\t\t\$sql.= \" ";
  117. $varpropselect.="t.".$prop['field'];
  118. if ($i < count($property)) $varpropselect.=",";
  119. $varpropselect.="\";";
  120. $varpropselect.="\n";
  121. }
  122. }
  123. //--------------------------------
  124. // Build skeleton_class.class.php
  125. //--------------------------------
  126. // Define working variables
  127. $table=strtolower($table);
  128. $tablenoprefix=preg_replace('/'.preg_quote(MAIN_DB_PREFIX).'/i','',$table);
  129. $classname=preg_replace('/_/','',ucfirst($tablenoprefix));
  130. $classmin=preg_replace('/_/','',strtolower($classname));
  131. // Read skeleton_class.class.php file
  132. $skeletonfile=$path.'skeleton_class.class.php';
  133. $sourcecontent=file_get_contents($skeletonfile);
  134. if (! $sourcecontent)
  135. {
  136. print "\n";
  137. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  138. print "Try to run script from skeletons directory.\n";
  139. exit;
  140. }
  141. // Define output variables
  142. $outfile='out.'.$classmin.'.class.php';
  143. $targetcontent=$sourcecontent;
  144. // Substitute class name
  145. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  146. $targetcontent=preg_replace('/\$element=\'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  147. $targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  148. $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
  149. // Substitute comments
  150. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  151. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  152. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  153. // Substitute table name
  154. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
  155. // Substitute declaration parameters
  156. $varprop="\n";
  157. $cleanparam='';
  158. foreach($property as $key => $prop)
  159. {
  160. if ($prop['field'] != 'rowid')
  161. {
  162. $varprop.="\tvar \$".$prop['field'];
  163. if ($prop['istime']) $varprop.="=''";
  164. $varprop.=";";
  165. if ($prop['comment']) $varprop.="\t// ".$prop['extra'];
  166. $varprop.="\n";
  167. }
  168. }
  169. $targetcontent=preg_replace('/var \$prop1;/', $varprop, $targetcontent);
  170. $targetcontent=preg_replace('/var \$prop2;/', '', $targetcontent);
  171. // Substitute clean parameters
  172. $varprop="\n";
  173. $cleanparam='';
  174. foreach($property as $key => $prop)
  175. {
  176. if ($prop['field'] != 'rowid' && ! $prop['istime'])
  177. {
  178. $varprop.="\t\tif (isset(\$this->".$prop['field'].")) \$this->".$prop['field']."=trim(\$this->".$prop['field'].");";
  179. $varprop.="\n";
  180. }
  181. }
  182. $targetcontent=preg_replace('/if \(isset\(\$this->prop1\)\) \$this->prop1=trim\(\$this->prop1\);/', $varprop, $targetcontent);
  183. $targetcontent=preg_replace('/if \(isset\(\$this->prop2\)\) \$this->prop2=trim\(\$this->prop2\);/', '', $targetcontent);
  184. // Substitute insert into parameters
  185. $varprop="\n";
  186. $cleanparam='';
  187. $i=0;
  188. foreach($property as $key => $prop)
  189. {
  190. $i++;
  191. $addfield=1;
  192. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  193. if ($prop['extra'] == 'auto_increment') $addfield=0;
  194. if ($addfield)
  195. {
  196. $varprop.="\t\t\$sql.= \"".$prop['field'];
  197. if ($i < count($property)) $varprop.=",";
  198. $varprop.="\";";
  199. $varprop.="\n";
  200. }
  201. }
  202. $targetcontent=preg_replace('/\$sql\.= " field1,";/', $varprop, $targetcontent);
  203. $targetcontent=preg_replace('/\$sql\.= " field2";/', '', $targetcontent);
  204. // Substitute insert values parameters
  205. $varprop="\n";
  206. $cleanparam='';
  207. $i=0;
  208. foreach($property as $key => $prop)
  209. {
  210. $i++;
  211. $addfield=1;
  212. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  213. if ($prop['extra'] == 'auto_increment') $addfield=0;
  214. if ($addfield)
  215. {
  216. $varprop.="\t\t\$sql.= \" ";
  217. if ($prop['istime'])
  218. {
  219. $varprop.='".(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':$this->db->idate(';
  220. $varprop.="\$this->".$prop['field']."";
  221. $varprop.='))."';
  222. if ($i < count($property)) $varprop.=",";
  223. $varprop.="\";";
  224. }
  225. elseif ($prop['ischar'])
  226. {
  227. $varprop.='".(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
  228. $varprop.='$this->db->escape($this->'.$prop['field'].')';
  229. $varprop.='."\'")."';
  230. if ($i < count($property)) $varprop.=",";
  231. $varprop.='";';
  232. }
  233. else
  234. {
  235. $varprop.='".(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
  236. $varprop.="\$this->".$prop['field']."";
  237. $varprop.='."\'")."';
  238. if ($i < count($property)) $varprop.=",";
  239. $varprop.='";';
  240. }
  241. $varprop.="\n";
  242. }
  243. }
  244. $targetcontent=preg_replace('/\$sql\.= " \'".\$this->prop1\."\',";/', $varprop, $targetcontent);
  245. $targetcontent=preg_replace('/\$sql\.= " \'".\$this->prop2\."\'";/', '', $targetcontent);
  246. // Substitute update values parameters
  247. $varprop="\n";
  248. $cleanparam='';
  249. $i=0;
  250. foreach($property as $key => $prop)
  251. {
  252. $i++;
  253. if ($prop['field'] != 'rowid')
  254. {
  255. $varprop.="\t\t\$sql.= \" ";
  256. $varprop.=$prop['field'].'=';
  257. if ($prop['istime'])
  258. {
  259. // (dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null')
  260. $varprop.='".(dol_strlen($this->'.$prop['field'].')!=0 ? "\'".$this->db->idate(';
  261. $varprop.='$this->'.$prop['field'];
  262. $varprop.=')."\'" : \'null\').';
  263. $varprop.='"';
  264. }
  265. else
  266. {
  267. $varprop.="\".";
  268. // $sql.= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
  269. if ($prop['ischar']) $varprop.='(isset($this->'.$prop['field'].')?"\'".$this->db->escape($this->'.$prop['field'].')."\'":"null")';
  270. // $sql.= " field1=".(isset($this->field1)?$this->field1:"null").",";
  271. else $varprop.='(isset($this->'.$prop['field'].')?$this->'.$prop['field'].':"null")';
  272. $varprop.=".\"";
  273. }
  274. if ($i < count($property)) $varprop.=',';
  275. $varprop.='";';
  276. $varprop.="\n";
  277. }
  278. }
  279. $targetcontent=preg_replace('/\$sql.= " field1=".\(isset\(\$this->field1\)\?"\'".\$this->db->escape\(\$this->field1\)."\'":"null"\).",";/', $varprop, $targetcontent);
  280. $targetcontent=preg_replace('/\$sql.= " field2=".\(isset\(\$this->field2\)\?"\'".\$this->db->escape\(\$this->field2\)."\'":"null"\)."";/', '', $targetcontent);
  281. // Substitute select parameters
  282. $targetcontent=preg_replace('/\$sql\.= " t\.field1,";/', $varpropselect, $targetcontent);
  283. $targetcontent=preg_replace('/\$sql\.= " t\.field2";/', '', $targetcontent);
  284. // Substitute select set parameters
  285. $varprop="\n";
  286. $cleanparam='';
  287. $i=0;
  288. foreach($property as $key => $prop)
  289. {
  290. $i++;
  291. if ($prop['field'] != 'rowid')
  292. {
  293. $varprop.="\t\t\t\t\$this->".$prop['field']." = ";
  294. if ($prop['istime']) $varprop.='$this->db->jdate(';
  295. $varprop.='$obj->'.$prop['field'];
  296. if ($prop['istime']) $varprop.=')';
  297. $varprop.=";";
  298. $varprop.="\n";
  299. }
  300. }
  301. $targetcontent=preg_replace('/\$this->prop1 = \$obj->field1;/', $varprop, $targetcontent);
  302. $targetcontent=preg_replace('/\$this->prop2 = \$obj->field2;/', '', $targetcontent);
  303. // Substitute initasspecimen parameters
  304. $varprop="\n";
  305. $cleanparam='';
  306. foreach($property as $key => $prop)
  307. {
  308. if ($prop['field'] != 'rowid')
  309. {
  310. $varprop.="\t\t\$this->".$prop['field']."='';";
  311. $varprop.="\n";
  312. }
  313. }
  314. $targetcontent=preg_replace('/\$this->prop1=\'prop1\';/', $varprop, $targetcontent);
  315. $targetcontent=preg_replace('/\$this->prop2=\'prop2\';/', '', $targetcontent);
  316. // Build file
  317. $fp=fopen($outfile,"w");
  318. if ($fp)
  319. {
  320. fputs($fp, $targetcontent);
  321. fclose($fp);
  322. print "\n";
  323. print "File '".$outfile."' has been built in current directory.\n";
  324. }
  325. else $error++;
  326. //--------------------------------
  327. // Build skeleton_script.php
  328. //--------------------------------
  329. // Read skeleton_script.php file
  330. $skeletonfile=$path.'skeleton_script.php';
  331. $sourcecontent=file_get_contents($skeletonfile);
  332. if (! $sourcecontent)
  333. {
  334. print "\n";
  335. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  336. print "Try to run script from skeletons directory.\n";
  337. exit;
  338. }
  339. // Define output variables
  340. $outfile='out.'.$classmin.'_script.php';
  341. $targetcontent=$sourcecontent;
  342. // Substitute class name
  343. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  344. $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
  345. $targetcontent=preg_replace('/\$element=\'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  346. $targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  347. $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
  348. // Substitute comments
  349. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  350. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  351. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  352. // Substitute table name
  353. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
  354. // Build file
  355. $fp=fopen($outfile,"w");
  356. if ($fp)
  357. {
  358. fputs($fp, $targetcontent);
  359. fclose($fp);
  360. print "File '".$outfile."' has been built in current directory.\n";
  361. }
  362. else $error++;
  363. //--------------------------------
  364. // Build skeleton_page.php
  365. //--------------------------------
  366. // Read skeleton_page.php file
  367. $skeletonfile=$path.'skeleton_page.php';
  368. $sourcecontent=file_get_contents($skeletonfile);
  369. if (! $sourcecontent)
  370. {
  371. print "\n";
  372. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  373. print "Try to run script from skeletons directory.\n";
  374. exit;
  375. }
  376. // Define output variables
  377. $outfile='out.'.$classmin.'_page.php';
  378. $targetcontent=$sourcecontent;
  379. // Substitute class name
  380. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  381. $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
  382. $targetcontent=preg_replace('/\$element=\'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  383. $targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  384. $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
  385. $targetcontent=preg_replace('/skeleton/', $classname, $targetcontent);
  386. // Substitute comments
  387. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  388. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  389. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  390. // Substitute table name
  391. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
  392. // Build file
  393. $fp=fopen($outfile,"w");
  394. if ($fp)
  395. {
  396. fputs($fp, $targetcontent);
  397. fclose($fp);
  398. print "File '".$outfile."' has been built in current directory.\n";
  399. }
  400. else $error++;
  401. // -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
  402. 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";
  403. return $error;
  404. ?>