# Implementing "Create Project" Buttons with Redirect Support

**Quick Implementation Guide for Pages That Need Website Creation**

---

## For Page Developers: How to Add "Create Project" Button

### Option 1: Simple Button (No Current Page Detection)
```php
<!-- On /crawl page -->
<a href="<?= site_url('onboarding?redirect=crawl') ?>" class="btn btn-primary">
  + Create Project
</a>
```

### Option 2: Dynamic Button (Auto-Detect Current Page)
```php
<?php
// In your controller, determine the current route/page
$currentPage = 'crawl';  // or 'reports', 'audit', etc.
// Only allow valid redirect pages
$validPages = ['dashboard', 'crawl', 'website', 'reports', 'audit', 'keywords', 'rank-tracking'];
if (!in_array($currentPage, $validPages)) {
    $currentPage = 'dashboard';
}
?>

<!-- In your view -->
<a href="<?= site_url("onboarding?redirect={$currentPage}") ?>" class="btn btn-primary">
  + Create Project
</a>
```

### Option 3: Using Router to Detect Current Page
```php
<?php
// In view or controller
$routeName = 'dashboard';  // Get from current route
$validRedirects = ['dashboard', 'crawl', 'website', 'reports', 'audit', 'keywords', 'rank-tracking'];
$redirectTarget = in_array($routeName, $validRedirects) ? $routeName : 'dashboard';
?>

<a href="<?= site_url("onboarding?redirect={$redirectTarget}") ?>" class="btn btn-primary">
  + Create Project
</a>
```

---

## Expected Behavior

### When User Clicks Button
```
User on /crawl → Clicks "Create Project" 
→ Navigates to /onboarding?redirect=crawl
→ Onboarding wizard displays
→ "Skip for now" link → /crawl
→ "Go to Crawl" button → /crawl
```

### After Creating Website
```
User fills form and clicks "Create Project"
→ API call to POST /api/v1/websites
→ Success response
→ Modal shows success message (1.5 seconds)
→ Redirects to /crawl (not dashboard)
→ User stays on relevant page
```

---

## Pages That Should Have "Create Project" Button

Based on current routes that might need a website to function:

| Page | Route | Status | Implementation |
|------|-------|--------|-----------------|
| Dashboard | `/dashboard` | ✅ Built-in modal | Ready |
| Crawl History | `/crawl` | TODO | Link to `/onboarding?redirect=crawl` |
| Audit Results | `/audit` | TODO | Link to `/onboarding?redirect=audit` |
| Keyword Tracking | `/keywords` | TODO | Link to `/onboarding?redirect=keywords` |
| Reports | `/reports` | TODO | Link to `/onboarding?redirect=reports` |
| Rank Tracking | `/rank-tracking` | TODO | Link to `/onboarding?redirect=rank-tracking` |
| Website Mgmt | `/website` | ✅ Has form | Direct form on page |

---

## Code Snippets Ready to Copy

### For Bootstrap Card (Empty State)
```php
<div class="card text-center p-5">
  <h3 class="mb-3">No Projects Yet</h3>
  <p class="text-muted mb-4">Create your first project to start crawling and analyzing.</p>
  <a href="<?= site_url('onboarding?redirect=crawl') ?>" class="btn btn-primary btn-lg">
    <i class="fas fa-plus"></i> Create Project
  </a>
</div>
```

### For Empty State Message with Link
```php
<?php if (empty($websites)): ?>
  <div class="alert alert-info">
    <strong>No projects connected.</strong> 
    <a href="<?= site_url('onboarding?redirect=crawl') ?>">Create your first project</a>
    to get started.
  </div>
<?php endif; ?>
```

### For Toolbar Button
```php
<div class="toolbar mb-3">
  <?php if (!empty($websites)): ?>
    <a href="<?= site_url('onboarding?redirect=crawl') ?>" class="btn btn-sm btn-outline-primary">
      + Create Project
    </a>
  <?php endif; ?>
</div>
```

---

## Testing the Implementation

After adding the button to a page:

```bash
# 1. Open the page in browser
# Example: http://192.168.30.106/php82/koushik/webcrawlers_latest.com/public/crawl

# 2. Click "Create Project" button
# Should navigate to: /onboarding?redirect=crawl

# 3. Verify the onboarding page shows:
# - "Go to Crawl" button (not "Go to Dashboard")
# - "Skip for now" link points to /crawl

# 4. Create a website through the modal

# 5. After success message, should redirect to /crawl
# (not /dashboard)
```

---

## Valid Redirect Targets

These are the ONLY pages that accept redirect parameter:

```php
$validRedirects = [
    'dashboard'      => '/dashboard - Main dashboard',
    'crawl'          => '/crawl - Crawl history',
    'website'        => '/website - Website management',
    'reports'        => '/reports - Reports dashboard',
    'audit'          => '/audit - Audit results',
    'keywords'       => '/keywords - Keyword tracking',
    'rank-tracking'  => '/rank-tracking - Rank tracking',
];
```

**Note**: If an invalid redirect is provided, it defaults to 'dashboard'. This is a security feature to prevent open redirect attacks.

---

## Common Questions

**Q: What if the page doesn't exist?**  
A: The redirect parameter is validated against a whitelist. Invalid pages default to 'dashboard'.

**Q: What if user closes the modal without creating a website?**  
A: Modal closes, user stays on onboarding page. No redirect happens.

**Q: What if website creation fails?**  
A: Modal shows error message, user can retry. No redirect happens on error.

**Q: Can I use any page name?**  
A: No, only whitelisted pages are supported. Update `OnboardingController.php` to add new pages.

**Q: How do I add a new page to the redirect whitelist?**  
A: Edit `app/Controllers/OnboardingController.php` and add to `$validRedirects` array.

---

## Adding New Redirect Target

To add support for a new page (e.g., `/keyword-research`):

**1. Update OnboardingController**
```php
// In app/Controllers/OnboardingController.php, line 19
$validRedirects = [
    'dashboard', 
    'crawl', 
    'website', 
    'reports', 
    'audit', 
    'keywords', 
    'rank-tracking',
    'keyword-research',  // ← ADD THIS
];
```

**2. Add Button to Page**
```php
<!-- In app/Views/keyword-research/index.php -->
<a href="<?= site_url('onboarding?redirect=keyword-research') ?>" class="btn btn-primary">
  + Create Project
</a>
```

**3. Test**
```
Navigate to /keyword-research?redirect=keyword-research
Verify redirect works
```

---

## Implementation Checklist

- [ ] Identify pages that need "Create Project" button
- [ ] Add button with `/onboarding?redirect=page-name` link
- [ ] Test that redirect parameter works
- [ ] Verify user returns to source page after creating website
- [ ] Test with different redirect pages
- [ ] Verify invalid redirects default to dashboard
- [ ] Update documentation
- [ ] Deploy and monitor

---

*Last updated: 2026-08-18*
