diff --git a/.gitignore b/.gitignore index d8517bd..6bd94b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -core/app/ +core/app/ \ No newline at end of file diff --git a/README.md b/README.md index 1cdbb4d..65ea872 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,4 @@ This is the startpage. For every selected table, Cruddiy has created 5 pages: In [![N|Cruddiy](https://j11g.com/cruddiy/bs4-cruddiy-app-index.png)](https://cruddiy.com) -You can rename or move the generated 'app' folder anywhere. It is completely self-contained. And you can delete Cruddiy, it is not needed anymore. Or you can can run it many more times (use the back button in the browser), until you get your pages just the way you like them. Each time it will overwrite your existing app. +You can rename or move the generated 'app' folder anywhere. It is completely self-contained. And you can delete Cruddiy, it is not needed anymore. Or you can can run it many more times (use the back button in the browser), until you get your pages just the way you like them. Each time it will overwrite your existing app. \ No newline at end of file diff --git a/core/columns.php b/core/columns.php index 624d305..23489c6 100644 --- a/core/columns.php +++ b/core/columns.php @@ -16,10 +16,15 @@
- -
+
+ + +          + + +
@@ -59,7 +64,22 @@ function get_col_types($table,$column){ $result = mysqli_query($link,$sql); $row = mysqli_fetch_assoc($result); return $row['Type'] ; - mysqli_free_result($result); + } + + function get_col_comments($table,$column){ + global $link; + $sql = "SHOW FULL FIELDS FROM $table where FIELD ="."'".$column."'"; + $result = mysqli_query($link,$sql); + $row = mysqli_fetch_assoc($result); + return $row['Comment'] ; + } + + function get_col_nullable($table,$column){ + global $link; + $sql = "SHOW FULL FIELDS FROM $table where FIELD ="."'".$column."'"; + $result = mysqli_query($link,$sql); + $row = mysqli_fetch_assoc($result); + return ($row['Null'] == "YES") ? true : 0; } function get_foreign_keys($table){ @@ -76,7 +96,6 @@ function get_foreign_keys($table){ $fks[] = $row['Foreign Key']; } return $fks; - mysqli_free_result($result); } $checked_tables_counter=0; @@ -98,6 +117,8 @@ function get_foreign_keys($table){ while ($column = mysqli_fetch_array($result)) { $column_type = get_col_types($tablename,$column[0]); + $column_comment = get_col_comments($tablename,$column[0]); + $column_nullable = get_col_nullable($tablename,$column[0]); if (in_array ("$column[0]", $primary_keys)) { $primary = "🔑"; @@ -123,21 +144,34 @@ function get_foreign_keys($table){ $fk = ""; } + if ($column_nullable) { + $nb = "🫙"; + } + else { + $nb = ""; + } + + echo ""; echo '
'. $primary . $auto . $fk . $column[0] . ' +
+ +
-
- +
+
-
'; +
+ +
+
'; $i++; } $checked_tables_counter++; @@ -179,11 +213,20 @@ function get_foreign_keys($table){ diff --git a/core/generate.php b/core/generate.php index ac084ea..55e87ca 100644 --- a/core/generate.php +++ b/core/generate.php @@ -26,6 +26,18 @@ $forced_deletion = false; $buttons_delimiter = ''; +$CSS_REFS = ''; +// $CSS_REFS = ' +// '; + +$JS_REFS = ' + +'; +// $JS_REFS = ' +// +// '; + + function column_type($columnname){ switch ($columnname) { case (preg_match("/text/i", $columnname) ? true : false) : @@ -37,11 +49,8 @@ function column_type($columnname){ case (preg_match("/varchar/i", $columnname) ? true : false) : return 3; break; - //Tinyint needs work to be selectable from dropdown list - //So for now a tinyint will be just a input field (in Create) - //and a prefilled input field (in Update). case (preg_match("/tinyint\(1\)/i", $columnname) ? true : false) : - return 5; + return 4; break; case (preg_match("/int/i", $columnname) ? true : false) : return 5; @@ -61,17 +70,57 @@ function column_type($columnname){ } } +function is_primary_key($t, $c){ + $cols = $_POST[$t . 'columns']; + foreach($cols as $col) { + if (isset($col['primary']) && $col['columnname'] == $c){ + return 1; + } + } + return 0; +} + +function get_sql_concat_select($copy_columns, $table, $name){ + $array = $copy_columns; + foreach($array as $key => $c) + { + $array[$key] = '`'. $table .'`.`'. $array[$key] .'`'; + } + return "\n\t\t\t, CONCAT_WS(' | ',". implode(', ', $array) .') AS `'. $name .'`'; +} + +function get_sql_select($copy_columns){ + $array = $copy_columns; + foreach($array as $key => $c) + { + $array[$key] = '`'.$array[$key].'`'; + } + return implode(', ', $array); +} + function generate_error(){ global $errorfile; - if (!file_put_contents("app/error.php", $errorfile, LOCK_EX)) { + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $errorfile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + if (!file_put_contents("app/error.php", $prestep2, LOCK_EX)) { die("Unable to open file!"); } echo "Generating Error file
"; } function generate_startpage(){ - global $startfile; - if (!file_put_contents("app/index.php", $startfile, LOCK_EX)) { + global $startfile; + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $startfile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + if (!file_put_contents("app/index.php", $prestep2, LOCK_EX)) { die("Unable to open file!"); } echo "Generating main index file.
"; @@ -155,14 +204,18 @@ function append_links_to_navbar($navbarfile, $start_page, $startpage_filename, $ } -function generate_index($tablename,$tabledisplay,$index_table_headers,$index_table_rows,$column_id, $columns_available, $index_sql_search) { +function generate_index($tablename,$tabledisplay,$index_table_headers,$index_table_rows,$column_id, $columns_available, $index_sql_search, $join_columns, $join_clauses) { global $indexfile; global $appname; + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $indexfile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); $columns_available = implode("', '", $columns_available); - $step0 = str_replace("{TABLE_NAME}", $tablename, $indexfile); - $step1 = str_replace("{TABLE_DISPLAY}", $tabledisplay, $step0); - $step2 = str_replace("{INDEX_QUERY}", "SELECT * FROM $tablename", $step1 ); + $step1 = str_replace("{TABLE_NAME}", $tablename, $prestep2); + $step2 = str_replace("{TABLE_DISPLAY}", $tabledisplay, $step1); $step3 = str_replace("{INDEX_TABLE_HEADERS}", $index_table_headers, $step2 ); $step4 = str_replace("{INDEX_TABLE_ROWS}", $index_table_rows, $step3 ); $step5 = str_replace("{COLUMN_ID}", $column_id, $step4 ); @@ -170,18 +223,29 @@ function generate_index($tablename,$tabledisplay,$index_table_headers,$index_tab $step7 = str_replace("{COLUMNS}", $columns_available, $step6 ); $step8 = str_replace("{INDEX_CONCAT_SEARCH_FIELDS}", $index_sql_search, $step7 ); $step9 = str_replace("{APP_NAME}", $appname, $step8 ); - if (!file_put_contents("app/".$tablename."-index.php", $step9, LOCK_EX)) { + $step10 = str_replace("{JOIN_COLUMNS}", $join_columns, $step9 ); + $step11 = str_replace("{JOIN_CLAUSES}", $join_clauses, $step10 ); + if (!file_put_contents("app/".$tablename."-index.php", $step11, LOCK_EX)) { die("Unable to open file!"); } echo "Generating $tablename Index file
"; } -function generate_read($tablename, $column_id, $read_records){ +function generate_read($tablename, $column_id, $read_records, $foreign_key_references, $join_columns, $join_clauses){ global $readfile; - $step0 = str_replace("{TABLE_NAME}", $tablename, $readfile); + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $readfile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + $step0 = str_replace("{TABLE_NAME}", $tablename, $prestep2); $step1 = str_replace("{TABLE_ID}", $column_id, $step0); $step2 = str_replace("{RECORDS_READ_FORM}", $read_records, $step1 ); - if (!file_put_contents("app/".$tablename."-read.php", $step2, LOCK_EX)) { + $step3 = str_replace("{FOREIGN_KEY_REFS}", $foreign_key_references, $step2 ); + $step4 = str_replace("{JOIN_COLUMNS}", $join_columns, $step3 ); + $step5 = str_replace("{JOIN_CLAUSES}", $join_clauses, $step4 ); + if (!file_put_contents("app/".$tablename."-read.php", $step5, LOCK_EX)) { die("Unable to open file!"); } echo "Generating $tablename Read file
"; @@ -189,7 +253,13 @@ function generate_read($tablename, $column_id, $read_records){ function generate_delete($tablename, $column_id){ global $deletefile; - $step0 = str_replace("{TABLE_NAME}", $tablename, $deletefile); + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $deletefile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + $step0 = str_replace("{TABLE_NAME}", $tablename, $prestep2); $step1 = str_replace("{TABLE_ID}", $column_id, $step0); if (!file_put_contents("app/".$tablename."-delete.php", $step1, LOCK_EX)) { die("Unable to open file!"); @@ -197,9 +267,15 @@ function generate_delete($tablename, $column_id){ echo "Generating $tablename Delete file

"; } -function generate_create($tablename,$create_records, $create_err_records, $create_sqlcolumns, $create_numberofparams, $create_sql_params, $create_html, $create_postvars) { +function generate_create($tablename,$create_records, $create_err_records, $create_sqlcolumns, $column_id, $create_numberofparams, $create_sql_params, $create_html, $create_postvars) { global $createfile; - $step0 = str_replace("{TABLE_NAME}", $tablename, $createfile); + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $createfile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + $step0 = str_replace("{TABLE_NAME}", $tablename, $prestep2); $step1 = str_replace("{CREATE_RECORDS}", $create_records, $step0); $step2 = str_replace("{CREATE_ERR_RECORDS}", $create_err_records, $step1); $step3 = str_replace("{CREATE_COLUMN_NAMES}", $create_sqlcolumns, $step2); @@ -207,7 +283,8 @@ function generate_create($tablename,$create_records, $create_err_records, $creat $step5 = str_replace("{CREATE_SQL_PARAMS}", $create_sql_params, $step4 ); $step6 = str_replace("{CREATE_HTML}", $create_html, $step5); $step7 = str_replace("{CREATE_POST_VARIABLES}", $create_postvars, $step6); - if (!file_put_contents("app/".$tablename."-create.php", $step7, LOCK_EX)) { + $step8 = str_replace("{COLUMN_ID}", $column_id, $step7); + if (!file_put_contents("app/".$tablename."-create.php", $step8, LOCK_EX)) { die("Unable to open file!"); } echo "Generating $tablename Create file
"; @@ -215,7 +292,13 @@ function generate_create($tablename,$create_records, $create_err_records, $creat function generate_update($tablename, $create_records, $create_err_records, $create_postvars, $column_id, $create_html, $update_sql_params, $update_sql_id, $update_column_rows, $update_sql_columns){ global $updatefile; - $step0 = str_replace("{TABLE_NAME}", $tablename, $updatefile); + global $CSS_REFS; + global $JS_REFS; + + $prestep1 = str_replace("{CSS_REFS}", $CSS_REFS, $updatefile); + $prestep2 = str_replace("{JS_REFS}", $JS_REFS, $prestep1); + + $step0 = str_replace("{TABLE_NAME}", $tablename, $prestep2); $step1 = str_replace("{CREATE_RECORDS}", $create_records, $step0); $step2 = str_replace("{CREATE_ERR_RECORDS}", $create_err_records, $step1); $step3 = str_replace("{COLUMN_ID}", $column_id, $step2); @@ -259,6 +342,21 @@ function generate($postdata) { // echo ""; // Go trough the POST array // Every table is a key + global $excluded_keys; + + // Array with structure $preview_columns[TABLE_NAME] where each instance contains an array of columns that + // are selected to be include in previews, such as select foreign keys and foreign key preview. + $preview_columns = array(); + foreach ($postdata as $key => $value){ + if (!in_array($key, $excluded_keys)) { + foreach ($_POST[$key] as $columns ) { + if (isset($columns['columninpreview'])){ + $preview_columns[$columns['tablename']][] = $columns['columnname']; + } + } + } + } + foreach ($postdata as $key => $value) { $tables = array(); $tablename = ''; @@ -267,6 +365,7 @@ function generate($postdata) { $columndisplay = ''; $columnvisible = ''; $columns_available = array(); + $index_sql_search = array(); $index_table_rows = ''; $index_table_headers = ''; $read_records = ''; @@ -285,7 +384,9 @@ function generate($postdata) { $update_sql_id = ''; $update_column_rows = ''; - global $excluded_keys; + $join_columns = ''; + $join_clauses = ''; + global $sort; global $link; global $forced_deletion; @@ -296,6 +397,32 @@ function generate($postdata) { $max = count_index_colums($key)+1; $total_columns = count($_POST[$key]); $total_params = count($_POST[$key]); + $tablename = $_POST[$key][0]['tablename']; + + // Find foreign key references to this table + $foreign_key_references = ""; + $sql_get_fk_ref = "SELECT i.TABLE_NAME as 'Table', k.COLUMN_NAME as 'Column', + k.REFERENCED_TABLE_NAME as 'FK Table', k.REFERENCED_COLUMN_NAME as 'FK Column', + i.CONSTRAINT_NAME as 'Constraint Name' + FROM information_schema.TABLE_CONSTRAINTS i + LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME + WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND k.REFERENCED_TABLE_NAME = '$tablename'"; + $result = mysqli_query($link, $sql_get_fk_ref); + if (mysqli_num_rows($result) > 0) { + while($row = mysqli_fetch_assoc($result)) { + $table = $row["Table"]; + $fk_column = $row["FK Column"]; + $column = $row["Column"]; + $foreign_key_references .= ' + $sql = "SELECT COUNT(*) AS count FROM `'. $table .'` WHERE `'. $column .'` = ". $row["'.$fk_column.'"] . ";"; + $number_of_refs = mysqli_fetch_assoc(mysqli_query($link, $sql))["count"]; + if ($number_of_refs > 0) + { + $html .= \'

View \' . $number_of_refs . \' ' . $table . ' with '. $column . ' = \'. $row["'.$fk_column.'"] .\'

\'; + }'; + } + } + $foreign_key_references = $foreign_key_references != "" ? '$html = "";' . $foreign_key_references . 'if ($html != "") {echo "

References to this ' . $tablename . ':

" . $html;}' : ""; //Specific INDEX page variables foreach ( $_POST[$key] as $columns ) { @@ -303,6 +430,14 @@ function generate($postdata) { $column_id = $columns['columnname']; } + // These variables contain the generated names, labels, input field and values for column. + // They are used at the end of this loop to create the {RECORDS_READ_FORM} and the {CREATE_HTML} + // $columndisplay contains the name of the column + $column_value = ""; + $column_input = ""; + + $type = column_type($columns['columntype']); + //INDEXFILE VARIABLES //Get the columns visible in the index file if (isset($columns['columnvisible'])){ @@ -317,9 +452,56 @@ function generate($postdata) { $columndisplay = $columns['columnname']; } - $columns_available [] = $columnname; - $index_table_headers .= 'echo "'.$columndisplay.'";'."\n\t\t\t\t\t\t\t\t\t\t"; - $index_table_rows .= 'echo "" . htmlspecialchars($row['. "'" . $columnname . "'" . ']) . "";'; + if (!empty($columns['columncomment'])){ + $columndisplay = "" . $columndisplay . ''; + } + + $columns_available [] = "$columnname"; + $index_sql_search [] = "`$tablename`.`$columnname`"; + $index_table_headers .= 'echo "'.$columndisplay.'";'."\n\t\t\t\t\t\t\t\t\t\t"; + + // Display date in locale format + if(!empty($columns['fk'])){ + //Get the Foreign Key + $tablename = $columns['tablename']; + $columnname = $columns['columnname']; + $sql_getfk = "SELECT i.TABLE_NAME as 'Table', k.COLUMN_NAME as 'Column', + k.REFERENCED_TABLE_NAME as 'FK Table', k.REFERENCED_COLUMN_NAME as 'FK Column', + i.CONSTRAINT_NAME as 'Constraint Name' + FROM information_schema.TABLE_CONSTRAINTS i + LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME + WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND k.TABLE_NAME = '$tablename' AND k.COLUMN_NAME = '$columnname'"; + $result = mysqli_query($link, $sql_getfk); + if (mysqli_num_rows($result) > 0) { + while($row = mysqli_fetch_assoc($result)) { + $fk_table = $row["FK Table"]; + $fk_column = $row["FK Column"]; + } + $join_column_name = $columnname . $fk_table . $fk_column; + $is_primary_ref = is_primary_key($fk_table, $fk_column); + $index_table_rows .= 'echo "" . get_fk_url($row["'.$columnname.'"], "'.$fk_table.'", "'.$fk_column.'", $row["'.$join_column_name.'"], '. $is_primary_ref .', true) . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } + } + else if ($type == 1) // Text + { + $index_table_rows .= 'echo "" . nl2br(htmlspecialchars($row['. "'" . $columnname . "'" . '] ?? "")) . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } + else if ($type == 4) // TinyInt / Bool + { + $index_table_rows .= 'echo "" . convert_bool($row['. "'" . $columnname . "'" . ']) . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } + else if ($type == 7) // Date + { + $index_table_rows .= 'echo "" . convert_date($row['. "'" . $columnname . "'" . ']) . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } + else if ($type == 8) // Datetime + { + $index_table_rows .= 'echo "" . convert_datetime($row['. "'" . $columnname . "'" . ']) . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } + else + { + $index_table_rows .= 'echo "" . htmlspecialchars($row['. "'" . $columnname . "'" . '] ?? "") . "";'."\n\t\t\t\t\t\t\t\t\t\t"; + } $i++; } } @@ -327,16 +509,26 @@ function generate($postdata) { //DETAIL CREATE UPDATE AND DELETE pages variables foreach ( $_POST[$key] as $columns ) { - //print_r($columns); if ($j < $total_columns) { + $type = column_type($columns['columntype']); + if (isset($columns['columndisplay'])){ $columndisplay = $columns['columndisplay']; } if (empty($columns['columndisplay'])){ $columndisplay = $columns['columnname']; } + + if (!$columns['columnnullable']) + { + $columndisplay .= "*"; + } + if (!empty($columns['columncomment'])){ + $columndisplay = "" . $columndisplay . ''; + } + if (!empty($columns['auto'])){ //Dont create html input field for auto-increment columns $j++; @@ -361,22 +553,25 @@ function generate($postdata) { if(empty($columns['auto'])) { $columnname = $columns['columnname']; - $read_records .= '
-

'.$columndisplay.'

-

-
'; - - $create_records .= "\$$columnname = \"\";\n"; - $create_record = "\$$columnname"; - $create_err_records .= "\$$columnname".'_err'." = \"\";\n"; - $create_err_record = "\$$columnname".'_err'; - $create_sqlcolumns [] = $columnname; - $create_sql_params [] = "\$$columnname"; - $create_postvars .= "$$columnname = trim(\$_POST[\"$columnname\"]);\n\t\t"; - - $update_sql_params [] = "$columnname".'=?'; - $update_sql_id = "$column_id".'=?'; - $update_column_rows .= "$$columnname = htmlspecialchars(\$row[\"$columnname\"]);\n\t\t\t\t\t"; + $columnname_var = preg_replace('/[^a-zA-Z0-9]+/', '_', $columnname); + + $create_records .= "\$$columnname_var = \"\";\n"; + $create_record = "\$$columnname_var"; + $create_err_records .= "\$$columnname_var".'_err'." = \"\";\n"; + $create_err_record = "\$$columnname_var".'_err'; + $create_sqlcolumns [] = "`$columnname`"; + $create_sql_params [] = "\$$columnname_var"; + + // Process POST vars that can be null differently + if ($columns['columnnullable']){ + $create_postvars .= "$$columnname_var = \$_POST[\"$columnname\"] == \"\" ? null : trim(\$_POST[\"$columnname\"]);\n\t\t"; + } else { + $create_postvars .= "$$columnname_var = trim(\$_POST[\"$columnname\"]);\n\t\t"; + } + + $update_sql_params [] = "`$columnname`".'=?'; + $update_sql_id = "`$column_id`".'=?'; + $update_column_rows .= "$$columnname_var = htmlspecialchars(\$row[\"$columnname\"] ?? \"\");\n\t\t\t\t\t"; //Foreign Key @@ -398,153 +593,188 @@ function generate($postdata) { //Be careful code below is particular regarding single and double quotes. + + $html = ' - \' . "$value" . \'\'; + if ($row["' . $fk_column . '"] == $' . $columnname_var . '){ + echo \'\'; } else { - echo \'\'; + echo \'\'; } } ?> - - - '; + '; + $column_input = $html; + unset($html); } // No Foreign Keys, just regular columns from here on } else { - $type = column_type($columns['columntype']); + // Display date in locale format + if ($type == 1) // Text + { + $column_value = ''; + } + else if ($type == 4) // TinyInt / Bool + { + $column_value = ''; + } + else if ($type == 7) // Date + { + $column_value = ''; + } + else if ($type == 8) // Datetime + { + $column_value = ''; + } + else + { + $column_value = ''; + } + + //$type = column_type($columns['columntype']); switch($type) { //TEXT - case 0: - $create_html [] = '
- - - -
'; + case 1: + $column_input = ''; break; //ENUM types case 2: //Make sure on the update form that the previously selected type is also selected from the list - $create_html [] = '
- - - -
'; + + $html = ''; + + $column_input = $html; + unset($html); break; //VARCHAR case 3: preg_match('#\((.*?)\)#', $columns['columntype'], $match); $maxlength = $match[1]; - - $create_html [] = '
- - - -
'; + $column_input = ''; break; - //TINYINT - Will never hit. Needs work. + + //TINYINT (bool) case 4: $regex = "/'(.*?)'/"; preg_match_all( $regex , $columns['columntype'] , $enum_array ); - - $create_html [] = '
- - - -
'; + $html = ''; + $column_input = $html; + unset($html); break; //INT case 5: - $create_html [] = '
- - - -
'; + $column_input = ''; break; //DECIMAL case 6: - $create_html [] = '
- - - -
'; + $column_input = ''; break; //DATE case 7: - $create_html [] = '
- - - -
'; + $column_input = ''; break; //DATETIME case 8: - $create_html [] = '
- - "> - -
'; + $column_input = '">'; break; default: - $create_html [] = '
- - - -
'; + $column_input = ''; break; } } - $j++; + + // Regular layout + $create_html [] = '
+ + '. $column_input .'
'; + $read_records .= '
+

'.$columndisplay.'

+

' . $column_value .'

'; + + // Different Layout + // $create_html [] = '
+ // + //
'. $column_input .'
'; + // $read_records .= '
+ //
'.$columndisplay.'
+ //
'. $column_value .'
'; + $j++; } } + if ($j == $total_columns) { $update_sql_columns = $create_sql_params; $update_sql_columns [] = "\$$column_id"; - $update_sql_columns = implode(",", $update_sql_columns); + $update_sql_columns = implode(", ", $update_sql_columns); - $index_sql_search = implode(",", $columns_available); + $index_sql_search = implode(", ", $index_sql_search); $create_numberofparams = array_fill(0, $total_params, '?'); - $create_numberofparams = implode(",", $create_numberofparams); - $create_sqlcolumns = implode(",", $create_sqlcolumns); - $create_sql_params = implode(",", $create_sql_params); + $create_numberofparams = implode(", ", $create_numberofparams); + $create_sqlcolumns = implode(", ", $create_sqlcolumns); + $create_sql_params = implode(", ", $create_sql_params); $create_html = implode("\n\t\t\t\t\t\t", $create_html); $update_sql_params = implode(",", $update_sql_params); @@ -578,9 +808,9 @@ function generate($postdata) { generate_navbar($value, $start_page, isset($_POST['keep_startpage']) && $_POST['keep_startpage'] == 'true' ? true : false, isset($_POST['append_links']) && $_POST['append_links'] == 'true' ? true : false, $tabledisplay); generate_error(); generate_startpage(); - generate_index($tablename,$tabledisplay,$index_table_headers,$index_table_rows,$column_id, $columns_available,$index_sql_search); - generate_create($tablename,$create_records, $create_err_records, $create_sqlcolumns, $create_numberofparams, $create_sql_params, $create_html, $create_postvars); - generate_read($tablename,$column_id,$read_records); + generate_index($tablename,$tabledisplay,$index_table_headers,$index_table_rows,$column_id, $columns_available,$index_sql_search, $join_columns, $join_clauses); + generate_create($tablename,$create_records, $create_err_records, $create_sqlcolumns, $column_id, $create_numberofparams, $create_sql_params, $create_html, $create_postvars); + generate_read($tablename,$column_id,$read_records,$foreign_key_references, $join_columns, $join_clauses); generate_update($tablename, $create_records, $create_err_records, $create_postvars, $column_id, $create_html, $update_sql_params, $update_sql_id, $update_column_rows, $update_sql_columns); generate_delete($tablename,$column_id); } diff --git a/core/helpers.php b/core/helpers.php index 955c3ea..9d81cca 100644 --- a/core/helpers.php +++ b/core/helpers.php @@ -1,6 +1,7 @@ "; } - switch($row['DATA_TYPE']) { + switch ($row['DATA_TYPE']) { // fix "Incorrect decimal value: '' error in STRICT_MODE or STRICT_TRANS_TABLE // @see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html @@ -43,10 +43,10 @@ function parse_columns($table_name, $postdata) { if ($row['COLUMN_DEFAULT'] != 'CURRENT_TIMESTAMP' && $row['IS_NULLABLE'] == 'YES') { $default = null; } else { - $default = date('Y-m-d H:i:s'); + $default = date('Y-m-d H:i:s'); } if ($postdata[$row['COLUMN_NAME']] == 'CURRENT_TIMESTAMP') { - $_POST[$row['COLUMN_NAME']] = date('Y-m-d H:i:s'); + $_POST[$row['COLUMN_NAME']] = date('Y-m-d H:i:s'); } break; } @@ -60,15 +60,15 @@ function parse_columns($table_name, $postdata) { // get extra attributes for table keys on CREATE and UPDATE events -function get_columns_attributes($table_name, $column) { +function get_columns_attributes($table_name, $column) +{ global $link; $sql = "SELECT COLUMN_DEFAULT, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS - WHERE table_name = '".$table_name."' - AND column_name = '".$column."'"; - $result = mysqli_query($link,$sql); - while($row = mysqli_fetch_assoc($result)) - { + WHERE table_name = '" . $table_name . "' + AND column_name = '" . $column . "'"; + $result = mysqli_query($link, $sql); + while ($row = mysqli_fetch_assoc($result)) { $debug = 0; if ($debug) { echo "
";
@@ -78,4 +78,51 @@ function get_columns_attributes($table_name, $column) {
         return $row;
     }
 }
+
+function print_error_if_exists($error)
+{
+    if (isset($error)) {
+        echo "";
+    }
+}
+
+function convert_date($date_str)
+{
+    if (isset($date_str)) {
+        $date = date('d-m-Y', strtotime($date_str));
+        return htmlspecialchars($date);
+    }
+}
+
+function convert_datetime($date_str)
+{
+    if (isset($date_str)) {
+        $date = date('d-m-Y H:i:s', strtotime($date_str));
+        return htmlspecialchars($date);
+    }
+}
+
+function convert_bool($var)
+{
+    if (isset($var)) {
+        return $var ? "True" : "False";
+    }
+}
+
+function get_fk_url($value, $fk_table, $fk_column, $representation, bool $pk=false, bool $index=false)
+// Gets a URL to the foreign key parents read page
+{
+    if (isset($value)) {
+        $value = htmlspecialchars($value);
+        if($pk)
+        {
+            return '' . $representation . '';
+        }
+        else
+        {
+            return '' . $representation . '';
+        }
+        
+    }
+}
 ?>
\ No newline at end of file
diff --git a/core/relations.php b/core/relations.php
index 9da6366..cfcacf8 100644
--- a/core/relations.php
+++ b/core/relations.php
@@ -50,6 +50,8 @@
 	$txt .= "\$db_password = '$password'; \n";
 	$txt .= "\$no_of_records_per_page = $numrecordsperpage; \n";
 	$txt .= "\$appname = '$appname'; \n\n";
+    $txt .= "\$protocol=(\$_SERVER['HTTPS'] == 'on' ? 'https' : 'http');\n";
+    $txt .= "\$domain = \$protocol . '://' . \$_SERVER['SCRIPT_NAME']; //Replace domain with your domain name. (Locally typically something like localhost)\n\n";
 	$txt .= "\$link = mysqli_connect(\$db_server, \$db_user, \$db_password, \$db_name); \n";
 
     $txt .= '$query = "SHOW VARIABLES LIKE \'character_set_database\'";' ."\n";
diff --git a/core/templates.php b/core/templates.php
index bbf9a79..97db503 100644
--- a/core/templates.php
+++ b/core/templates.php
@@ -6,7 +6,7 @@
 
     
     {APP_NAME}
-    
+    {CSS_REFS}