module creator

This commit is contained in:
Xenomporio 2022-06-24 18:19:26 +02:00
parent 448e1e569c
commit a80e69efa2
4 changed files with 424 additions and 0 deletions

View File

@ -0,0 +1,181 @@
<?php
/*
* Create module for listing and editing based on SQL table
* Create template files accordingly
*
* Copyright (c) 2022 Xenomporio project
*
* Placeholders:
* PLACEHOLDER_MODULENAME
* PLACEHOLDER_MODULECLASSNAME
* PLACEHOLDER_LIST
* PLACEHOLDER_EDIT
* PLACEHOLDER_DELETE
* PLACEHOLDER_SQL_LIST
* PLACEHOLDER_SQL_LIST
* PLACEHOLDER_GET_INPUT
* PLACEHOLDER_SET_INPUT
* PLACEHOLDER_COLUMNS
* PLACEHOLDER_SET_TPL
*/
$host = 'localhost';
$user = 'xenomporiodev';
$passwd = 'xenomporiodev';
$schema = 'xenomporiodev';
if ($argc == 2) {
$module_name = $argv[1];
$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/";
if (file_exists($php_file_name)) {
echo("File exists: ." . $php_file_name . "\n");
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();
$edit_form = "";
/* Iterate through the result set */
echo "FIELD\t\t\t\tType\t\tNull\tKey\tDefault\tExtra\n";
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $key => $value) {
echo($value);
switch ($key) {
case 'Field':
$colwidth = 32;
if ($value != 'id') {
$columns[] = $value;
}
break;
case 'Type':
$colwidth = 16;
break;
default:
$colwidth = 8;
break;
}
for ($filler = strlen($value); $filler < $colwidth; $filler++) {
echo(" ");
}
}
// Build edit form
// <tr><td>{|Bezeichnung|}:*</td><td><input type="text" id="bezeichnung" name="bezeichnung" value="[BEZEICHNUNG]" size="40"></td></tr>
if ($row['Field'] != 'id') {
$edit_form = $edit_form . '<tr><td>{|' . $row['Field'] . '|}:</td><td><input type="text" name="' . $row['Field'] . '" value="[' . strtoupper($row['Field']) . ']" size="40"></td></tr>' . "\n";
}
echo("\n");
}
// Create php file
$list_of_columns = implode(', ', $columns);
$list_of_columns_in_quotes = "'" . implode('\', \'', $columns) . "'";
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)) {
echo("Failed to load" . $php_template_file_name . "\n");
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);
$php_file_contents = str_replace('PLACEHOLDER_SQL_LIST', "SELECT id, $list_of_columns, id FROM $module_name", $php_file_contents);
$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);
if ($verbose) {
echo($php_file_contents);
}
$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");
if ($verbose) {
echo($list_template_contents);
}
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);
if ($verbose) {
echo($edit_template_contents);
}
fwrite($template_edit_file, $edit_template_contents);
fclose($template_edit_file);
echo("\n\nCreated module files: \n");
echo ($target_php_folder . $php_file_name . "\n");
echo ($target_tpl_folder . $template_list_file_name . "\n");
echo ($target_tpl_folder . $template_edit_file_name . "\n");
} else {
echo("Wrong parameters\n");
}

View File

@ -0,0 +1,49 @@
<!-- Example for multiple tabs
<div id="tabs">
<ul>
<li><a href="#tabs-1">First Tab</a></li>
<li><a href="#tabs-2">Second Tab</a></li>
</ul>
-->
<form action="" method="post">
<div id="tabs-1">
[MESSAGE]
<div class="row">
<div class="row-height">
<div class="col-xs-12 col-md-12 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|PLACEHOLDER_LEGEND|}</legend>
<table width="100%" border="0" class="mkTableFormular">
PLACEHOLDER_FIELDS
</table>
</fieldset>
<input type="submit" name="submit" value="Speichern" style="float:right"/>
</div>
</div>
</div>
</div>
</div>
<!-- Example for second tab
<div id="tabs-2">
[MESSAGE]
<div class="row">
<div class="row-height">
<div class="col-xs-12 col-md-12 col-md-height">
<div class="inside inside-full-height">
<fieldset>
<legend>{|Legend goes here|}</legend>
<table width="100%" border="0" class="mkTableFormular">
PLACEHOLDER_FIELDS
</table>
</fieldset>
<input type="submit" name="submit" value="Speichern" style="float:right"/>
</div>
</div>
</div>
</div>
</div>
-->
</form>

View File

@ -0,0 +1,5 @@
<div id="tabs-1">
[MESSAGE]
[TAB1]
[TAB1NEXT]
</div>

View File

