# Content API

The Content API is available in PHP content files (`.php`) and provides a safe, read-only interface to CMS data.

## Usage in PHP content files

```php
<?php
/** @var ContentAPI $api */

// Get all pages
$allPages = $api->getAllPages();
// Result: ['index' => 'Home', 'about-us' => 'About Us', ...]

// Get a specific page
$page = $api->getPage('about-us');
// Result: ['title' => 'About Us', 'content' => '...', 'path' => 'about-us', 'layout' => 'full_content', 'metadata' => [...]]

// Get menu structure
$menu = $api->getMenu();
// Result: [['title' => 'Home', 'path' => 'index', 'type' => 'file', 'active' => true], ...]

// Get config value (dot notation)
$siteTitle = $api->getConfig('site_title');
$searchEnabled = $api->getConfig('features.search_enabled', false);
```

## Available methods

### Pages

- `getAllPages(): array` - All pages as `['path' => 'title']` pairs
- `getPage(string $path): ?array` - Specific page with `title`, `content`, `path`, `layout`, `metadata`
- `pageExists(string $path): bool` - Check if a page exists
- `getCurrentPageTitle(): string` - Title of current page
- `getCurrentPagePath(): string` - Path of current page
- `isHomepage(): bool` - Whether current page is the homepage

### Menu & Navigation

- `getMenu(): array` - Hierarchical menu structure with `title`, `path`, `children`, `active`
- `buildUrl(string $page = 'index', ?string $lang = null, array $params = []): string` - Build a URL for a page

### Configuration

- `getConfig(string $key, mixed $default = null): mixed` - Config value via dot notation (e.g. `'features.search_enabled'`)
- `getSiteTitle(): string` - Site title from config

### Language

- `getCurrentLanguage(): string` - Current language code (e.g. `'nl'`)
- `getAvailableLanguages(): array` - Available languages (e.g. `['nl', 'en']`)
- `t(string $key): string` - Translate a language key

### Search

- `getSearchResults(): array` - Search results (empty if not searching)
- `isSearching(): bool` - Whether a search is currently active

## Example: Show recent pages

```php
<?php
/** @var ContentAPI $api */
$pages = $api->getAllPages();
$currentLang = $api->getCurrentLanguage();
?>
<ul>
<?php foreach (array_slice($pages, 0, 5, true) as $path => $title): ?>
    <li>
        <a href="/<?= $currentLang ?>/<?= htmlspecialchars($path) ?>">
            <?= htmlspecialchars($title) ?>
        </a>
    </li>
<?php endforeach; ?>
</ul>
```

## Example: Using config values

```php
<?php
/** @var ContentAPI $api */
if ($api->getConfig('features.search_enabled', false)): ?>
    <form method="GET" action="">
        <input type="search" name="search" placeholder="<?= $api->t('search_placeholder') ?>">
        <button type="submit"><?= $api->t('search_button') ?></button>
    </form>
<?php endif; ?>
```

## CMSAPI (for plugins)

Plugins use the `CMSAPI` class via `$this->api`. It offers similar methods:

```php
$this->api->getCurrentPageTitle();
$this->api->getCurrentPageContent();
$this->api->getMenu();
$this->api->getConfig('site_title');
$this->api->getCurrentLanguage();
$this->api->isHomepage();
$this->api->hasContent();
$this->api->getSearchResults();
$this->api->isSearching();
$this->api->getAvailableLanguages();
$this->api->createUrl('about-us');
$this->api->translate('home');
```

Additionally, CMSAPI provides:

- `getCurrentPage(): array` - Full page data
- `getCurrentPageUrl(): string` - URL of current page
- `getCurrentPageFileInfo(): ?array` - File info (created, modified)
- `getBreadcrumb(): string` - Breadcrumb HTML
- `executePhpFile(string $filePath): string` - Execute PHP file and capture output
- `getFileContent(string $filePath): string` - Get content from PHP/HTML/Markdown file
- `contentFileExists(string $filename): bool` - Check if file exists in content directory