Skip to content

Commit

Permalink
Replaced index startpage (with buttons) for a persistent NavBar on top.
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vandenberg committed Mar 14, 2022
1 parent 02883e1 commit c79a978
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 51 deletions.
56 changes: 34 additions & 22 deletions core/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$sort = '';
$excluded_keys = array('singlebutton', 'keep_startpage', 'append_links');
$generate_start_checked_links = array();
$startpage_filename = "app/index.php";
$startpage_filename = "app/navbar.php";
$forced_deletion = false;
$buttons_delimiter = '<!-- TABLE_BUTTONS -->';

Expand Down Expand Up @@ -69,8 +69,17 @@ function generate_error(){
echo "Generating Error file<br>";
}

function generate_start($tablename, $start_page, $keep_startpage, $append_links, $td){
global $startfile;
function generate_startpage(){
global $startfile;
if (!file_put_contents("app/index.php", $startfile, LOCK_EX)) {
die("Unable to open file!");
}
echo "Generating main index file.<br>";

}

function generate_navbar($tablename, $start_page, $keep_startpage, $append_links, $td){
global $navbarfile;
global $generate_start_checked_links;
global $startpage_filename;

Expand All @@ -81,43 +90,43 @@ function generate_start($tablename, $start_page, $keep_startpage, $append_links,
if (!file_exists($startpage_filename)) {
// called on the first run of the POST loop
echo "Generating fresh Startpage file<br>";
$step0 = str_replace("{TABLE_BUTTONS}", $start_page, $startfile);
$step0 = str_replace("{TABLE_BUTTONS}", $start_page, $navbarfile);
if (!file_put_contents($startpage_filename, $step0, LOCK_EX)) {
die("Unable to open fresh startpage file!");
}
} else {
// called on subsequent runs of the POST loop
echo "Populating Startpage file<br>";
$startfile = file_get_contents($startpage_filename);
if (!$startfile) {
$navbarfile = file_get_contents($startpage_filename);
if (!$navbarfile) {
die("Unable to open existing startpage file!");
}
append_links_to_startpage($startfile, $start_page, $startpage_filename, $generate_start_checked_links,$td);
append_links_to_navbar($navbarfile, $start_page, $startpage_filename, $generate_start_checked_links,$td);
}
} else {
if ($append_links) {
// load existing template
echo "Retrieving existing Startpage file<br>";
$startfile = file_get_contents($startpage_filename);
if (!$startfile) {
$navbarfile = file_get_contents($startpage_filename);
if (!$navbarfile) {
die("Unable to open existing startpage file!");
}
append_links_to_startpage($startfile, $start_page, $startpage_filename, $generate_start_checked_links,$td);
append_links_to_navbar($navbarfile, $start_page, $startpage_filename, $generate_start_checked_links,$td);
}
}
}

function append_links_to_startpage($startfile, $start_page, $startpage_filename, $generate_start_checked_links, $td) {
function append_links_to_navbar($navbarfile, $start_page, $startpage_filename, $generate_start_checked_links, $td) {
global $buttons_delimiter;

// extract existing links from app/index.php
echo "Looking for new link to append to Startpage file<br>";
$startfile_appended = $startfile;
$navbarfile_appended = $navbarfile;
$link_matcher_pattern = '/href=["\']?([^"\'>]+)["\']?/im';
preg_match_all($link_matcher_pattern, $startfile, $startfile_links);
if (count($startfile_links)) {
foreach($startfile_links[1] as $startfile_link) {
// echo '- Found existing link '.$startfile_link.'<br>';
preg_match_all($link_matcher_pattern, $navbarfile, $navbarfile_links);
if (count($navbarfile_links)) {
foreach($navbarfile_links[1] as $navbarfile_link) {
// echo '- Found existing link '.$navbarfile_link.'<br>';
}
}

Expand All @@ -126,13 +135,13 @@ function append_links_to_startpage($startfile, $start_page, $startpage_filename,
if (count($start_page_links)) {
foreach($start_page_links[1] as $start_page_link) {
if (!in_array($start_page_link, $generate_start_checked_links)) {
if (in_array($start_page_link, $startfile_links[1])) {
if (in_array($start_page_link, $navbarfile_links[1])) {
echo '- Not appending '.$start_page_link.' as it already exists<br>';
} else {
echo '- Appending '.$start_page_link.'<br>';
array_push($startfile_links[1], $start_page_link);
$button_string = "\t".'<a href="'.$start_page_link.'" class="btn btn-primary" role="button">'.$td.'</a>'."\n\t".$buttons_delimiter;
$step0 = str_replace($buttons_delimiter, $button_string, $startfile);
array_push($navbarfile_links[1], $start_page_link);
$button_string = "\t".'<a class="dropdown-item" href="'.$start_page_link.'">'.$td.'</a>'."\n\t".$buttons_delimiter;
$step0 = str_replace($buttons_delimiter, $button_string, $navbarfile);
if (!file_put_contents($startpage_filename, $step0, LOCK_EX)) {
die("Unable to open file!");
}
Expand Down Expand Up @@ -539,7 +548,9 @@ function generate($postdata) {

foreach($tables as $key => $value) {
//echo "$key is at $value";
$start_page .= '<a href="'. $key . '-index.php" class="btn btn-primary" role="button">'. $value. '</a> ';
//$start_page .= '<a href="'. $key . '-index.php" class="btn btn-primary" role="button">'. $value. '</a> ';
//$button_string = "\t".'<a class="dropdown-item" href="'.$start_page_link.'">'.$td.'</a>'."\n\t".$buttons_delimiter;
$start_page .= '<a href="'. $key . '-index.php" class="dropdown-item">'. $value. '</a> ';
$start_page .= "\n\t";
}

Expand All @@ -558,8 +569,9 @@ function generate($postdata) {
echo '<br>';
}

generate_start($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_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);
Expand Down
79 changes: 50 additions & 29 deletions core/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
</style>
</head>
<?php require_once('navbar.php'); ?>
<body>
<section class="pt-5">
<div class="container-fluid">
Expand Down Expand Up @@ -243,6 +244,7 @@
<title>View Record</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<?php require_once('navbar.php'); ?>
<body>
<section class="pt-5">
<div class="container-fluid">
Expand Down Expand Up @@ -321,6 +323,7 @@
<title>View Record</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<?php require_once('navbar.php'); ?>
<body>
<section class="pt-5">
<div class="container-fluid">
Expand Down Expand Up @@ -398,6 +401,7 @@
<title>Create Record</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<?php require_once('navbar.php'); ?>
<body>
<section class="pt-5">
<div class="container-fluid">
Expand Down Expand Up @@ -529,6 +533,7 @@
<title>Update Record</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<?php require_once('navbar.php'); ?>
<body>
<section class="pt-5">
<div class="container-fluid">
Expand Down Expand Up @@ -586,34 +591,50 @@
EOT;

$startfile = <<<'EOT'
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select CRUD pages</title>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cruddiy Admin</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style type="text/css">
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 5px;
}
</style>
</head>
<body>
<fieldset>
<center>
<legend>Available CRUD pages</legend>
<div class="form-group">
{TABLE_BUTTONS}
<!-- TABLE_BUTTONS -->
</div>
</center>
</fieldset>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<style type="text/css">
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 5px;
}
</style>
</head>
<?php require_once('navbar.php'); ?>
</html>
EOT;

$navbarfile = <<<'EOT'
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand nav-link disabled" href="#">Admin</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Page
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{TABLE_BUTTONS}
<!-- TABLE_BUTTONS -->
</div>
</li>
</ul>
</div>
</nav>
EOT;


0 comments on commit c79a978

Please sign in to comment.