Initial commit
This commit is contained in:
@@ -14,8 +14,9 @@ class TableController extends Controller
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Only logged-in users can access the table manager
|
||||
// Only admin users can access the table manager
|
||||
Auth::checkAuthentication();
|
||||
Auth::checkAdminAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +39,7 @@ class TableController extends Controller
|
||||
$page = (int)$page;
|
||||
$per_page = 20;
|
||||
|
||||
$this->View->render('table/show', array(
|
||||
$this->View->renderDbManager('table/show', array(
|
||||
'database_name' => $database_name,
|
||||
'table_name' => $table_name,
|
||||
'columns' => TableModel::getTableColumns($database_name, $table_name),
|
||||
@@ -91,7 +92,7 @@ class TableController extends Controller
|
||||
}
|
||||
|
||||
// Show create table form
|
||||
$this->View->render('table/create', array(
|
||||
$this->View->renderDbManager('table/create', array(
|
||||
'database_name' => $database_name
|
||||
));
|
||||
}
|
||||
@@ -112,7 +113,7 @@ class TableController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
$this->View->render('table/structure', array(
|
||||
$this->View->renderDbManager('table/structure', array(
|
||||
'database_name' => $database_name,
|
||||
'table_name' => $table_name,
|
||||
'columns' => TableModel::getTableColumns($database_name, $table_name),
|
||||
@@ -172,7 +173,7 @@ class TableController extends Controller
|
||||
}
|
||||
|
||||
// Show add column form
|
||||
$this->View->render('table/add_column', array(
|
||||
$this->View->renderDbManager('table/add_column', array(
|
||||
'database_name' => $database_name,
|
||||
'table_name' => $table_name
|
||||
));
|
||||
@@ -237,12 +238,160 @@ class TableController extends Controller
|
||||
Redirect::to('database/show/' . urlencode($database_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a row in the table (AJAX)
|
||||
* @param string $database_name
|
||||
* @param string $table_name
|
||||
*/
|
||||
public function updateRow($database_name = null, $table_name = null)
|
||||
{
|
||||
if (!$database_name || !$table_name) {
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid parameters']);
|
||||
return;
|
||||
}
|
||||
Redirect::to('database/index');
|
||||
return;
|
||||
}
|
||||
|
||||
$pk_value = Request::post('pk_value');
|
||||
$data = Request::post('data');
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (!$pk_value || !$data) {
|
||||
echo json_encode(['success' => false, 'message' => 'Missing required data']);
|
||||
return;
|
||||
}
|
||||
|
||||
if (TableModel::updateRow($database_name, $table_name, $pk_value, $data)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Row updated successfully'
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to update row'
|
||||
]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('table/show/' . urlencode($database_name) . '/' . urlencode($table_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a row from the table (AJAX)
|
||||
* @param string $database_name
|
||||
* @param string $table_name
|
||||
*/
|
||||
public function deleteRow($database_name = null, $table_name = null)
|
||||
{
|
||||
if (!$database_name || !$table_name) {
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid parameters']);
|
||||
return;
|
||||
}
|
||||
Redirect::to('database/index');
|
||||
return;
|
||||
}
|
||||
|
||||
$pk_value = Request::post('pk_value');
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (!$pk_value) {
|
||||
echo json_encode(['success' => false, 'message' => 'Missing primary key value']);
|
||||
return;
|
||||
}
|
||||
|
||||
if (TableModel::deleteRow($database_name, $table_name, $pk_value)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Row deleted successfully'
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to delete row'
|
||||
]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('table/show/' . urlencode($database_name) . '/' . urlencode($table_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new row into the table (AJAX)
|
||||
* @param string $database_name
|
||||
* @param string $table_name
|
||||
*/
|
||||
public function insertRow($database_name = null, $table_name = null)
|
||||
{
|
||||
if (!$database_name || !$table_name) {
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid parameters']);
|
||||
return;
|
||||
}
|
||||
Redirect::to('database/index');
|
||||
return;
|
||||
}
|
||||
|
||||
$data = Request::post('data');
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (!$data) {
|
||||
echo json_encode(['success' => false, 'message' => 'Missing row data']);
|
||||
return;
|
||||
}
|
||||
|
||||
$insertId = TableModel::insertRow($database_name, $table_name, $data);
|
||||
if ($insertId !== false) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Row inserted successfully',
|
||||
'insert_id' => $insertId
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to insert row'
|
||||
]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('table/show/' . urlencode($database_name) . '/' . urlencode($table_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is an AJAX request
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||||
}
|
||||
|
||||
/**
|
||||
* Export table as raw SQL text
|
||||
* @param string $database_name
|
||||
* @param string $table_name
|
||||
*/
|
||||
public function export($database_name, $table_name)
|
||||
{
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Content-Disposition: inline; filename="' . $table_name . '.sql"');
|
||||
|
||||
echo DatabaseModel::exportTable($database_name, $table_name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user