function render_table($column_to_be_ordered_by) { $column_names = array_keys($this->column_info); $column_to_subcolumn_names = array(); foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } $subcolumns = array_get($this->column_info[$name], 'subcolumns', array()); if (!$subcolumns || !array_get($this->column_info[$name], 'custom')) { continue; } $column_to_subcolumn_names[$name] = $subcolumns; } $rowspan_if_needed = $column_to_subcolumn_names ? ' rowspan="2"' : ''; $headers = "<tr><td{$rowspan_if_needed}>ID</td>"; foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } $label = htmlspecialchars($this->column_label($name)); if (array_get($column_to_subcolumn_names, $name)) { $count = count($column_to_subcolumn_names[$name]); $headers .= "<td colspan=\"{$count}\">{$label}</td>"; } else { $headers .= "<td{$rowspan_if_needed}>{$label}</td>"; } } $headers .= "</tr>\n"; if ($column_to_subcolumn_names) { $headers .= '<tr>'; foreach ($column_names as $name) { $subcolumn_names = array_get($column_to_subcolumn_names, $name); if (!$subcolumn_names) { continue; } foreach ($subcolumn_names as $label) { $headers .= '<td>' . htmlspecialchars($label) . '</td>'; } } $headers .= "</tr>\n"; } echo <<<END <table> <thead> {$headers} </thead> <tbody> END; assert(ctype_alnum_underscore($column_to_be_ordered_by)); $rows = $this->db->fetch_table($this->table, $this->prefix . '_' . $column_to_be_ordered_by); if ($rows) { foreach ($rows as $row) { $id = intval($row[$this->prefix . '_id']); $custom_cells_list = array(); $maximum_rows = 1; foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } if ($custom = array_get($this->column_info[$name], 'custom')) { $custom_cells_list[$name] = $custom($row); $maximum_rows = max($maximum_rows, count($custom_cells_list[$name])); } } $rowspan_if_needed = $maximum_rows > 1 ? ' rowspan="' . $maximum_rows . '"' : ''; echo "<tr>\n<td{$rowspan_if_needed}>{$id}</td>\n"; foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } if (array_key_exists($name, $custom_cells_list)) { $this->render_custom_cells(array_get($column_to_subcolumn_names, $name), $custom_cells_list[$name], 0); continue; } $value = htmlspecialchars(array_get($row, $this->prefix . '_' . $name), ENT_QUOTES); $editing_mode = array_get($this->column_info[$name], 'editing_mode'); if (!$editing_mode) { echo "<td{$rowspan_if_needed}>{$value}</td>\n"; continue; } echo <<<END <td{$rowspan_if_needed}> <form method="POST"> <input type="hidden" name="id" value="{$id}"> <input type="hidden" name="action" value="update"> <input type="hidden" name="updated-column" value="{$name}"> END; $this->render_form_control_for_column($editing_mode, $name, $value, TRUE); echo "</form></td>\n"; } echo "</tr>\n"; for ($row = 1; $row < $maximum_rows; $row++) { echo "<tr>\n"; foreach ($column_names as $name) { if (array_key_exists($name, $custom_cells_list)) { $this->render_custom_cells(array_get($column_to_subcolumn_names, $name), $custom_cells_list[$name], $row); } } echo "</tr>\n"; } } } echo <<<END </tbody> </table> END; }
function fetch_table($table_name, $column_to_be_ordered_by = null) { if (!$this->connection || !ctype_alnum_underscore($table_name) || $column_to_be_ordered_by && !ctype_alnum_underscore($column_to_be_ordered_by)) { return false; } $clauses = ''; if ($column_to_be_ordered_by) { $clauses .= 'ORDER BY ' . $column_to_be_ordered_by; } return $this->query_and_fetch_all("SELECT * FROM {$table_name} {$clauses}"); }
function render_table($column_to_be_ordered_by) { $column_names = array_keys($this->column_info); $labels = array(); foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } array_push($labels, htmlspecialchars($this->column_label($name))); } $headers = join('</td><td>', $labels); echo <<<END <table> <thead><tr><td>ID</td><td>{$headers}</td></tr></thead> <tbody> END; assert(ctype_alnum_underscore($column_to_be_ordered_by)); $rows = $this->db->fetch_table($this->table, $this->prefix . $column_to_be_ordered_by); if ($rows) { foreach ($rows as $row) { $id = intval($row[$this->prefix . 'id']); echo "<tr>\n<td>{$id}</td>\n"; foreach ($column_names as $name) { if (array_get($this->column_info[$name], 'pre_insertion')) { continue; } $custom = array_get($this->column_info[$name], 'custom'); if ($custom) { echo "<td>"; $custom($row); echo "</td>\n"; continue; } $value = htmlspecialchars($row[$this->prefix . $name], ENT_QUOTES); $editing_mode = array_get($this->column_info[$name], 'editing_mode'); if (!$editing_mode) { echo "<td>{$value}</td>\n"; continue; } echo <<<END <td> <form method="POST"> <input type="hidden" name="id" value="{$id}"> <input type="hidden" name="action" value="update"> END; $size = array_get($this->column_info[$name], 'size'); $this->render_form_control_for_column($editing_mode, $name, $value, TRUE, $size); echo "</form></td>\n"; } echo "</tr>\n"; } } echo <<<END </tbody> </table> END; }