Deploy on 2026-06-05 15:23:02
This commit is contained in:
@@ -5,18 +5,71 @@ namespace App\Controllers\Admin;
|
||||
use App\Controllers\Controller;
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Services\Database\Connection;
|
||||
use PDO;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display admin dashboard.
|
||||
*/
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->pdo = $connection->getPdo();
|
||||
}
|
||||
|
||||
public function index(Request $request, Response $response): string
|
||||
{
|
||||
$user = $request->routeParam('_authenticated_user');
|
||||
$lang = $this->session->get('lang', 'en');
|
||||
|
||||
// Real stats from database
|
||||
$orgCount = (int)$this->pdo->query("SELECT COUNT(*) FROM organizations WHERE deleted_at IS NULL")->fetchColumn();
|
||||
$vcCount = (int)$this->pdo->query("SELECT COUNT(*) FROM organizations WHERE type='vc' AND deleted_at IS NULL")->fetchColumn();
|
||||
$acceleratorCount = (int)$this->pdo->query("SELECT COUNT(*) FROM organizations WHERE type='accelerator' AND deleted_at IS NULL")->fetchColumn();
|
||||
$opportunityCount = (int)$this->pdo->query("SELECT COUNT(*) FROM opportunities WHERE deleted_at IS NULL AND status='active'")->fetchColumn();
|
||||
$contactCount = (int)$this->pdo->query("SELECT COUNT(*) FROM contacts WHERE deleted_at IS NULL")->fetchColumn();
|
||||
$sourceCount = (int)$this->pdo->query("SELECT COUNT(*) FROM sources WHERE status='active'")->fetchColumn();
|
||||
$todayOpps = (int)$this->pdo->query("SELECT COUNT(*) FROM opportunities WHERE DATE(created_at) = CURDATE()")->fetchColumn();
|
||||
|
||||
// Recent opportunities
|
||||
$stmt = $this->pdo->query(
|
||||
"SELECT o.*, org.name as org_name FROM opportunities o
|
||||
LEFT JOIN organizations org ON org.id = o.organization_id
|
||||
WHERE o.deleted_at IS NULL
|
||||
ORDER BY o.created_at DESC LIMIT 10"
|
||||
);
|
||||
$recentOpps = $stmt->fetchAll() ?: [];
|
||||
|
||||
// Opportunities by type
|
||||
$byType = $this->pdo->query(
|
||||
"SELECT type, COUNT(*) as count FROM opportunities WHERE deleted_at IS NULL GROUP BY type"
|
||||
)->fetchAll(PDO::FETCH_KEY_PAIR) ?: [];
|
||||
|
||||
// Recent activity
|
||||
$stmt = $this->pdo->query("SELECT * FROM activity_logs ORDER BY created_at DESC LIMIT 10");
|
||||
$recentActivities = $stmt->fetchAll() ?: [];
|
||||
|
||||
$langFile = __DIR__ . "/../../resources/lang/{$lang}.php";
|
||||
$t = file_exists($langFile) ? require $langFile : [];
|
||||
|
||||
return $this->render('admin/dashboard', [
|
||||
'user' => $user,
|
||||
'title' => 'Dashboard',
|
||||
'title' => $t['dashboard'] ?? 'Dashboard',
|
||||
't' => $t,
|
||||
'lang' => $lang,
|
||||
'stats' => [
|
||||
'organizations' => $orgCount,
|
||||
'vc' => $vcCount,
|
||||
'accelerators' => $acceleratorCount,
|
||||
'opportunities' => $opportunityCount,
|
||||
'contacts' => $contactCount,
|
||||
'sources' => $sourceCount,
|
||||
'today' => $todayOpps,
|
||||
],
|
||||
'recent_opportunities' => $recentOpps,
|
||||
'opportunities_by_type' => $byType,
|
||||
'recent_activities' => $recentActivities,
|
||||
], 'admin');
|
||||
}
|
||||
}
|
||||
}
|
||||
97
app/Controllers/Admin/SettingsController.php
Normal file
97
app/Controllers/Admin/SettingsController.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Admin;
|
||||
|
||||
use App\Controllers\Controller;
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Services\Database\Connection;
|
||||
use App\Services\Notification\TelegramNotifier;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
private PDO $pdo;
|
||||
private TelegramNotifier $notifier;
|
||||
|
||||
public function __construct(Connection $connection, TelegramNotifier $notifier)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->pdo = $connection->getPdo();
|
||||
$this->notifier = $notifier;
|
||||
}
|
||||
|
||||
public function index(Request $request, Response $response): string
|
||||
{
|
||||
$lang = $this->session->get('lang', 'en');
|
||||
$langFile = __DIR__ . "/../../resources/lang/{$lang}.php";
|
||||
$t = file_exists($langFile) ? require $langFile : [];
|
||||
|
||||
// Get telegram settings from database
|
||||
$tgToken = $this->getSetting('telegram_bot_token', '');
|
||||
$tgChatId = $this->getSetting('telegram_chat_id', '');
|
||||
$tgEnabled = $this->getSetting('telegram_enabled', '0');
|
||||
|
||||
return $this->render('admin/settings/index', [
|
||||
't' => $t,
|
||||
'lang' => $lang,
|
||||
'tg_token' => $tgToken,
|
||||
'tg_chat_id' => $tgChatId,
|
||||
'tg_enabled' => $tgEnabled,
|
||||
], 'admin');
|
||||
}
|
||||
|
||||
public function save(Request $request, Response $response): void
|
||||
{
|
||||
$tgToken = $request->post('telegram_bot_token', '');
|
||||
$tgChatId = $request->post('telegram_chat_id', '');
|
||||
$tgEnabled = $request->post('telegram_enabled', '0');
|
||||
|
||||
$this->saveSetting('telegram_bot_token', $tgToken);
|
||||
$this->saveSetting('telegram_chat_id', $tgChatId);
|
||||
$this->saveSetting('telegram_enabled', $tgEnabled);
|
||||
|
||||
$this->session->setFlash('success', 'Settings saved successfully.');
|
||||
$response->redirect('/admin/settings');
|
||||
}
|
||||
|
||||
public function testTelegram(Request $request, Response $response): void
|
||||
{
|
||||
$tgToken = $request->post('telegram_bot_token', '');
|
||||
$tgChatId = $request->post('telegram_chat_id', '');
|
||||
|
||||
$this->notifier->configure($tgToken, $tgChatId);
|
||||
|
||||
if ($this->notifier->sendTest()) {
|
||||
$this->session->setFlash('success', 'Test notification sent to Telegram!');
|
||||
} else {
|
||||
$this->session->setFlash('error', 'Failed to send Telegram notification. Check your token and chat ID.');
|
||||
}
|
||||
$response->redirect('/admin/settings');
|
||||
}
|
||||
|
||||
public function switchLang(Request $request, Response $response): void
|
||||
{
|
||||
$lang = $request->get('lang', 'en');
|
||||
if (in_array($lang, ['ar', 'en'])) {
|
||||
$this->session->set('lang', $lang);
|
||||
}
|
||||
$ref = $request->getHeader('Referer') ?? '/admin/dashboard';
|
||||
$response->redirect($ref);
|
||||
}
|
||||
|
||||
private function getSetting(string $key, string $default = ''): string
|
||||
{
|
||||
$stmt = $this->pdo->prepare("SELECT `value` FROM settings WHERE `key` = ?");
|
||||
$stmt->execute([$key]);
|
||||
$val = $stmt->fetchColumn();
|
||||
return $val !== false ? $val : $default;
|
||||
}
|
||||
|
||||
private function saveSetting(string $key, string $value): void
|
||||
{
|
||||
$stmt = $this->pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = ?");
|
||||
$stmt->execute([$key, $value, $value]);
|
||||
}
|
||||
}
|
||||
@@ -75,8 +75,9 @@ class SourcesController extends Controller
|
||||
$response->redirect('/admin/sources');
|
||||
}
|
||||
|
||||
public function run(Request $request, Response $response, int $id): void
|
||||
public function run(Request $request, Response $response): void
|
||||
{
|
||||
$id = (int)$request->routeParam('id');
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM sources WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$source = $stmt->fetch();
|
||||
|
||||
Reference in New Issue
Block a user