110 lines
4.7 KiB
PHP
110 lines
4.7 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
private PDO $pdo;
|
|
|
|
public function __construct(Connection $connection)
|
|
{
|
|
parent::__construct();
|
|
$this->pdo = $connection->getPdo();
|
|
}
|
|
|
|
public function index(Request $request, Response $response): string
|
|
{
|
|
// 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();
|
|
$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(
|
|
"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() ?: [];
|
|
|
|
// 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', [
|
|
'stats' => [
|
|
'organizations' => $orgCount,
|
|
'vc' => $vcCount,
|
|
'accelerators' => $acceleratorCount,
|
|
'opportunities' => $opportunityCount,
|
|
'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');
|
|
}
|
|
} |