2022-06-24 18:19:26 +02:00
<? php
/*
* Create module for listing and editing based on SQL table
* Create template files accordingly
*
2022-10-14 14:18:26 +02:00
* Copyright (c) 2022 OpenXE project
2022-06-24 18:19:26 +02:00
*
* Placeholders:
* PLACEHOLDER_MODULENAME
* PLACEHOLDER_MODULECLASSNAME
* PLACEHOLDER_LIST
* PLACEHOLDER_EDIT
* PLACEHOLDER_DELETE
* PLACEHOLDER_SQL_LIST
2022-10-21 16:11:56 +00:00
* PLACEHOLDER_DROPNBOX
2022-06-24 18:19:26 +02:00
* PLACEHOLDER_GET_INPUT
* PLACEHOLDER_SET_INPUT
* PLACEHOLDER_COLUMNS
* PLACEHOLDER_SET_TPL
*/
$host = 'localhost' ;
2022-10-21 16:11:56 +00:00
$user = 'openxe' ;
$passwd = 'openxe' ;
$schema = 'openxe' ;
2022-06-24 18:19:26 +02:00
2022-11-07 10:11:33 +00:00
echo ( " \n " );
2022-06-25 11:33:55 +02:00
if ( $argc >= 2 ) {
if ( in_array ( '-v' , $argv )) {
$verbose = true ;
} else {
$verbose = false ;
}
2022-06-25 13:49:22 +02:00
if ( in_array ( '-f' , $argv )) {
$force = true ;
} else {
$force = false ;
}
2022-11-07 10:11:33 +00:00
if ( strpos ( $argv [ 1 ], '-' ) == 0 ) {
$module_name = $argv [ 1 ];
2022-06-25 11:33:55 +02:00
} else {
info ();
exit ;
}
2022-11-07 10:11:33 +00:00
// column selection
$selected_columns = array ();
// Set selected_columns here or per parameter -c col1,col2,col3
// $selected_columns = array('belegnr','adresse','projekt');
if ( in_array ( '-c' , $argv )) {
$pos = array_keys ( $argv , '-c' )[ 0 ];
$pos ++ ;
if ( isset ( $argv [ $pos ])) {
$selected_columns = explode ( ',' , $argv [ $pos ]);
}
}
if ( empty ( $selected_columns )) {
echo ( "Selected all columns. \n " );
} else {
echo ( "Selected " . count ( $selected_columns ) . " columns, \n " );
}
2022-06-24 18:19:26 +02:00
$module_class_name = ucfirst ( $module_name );
$php_file_name = $module_name . ".php" ;
$php_template_file_name = "module_creator_php_template.txt" ;
$template_list_file_name = $module_name . "_list.tpl" ;
$template_edit_file_name = $module_name . "_edit.tpl" ;
$target_php_folder = "../../www/pages/" ;
$target_tpl_folder = "../../www/pages/content/" ;
2022-07-22 11:55:58 +02:00
$table_short_name = substr ( $module_name , 0 , 1 );
2022-06-24 18:19:26 +02:00
2022-06-25 13:49:22 +02:00
if ( ! $force && file_exists ( $target_php_folder . $php_file_name )) {
echo ( "File exists: ." . $target_php_folder . $php_file_name . " \n " );
echo ( "Use -f to force overwrite. \n " );
2022-06-24 18:19:26 +02:00
exit ;
}
// First get the contents of the database table structure
$mysqli = mysqli_connect ( $host , $user , $passwd , $schema );
/* Check if the connection succeeded */
if ( ! $mysqli ) {
echo "Connection failed \n " ;
echo "Error number: " . mysqli_connect_errno () . " \n " ;
echo "Error message: " . mysqli_connect_error () . " \n " ;
exit ;
}
echo "Successfully connected! \n " ;
$query = "SHOW COLUMNS FROM " . $module_name ;
$result = mysqli_query ( $mysqli , $query );
if ( ! $result ) {
echo "Query error: " . mysqli_error ( $mysqli );
exit ;
}
$columns = array ();
2022-07-22 11:55:58 +02:00
$sql_columns = array ();
2022-06-24 18:19:26 +02:00
$edit_form = "" ;
/* Iterate through the result set */
echo "FIELD \t\t\t\t Type \t\t Null \t Key \t Default \t Extra \n " ;
while ( $row = mysqli_fetch_assoc ( $result )) {
2022-11-07 10:11:33 +00:00
$column_processed = false ;
if ( empty ( $selected_columns ) || in_array ( $row [ 'Field' ], $selected_columns ) || $row [ 'Field' ] == 'id' ) {
foreach ( $row as $key => $value ) {
echo ( $value );
switch ( $key ) {
case 'Field' :
$colwidth = 32 ;
if ( $value != 'id' ) {
$column_processed = true ;
$columns [] = $value ;
$sql_columns [] = $table_short_name . "." . $value ;
}
break ;
case 'Type' :
$colwidth = 16 ;
break ;
default :
$colwidth = 8 ;
break ;
}
for ( $filler = strlen ( $value ); $filler < $colwidth ; $filler ++ ) {
echo ( " " );
}
2022-06-24 18:19:26 +02:00
}
2022-11-07 10:11:33 +00:00
// Build edit form
// <tr><td>{|Bezeichnung|}:*</td><td><input type="text" id="bezeichnung" name="bezeichnung" value="[BEZEICHNUNG]" size="40"></td></tr>
2022-06-24 18:19:26 +02:00
2022-11-07 10:11:33 +00:00
if ( $row [ 'Field' ] != 'id' ) {
$edit_form = $edit_form . '<tr><td>{|' . ucfirst ( $row [ 'Field' ]) . '|}:</td><td><input type="text" name="' . $row [ 'Field' ] . '" id="' . $row [ 'Field' ] . '" value="[' . strtoupper ( $row [ 'Field' ]) . ']" size="20"></td></tr>' . " \n " ;
}
echo ( " \n " );
2022-06-24 18:19:26 +02:00
}
2022-11-07 10:11:33 +00:00
}
2022-06-24 18:19:26 +02:00
2022-11-07 10:11:33 +00:00
if ( empty ( $columns )) {
echo ( "No matching columns found! \n " );
exit ();
2022-06-24 18:19:26 +02:00
}
// Create php file
$list_of_columns = implode ( ', ' , $columns );
$list_of_columns_in_quotes = "'" . implode ( '\', \'' , $columns ) . "'" ;
2022-07-22 11:55:58 +02:00
$sql_list_of_columns = implode ( ', ' , $sql_columns );
$sql_list_of_columns_in_quotes = "'" . implode ( '\', \'' , $sql_columns ) . "'" ;
2022-06-24 18:19:26 +02:00
2022-06-25 11:33:55 +02:00
$get_input = "" ;
$set_input = "" ;
2022-06-24 18:19:26 +02:00
foreach ( $columns as $column ) {
$get_input = $get_input . " \$ input[' $column '] = \$ this->app->Secure->GetPOST(' $column '); \n\t " ;
$set_input = $set_input . " \$ this->app->Tpl->Set('" . strtoupper ( $column ) . "', \$ input[' $column ']); \n\t " ;
}
$php_file_contents = file_get_contents ( $php_template_file_name );
if ( empty ( $php_file_contents )) {
2022-06-28 17:07:05 +02:00
echo ( "Failed to load " . $php_template_file_name . " \n " );
2022-06-24 18:19:26 +02:00
exit ;
}
$php_file_contents = str_replace ( 'PLACEHOLDER_MODULENAME' , $module_name , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_MODULECLASSNAME' , $module_class_name , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_LIST' , $module_name . "_list" , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_EDIT' , $module_name . "_edit" , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_DELETE' , $module_name . "_delete" , $php_file_contents );
2022-10-21 16:11:56 +00:00
$php_file_contents = str_replace ( 'PLACEHOLDER_DROPNBOX' , "'<img src=./themes/new/images/details_open.png class=details>' AS `open`, CONCAT('<input type= \\\" checkbox \\\" name= \\\" auswahl[] \\\" value= \\\" '," . $table_short_name . ".id,' \\\" />') AS `auswahl`" , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_SQL_LIST' , "SELECT SQL_CALC_FOUND_ROWS $table_short_name .id, \$ dropnbox, $sql_list_of_columns , $table_short_name .id FROM $module_name $table_short_name " , $php_file_contents );
2022-06-24 18:19:26 +02:00
$php_file_contents = str_replace ( 'PLACEHOLDER_SQL_EDIT' , "INSERT INTO $module_name ( $list_of_columns , id) values (' \" .implode('\', \'', \$ input). \" ', \$ id) ON DUPLICATE KEY UPDATE SET " , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_GET_INPUT' , $get_input , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_SET_INPUT' , $set_input , $php_file_contents );
$php_file_contents = str_replace ( 'PLACEHOLDER_COLUMNS' , $list_of_columns_in_quotes , $php_file_contents );
2022-07-22 11:55:58 +02:00
$php_file_contents = str_replace ( 'PLACEHOLDER_SQL_COLUMNS' , $sql_list_of_columns_in_quotes , $php_file_contents );
2022-06-24 18:19:26 +02:00
$php_file = fopen ( $target_php_folder . $php_file_name , "w" );
if ( empty ( $php_file )) {
echo ( "Failed to write to " . $target_php_folder . $php_file_name );
}
$template_list_file = fopen ( $target_tpl_folder . $template_list_file_name , "w" );
if ( empty ( $template_list_file )) {
echo ( "Failed to write to " . $target_tpl_folder . $template_list_file_name );
}
$template_edit_file = fopen ( $target_tpl_folder . $template_edit_file_name , "w" );
if ( empty ( $template_edit_file )) {
echo ( "Failed to write to " . $target_tpl_folder . $template_edit_file_name );
}
fwrite ( $php_file , $php_file_contents );
fclose ( $php_file );
$list_template_contents = file_get_contents ( "module_creator_list.tpl" );
fwrite ( $template_list_file , $list_template_contents );
fclose ( $template_list_file );
$edit_template_contents = file_get_contents ( "module_creator_edit.tpl" );
$edit_template_contents = str_replace ( 'PLACEHOLDER_LEGEND' , "<!--Legend for this form area goes here>-->" . $module_name , $edit_template_contents );
$edit_template_contents = str_replace ( 'PLACEHOLDER_FIELDS' , $edit_form , $edit_template_contents );
fwrite ( $template_edit_file , $edit_template_contents );
fclose ( $template_edit_file );
echo ( " \n\n Created module files: \n " );
echo ( $target_php_folder . $php_file_name . " \n " );
2022-06-25 11:33:55 +02:00
if ( $verbose ) {
echo ( "----------- \n\n " );
echo ( $php_file_contents );
echo ( "----------- \n\n " );
}
2022-06-24 18:19:26 +02:00
echo ( $target_tpl_folder . $template_list_file_name . " \n " );
2022-06-25 11:33:55 +02:00
if ( $verbose ) {
echo ( "----------- \n\n " );
echo ( $list_template_contents );
echo ( "----------- \n\n " );
}
2022-06-24 18:19:26 +02:00
echo ( $target_tpl_folder . $template_edit_file_name . " \n " );
2022-06-25 11:33:55 +02:00
if ( $verbose ) {
echo ( "----------- \n\n " );
echo ( $edit_template_contents );
echo ( "----------- \n\n " );
}
2022-06-24 18:19:26 +02:00
} else {
2022-06-25 11:33:55 +02:00
info ();
exit ;
}
function info () {
2022-10-14 14:18:26 +02:00
echo ( " \n OpenXE module creator \n " );
echo ( "Copyright 2022 (c) OpenXE project \n\n " );
2022-06-25 11:33:55 +02:00
echo ( "Create a module.php file, a template for listing and a template for editing, based on a SQL table \n " );
echo ( " \n " );
echo ( "arg1: SQL table name \n " );
echo ( "Options \n " );
echo ( " \t -v: verbose output \n " );
2022-11-07 10:11:33 +00:00
echo ( " \t -f: force override of existing files \n " );
echo ( " \t -c: select columns like this: -c col1,col2,col3,col3 \n " );
2022-06-25 11:33:55 +02:00
echo ( " \n " );
2022-06-24 18:19:26 +02:00
}