|
|
|
@ -11,6 +11,14 @@ declare(strict_types=1); |
|
|
|
namespace CodeHydrater; |
|
|
|
namespace CodeHydrater; |
|
|
|
|
|
|
|
|
|
|
|
final class html { |
|
|
|
final class html { |
|
|
|
|
|
|
|
const ECHO = true; |
|
|
|
|
|
|
|
public static function do_echo(?string $input, bool $echo = self::ECHO): ?string { |
|
|
|
|
|
|
|
if ($echo) { |
|
|
|
|
|
|
|
echo $input; |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return $input; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Purpose: To output all HTML select (drop down) option values. |
|
|
|
* Purpose: To output all HTML select (drop down) option values. |
|
|
|
@ -53,6 +61,25 @@ final class html { |
|
|
|
return $values; |
|
|
|
return $values; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static function make_headers_on_table(array $header_fields, array $options = []) { |
|
|
|
|
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
|
|
|
|
$nl = PHP_EOL; |
|
|
|
|
|
|
|
$default_table_setup = "border='1' cellpadding='1' cellspacing='1'"; |
|
|
|
|
|
|
|
$table = $options['table_setup'] ?? $default_table_setup; |
|
|
|
|
|
|
|
$escape = $options['escape'] ?? true; |
|
|
|
|
|
|
|
$th = $options['th'] ?? ""; |
|
|
|
|
|
|
|
$th_class = (! empty($th)) ? " class=\"$th\" " : ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$ret = self::do_echo("{$nl}<table {$table}>{$nl}", $echo); |
|
|
|
|
|
|
|
$ret .= self::do_echo("\t<tr>{$nl}", $echo); |
|
|
|
|
|
|
|
foreach($header_fields as $header_field) { |
|
|
|
|
|
|
|
$field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field; |
|
|
|
|
|
|
|
$ret .= self::do_echo("\t\t<th{$th_class}>{$field}</th>{$nl}", $echo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$ret .= self::do_echo("\t</tr>{$nl}", $echo); |
|
|
|
|
|
|
|
return $ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Used by Memory_Usage script. |
|
|
|
* Used by Memory_Usage script. |
|
|
|
* Displays a table from input arrays -- fields |
|
|
|
* Displays a table from input arrays -- fields |
|
|
|
@ -64,70 +91,118 @@ final class html { |
|
|
|
public static function show_table( |
|
|
|
public static function show_table( |
|
|
|
array $header_fields, |
|
|
|
array $header_fields, |
|
|
|
array $fields, |
|
|
|
array $fields, |
|
|
|
bool $escape = true |
|
|
|
array $options = [] |
|
|
|
): void { |
|
|
|
): ?string { |
|
|
|
|
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
$nl = PHP_EOL; |
|
|
|
$nl = PHP_EOL; |
|
|
|
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}"; |
|
|
|
$ret = self::make_headers_on_table($header_fields, $options); |
|
|
|
echo "\t<tr>{$nl}"; |
|
|
|
$escape = $options['escape'] ?? true; |
|
|
|
foreach($header_fields as $header_field) { |
|
|
|
|
|
|
|
$field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field; |
|
|
|
|
|
|
|
echo "\t\t<th>{$field}</th>{$nl}"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
echo "\t</tr>{$nl}"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach($fields as $field) { |
|
|
|
foreach($fields as $field) { |
|
|
|
echo "\t<tr>{$nl}"; |
|
|
|
$ret .= self::do_echo("\t<tr>{$nl}", $echo); |
|
|
|
foreach($field as $td) { |
|
|
|
foreach($field as $td) { |
|
|
|
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; |
|
|
|
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; |
|
|
|
echo "\t\t<td>{$cell}</td>{$nl}"; |
|
|
|
$ret .= self::do_echo("\t\t<td>{$cell}</td>{$nl}", $echo); |
|
|
|
} |
|
|
|
} |
|
|
|
echo "\t</tr>{$nl}"; |
|
|
|
$ret .= self::do_echo("\t</tr>{$nl}", $echo); |
|
|
|
} |
|
|
|
} |
|
|
|
echo "</table>{$nl}"; |
|
|
|
$ret .= self::do_echo("</table>{$nl}", $echo); |
|
|
|
|
|
|
|
return $ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
private static function make_cell_table_helper_for_IO(\Iterator $record, array $options = []): array { |
|
|
|
* Generators use Memory in an Effient way!!!! So use this. |
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
* @param array $header_fields |
|
|
|
$escape = $options['escape'] ?? true; |
|
|
|
* @param array $db_field_names |
|
|
|
$cell_class = $options['cell_class'] ?? ""; |
|
|
|
* @param \Iterator $records |
|
|
|
$row_class = $options['row_class'] ?? ""; |
|
|
|
* @param bool $escape |
|
|
|
$cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : ""; |
|
|
|
* @return void |
|
|
|
$row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : ""; |
|
|
|
*/ |
|
|
|
$nl = PHP_EOL; |
|
|
|
public static function show_table_from_generator( |
|
|
|
$ret = self::do_echo("\t<tr{$row_class_tag}>{$nl}", $echo); |
|
|
|
array $header_fields, |
|
|
|
|
|
|
|
\Iterator $records, |
|
|
|
$errors = []; |
|
|
|
bool $escape = true |
|
|
|
foreach($record as $html) { |
|
|
|
): void { |
|
|
|
$key = $html['name'] ?? ""; |
|
|
|
$nl = PHP_EOL; |
|
|
|
$value = $html['html']; |
|
|
|
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}"; |
|
|
|
|
|
|
|
echo "\t<tr>{$nl}"; |
|
|
|
if (\CodeHydrater\common::get_count($html['errors'])) { |
|
|
|
foreach($header_fields as $header_field) { |
|
|
|
$errors[$key] = $html['errors'][$key]; |
|
|
|
$field = ($escape) ? \bs_tts\safer_io::h($header_field) : $header_field; |
|
|
|
} |
|
|
|
echo "\t\t<th>{$field}</th>{$nl}"; |
|
|
|
|
|
|
|
} |
|
|
|
$td = $value ?? ""; |
|
|
|
echo "\t</tr>{$nl}"; |
|
|
|
if (is_string($td)) { |
|
|
|
|
|
|
|
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$cell = (string) $td; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$ret .= self::do_echo("\t</tr>{$nl}", $echo); |
|
|
|
|
|
|
|
return ['errors' => $errors, 'data' => $ret]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
foreach($records as $record) { |
|
|
|
private static function make_cell_table_helper(array $record, array $options = []): ?string { |
|
|
|
echo "\t<tr>{$nl}"; |
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
|
|
|
|
$escape = $options['escape'] ?? true; |
|
|
|
|
|
|
|
$cell_class = $options['cell_class'] ?? ""; |
|
|
|
|
|
|
|
$row_class = $options['row_class'] ?? ""; |
|
|
|
|
|
|
|
$cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : ""; |
|
|
|
|
|
|
|
$row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : ""; |
|
|
|
|
|
|
|
$nl = PHP_EOL; |
|
|
|
|
|
|
|
$ret = self::do_echo("\t<tr{$row_class_tag}>{$nl}", $echo); |
|
|
|
foreach($record as $key => $value) { |
|
|
|
foreach($record as $key => $value) { |
|
|
|
if (! is_string($key)) { |
|
|
|
if (! is_string($key)) { |
|
|
|
continue; // Remove Duplicate records |
|
|
|
continue; // Remove Duplicate records |
|
|
|
} |
|
|
|
} |
|
|
|
$td = $value ?? ""; |
|
|
|
$td = $value ?? ""; |
|
|
|
if (is_string($td)) { |
|
|
|
if (is_string($td)) { |
|
|
|
$cell = ($escape) ? \bs_tts\safer_io::h($td) : $td; |
|
|
|
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$cell = (string) $td; |
|
|
|
$cell = (string) $td; |
|
|
|
} |
|
|
|
} |
|
|
|
echo "\t\t<td>{$cell}</td>{$nl}"; |
|
|
|
$ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo); |
|
|
|
} |
|
|
|
} |
|
|
|
echo "\t</tr>{$nl}"; |
|
|
|
$ret .= self::do_echo("\t</tr>{$nl}", $echo); |
|
|
|
|
|
|
|
return $ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Generators use Memory in an Effient way!!!! So use this. |
|
|
|
|
|
|
|
* @param array $header_fields |
|
|
|
|
|
|
|
* @param array $db_field_names |
|
|
|
|
|
|
|
* @param $records, note: \Iterator not always can be used here |
|
|
|
|
|
|
|
* @param bool $escape |
|
|
|
|
|
|
|
* @return void |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function show_table_from( |
|
|
|
|
|
|
|
array $header_fields, |
|
|
|
|
|
|
|
$records, |
|
|
|
|
|
|
|
array $options = [], |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
|
|
|
|
$ret = self::make_headers_on_table($header_fields, $options); |
|
|
|
|
|
|
|
switch($options['helper']) { |
|
|
|
|
|
|
|
case "single-row": |
|
|
|
|
|
|
|
$ret .= self::make_cell_table_helper($records, $options); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case "io": |
|
|
|
|
|
|
|
$ret = self::make_cell_table_helper_for_IO($records, $options); |
|
|
|
|
|
|
|
$ret['data'] .= self::do_echo($ret['data'] . "</table>" . PHP_EOL); |
|
|
|
|
|
|
|
return $ret; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
foreach($records as $record) { |
|
|
|
|
|
|
|
self::make_cell_table_helper($record, $options); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
echo "</table>{$nl}"; |
|
|
|
$ret .= self::do_echo("</table>" . PHP_EOL, $echo); |
|
|
|
|
|
|
|
return $ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function show_errors(?array $errors = []): void { |
|
|
|
public static function show_errors( |
|
|
|
|
|
|
|
?array $errors = [], |
|
|
|
|
|
|
|
array $options = [] |
|
|
|
|
|
|
|
): ?string { |
|
|
|
|
|
|
|
$echo = $options['echo'] ?? self::ECHO; |
|
|
|
|
|
|
|
$ret = ""; |
|
|
|
if ($errors) { |
|
|
|
if ($errors) { |
|
|
|
$message = "Please correct the following errors: "; |
|
|
|
$message = "Please correct the following errors: "; |
|
|
|
$i = 0; |
|
|
|
$i = 0; |
|
|
|
@ -135,7 +210,8 @@ final class html { |
|
|
|
$i++; |
|
|
|
$i++; |
|
|
|
$message .= "<br/>{$i}) " . $error; |
|
|
|
$message .= "<br/>{$i}) " . $error; |
|
|
|
} |
|
|
|
} |
|
|
|
echo $message; |
|
|
|
$ret .= self::do_echo($message, $echo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $ret; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |