diff --git a/db/migrations/07_startup_followers.sql b/db/migrations/07_startup_followers.sql new file mode 100644 index 0000000..c72689f --- /dev/null +++ b/db/migrations/07_startup_followers.sql @@ -0,0 +1,10 @@ +-- Migration: Add startup_followers table +CREATE TABLE IF NOT EXISTS startup_followers ( + id INT AUTO_INCREMENT PRIMARY KEY, + startup_id INT NOT NULL, + user_id INT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY unique_follow (startup_id, user_id), + FOREIGN KEY (startup_id) REFERENCES startups(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +); diff --git a/startup_details.php b/startup_details.php index 5af3208..2f95594 100644 --- a/startup_details.php +++ b/startup_details.php @@ -22,6 +22,11 @@ if (!$startup) { exit; } +// Check if current user is following +$stmt = db()->prepare("SELECT 1 FROM startup_followers WHERE startup_id = ? AND user_id = ?"); +$stmt->execute([$startup_id, $user_id]); +$isFollowing = (bool)$stmt->fetchColumn(); + // Check for active funding round $stmt = db()->prepare("SELECT * FROM funding_rounds WHERE startup_id = ? AND status = 'Active' LIMIT 1"); $stmt->execute([$startup_id]); @@ -34,6 +39,19 @@ $success = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $action = $_POST['action']; + // Follow/Unfollow Actions (Available to all logged in users except the founder of this startup) + if ($action === 'follow' && $startup['founder_id'] != $user_id) { + $stmt = db()->prepare("INSERT IGNORE INTO startup_followers (startup_id, user_id) VALUES (?, ?)"); + $stmt->execute([$startup_id, $user_id]); + $success = "You are now following " . $startup['name'] . "!"; + $isFollowing = true; + } elseif ($action === 'unfollow' && $startup['founder_id'] != $user_id) { + $stmt = db()->prepare("DELETE FROM startup_followers WHERE startup_id = ? AND user_id = ?"); + $stmt->execute([$startup_id, $user_id]); + $success = "You have unfollowed " . $startup['name'] . "."; + $isFollowing = false; + } + // Founder Actions if ($user['role'] === 'founder' && $startup['founder_id'] == $user_id) { $round_id = isset($_POST['round_id']) ? (int)$_POST['round_id'] : 0; @@ -100,26 +118,32 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $stmt = db()->prepare("INSERT INTO startup_updates (startup_id, founder_id, title, content) VALUES (?, ?, ?, ?)"); $stmt->execute([$startup_id, $user_id, $title, $content]); - // 2. Identify All Unique Investors for this startup - $stmt = db()->prepare("SELECT DISTINCT u.id, u.email, u.full_name FROM investments i JOIN users u ON i.investor_id = u.id WHERE i.startup_id = ? AND i.status = 'approved'"); - $stmt->execute([$startup_id]); - $investorsToNotify = $stmt->fetchAll(); + // 2. Identify All Unique Investors AND Followers for this startup + $stmt = db()->prepare(" + SELECT DISTINCT u.id, u.email, u.full_name + FROM users u + LEFT JOIN investments i ON i.investor_id = u.id AND i.startup_id = ? AND i.status = 'approved' + LEFT JOIN startup_followers f ON f.user_id = u.id AND f.startup_id = ? + WHERE i.investor_id IS NOT NULL OR f.user_id IS NOT NULL + "); + $stmt->execute([$startup_id, $startup_id]); + $peopleToNotify = $stmt->fetchAll(); // 3. Notify them - foreach ($investorsToNotify as $invUser) { + foreach ($peopleToNotify as $targetUser) { // DB Notification $notif = db()->prepare("INSERT INTO notifications (user_id, content) VALUES (?, ?)"); - $notif->execute([$invUser['id'], "New progress update from " . $startup['name'] . ": " . $title]); + $notif->execute([$targetUser['id'], "New progress update from " . $startup['name'] . ": " . $title]); // Email Notification $subject = "New Update: " . $startup['name']; - $emailHtml = "
Hi " . htmlspecialchars($invUser['full_name']) . ", " . htmlspecialchars($startup['name']) . " has posted a new progress update:
" . nl2br(htmlspecialchars($content)) . "
"; - $emailText = "New Update: " . $title . "\n\nHi " . $invUser['full_name'] . ", " . $startup['name'] . " has posted a new progress update: " . $content; - MailService::sendMail($invUser['email'], $subject, $emailHtml, $emailText); + $emailHtml = "Hi " . htmlspecialchars($targetUser['full_name']) . ", " . htmlspecialchars($startup['name']) . " has posted a new progress update:
" . nl2br(htmlspecialchars($content)) . "
"; + $emailText = "New Update: " . $title . "\n\nHi " . $targetUser['full_name'] . ", " . $startup['name'] . " has posted a new progress update: " . $content; + MailService::sendMail($targetUser['email'], $subject, $emailHtml, $emailText); } db()->commit(); - $success = "Update posted and " . count($investorsToNotify) . " investors notified."; + $success = "Update posted and " . count($peopleToNotify) . " backers/followers notified."; } catch (Exception $e) { db()->rollBack(); $error = "Failed to post update: " . $e->getMessage(); @@ -263,13 +287,24 @@ $platformName = defined('PLATFORM_NAME') ? PLATFORM_NAME : 'Gatsby';