Add complete ScoutIQ system: Crawler (RSS+AI), CRUD Controllers (Organizations, Contacts, Opportunities, Sources), dynamic Views, API routes, CLI collector

This commit is contained in:
Hamza-Ayed
2026-06-05 02:23:32 +03:00
parent d2f323a563
commit bd7984f8e3
20 changed files with 2084 additions and 0 deletions

View File

@@ -0,0 +1,163 @@
<?php
namespace App\Controllers\Admin;
use App\Controllers\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Services\Database\Connection;
use PDO;
use Throwable;
class ContactsController extends Controller
{
private PDO $pdo;
public function __construct(Connection $connection)
{
parent::__construct();
$this->pdo = $connection->getPdo();
}
public function index(Request $request, Response $response): string
{
$search = $request->get('search', '');
$page = max(1, (int)$request->get('page', 1));
$perPage = 20;
$offset = ($page - 1) * $perPage;
$where = ['c.deleted_at IS NULL'];
$params = [];
if ($search) {
$where[] = '(c.name LIKE ? OR c.email LIKE ? OR c.phone LIKE ?)';
$params[] = "%{$search}%";
$params[] = "%{$search}%";
$params[] = "%{$search}%";
}
$whereClause = implode(' AND ', $where);
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM contacts c WHERE {$whereClause}");
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$stmt = $this->pdo->prepare(
"SELECT c.*, org.name as org_name,
(SELECT COUNT(*) FROM interactions WHERE contact_id = c.id) as interaction_count
FROM contacts c
LEFT JOIN organizations org ON org.id = c.organization_id
WHERE {$whereClause}
ORDER BY c.updated_at DESC
LIMIT ? OFFSET ?"
);
$stmt->execute(array_merge($params, [$perPage, $offset]));
$contacts = $stmt->fetchAll();
return $this->render('admin/contacts/index', [
'contacts' => $contacts,
'total' => $total,
'page' => $page,
'perPage' => $perPage,
'search' => $search,
], 'admin');
}
public function show(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare(
"SELECT c.*, org.name as org_name, org.id as org_id
FROM contacts c
LEFT JOIN organizations org ON org.id = c.organization_id
WHERE c.id = ? AND c.deleted_at IS NULL"
);
$stmt->execute([$id]);
$contact = $stmt->fetch();
if (!$contact) { $response->redirect('/admin/contacts'); return ''; }
$stmt = $this->pdo->prepare("SELECT * FROM interactions WHERE contact_id = ? ORDER BY created_at DESC");
$stmt->execute([$id]);
$interactions = $stmt->fetchAll();
return $this->render('admin/contacts/show', [
'contact' => $contact,
'interactions' => $interactions,
], 'admin');
}
public function create(Request $request, Response $response): string
{
$orgId = $request->get('organization_id', '');
$orgs = $this->pdo->query("SELECT id, name FROM organizations WHERE deleted_at IS NULL ORDER BY name")->fetchAll();
return $this->render('admin/contacts/form', [
'contact' => null,
'organizations' => $orgs,
'selectedOrgId' => $orgId,
], 'admin');
}
public function edit(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare("SELECT * FROM contacts WHERE id = ? AND deleted_at IS NULL");
$stmt->execute([$id]);
$contact = $stmt->fetch();
if (!$contact) { $response->redirect('/admin/contacts'); return ''; }
$orgs = $this->pdo->query("SELECT id, name FROM organizations WHERE deleted_at IS NULL ORDER BY name")->fetchAll();
return $this->render('admin/contacts/form', [
'contact' => $contact,
'organizations' => $orgs,
'selectedOrgId' => $contact['organization_id'],
], 'admin');
}
public function store(Request $request, Response $response): void
{
$id = $request->post('id', '');
$name = $request->post('name', '');
$email = $request->post('email', '');
$phone = $request->post('phone', '');
$position = $request->post('position', '');
$organizationId = $request->post('organization_id', '');
$notes = $request->post('notes', '');
try {
if ($id) {
$stmt = $this->pdo->prepare("UPDATE contacts SET name=?, email=?, phone=?, position=?, organization_id=?, notes=? WHERE id=?");
$stmt->execute([$name, $email ?: null, $phone ?: null, $position ?: null, $organizationId ?: null, $notes, $id]);
} else {
$stmt = $this->pdo->prepare("INSERT INTO contacts (name, email, phone, position, organization_id, notes) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$name, $email ?: null, $phone ?: null, $position ?: null, $organizationId ?: null, $notes]);
$id = $this->pdo->lastInsertId();
}
$this->session->setFlash('success', 'Contact saved.');
$response->redirect('/admin/contacts/' . $id);
} catch (Throwable $e) {
$this->session->setFlash('error', 'Error: ' . $e->getMessage());
$response->redirect('/admin/contacts');
}
}
public function delete(Request $request, Response $response, int $id): void
{
$this->pdo->prepare("UPDATE contacts SET deleted_at = NOW() WHERE id = ?")->execute([$id]);
$this->session->setFlash('success', 'Contact deleted.');
$response->redirect('/admin/contacts');
}
public function addInteraction(Request $request, Response $response, int $contactId): void
{
$type = $request->post('type', 'note');
$notes = $request->post('notes', '');
try {
$stmt = $this->pdo->prepare("INSERT INTO interactions (contact_id, type, notes) VALUES (?, ?, ?)");
$stmt->execute([$contactId, $type, $notes]);
$this->session->setFlash('success', 'Interaction logged.');
} catch (Throwable $e) {
$this->session->setFlash('error', 'Error: ' . $e->getMessage());
}
$response->redirect('/admin/contacts/' . $contactId);
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace App\Controllers\Admin;
use App\Controllers\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Services\Database\Connection;
use PDO;
use Throwable;
class OpportunitiesController extends Controller
{
private PDO $pdo;
public function __construct(Connection $connection)
{
parent::__construct();
$this->pdo = $connection->getPdo();
}
public function index(Request $request, Response $response): string
{
$type = $request->get('type', '');
$search = $request->get('search', '');
$status = $request->get('status', '');
$page = max(1, (int)$request->get('page', 1));
$perPage = 20;
$offset = ($page - 1) * $perPage;
$where = ['o.deleted_at IS NULL'];
$params = [];
if ($type) { $where[] = 'o.type = ?'; $params[] = $type; }
if ($status) { $where[] = 'o.status = ?'; $params[] = $status; }
if ($search) {
$where[] = '(o.title LIKE ? OR o.description LIKE ?)';
$params[] = "%{$search}%";
$params[] = "%{$search}%";
}
$whereClause = implode(' AND ', $where);
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM opportunities o WHERE {$whereClause}");
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$stmt = $this->pdo->prepare(
"SELECT o.*, org.name as org_name,
GROUP_CONCAT(DISTINCT t.name) as tag_names
FROM opportunities o
LEFT JOIN organizations org ON org.id = o.organization_id
LEFT JOIN opportunity_tags ot ON ot.opportunity_id = o.id
LEFT JOIN tags t ON t.id = ot.tag_id
WHERE {$whereClause}
GROUP BY o.id
ORDER BY o.score DESC, o.created_at DESC
LIMIT ? OFFSET ?"
);
$stmt->execute(array_merge($params, [$perPage, $offset]));
$opportunities = $stmt->fetchAll();
// Get type counts for sidebar
$typeCounts = $this->pdo->query(
"SELECT type, COUNT(*) as count FROM opportunities WHERE deleted_at IS NULL GROUP BY type"
)->fetchAll(PDO::FETCH_KEY_PAIR);
return $this->render('admin/opportunities/index', [
'opportunities' => $opportunities,
'total' => $total,
'page' => $page,
'perPage' => $perPage,
'type' => $type,
'status' => $status,
'search' => $search,
'typeCounts' => $typeCounts,
'types' => ['grant', 'competition', 'demo_day', 'event', 'partnership', 'investment', 'other'],
'statuses' => ['active', 'closed', 'expired'],
], 'admin');
}
public function show(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare(
"SELECT o.*, org.name as org_name, org.type as org_type, org.website_url as org_website,
GROUP_CONCAT(DISTINCT t.name) as tag_names
FROM opportunities o
LEFT JOIN organizations org ON org.id = o.organization_id
LEFT JOIN opportunity_tags ot ON ot.opportunity_id = o.id
LEFT JOIN tags t ON t.id = ot.tag_id
WHERE o.id = ? AND o.deleted_at IS NULL
GROUP BY o.id"
);
$stmt->execute([$id]);
$opportunity = $stmt->fetch();
if (!$opportunity) {
$response->redirect('/admin/opportunities');
return '';
}
// Get applications
$stmt = $this->pdo->prepare("SELECT * FROM applications WHERE opportunity_id = ? AND deleted_at IS NULL ORDER BY created_at DESC");
$stmt->execute([$id]);
$applications = $stmt->fetchAll();
return $this->render('admin/opportunities/show', [
'opportunity' => $opportunity,
'applications' => $applications,
], 'admin');
}
}

View File

@@ -0,0 +1,219 @@
<?php
namespace App\Controllers\Admin;
use App\Controllers\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Services\Database\Connection;
use PDO;
use Throwable;
class OrganizationsController extends Controller
{
private PDO $pdo;
public function __construct(Connection $connection)
{
parent::__construct();
$this->pdo = $connection->getPdo();
}
/**
* List all organizations with filters.
*/
public function index(Request $request, Response $response): string
{
$type = $request->get('type', '');
$search = $request->get('search', '');
$page = max(1, (int)$request->get('page', 1));
$perPage = 20;
$offset = ($page - 1) * $perPage;
$where = ['o.deleted_at IS NULL'];
$params = [];
if ($type) {
$where[] = 'o.type = ?';
$params[] = $type;
}
if ($search) {
$where[] = '(o.name LIKE ? OR o.domain LIKE ? OR o.description LIKE ?)';
$params[] = "%{$search}%";
$params[] = "%{$search}%";
$params[] = "%{$search}%";
}
$whereClause = implode(' AND ', $where);
// Count total
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM organizations o WHERE {$whereClause}");
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
// Fetch page
$stmt = $this->pdo->prepare(
"SELECT o.*,
(SELECT COUNT(*) FROM opportunities WHERE organization_id = o.id AND deleted_at IS NULL) as opportunities_count
FROM organizations o
WHERE {$whereClause}
ORDER BY o.updated_at DESC
LIMIT ? OFFSET ?"
);
$stmt->execute(array_merge($params, [$perPage, $offset]));
$organizations = $stmt->fetchAll();
return $this->render('admin/organizations/index', [
'organizations' => $organizations,
'total' => $total,
'page' => $page,
'perPage' => $perPage,
'type' => $type,
'search' => $search,
'types' => ['vc', 'angel', 'accelerator', 'incubator', 'venture_studio', 'partner'],
'statuses' => ['New', 'Researching', 'Contacted', 'Follow Up', 'Meeting Scheduled', 'Interested', 'Rejected', 'Invested'],
], 'admin');
}
/**
* Show single organization.
*/
public function show(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare("SELECT * FROM organizations WHERE id = ? AND deleted_at IS NULL");
$stmt->execute([$id]);
$org = $stmt->fetch();
if (!$org) {
$response->redirect('/admin/organizations');
return '';
}
// Get opportunities for this org
$stmt = $this->pdo->prepare(
"SELECT o.*, GROUP_CONCAT(t.name) as tag_names
FROM opportunities o
LEFT JOIN opportunity_tags ot ON ot.opportunity_id = o.id
LEFT JOIN tags t ON t.id = ot.tag_id
WHERE o.organization_id = ? AND o.deleted_at IS NULL
GROUP BY o.id
ORDER BY o.score DESC"
);
$stmt->execute([$id]);
$opportunities = $stmt->fetchAll();
// Get contacts for this org
$stmt = $this->pdo->prepare(
"SELECT c.*,
(SELECT COUNT(*) FROM interactions WHERE contact_id = c.id) as interaction_count
FROM contacts c
WHERE c.organization_id = ? AND c.deleted_at IS NULL
ORDER BY c.created_at DESC"
);
$stmt->execute([$id]);
$contacts = $stmt->fetchAll();
// Get activity logs
$stmt = $this->pdo->prepare(
"SELECT * FROM activity_logs WHERE description LIKE ? ORDER BY created_at DESC LIMIT 20"
);
$stmt->execute(['%' . $org['name'] . '%']);
$activities = $stmt->fetchAll();
return $this->render('admin/organizations/show', [
'org' => $org,
'opportunities' => $opportunities,
'contacts' => $contacts,
'activities' => $activities,
], 'admin');
}
/**
* Show create/edit form.
*/
public function create(Request $request, Response $response): string
{
return $this->render('admin/organizations/form', [
'org' => null,
'types' => ['vc', 'angel', 'accelerator', 'incubator', 'venture_studio', 'partner'],
'statuses' => ['New', 'Researching', 'Contacted', 'Follow Up', 'Meeting Scheduled', 'Interested', 'Rejected', 'Invested'],
], 'admin');
}
/**
* Show edit form.
*/
public function edit(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare("SELECT * FROM organizations WHERE id = ? AND deleted_at IS NULL");
$stmt->execute([$id]);
$org = $stmt->fetch();
if (!$org) {
$response->redirect('/admin/organizations');
return '';
}
return $this->render('admin/organizations/form', [
'org' => $org,
'types' => ['vc', 'angel', 'accelerator', 'incubator', 'venture_studio', 'partner'],
'statuses' => ['New', 'Researching', 'Contacted', 'Follow Up', 'Meeting Scheduled', 'Interested', 'Rejected', 'Invested'],
], 'admin');
}
/**
* Save organization (create or update).
*/
public function store(Request $request, Response $response): void
{
$id = $request->post('id', '');
$name = $request->post('name', '');
$domain = $request->post('domain', '');
$type = $request->post('type', 'partner');
$country = $request->post('country', '');
$city = $request->post('city', '');
$websiteUrl = $request->post('website_url', '');
$description = $request->post('description', '');
$crmStatus = $request->post('crm_status', 'New');
$fundingStage = $request->post('funding_stage', '');
try {
if ($id) {
$stmt = $this->pdo->prepare(
"UPDATE organizations SET name=?, domain=?, type=?, country=?, city=?, website_url=?,
description=?, crm_status=?, funding_stage=? WHERE id=?"
);
$stmt->execute([$name, $domain ?: null, $type, $country ?: null, $city ?: null, $websiteUrl ?: null, $description, $crmStatus, $fundingStage ?: null, $id]);
$this->session->setFlash('success', 'Organization updated successfully.');
} else {
$stmt = $this->pdo->prepare(
"INSERT INTO organizations (name, domain, type, country, city, website_url, description, crm_status, funding_stage)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([$name, $domain ?: null, $type, $country ?: null, $city ?: null, $websiteUrl ?: null, $description, $crmStatus, $fundingStage ?: null]);
$id = $this->pdo->lastInsertId();
$this->session->setFlash('success', 'Organization created successfully.');
}
$response->redirect('/admin/organizations/' . $id);
} catch (Throwable $e) {
$this->session->setFlash('error', 'Error saving organization: ' . $e->getMessage());
$response->redirect('/admin/organizations' . ($id ? '/' . $id : '/create'));
}
}
/**
* Delete organization (soft delete).
*/
public function delete(Request $request, Response $response, int $id): void
{
try {
$stmt = $this->pdo->prepare("UPDATE organizations SET deleted_at = NOW() WHERE id = ?");
$stmt->execute([$id]);
$this->session->setFlash('success', 'Organization deleted.');
} catch (Throwable $e) {
$this->session->setFlash('error', 'Error deleting organization.');
}
$response->redirect('/admin/organizations');
}
}

View File

@@ -0,0 +1,93 @@
<?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\Crawler\Collector;
use PDO;
use Throwable;
class SourcesController extends Controller
{
private PDO $pdo;
private Collector $collector;
public function __construct(Connection $connection, Collector $collector)
{
parent::__construct();
$this->pdo = $connection->getPdo();
$this->collector = $collector;
}
public function index(Request $request, Response $response): string
{
$sources = $this->collector->getActiveSources();
// Also get inactive ones
$stmt = $this->pdo->query("SELECT * FROM sources ORDER BY status, name");
$allSources = $stmt->fetchAll();
return $this->render('admin/sources/index', ['sources' => $allSources], 'admin');
}
public function create(Request $request, Response $response): string
{
return $this->render('admin/sources/form', ['source' => null], 'admin');
}
public function edit(Request $request, Response $response, int $id): string
{
$stmt = $this->pdo->prepare("SELECT * FROM sources WHERE id = ?");
$stmt->execute([$id]);
$source = $stmt->fetch();
if (!$source) { $response->redirect('/admin/sources'); return ''; }
return $this->render('admin/sources/form', ['source' => $source], 'admin');
}
public function store(Request $request, Response $response): void
{
$id = $request->post('id', '');
$name = $request->post('name', '');
$url = $request->post('url', '');
$type = $request->post('type', 'rss');
$status = $request->post('status', 'active');
try {
if ($id) {
$stmt = $this->pdo->prepare("UPDATE sources SET name=?, url=?, type=?, status=? WHERE id=?");
$stmt->execute([$name, $url, $type, $status, $id]);
} else {
$stmt = $this->pdo->prepare("INSERT INTO sources (name, url, type, status) VALUES (?, ?, ?, ?)");
$stmt->execute([$name, $url, $type, $status]);
}
$this->session->setFlash('success', 'Source saved.');
} catch (Throwable $e) {
$this->session->setFlash('error', 'Error: ' . $e->getMessage());
}
$response->redirect('/admin/sources');
}
public function delete(Request $request, Response $response, int $id): void
{
$this->pdo->prepare("DELETE FROM sources WHERE id = ?")->execute([$id]);
$this->session->setFlash('success', 'Source deleted.');
$response->redirect('/admin/sources');
}
public function run(Request $request, Response $response, int $id): void
{
$stmt = $this->pdo->prepare("SELECT * FROM sources WHERE id = ?");
$stmt->execute([$id]);
$source = $stmt->fetch();
if (!$source) { $this->session->setFlash('error', 'Source not found.'); $response->redirect('/admin/sources'); return; }
try {
$result = $this->collector->collectSource($source);
$this->session->setFlash('success', "Collected {$result['entries_found']} entries, {$result['opportunities']} new opportunities.");
} catch (Throwable $e) {
$this->session->setFlash('error', 'Collection error: ' . $e->getMessage());
}
$response->redirect('/admin/sources');
}
}