Setup helper classes to handle stroring and formatting data retrieved from the DB, and implemented them in get method of table.php

This commit is contained in:
Tristan Ancelet 2023-05-27 14:27:46 -05:00
parent 849d7d27e0
commit 8b5782c5de
2 changed files with 173 additions and 55 deletions

117
Libraries/table/class.php Normal file
View File

@ -0,0 +1,117 @@
<?php
class Table_Cell {
private $formats = array(
"Normal" => "<div class=\"table_cell\">%s</div>",
"Header" => "<div class=\"table_header\">%s</div>"
);
private $type;
private $value;
public function __construct($value, $type){
$this->set_value($value);
$this->set_type($type);
}
public function set_type($type){
if (array_key_exists($type, $this->formats)){
$this->type = $type;
} else {
exit("The type provided ({$type}) is not defined. ");
}
}
public function set_value($value){
$this->value = $value;
}
public function get_format(){
return $this->formats[$this->type];
}
public function get_html(){
return sprintf($this->formats[$this->type], $this->value);
}
}
class Table_Row {
private $cells = array();
public function add_cell($value, $type){
$cell = new Table_Cell($value, $type);
array_push($this->cells, $cell);
return $cell;
}
public function get_html(){
$output="";
$output="$output<div class=\"table_row\">";
foreach ($this->cells as $cell){
$output="{$output}{$cell->get_html()}";
}
$output="{$output}</div>";
return $output;
}
}
class Table {
private $name;
private $headers;
private $rows = array();
public function __construct($name){
$this->set_name($name);
}
private function add_row(){
$row = new Table_Row();
array_push($this->rows, $row);
return $row;
}
public function set_name($name){
$this->name = $name;
}
public function Load($db, $query_modifier){
$res = $db->query("SELECT * FROM $this->name $query_modifier");
// Getting header row
$col_count=0;
$header_row = $this->add_row();
for ($i = 0; $i < $res->numColumns(); $i++ ){
$value = $res->columnName($i);
$header_row->add_cell($value, "Header");
$col_count++;
}
while ($db_row = $res->fetchArray()){
$row = $this->add_row();
for ($i = 0; $i < $col_count; $i++){
$value = $db_row[$i];
$row->add_cell($value, "Normal");
}
}
}
public function get_html(){
$output="";
$output="{$output}<div class=\"wrapper bordered center_text\">";
$output="{$output}<div class=\"log_table bordered\">";
foreach ($this->rows as $row){
$row_html = $row->get_html();
$output="{$output}{$row_html}";
}
$output="{$output}</div>";
$output="{$output}</div>";
return $output;
}
}
?>

View File

@ -1,4 +1,6 @@
<?php <?php
require("class.php");
function get_main () { function get_main () {
$db = $GLOBALS["db"]; $db = $GLOBALS["db"];
/* /*
@ -18,77 +20,76 @@ function get_main () {
// END: Getting Args if exists // END: Getting Args if exists
switch ($action){
if ($action == "show"){ case "show":
if ( ! array_key_exists("name", $_GET) ){ // BEGIN: Get show args
echo "A tablename was not provided with the request"; if ( ! array_key_exists("name", $_GET) ){
return 1; echo "A tablename was not provided with the request";
} else { return 1;
$name=$_GET['name']; } else {
} $name=$_GET['name'];
if ( array_key_exists("limit", $_GET)){
$limit=$_GET['limit'];
} else {
$limit=0;
}
if ( array_key_exists("columns", $_GET)){
$columns = explode(',', $_GET["columns"]);
}
$query_modifier="";
if (filter_var($limit, FILTER_VALIDATE_INT)){
if ($limit > 0){
$query_modifier="WHERE LIMIT $limit";
}
}else {
$query_modifier="";
} }
$db = new SQLite3("../../webcron.db"); if ( array_key_exists("limit", $_GET)){
$limit=$_GET['limit'];
} else {
$limit=0;
}
$res = $db->query("SELECT * FROM $name $query_modifier"); if ( array_key_exists("columns", $_GET)){
echo '<div class="wrapper bordered center_text">'; $columns = explode(',', $_GET["columns"]);
echo '<div class="log_table bordered">'; }
echo '<div class="table_row">'; // END: Get show args
if ( !empty($columns) ) { $query_modifier="";
foreach($columns as $column_name){ if (filter_var($limit, FILTER_VALIDATE_INT)){
echo "<div class=\"table_header\">{$column_name}</div>"; if ($limit > 0){
} $query_modifier="WHERE LIMIT $limit";
} else { }
$counter=0; }else {
for ($i = 0; $i < $res->numColumns(); $i++ ){ $query_modifier="";
echo "<div class=\"table_header\">{$res->columnName($i)}</div>";
$counter++;
}
} }
$res = $db->query("SELECT * FROM $name $query_modifier");
echo '</div>'; echo '<div class="wrapper bordered center_text">';
while ($row = $res->fetchArray()){ echo '<div class="log_table bordered">';
echo "<div class=\"table_row\">"; echo '<div class="table_row">';
if ( !empty($columns) ) { if ( !empty($columns) ) {
foreach($columns as $column_name){ foreach($columns as $column_name){
echo "<div class=\"table_cell\">{$row[$column_name]}</div>"; echo "<div class=\"table_header\">{$column_name}</div>";
} }
} else { } else {
for ($i = 0; $i < $counter; $i++){ $counter=0;
echo "<div class=\"table_cell\">{$row[$i]}</div>"; for ($i = 0; $i < $res->numColumns(); $i++ ){
echo "<div class=\"table_header\">{$res->columnName($i)}</div>";
$counter++;
} }
} }
echo "</div>";
}
echo '</div>';
echo '</div>';
} else if ($action == "list"){
$res = $db->query("SELECT name FROM sqlite_master WHERE type='table';");
echo '</div>';
while ($row = $res->fetchArray()){
echo "<div class=\"table_row\">";
if ( !empty($columns) ) {
foreach($columns as $column_name){
echo "<div class=\"table_cell\">{$row[$column_name]}</div>";
}
} else {
for ($i = 0; $i < $counter; $i++){
echo "<div class=\"table_cell\">{$row[$i]}</div>";
}
}
echo "</div>";
}
echo '</div>';
echo '</div>';
break;
case "list":
$res = $db->query("SELECT name FROM sqlite_master WHERE type='table';");
while ($row = $res->fetchArray()){ while ($row = $res->fetchArray()){
echo "<button onclick='getTable(\"{$row['name']}\")'>{$row['name']}</button><br> "; echo "<button onclick='getTable(\"{$row['name']}\")'>{$row['name']}</button><br> ";
} }
break;
} }
} }