Deploy on 2026-06-05 16:08:53

This commit is contained in:
Hamza-Ayed
2026-06-05 16:08:53 +03:00
parent c0da60069f
commit 54065628bf
12 changed files with 316 additions and 167 deletions

View File

@@ -20,9 +20,6 @@ class DashboardController extends Controller
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();
@@ -31,6 +28,8 @@ class DashboardController extends Controller
$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();
$todayContacts = (int)$this->pdo->query("SELECT COUNT(*) FROM contacts WHERE DATE(created_at) = CURDATE() AND deleted_at IS NULL")->fetchColumn();
$todayOrgs = (int)$this->pdo->query("SELECT COUNT(*) FROM organizations WHERE DATE(created_at) = CURDATE() AND deleted_at IS NULL")->fetchColumn();
// Recent opportunities
$stmt = $this->pdo->query(
@@ -50,14 +49,45 @@ class DashboardController extends Controller
$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 : [];
// 6-month growth data
$months = [];
$opportunityMonthly = [];
$interactionMonthly = [];
for ($i = 5; $i >= 0; $i--) {
$date = new \DateTime("-$i months");
$key = $date->format('Y-m');
$name = $date->format('M');
$months[$key] = $name;
$opportunityMonthly[$key] = 0;
$interactionMonthly[$key] = 0;
}
$oppsQuery = $this->pdo->query(
"SELECT DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count
FROM opportunities
WHERE deleted_at IS NULL AND created_at >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
GROUP BY month"
)->fetchAll(PDO::FETCH_KEY_PAIR) ?: [];
$interQuery = $this->pdo->query(
"SELECT DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count
FROM interactions
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
GROUP BY month"
)->fetchAll(PDO::FETCH_KEY_PAIR) ?: [];
foreach ($oppsQuery as $monthKey => $count) {
if (isset($opportunityMonthly[$monthKey])) {
$opportunityMonthly[$monthKey] = (int)$count;
}
}
foreach ($interQuery as $monthKey => $count) {
if (isset($interactionMonthly[$monthKey])) {
$interactionMonthly[$monthKey] = (int)$count;
}
}
return $this->render('admin/dashboard', [
'user' => $user,
'title' => $t['dashboard'] ?? 'Dashboard',
't' => $t,
'lang' => $lang,
'stats' => [
'organizations' => $orgCount,
'vc' => $vcCount,
@@ -66,10 +96,15 @@ class DashboardController extends Controller
'contacts' => $contactCount,
'sources' => $sourceCount,
'today' => $todayOpps,
'today_contacts' => $todayContacts,
'today_orgs' => $todayOrgs,
],
'recent_opportunities' => $recentOpps,
'opportunities_by_type' => $byType,
'recent_activities' => $recentActivities,
'growth_labels' => array_values($months),
'growth_opportunities' => array_values($opportunityMonthly),
'growth_interactions' => array_values($interactionMonthly),
], 'admin');
}
}

View File

@@ -24,18 +24,12 @@ class SettingsController extends Controller
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,
@@ -47,33 +41,29 @@ class SettingsController extends Controller
$tgToken = $request->post('telegram_bot_token', '');
$tgChatId = $request->post('telegram_chat_id', '');
$tgEnabled = $request->post('telegram_enabled', '0');
$action = $request->post('action', 'save');
$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!');
if ($action === 'test') {
$this->notifier->configure($tgToken, $tgChatId);
if ($this->notifier->sendTest()) {
$this->session->setFlash('success', 'Settings saved and test notification sent to Telegram!');
} else {
$err = $this->notifier->getLastError() ?: 'Check your token and chat ID.';
$this->session->setFlash('error', 'Settings saved, but failed to send Telegram notification: ' . $err);
}
} else {
$this->session->setFlash('error', 'Failed to send Telegram notification. Check your token and chat ID.');
$this->session->setFlash('success', 'Settings saved successfully.');
}
$response->redirect('/admin/settings');
}
public function switchLang(Request $request, Response $response): void
public function switchLang(Request $request, Response $response, string $lang = 'en'): void
{
$lang = $request->get('lang', 'en');
$lang = $request->routeParam('lang', $lang);
if (in_array($lang, ['ar', 'en'])) {
$this->session->set('lang', $lang);
}