Finished messenger
This commit is contained in:
144
application/controller/DatabaseController.php
Normal file
144
application/controller/DatabaseController.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class DatabaseController
|
||||
*
|
||||
* Controller for managing databases and showing their structure
|
||||
*/
|
||||
class DatabaseController extends Controller
|
||||
{
|
||||
/**
|
||||
* Construct this object by extending the basic Controller class
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Only logged-in users can access the database manager
|
||||
Auth::checkAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* Main database management interface
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->View->render('database/index', array(
|
||||
'databases' => DatabaseModel::getAllDatabases(),
|
||||
'current_db' => Config::get('DB_NAME')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show details of a specific database
|
||||
* @param string $database_name
|
||||
*/
|
||||
public function show($database_name = null)
|
||||
{
|
||||
if (!$database_name) {
|
||||
$database_name = Config::get('DB_NAME');
|
||||
}
|
||||
|
||||
$this->View->render('database/show', array(
|
||||
'tables' => DatabaseModel::getTablesInDatabase($database_name),
|
||||
'database_name' => $database_name,
|
||||
'table_info' => DatabaseModel::getTableDetails($database_name)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new database
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$database_name = Request::post('database_name');
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if (DatabaseModel::createDatabase($database_name)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Database created successfully'
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to create database'
|
||||
]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('database');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a database
|
||||
* @param string $database_name
|
||||
*/
|
||||
public function delete($database_name)
|
||||
{
|
||||
// Prevent deletion of the current database
|
||||
if ($database_name === Config::get('DB_NAME')) {
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Cannot delete the currently connected database'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
Redirect::to('database');
|
||||
return;
|
||||
}
|
||||
|
||||
$success = DatabaseModel::deleteDatabase($database_name);
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Database deleted successfully'
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to delete database'
|
||||
]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('database');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database structure as JSON (AJAX endpoint)
|
||||
* @param string $database_name
|
||||
*/
|
||||
public function getStructure($database_name = null)
|
||||
{
|
||||
if (!$database_name) {
|
||||
$database_name = Config::get('DB_NAME');
|
||||
}
|
||||
|
||||
$structure = DatabaseModel::getDatabaseStructure($database_name);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'structure' => $structure
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is an AJAX request
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user