build_class_from_table.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2008-2014 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 3 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]) || ! isset($argv[2]) || (isset($argv[3]) && ! isset($argv[7])))
  43. {
  44. print "Usage: $script_file tablename modulename [server port databasename user pass]\n";
  45. exit;
  46. }
  47. if (isset($argv[3]) && isset($argv[4]) && isset($argv[5]) && isset($argv[6]) && isset($argv[7]))
  48. {
  49. print 'Use specific database ids'."\n";
  50. $db=getDoliDBInstance('mysqli',$argv[3],$argv[6],$argv[7],$argv[5],$argv[4]);
  51. }
  52. if ($db->type != 'mysql' && $db->type != 'mysqli')
  53. {
  54. print "Error: This script works with mysql or mysqli driver only\n";
  55. exit;
  56. }
  57. $table=$argv[1];
  58. $module=$argv[2];
  59. // Show parameters
  60. print 'Tablename: '.$table."\n";
  61. print 'Modulename: '.$module."\n";
  62. print "Current dir: ".getcwd()."\n";
  63. print "Database name: ".$db->database_name."\n";
  64. // Define array with list of properties
  65. $property=array();
  66. $foundprimary=0;
  67. $resql=$db->DDLDescTable($table);
  68. if ($resql)
  69. {
  70. $i=0;
  71. while($obj=$db->fetch_object($resql))
  72. {
  73. //var_dump($obj);
  74. $i++;
  75. $property[$i]['field']=$obj->Field;
  76. if ($obj->Key == 'PRI')
  77. {
  78. $property[$i]['primary']=1;
  79. $foundprimary=1;
  80. }
  81. else
  82. {
  83. $property[$i]['primary']=1;
  84. }
  85. $property[$i]['type'] =$obj->Type;
  86. $property[$i]['null'] =$obj->Null;
  87. $property[$i]['extra']=$obj->Extra;
  88. if ($property[$i]['type'] == 'date'
  89. || $property[$i]['type'] == 'datetime'
  90. || $property[$i]['type'] == 'timestamp')
  91. {
  92. $property[$i]['istime']=true;
  93. }
  94. else
  95. {
  96. $property[$i]['istime']=false;
  97. }
  98. if (preg_match('/varchar/i',$property[$i]['type'])
  99. || preg_match('/text/i',$property[$i]['type']))
  100. {
  101. $property[$i]['ischar']=true;
  102. }
  103. else
  104. {
  105. $property[$i]['ischar']=false;
  106. }
  107. if (preg_match('/int/i',$property[$i]['type']))
  108. {
  109. $property[$i]['isint']=true;
  110. }
  111. else
  112. {
  113. $property[$i]['isint']=false;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. print "Error: Failed to get description for table '".$table."'.\n";
  120. return false;
  121. }
  122. //var_dump($property);
  123. // Define substitute fetch/select parameters
  124. $varpropselect="\n";
  125. $cleanparam='';
  126. $i=0;
  127. foreach($property as $key => $prop)
  128. {
  129. $i++;
  130. if ($prop['field'] != 'rowid')
  131. {
  132. $varpropselect.="\t\t\$sql .= \" ";
  133. $varpropselect.="t.".$prop['field'];
  134. if ($i < count($property)) $varpropselect.=",";
  135. $varpropselect.="\";";
  136. $varpropselect.="\n";
  137. }
  138. }
  139. //--------------------------------
  140. // Build skeleton_class.class.php
  141. //--------------------------------
  142. // Define working variables
  143. $table=strtolower($table);
  144. $tablenoprefix=preg_replace('/'.preg_quote(MAIN_DB_PREFIX,'/').'/i','',$table);
  145. $classname=preg_replace('/_/','',ucfirst($tablenoprefix));
  146. $classmin=preg_replace('/_/','',strtolower($classname));
  147. // Read skeleton_class.class.php file
  148. $skeletonfile=$path.'skeleton_class.class.php';
  149. $sourcecontent=file_get_contents($skeletonfile);
  150. if (! $sourcecontent)
  151. {
  152. print "\n";
  153. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  154. print "Try to run script from skeletons directory.\n";
  155. exit;
  156. }
  157. // Define output variables
  158. $outfile=$classmin.'.class.php';
  159. $targetcontent=$sourcecontent;
  160. // Substitute module name
  161. $targetcontent=preg_replace('/dev\/skeletons/', $module, $targetcontent);
  162. $targetcontent=preg_replace('/mymodule othermodule1 othermodule2/', $module, $targetcontent);
  163. $targetcontent=preg_replace('/mymodule/', $module, $targetcontent);
  164. // Substitute class name
  165. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  166. $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element = \''.$classmin.'\'', $targetcontent);
  167. $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element = \''.$tablenoprefix.'\'', $targetcontent);
  168. $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
  169. $targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent);
  170. $targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
  171. // Substitute comments
  172. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  173. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  174. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  175. // Substitute table name
  176. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
  177. // Substitute declaration parameters
  178. $varprop="\n";
  179. $cleanparam='';
  180. foreach($property as $key => $prop)
  181. {
  182. if ($prop['field'] != 'rowid' && $prop['field'] != 'id')
  183. {
  184. $varprop.="\tpublic \$".$prop['field'];
  185. if ($prop['istime']) $varprop.=" = ''";
  186. $varprop.=";";
  187. if ($prop['comment']) $varprop.="\t// ".$prop['extra'];
  188. $varprop.="\n";
  189. }
  190. }
  191. $targetcontent=preg_replace('/'.preg_quote('public $prop1;','/').'/', $varprop, $targetcontent);
  192. $targetcontent=preg_replace('/'.preg_quote('public $prop2;','/').'/', '', $targetcontent);
  193. $targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 1((\s|\n|\r|\t)*)/', '', $targetcontent);
  194. $targetcontent=preg_replace('/\*((\s|\n|\r|\t)*)\@var mixed Sample property 2((\s|\n|\r|\t)*)/', '', $targetcontent);
  195. // Substitute clean parameters
  196. $varprop="\n";
  197. $cleanparam='';
  198. foreach($property as $key => $prop)
  199. {
  200. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  201. {
  202. $varprop.="\t\tif (isset(\$this->".$prop['field'].")) {\n\t\t\t \$this->".$prop['field']." = trim(\$this->".$prop['field'].");\n\t\t}";
  203. $varprop.="\n";
  204. }
  205. }
  206. $targetcontent=preg_replace('/if \(isset\(\$this->prop1\)\) {((\n|\r|\t)*)\$this->prop1 = trim\(\$this->prop1\);((\n|\r|\t)*)}/', $varprop, $targetcontent);
  207. $targetcontent=preg_replace('/if \(isset\(\$this->prop2\)\) {((\n|\r|\t)*)\$this->prop2 = trim\(\$this->prop2\);((\n|\r|\t)*)}/', '', $targetcontent);
  208. $no_output_field=0;
  209. foreach($property as $key => $prop)
  210. {
  211. if ($prop['field'] == 'tms') $no_output_field++; // This is a field of type timestamp edited automatically
  212. if ($prop['extra'] == 'auto_increment') $no_output_field++;
  213. }
  214. // Substitute insert into parameters
  215. $varprop="\n";
  216. $cleanparam='';
  217. $i=0;
  218. foreach($property as $key => $prop)
  219. {
  220. $addfield=1;
  221. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  222. if ($prop['extra'] == 'auto_increment') $addfield=0;
  223. if ($addfield)
  224. {
  225. $varprop.="\t\t\$sql.= '".$prop['field'];
  226. if ($i < (count($property)-$no_output_field)) $varprop.=",";
  227. $varprop.="';";
  228. $varprop.="\n";
  229. }
  230. $i++;
  231. }
  232. $targetcontent=preg_replace('/\$sql \.= \' field1,\';/', $varprop, $targetcontent);
  233. $targetcontent=preg_replace('/\$sql \.= \' field2\';/', '', $targetcontent);
  234. // Substitute insert values parameters
  235. $varprop="\n";
  236. $cleanparam='';
  237. $i=0;
  238. //Count nb field to output to manage commat at end SQL instruction
  239. foreach($property as $key => $prop)
  240. {
  241. $addfield=1;
  242. if ($prop['field'] == 'tms') $addfield=0; // This is a field of type timestamp edited automatically
  243. if ($prop['extra'] == 'auto_increment') $addfield=0;
  244. if ($addfield)
  245. {
  246. $i++;
  247. $varprop.="\t\t\$sql .= ' ";
  248. if ($prop['field']=='datec')
  249. {
  250. $varprop.='\'."\'".$this->db->idate(dol_now())."\'"';
  251. }
  252. elseif ($prop['istime'])
  253. {
  254. $varprop.='\'.(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].')==0?\'NULL\':"\'".$this->db->idate(';
  255. $varprop.="\$this->".$prop['field'];
  256. $varprop.=").\"'\")";
  257. }
  258. elseif ($prop['ischar'])
  259. {
  260. $varprop.="'.(! isset(\$this->".$prop['field'].")?'NULL':\"'\".";
  261. $varprop.="\$this->db->escape(\$this->".$prop['field'].")";
  262. $varprop.=".\"'\")";
  263. }
  264. elseif ($prop['field']=='fk_user_mod' || $prop['field']=='fk_user_author')
  265. {
  266. $varprop.="'.\$user->id";
  267. }
  268. elseif ($prop['isint'])
  269. {
  270. $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':';
  271. $varprop.="\$this->".$prop['field'];
  272. $varprop.=')';
  273. }
  274. else
  275. {
  276. $varprop.='\'.(! isset($this->'.$prop['field'].')?\'NULL\':"\'".';
  277. $varprop.="\$this->".$prop['field'];
  278. $varprop.='."\'")';
  279. }
  280. if ($i < (count($property)-$no_output_field)) $varprop.=".','";
  281. $varprop.=';';
  282. $varprop.="\n";
  283. }
  284. }
  285. $patern1='/\$sql \.= \' (.*)\' \. \$this->prop1 \. \'(.*),\';/';
  286. $patern2='/\$sql \.= \' (.*)\' \. \$this->prop2 \. \'(.*)\';/';
  287. $targetcontent=preg_replace($patern1, $varprop, $targetcontent);
  288. $targetcontent=preg_replace($patern2, '', $targetcontent);
  289. // Substitute update values parameters
  290. //Count nb field to output to manage commat at end SQL instruction
  291. $no_output_field=0;
  292. foreach($property as $key => $prop)
  293. {
  294. if ($prop['extra'] == 'auto_increment') $no_output_field++;
  295. }
  296. $varprop="\n";
  297. $cleanparam='';
  298. $i=0;
  299. foreach($property as $key => $prop)
  300. {
  301. $addfield=1;
  302. if ($prop['extra'] == 'auto_increment') $addfield=0;
  303. if ($addfield)
  304. {
  305. $i++;
  306. $varprop.="\t\t\$sql .= ' ";
  307. $varprop.=$prop['field'].' = ';
  308. if ($prop['field']=='tms') {
  309. $varprop.='\'.(dol_strlen($this->'.$prop['field'].') != 0 ? "\'".$this->db->idate(';
  310. $varprop.='$this->'.$prop['field'];
  311. $varprop.=')."\'" : "\'".$this->db->idate(dol_now())."\'")';
  312. }
  313. elseif ($prop['istime'])
  314. {
  315. $varprop.='\'.(! isset($this->'.$prop['field'].') || dol_strlen($this->'.$prop['field'].') != 0 ? "\'".$this->db->idate(';
  316. $varprop.='$this->'.$prop['field'];
  317. $varprop.=')."\'" : \'null\')';
  318. }
  319. elseif ($prop['field']=='fk_user_mod') {
  320. $varprop.="'.\$user->id";
  321. }
  322. else
  323. {
  324. $varprop.="'.";
  325. if ($prop['ischar']) $varprop.='(isset($this->'.$prop['field'].')?"\'".$this->db->escape($this->'.$prop['field'].')."\'":"null")';
  326. elseif ($prop['isint']) $varprop.='(isset($this->'.$prop['field'].')?$this->'.$prop['field'].':"null")';
  327. else $varprop.='(isset($this->'.$prop['field'].')?$this->'.$prop['field'].':"null")';
  328. }
  329. if ($i < (count($property)-$no_output_field)) $varprop.=".','";
  330. $varprop.=';';
  331. $varprop.="\n";
  332. }
  333. }
  334. $targetcontent=preg_replace('/\$sql \.= " field1=".\(isset\(\$this->field1\)\?"\'".\$this->db->escape\(\$this->field1\)."\'":"null"\).",";/', $varprop, $targetcontent);
  335. $targetcontent=preg_replace('/\$sql \.= " field2=".\(isset\(\$this->field2\)\?"\'".\$this->db->escape\(\$this->field2\)."\'":"null"\)."";/', '', $targetcontent);
  336. // Substitute fetch/select parameters
  337. $targetcontent=preg_replace('/\$sql \.= \' t\.field1,\';/', $varpropselect, $targetcontent);
  338. $targetcontent=preg_replace('/\$sql \.= \' t\.field2\';/', '', $targetcontent);
  339. // Substitute select set parameters
  340. $varprop="\n";
  341. $varpropline="\n";
  342. $cleanparam='';
  343. $i=0;
  344. foreach($property as $key => $prop)
  345. {
  346. $i++;
  347. if ($prop['field'] != 'rowid' && $prop['field'] != 'id')
  348. {
  349. $varprop.="\t\t\t\t\$this->".$prop['field']." = ";
  350. if ($prop['istime']) $varprop.='$this->db->jdate(';
  351. $varprop.='$obj->'.$prop['field'];
  352. if ($prop['istime']) $varprop.=')';
  353. $varprop.=";";
  354. $varprop.="\n";
  355. $varpropline.="\t\t\t\t\$line->".$prop['field']." = ";
  356. if ($prop['istime']) $varpropline.='$this->db->jdate(';
  357. $varpropline.='$obj->'.$prop['field'];
  358. if ($prop['istime']) $varpropline.=')';
  359. $varpropline.=";";
  360. $varpropline.="\n";
  361. }
  362. }
  363. $targetcontent=preg_replace('/\$this->prop1 = \$obj->field1;/', $varprop, $targetcontent);
  364. $targetcontent=preg_replace('/\$this->prop2 = \$obj->field2;/', '', $targetcontent);
  365. //Substirute fetchAll
  366. $targetcontent=preg_replace('/\$line->prop1 = \$obj->field1;/', $varpropline, $targetcontent);
  367. $targetcontent=preg_replace('/\$line->prop2 = \$obj->field2;/', '', $targetcontent);
  368. // Substitute initasspecimen parameters
  369. $varprop="\n";
  370. $cleanparam='';
  371. foreach($property as $key => $prop)
  372. {
  373. if ($prop['field'] != 'rowid' && $prop['field'] != 'id')
  374. {
  375. $varprop.="\t\t\$this->".$prop['field']." = '';";
  376. $varprop.="\n";
  377. }
  378. }
  379. $targetcontent=preg_replace('/\$this->prop1 = \'prop1\';/', $varprop, $targetcontent);
  380. $targetcontent=preg_replace('/\$this->prop2 = \'prop2\';/', '', $targetcontent);
  381. // Build file
  382. $fp=fopen($outfile,"w");
  383. if ($fp)
  384. {
  385. fputs($fp, $targetcontent);
  386. fclose($fp);
  387. print "\n";
  388. print "File '".$outfile."' has been built in current directory.\n";
  389. }
  390. else $error++;
  391. //--------------------------------------------------------------------
  392. // Build skeleton_script.php, skeleton_list.php and skeleton_card.php
  393. //--------------------------------------------------------------------
  394. $skeletonfiles=array(
  395. $path.'skeleton_script.php' => $classmin.'_script.php',
  396. $path.'skeleton_list.php' => $classmin.'_list.php',
  397. $path.'skeleton_card.php' => $classmin.'_card.php'
  398. );
  399. foreach ($skeletonfiles as $skeletonfile => $outfile)
  400. {
  401. $sourcecontent=file_get_contents($skeletonfile);
  402. if (! $sourcecontent)
  403. {
  404. print "\n";
  405. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  406. print "Try to run script from skeletons directory.\n";
  407. exit;
  408. }
  409. // Define output variables
  410. $targetcontent=$sourcecontent;
  411. // Substitute module name
  412. $targetcontent=preg_replace('/dev\/skeletons/', $module, $targetcontent);
  413. $targetcontent=preg_replace('/mymodule othermodule1 othermodule2/', $module, $targetcontent);
  414. $targetcontent=preg_replace('/mymodule/', $module, $targetcontent);
  415. // Substitute class name
  416. $targetcontent=preg_replace('/skeleton_class\.class\.php/', $classmin.'.class.php', $targetcontent);
  417. $targetcontent=preg_replace('/skeleton_script\.php/', $classmin.'_script.php', $targetcontent);
  418. $targetcontent=preg_replace('/\$element = \'skeleton\'/', '\$element=\''.$classmin.'\'', $targetcontent);
  419. $targetcontent=preg_replace('/\$table_element = \'skeleton\'/', '\$table_element=\''.$classmin.'\'', $targetcontent);
  420. $targetcontent=preg_replace('/Skeleton_Class/', $classname, $targetcontent);
  421. $targetcontent=preg_replace('/skeletons/', $classmin, $targetcontent);
  422. $targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
  423. // Substitute comments
  424. $targetcontent=preg_replace('/This file is an example to create a new class file/', 'Put here description of this class', $targetcontent);
  425. $targetcontent=preg_replace('/\s*\/\/\.\.\./', '', $targetcontent);
  426. $targetcontent=preg_replace('/Put here some comments/','Initialy built by build_class_from_table on '.strftime('%Y-%m-%d %H:%M',mktime()), $targetcontent);
  427. // Substitute table name
  428. $targetcontent=preg_replace('/MAIN_DB_PREFIX."mytable/', 'MAIN_DB_PREFIX."'.$tablenoprefix, $targetcontent);
  429. // Substitute GETPOST search_fieldx
  430. $varprop="\n";
  431. $cleanparam='';
  432. foreach($property as $key => $prop)
  433. {
  434. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  435. {
  436. if ($prop['isint']) $varprop.='$search_'.$prop['field']."=GETPOST('search_".$prop['field']."','int');\n";
  437. else $varprop.='$search_'.$prop['field']."=GETPOST('search_".$prop['field']."','alpha');\n";
  438. }
  439. }
  440. $targetcontent=preg_replace('/'.preg_quote('$search_field1=GETPOST("search_field1");','/').'/', $varprop, $targetcontent);
  441. $targetcontent=preg_replace('/'.preg_quote('$search_field2=GETPOST("search_field2");','/').'/', '', $targetcontent);
  442. // Substitute GETPOST fieldx
  443. $varprop="\n";
  444. $cleanparam='';
  445. foreach($property as $key => $prop)
  446. {
  447. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  448. {
  449. if ($prop['isint']) $varprop.="\t\$object->".$prop['field']."=GETPOST('".$prop['field']."','int');\n";
  450. else $varprop.="\t\$object->".$prop['field']."=GETPOST('".$prop['field']."','alpha');\n";
  451. }
  452. }
  453. $targetcontent=preg_replace('/'.preg_quote('$object->prop1=GETPOST("field1");','/').'/', $varprop, $targetcontent);
  454. $targetcontent=preg_replace('/'.preg_quote('$object->prop2=GETPOST("field2");','/').'/', '', $targetcontent);
  455. // Substitute reset search_field = '';
  456. $varprop="\n";
  457. $cleanparam='';
  458. foreach($property as $key => $prop)
  459. {
  460. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  461. {
  462. $varprop.='$search_'.$prop['field']."='';\n";
  463. }
  464. }
  465. $targetcontent=preg_replace('/'.preg_quote('$search_field1=\'\';','/').'/', $varprop, $targetcontent);
  466. $targetcontent=preg_replace('/'.preg_quote('$search_field2=\'\';','/').'/', '', $targetcontent);
  467. // Substitute fetch/select parameters
  468. $targetcontent=preg_replace('/\$sql\s*\.= " t\.field1,";/', $varpropselect, $targetcontent);
  469. $targetcontent=preg_replace('/\$sql\s*\.= " t\.field2";/', '', $targetcontent);
  470. // Substitute where for search
  471. $varprop="\n";
  472. $cleanparam='';
  473. foreach($property as $key => $prop)
  474. {
  475. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  476. {
  477. $varprop.='if ($search_'.$prop['field'].') $sql.= natural_search("'.$prop['field'].'",$search_'.$prop['field'].');'."\n";
  478. }
  479. }
  480. $targetcontent=preg_replace('/'.preg_quote('if ($search_field1) $sql.= natural_search("field1",$search_field1);','/').'/', $varprop, $targetcontent);
  481. $targetcontent=preg_replace('/'.preg_quote('if ($search_field2) $sql.= natural_search("field2",$search_field2);','/').'/', '', $targetcontent);
  482. // substitute $params.=
  483. $varprop="\n";
  484. $cleanparam='';
  485. foreach($property as $key => $prop)
  486. {
  487. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  488. {
  489. $varprop.="if (\$search_".$prop['field']." != '') \$params.= '&amp;search_".$prop['field']."='.urlencode(\$search_".$prop['field'].");\n";
  490. }
  491. }
  492. $targetcontent=preg_replace('/'.preg_quote("if (\$search_field1 != '') \$params.= '&amp;search_field1='.urlencode(\$search_field1);",'/').'/', $varprop, $targetcontent);
  493. $targetcontent=preg_replace('/'.preg_quote("if (\$search_field2 != '') \$params.= '&amp;search_field2='.urlencode(\$search_field2);",'/').'/', '', $targetcontent);
  494. // Substitute arrayfields
  495. $varprop="\n";
  496. $cleanparam='';
  497. foreach($property as $key => $prop)
  498. {
  499. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  500. {
  501. $varprop.="'t.".$prop['field']."'=>array('label'=>\$langs->trans(\"Field".$prop['field']."\"), 'checked'=>1),\n";
  502. }
  503. }
  504. $targetcontent=preg_replace('/'.preg_quote("'t.field1'=>array('label'=>\$langs->trans(\"Field1\"), 'checked'=>1),",'/').'/', $varprop, $targetcontent);
  505. $targetcontent=preg_replace('/'.preg_quote("'t.field2'=>array('label'=>\$langs->trans(\"Field2\"), 'checked'=>1),",'/').'/', '', $targetcontent);
  506. // Substitute print_liste_field_titre
  507. $varprop="\n";
  508. $cleanparam='';
  509. foreach($property as $key => $prop)
  510. {
  511. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  512. {
  513. $varprop.="if (! empty(\$arrayfields['t.".$prop['field']."']['checked'])) print_liste_field_titre(\$arrayfields['t.".$prop['field']."']['label'],\$_SERVER['PHP_SELF'],'t.".$prop['field']."','',\$params,'',\$sortfield,\$sortorder);\n";
  514. }
  515. }
  516. $targetcontent=preg_replace('/LIST_OF_TD_TITLE_FIELDS/', $varprop, $targetcontent);
  517. // Substitute fields title search
  518. $varprop="\n";
  519. $cleanparam='';
  520. foreach($property as $key => $prop)
  521. {
  522. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  523. {
  524. $varprop.="if (! empty(\$arrayfields['t.".$prop['field']."']['checked'])) print '<td class=\"liste_titre\"><input type=\"text\" class=\"flat\" name=\"search_".$prop['field']."\" value=\"'.\$search_".$prop['field'].".'\" size=\"10\"></td>';\n";
  525. }
  526. }
  527. $targetcontent=preg_replace('/LIST_OF_TD_TITLE_SEARCH/', $varprop, $targetcontent);
  528. // Substitute where for <td>.fieldx.</td>
  529. $varprop="\n";
  530. $cleanparam='';
  531. foreach($property as $key => $prop)
  532. {
  533. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  534. {
  535. $varprop.="if (! empty(\$arrayfields['t.".$prop['field']."']['checked'])) print '<td>'.\$obj->".$prop['field'].".'</td>';\n";
  536. }
  537. }
  538. $targetcontent=preg_replace('/'.preg_quote("if (! empty(\$arrayfields['t.field1']['checked'])) print '<td>'.\$obj->field1.'</td>';",'/').'/', $varprop, $targetcontent);
  539. $targetcontent=preg_replace('/'.preg_quote("if (! empty(\$arrayfields['t.field2']['checked'])) print '<td>'.\$obj->field2.'</td>';",'/').'/', '', $targetcontent);
  540. // LIST_OF_TD_LABEL_FIELDS_CREATE - List of td for card view
  541. $varprop="\n";
  542. $cleanparam='';
  543. foreach($property as $key => $prop)
  544. {
  545. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  546. {
  547. $varprop.="print '<tr><td class=\"fieldrequired\">'.\$langs->trans(\"Field".$prop['field']."\").'</td><td><input class=\"flat\" type=\"text\" name=\"".$prop['field']."\" value=\"'.GETPOST('".$prop['field']."').'\"></td></tr>';\n";
  548. }
  549. }
  550. $targetcontent=preg_replace('/LIST_OF_TD_LABEL_FIELDS_CREATE/', $varprop, $targetcontent);
  551. // LIST_OF_TD_LABEL_FIELDS_EDIT - List of td for card view
  552. $varprop="\n";
  553. $cleanparam='';
  554. foreach($property as $key => $prop)
  555. {
  556. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  557. {
  558. $varprop.="print '<tr><td class=\"fieldrequired\">'.\$langs->trans(\"Field".$prop['field']."\").'</td><td><input class=\"flat\" type=\"text\" name=\"".$prop['field']."\" value=\"'.\$object->".$prop['field'].".'\"></td></tr>';\n";
  559. }
  560. }
  561. $targetcontent=preg_replace('/LIST_OF_TD_LABEL_FIELDS_EDIT/', $varprop, $targetcontent);
  562. // LIST_OF_TD_LABEL_FIELDS_VIEW - List of td for card view
  563. $varprop="\n";
  564. $cleanparam='';
  565. foreach($property as $key => $prop)
  566. {
  567. if ($prop['field'] != 'rowid' && $prop['field'] != 'id' && ! $prop['istime'])
  568. {
  569. $varprop.="print '<tr><td class=\"fieldrequired\">'.\$langs->trans(\"Field".$prop['field']."\").'</td><td>\$object->".$prop['field']."</td></tr>';\n";
  570. }
  571. }
  572. $targetcontent=preg_replace('/LIST_OF_TD_LABEL_FIELDS_VIEW/', $varprop, $targetcontent);
  573. // LIST_OF_TD_FIELDS_LIST
  574. // Build file
  575. $fp=fopen($outfile,"w");
  576. if ($fp)
  577. {
  578. fputs($fp, $targetcontent);
  579. fclose($fp);
  580. print "File '".$outfile."' has been built in current directory.\n";
  581. }
  582. else $error++;
  583. }
  584. // -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
  585. print "You can now move generated files to store them into directory /yourmodule/class (for .class.php file) or /yourmodule.\n";
  586. return $error;