@ -0,0 +1,189 @@
<?php
/*
* Copyright (c) 2022 Xenomporio project
*/
use Xentral\Components\Database\Exception\QueryFailureException;
class PLACEHOLDER_MODULECLASSNAME {
function __construct($app, $intern = false) {
$this->app = $app;
if ($intern)
return;
$this->app->ActionHandlerInit($this);
$this->app->ActionHandler("list", "PLACEHOLDER_LIST");
$this->app->ActionHandler("create", "PLACEHOLDER_EDIT"); // This automatically adds a "New" button
$this->app->ActionHandler("edit", "PLACEHOLDER_EDIT");
$this->app->ActionHandler("delete", "PLACEHOLDER_DELETE");
$this->app->DefaultActionHandler("list");
$this->app->ActionHandlerListen($app);
}
public function Install() {
/* Fill out manually later */
}
static function TableSearch(&$app, $name, $erlaubtevars) {
switch ($name) {
case "PLACEHOLDER_LIST":
$allowed['PLACEHOLDER_LIST'] = array('list');
$heading = array(PLACEHOLDER_COLUMNS, 'Men&uuml;');
$width = array('10%'); // Fill out manually later
$findcols = array(PLACEHOLDER_COLUMNS);
$searchsql = array(PLACEHOLDER_COLUMNS);
$defaultorder = 1;
$defaultorderdesc = 0;
$menu = "<table cellpadding=0 cellspacing=0><tr><td nowrap>" . "<a href=\"index.php?module=PLACEHOLDER_MODULENAME&action=edit&id=%value%\"><img src=\"./themes/{$app->Conf->WFconf['defaulttheme']}/images/edit.png\" border=\"0\"></a>&nbsp;<a href=\"#\" onclick=DeleteDialog(\"index.php?module=PLACEHOLDER_MODULENAME&action=delete&id=%value%\");>" . "<img src=\"themes/{$app->Conf->WFconf['defaulttheme']}/images/delete.svg\" border=\"0\"></a>" . "</td></tr></table>";
$sql = "PLACEHOLDER_SQL_LIST";
$where = "1
";
// $groupby = "";
$count = "SELECT count(DISTINCT id) FROM PLACEHOLDER_MODULENAME WHERE $where";
break;
}
$erg = false;
foreach ($erlaubtevars as $k => $v) {
if (isset($$v))
$erg[$v] = $$v;
}
return $erg;
}
function PLACEHOLDER_LIST() {
$this->app->erp->MenuEintrag("index.php?module=PLACEHOLDER_MODULENAME&action=list", "&Uuml;bersicht");
$this->app->erp->MenuEintrag("index.php?module=PLACEHOLDER_MODULENAME&action=create", "Neu anlegen");
$this->app->erp->MenuEintrag("index.php", "Zur&uuml;ck");
$this->app->YUI->TableSearch('TAB1', 'PLACEHOLDER_LIST', "show", "", "", basename(__FILE__), __CLASS__);
$this->app->Tpl->Parse('PAGE', "PLACEHOLDER_MODULENAME_list.tpl");
}
public function PLACEHOLDER_DELETE() {
$id = (int) $this->app->Secure->GetGET('id');
$this->app->DB->Delete("DELETE FROM `PLACEHOLDER_MODULENAME` WHERE `id` = '{$id}'");
$this->app->Tpl->Set('MESSAGE', "<div class=\"error\">Der Eintrag wurde gel&ouml;scht.</div>");
$this->PLACEHOLDER_LIST();
}
/*
* Edit PLACEHOLDER_MODULENAME item
* If id is empty, create a new one
*/
function PLACEHOLDER_EDIT() {
$id = $this->app->Secure->GetGET('id');
$this->app->Tpl->Set('ID', $id);
$this->app->erp->MenuEintrag("index.php?module=PLACEHOLDER_MODULENAME&action=edit&id=$id", "Details");
$this->app->erp->MenuEintrag("index.php?module=PLACEHOLDER_MODULENAME&action=list", "Zur&uuml;ck zur &Uuml;bersicht");
$id = $this->app->Secure->GetGET('id');
$input = $this->GetInput();
$submit = $this->app->Secure->GetPOST('submit');
if (empty($id)) {
// New item
$id = 'NULL';
}
if ($submit != '')
{
// Write to database
// Add checks here
$columns = "id, ";
$values = "$id, ";
$update = "";
$fix = "";
foreach ($input as $key => $value) {
$columns = $columns.$fix.$key;
$values = $values.$fix."'".$value."'";
$update = $update.$fix.$key." = '$value'";
$fix = ", ";
}
// echo($columns."<br>");
// echo($values."<br>");
// echo($update."<br>");
$sql = "INSERT INTO PLACEHOLDER_MODULENAME (".$columns.") VALUES (".$values.") ON DUPLICATE KEY UPDATE ".$update;
echo($sql);
$this->app->DB->Update($sql);
if ($id == 'NULL') {
$msg = $this->app->erp->base64_url_encode("<div class=\"success\">Das Element wurde erfolgreich angelegt.</div>");
header("Location: index.php?module=PLACEHOLDER_MODULENAME&action=list&msg=$msg");
} else {
$this->app->Tpl->Set('MESSAGE', "<div class=\"success\">Die Einstellungen wurden erfolgreich &uuml;bernommen.</div>");
}
}
// Load values again from database
$result = $this->app->DB->SelectArr("PLACEHOLDER_SQL_LIST"." WHERE id=$id");
foreach ($result[0] as $key => $value) {
$this->app->Tpl->Set(strtoupper($key), $value);
}
/*
* Add displayed items later
*
$this->app->Tpl->Add('KURZUEBERSCHRIFT2', $email);
$this->app->Tpl->Add('EMAIL', $email);
$this->app->Tpl->Add('ANGEZEIGTERNAME', $angezeigtername);
*/
// $this->SetInput($input);
$this->app->Tpl->Parse('PAGE', "PLACEHOLDER_MODULENAME_edit.tpl");
}
/**
* Get all paramters from html form and save into $input
*/
public function GetInput(): array {
$input = array();
//$input['EMAIL'] = $this->app->Secure->GetPOST('email');
PLACEHOLDER_GET_INPUT
return $input;
}
/*
* Set all fields in the page corresponding to $input
*/
function SetInput($input) {
// $this->app->Tpl->Set('EMAIL', $input['email']);
PLACEHOLDER_SET_INPUT
}
}