PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/web/editor/common/model.php

https://bitbucket.org/keento/diagramo-janis
PHP | 148 lines | 67 code | 44 blank | 37 comment | 3 complexity | b693714b7f0fe52902b3a64e41c2da43 MD5 | raw file
Possible License(s): GPL-2.0, IPL-1.0
  1. <?php
  2. include(dirname(__FILE__).'/settings.php');
  3. //TODO: Blob should not be 'refined' with ' htmlentities(stripslashes....'
  4. #create connection
  5. $con= mysql_connect(DB_ADDRESS, DB_USER_NAME, DB_USER_PASS);
  6. $ok = mysql_select_db(DB_NAME);
  7. #find all tables
  8. $tables = array();
  9. $query = "SHOW TABLES";
  10. $result = mysql_query($query, $con);
  11. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  12. $tableName = $row['Tables_in_' . DB_NAME];
  13. #print $tableName . "\n";
  14. $tables[] = $tableName;
  15. }
  16. #find columns their properties
  17. $protoModels = array();
  18. foreach($tables as $table){
  19. $query = "SHOW COLUMNS FROM $table";
  20. print $query . "\n";
  21. $result = mysql_query($query, $con);
  22. $protoModels[$table] = array();
  23. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  24. $columnName = $row['Field'];
  25. //print "\t". $columnName . "\n";
  26. $isKey = $row['Key'] === 'PRI' ? 1:0;
  27. print "\t". $isKey . "\n";
  28. $isAutoIncrement = $row['Extra'] === 'auto_increment' ? 1:0;
  29. #print "\tColumn $columnName, key: $isKey, auto-increment: $isAutoIncrement \n";
  30. $protoModels[$table][] = array('columnName'=>$columnName, 'isKey'=>$isKey, 'isAutoIncrement'=>$isAutoIncrement, 'type'=>$row['Type'], 'null'=>$row['Null']);
  31. }//while
  32. }//foreach
  33. #print_r($protoModels);
  34. #GENERATE MODEL
  35. #generate model for each table
  36. $fh = fopen(dirname(__FILE__).'/entities.php','w+');
  37. #php start
  38. fwrite($fh, "<?php\n");
  39. #include Entity
  40. fwrite($fh, "\$currentFolder = dirname(__FILE__);\n");
  41. foreach($protoModels as $protoName => $protoData){
  42. #Recover from Novak's request (remove plural), actually remove the last 's'
  43. print_r('Initial:' . $protoName."\n");
  44. print_r('After:' . $protoName."\n");
  45. $entityName = ucfirst($protoName);
  46. #class start
  47. fwrite($fh, "\n\nclass $entityName {");
  48. #enum types ---to--> CONST
  49. foreach($protoData as $protoProperty){
  50. #print_r($protoProperty);
  51. #read this: http://www.hudzilla.org/phpwiki/index.php?title=Finding_a_string_within_a_string
  52. if(strpos($protoProperty['type'],'enum')===false){
  53. #do nothing
  54. }
  55. else{
  56. $type = $protoProperty['type'];
  57. $columnName = $protoProperty['columnName'];
  58. $start = strpos($type, '(');
  59. $end = strpos($type, ')');
  60. $sub = substr($type, $start + 1, $end - $start -1 );
  61. $tmps = explode (',', $sub);
  62. $values = array();
  63. foreach($tmps as $t){
  64. $values[] = substr($t, 1, strlen($t) -2);
  65. }
  66. #print_r($values);
  67. foreach($values as $value){
  68. fwrite($fh, sprintf("\n\tconst %s = '%s';", strtoupper($columnName . '_' . $value), $value) );
  69. }
  70. }
  71. }
  72. #columns ---to--> FIELDS
  73. fwrite($fh, "\n");
  74. foreach($protoData as $protoProperty){
  75. fwrite($fh, "\n\tpublic $" . $protoProperty['columnName'] .";");
  76. }
  77. fwrite($fh, "\n");
  78. #generate loadFromSQL function
  79. fwrite($fh, "\n\tfunction loadFromSQL(\$row) {");
  80. foreach($protoData as $protoProperty){
  81. //stripslashes is not needed as the inverse function of addslashes....fwrite($fh, sprintf("\n\t\t\$this->%s = is_null(\$row['%s']) ? null : stripslashes(\$row['%s']);",$protoProperty['columnName'] , $protoProperty['columnName'], $protoProperty['columnName']));
  82. fwrite($fh, sprintf("\n\t\t\$this->%s = is_null(\$row['%s']) ? null : \$row['%s'];",$protoProperty['columnName'] , $protoProperty['columnName'], $protoProperty['columnName']));
  83. }
  84. fwrite($fh, "\n\t}\n");
  85. /*
  86. #generate validate function
  87. fwrite($fh, "\n\tfunction validate() {");
  88. foreach($protoData as $protoProperty) {
  89. if($protoProperty['null'] == 'NO') {
  90. if(strpos($protoProperty['type'], 'int')) {
  91. fwrite($fh, sprintf("\n\t\t\(!validateInteger(\$this->%s)) ? return false : null;", $protoProperty['columnName']));
  92. }
  93. }
  94. }
  95. fwrite($fh, "\t}\n");
  96. */
  97. #class end
  98. fwrite($fh, '}');
  99. } //end foreach
  100. #php end
  101. fwrite($fh, "\n?>");
  102. #close stream
  103. fclose($fh);
  104. ?>