<!DOCTYPE html>
<!--
********************************************************************************

    filename:    burnbotdbdoc.php
    description: database table fields
    inputs:       none
    outputs:      none
    actions:      none
    redirections: none 

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
        <style>
            table {
                border-collapse: collapse;
            }

            table, td, th {
                border: 1px solid black;
                text-align: justify;
            }
.center {
    margin: auto;
    width: 100%;
    padding: 10px;
        </style>
        <title>Polycentric Systems</title>
    </head>
    <body>
        <?php include 'navigationmanageswdocs.html' ?>
        <h3>BURNBOT DATABASE TABLES</h3>
        <div class=center>
        <?php
/*
            ... get constants as name/value pairs
*/
            $lines = file("functions.php");
            foreach($lines as $line) {
                if(strpos($line,"define") !== false) {
                    $line = trim($line);
                    $startName = strpos($line,'"');
                    $endName = strpos(substr($line, $startName + 1), '"') + $startName + 1;
                    $startValue = strpos(substr($line, $endName + 1), '"') + $endName + 1;
                    $endValue = strpos(substr($line, $startValue + 1), '"') + $startValue + 1;
                    $name = substr($line, $startName + 1, $endName - $startName - 1);
                    $value = substr($line, $startValue + 1, $endValue - $startValue - 1);
                    $constants[$name] = $value;
                }
            }
            $dir = getcwd();
            if (!is_dir($dir)){
                echo "not directory";
            } elseif (!$pdir= opendir($dir)){
                echo "failed to open directory";
            } else {
//               $csvFile = fopen("docdb.csv", "w");
               $csv = "TABLE,FIELD,TYPE/SIZE/VALUES,SIGNED/UNSIGNED,NULL/NOT NULL,AUTO_INCREMENT,PRIMARY KEY,COMMENT\n";
//               fwrite($csvFile, $csv);
               echo "<table>";
                   echo"<tr>";
                       echo "<th>TABLE</th>";
                       echo "<th>FIELD</th>";
                       echo "<th>TYPE/SIZE/VALUES</th>";
                       echo "<th>SIGNED/UNSIGNED</th>";
                       echo "<th>NULL/NOT NULL</th>";
                       echo "<th>AUTO_INCREMENT</th>";
                       echo "<th>PRIMARY KEY</th>";
                       echo "<th>COMMENT</th>";
                   echo "</tr>";
                   $firstField = false;
                   $files = array();
                   while (($file = readdir($pdir)) !== false) {
                       array_push($files, $file);
                   }
                   sort($files);
                   foreach($files as $file) {
                       $init = strpos($file, "init");
                       $x = strpos($file, "x");
                       if ($init !== false and $x === false) {
                           $lines = file($dir . "/" . $file);
                           foreach($lines as $line) {
                               echo"<tr>";
                               $line = trim($line); // remove whitespaces on both ends of line
                               $csv = "";
/*
                               ... TABLE
*/
                               $createTable = "CREATE TABLE";
                               $createTableLen = strlen($createTable);
                               $createTablePos = strpos($line, $createTable);
                               if ($createTablePos !== false) {
                                   $firstField = true;
                                   $dbTable = substr($line, $createTablePos + $createTableLen + 1, -3);
/*
                               ... FIELD
*/
                               } elseif ($dbTable != "") {
                                   if (substr($line,0,3) == '. "') {
                                       $line = substr($line,3);
                                   }
                                   foreach($constants as $constant => $value) {
                                       if(!strpos($line, $constant)) {
                                       } else {
                                         $line = $value;
                                       }
                                   }
                                   $blankPos = strpos($line, " ");
                                   $field = substr($line, 0, $blankPos);
                                   if ($firstField) {
                                       $csv = $dbTable . "," . $field;
                                       $firstField = false;
                                       echo "<th>" . $dbTable . "</th>";
                                       echo "<th>" . $field . "</th>";
                                   } else {
                                       $csv = "," . $field;
                                   echo "<th></th>";
                                   echo "<th>" . $field . "</th>";
                                   }
/*
                                   ... INT/FLOAT/ENUM/VARCHAR/DATETIME
*/
                                   if (strpos($line, "INT") !== false) {
                                       $type = "INT";
                                   } elseif (strpos($line, "FLOAT") !== false) {
                                       $type = "FLOAT";
                                   } elseif (strpos($line, "ENUM") !== false) {
                                       $leftParenPos = strpos($line, "(");
                                       $rightParenPos = strpos($line, ")");
                                       $len = $rightParenPos - $leftParenPos + 1;
                                       $enumContents = substr($line, $leftParenPos, $len);
                                       $type = 'ENUM' . $enumContents;
                                   } elseif (strpos($line, "VARCHAR(256)") !== false) {
                                       $type = "VARCHAR(256)";
                                   } elseif (strpos($line, "DATETIME") !== false) {
                                       $type = "DATETIME";
                                   } else {
                                       $type = "";
                                   }
                                   $csv = $csv . "," .  $type;
                                   echo "<th>" . $type . "</th>";
/*
                                   ... SIGNED/UNSIGNED
*/
                                   if (strpos($line, "UNSIGNED") !== false) {
                                       $signed = "UNSIGNED";
                                   } elseif (strpos($line, "SIGNED") !== false) {
                                       $signed = "SIGNED";
                                   } else {
                                       $signed = "";
                                   }
                                   $csv = $csv . "," .  $signed;
                                   echo "<th>" . $signed . "</th>";
/*
                                   ... NULL/NOT NULL
*/
                                   if (strpos($line, "NOT NULL") !== false) {
                                       $null = "NOT NULL";
                                   } elseif (strpos($line, "NULL") !== false) {
                                       $null = "NULL";
                                   } else {
                                       $null = "";
                                   }
                                   $csv = $csv . "," .  $null;
                                   echo "<th>" . $null . "</th>";
/*
                                   ... AUTO INCREMENT
*/
                                   if (strpos($line, "AUTO_INCREMENT") !== false) {
                                       $autoIncrement = "AUTO_INCREMENT";
                                   } else {
                                       $autoIncrement = "";
                                   }
                                   $csv = $csv . "," .  $autoIncrement;
                                   echo "<th>" . $autoIncrement . "</th>";
/*
                                   ... PRIMARY KEY
*/
                                   if (strpos($line, "PRIMARY KEY") !== false) {
                                       $primaryKey = "PRIMARY KEY";
                                   } else {
                                       $primaryKey = "";
                                   }
                                   $csv = $csv . "," .  $primaryKey;
                                   echo "<th>" . $primaryKey . "</th>";
/*
                                   ... COMMENT
*/
                                   if ($strpos = strpos($line, "//")) {
                                       $comment = substr($line, $strpos + 2);
                                   } else{
                                       $comment = "";
                                   }
                                   $csv = $csv . "," .  '"' . $comment . '"';
                                   echo "<th>" . $comment . "</th>";
/*
                                   ... write line to csv file
*/
                                   $csv = $csv . "\n";
//                                   fwrite($csvFile, $csv);

                                   if (strpos($line, ";") !== false) {
                                       $dbTable = "";
                                   }
                               }
                           }
                       }
                       echo "</tr>";
                   }
//                   fclose($csvFile);
                   echo "</table>";
               }
          ?>
    </body>
</html>